public Schedule MakeNewFromPrototype()
{
// number of time-space slots
int size = _slots.Length;
// make new chromosome, copy chromosome setup
Schedule newChromosome = new Schedule(this, true);
// place classes at random position
List<CourseClass> cc = Configuration.GetInstance.GetCourseClasses();
foreach (CourseClass it in cc)
{
// determine random position of class
int num_rooms = Configuration.GetInstance.GetNumberOfRooms();
int dur = it.GetDuration;
Random rand = new Random();
int day = rand.Next() % DAYS_NUM;
int room = rand.Next() % num_rooms;
int time = rand.Next() % (DAY_HOURS + 1 - dur);
int pos = (day * num_rooms * DAY_HOURS) + (room * DAY_HOURS + time); // (Base) + (offset) time's
// fill time-space slots, for each hour of class
for (int i = dur - 1; i >= 0; i--)
newChromosome._slots[pos + i].Add(it);
// insert in class table of chromosome
newChromosome._classes.Add(it, pos);
}
newChromosome.CalculateFitness();
// return smart pointer
return newChromosome;
}
// Starts and executes algorithm public bool Start() { #region Start by initialize new population if (_prototype == null) { return(false); } if (Monitor.TryEnter(Locker0, 10)) { // do not run already running algorithm if (_state == AlgorithmState.AS_RUNNING) { Monitor.Exit(Locker0); return(false); } _state = AlgorithmState.AS_RUNNING; Monitor.Exit(Locker0); } else { return(false); } if (_observer != null) { // notify observer that execution of algorithm has changed it state _observer.EvolutionStateChanged(_state); } if (!ResultControls.ResultForm._setting.Parallel_Process) // Single Process for GA { // clear best chromosome group from previous execution ClearBest_Sequence(); // initialize new population with chromosomes randomly built using prototype for (int i = 0; i < _chromosomes.Length; i++) { // remove chromosome from previous execution if (_chromosomes[i] != null) { _chromosomes[i] = null; } // add new chromosome to population _chromosomes[i] = _prototype.MakeNewFromPrototype(); AddToBest_Sequence(i); } } else // Multi Process for GA { // clear best chromosome group from previous execution ClearBest_Parallel(); // initialize new population with chromosomes randomly built using prototype ParallelOptions options = new ParallelOptions(); options.MaxDegreeOfParallelism = numCore; Parallel.For(0, _chromosomes.Length, options, i => { // remove chromosome from previous execution if (_chromosomes[i] != null) { _chromosomes[i] = null; } // add new chromosome to population _chromosomes[i] = _prototype.MakeNewFromPrototype(); AddToBest_Parallel(i); }); } _currentGeneration = 0; #endregion // // create new Threads as for manual selected CPU core's // if (ResultControls.ResultForm._setting.Parallel_Process) // Multi Process { MultiThreads = new Thread[numCore]; for (int cpu = 0; cpu < numCore; ++cpu) { MultiThreads[cpu] = new Thread(new ParameterizedThreadStart(GA_Start)); MultiThreads[cpu].Name = "MultiThread_" + cpu.ToString(); MultiThreads[cpu].Start(true as object); } System.Diagnostics.Process.GetCurrentProcess().Threads.AsParallel(); } else // Single Process { MultiThreads = new Thread[1]; MultiThreads[0] = new Thread(new ParameterizedThreadStart(GA_Start)); MultiThreads[0].Name = "MultiThread_0"; MultiThreads[0].Start((object)false); } return(true); }