// new Thread for Start GA
private void GA_Start(object Parallel_Mutex_On)
{
if ((Boolean)Parallel_Mutex_On) // Parallel Process Requirement's
{
#region GA for Mutex On
while (true) //------------------------------------------------------------------------
{
// user has stopped execution?
if (_state == AlgorithmState.AS_CRITERIA_STOPPED || _state == AlgorithmState.AS_USER_STOPPED)
{
break;
}
else if (_state == AlgorithmState.AS_SUSPENDED)
{
if (Thread.CurrentThread.IsAlive)
{
Thread.CurrentThread.Suspend();
}
}
// Save a Elite Chromosome for protection in Mutation and etc.
Schedule best = GetBestChromosome();
// algorithm has reached criteria?
if (best.GetFitness() >= 1)
{
_state = AlgorithmState.AS_CRITERIA_STOPPED;
break;
}
// produce offspring
Schedule[] offspring;
offspring = new Schedule[_replaceByGeneration];
Random rand = new Random();
for (int j = 0; j < _replaceByGeneration; j++)
{
Schedule p1;
Schedule p2;
// selects parent randomly
lock (Locker1)
{
p1 = _chromosomes[(rand.Next() % _chromosomes.Length)].MakeCopy(false);
}
lock (Locker1)
{
p2 = _chromosomes[(rand.Next() % _chromosomes.Length)].MakeCopy(false);
}
offspring[j] = p1.Crossover(p2);
lock (Locker1)
{
offspring[j].Mutation();
offspring[j].CalculateFitness();
}
}
// replace chromosomes of current operation with offspring
for (int j = 0; j < _replaceByGeneration; j++)
{
int ci;
do
{
// select chromosome for replacement randomly
ci = rand.Next() % _chromosomes.Length;
// protect best chromosomes from replacement
} while (IsInBest(ci));
lock (Locker1)
{
// replace chromosomes
_chromosomes[ci] = null;
_chromosomes[ci] = offspring[j];
}
// try to add new chromosomes in best chromosome group
AddToBest_Parallel(ci);
}
// algorithm has found new best chromosome
if (best != GetBestChromosome())
// notify observer
{
lock (Locker1)
{
_observer.NewBestChromosome(GetBestChromosome(), ResultControls.ResultForm._setting.Display_RealTime);
}
}
_currentGeneration++;
}
// The GA job's is Complete!
if (_observer != null)
{
lock (Locker0)
{
// notify observer that execution of algorithm has changed it state
_observer.EvolutionStateChanged(_state);
}
}
Thread.CurrentThread.Abort();
#endregion
}
else
{
#region GA for Mutex Off
while (true) //------------------------------------------------------------------------
{
// user has stopped execution?
if (_state == AlgorithmState.AS_CRITERIA_STOPPED || _state == AlgorithmState.AS_USER_STOPPED)
{
break;
}
else if (_state == AlgorithmState.AS_SUSPENDED)
{
if (Thread.CurrentThread.IsAlive)
{
Thread.CurrentThread.Suspend();
}
}
// Save a Elite Chromosome for protection in Mutation and etc.
Schedule best = GetBestChromosome();
// algorithm has reached criteria?
if (best.GetFitness() >= 1)
{
_state = AlgorithmState.AS_CRITERIA_STOPPED;
break;
}
// produce offspring
Schedule[] offspring;
offspring = new Schedule[_replaceByGeneration];
Random rand = new Random();
for (int j = 0; j < _replaceByGeneration; j++)
{
// selects parent randomly
Schedule p1 = _chromosomes[(rand.Next() % _chromosomes.Length)];
Schedule p2 = _chromosomes[(rand.Next() % _chromosomes.Length)];
offspring[j] = p1.Crossover(p2);
offspring[j].Mutation();
offspring[j].CalculateFitness();
}
// replace chromosomes of current operation with offspring
for (int j = 0; j < _replaceByGeneration; j++)
{
int ci;
do
{
// select chromosome for replacement randomly
ci = rand.Next() % _chromosomes.Length;
// protect best chromosomes from replacement
} while (IsInBest(ci));
// replace chromosomes
_chromosomes[ci] = null;
_chromosomes[ci] = offspring[j];
// try to add new chromosomes in best chromosome group
AddToBest_Sequence(ci);
}
// algorithm has found new best chromosome
if (best != GetBestChromosome())
// notify observer
{
_observer.NewBestChromosome(GetBestChromosome(), ResultControls.ResultForm._setting.Display_RealTime);
}
_currentGeneration++;
}
// The GA job's is Complete!
if (_observer != null)
{
// notify observer that execution of algorithm has changed it state
_observer.EvolutionStateChanged(_state);
}
Thread.CurrentThread.Abort();
#endregion
}
}