예제 #1
0
        private List<Schedule> PopulateSchedules(int numberOfGenes, int geneLength)
        {
            List<Schedule> schedules = new List<Schedule>();
            Random r = new Random();

            for (int scheduleIdx = 0; scheduleIdx < numberOfSchedules; scheduleIdx++)
            {
                Schedule schedule = new Schedule();

                //If random number is same as number of slots, that's an empty slot (because range starts at 0
                //and ends at number of slots - 1 in the slot list).
                for (int geneIdx = 0; geneIdx < numberOfGenes; geneIdx++)
                {
                    int random = r.Next(0, numberOfGenes);
                    schedule.ScheduleString += Helpers.ConvertIntToBinaryString(random, geneLength);
                }
                schedules.Add(schedule);
            }

            return schedules;
        }
예제 #2
0
        private List<Schedule> CrossOver(List<Schedule> oldPopulation)
        {
            List<Schedule> newPopulation = new List<Schedule>();
            int highestFitness = GetHightestFitness(oldPopulation);
            Random r = new Random(DateTime.Now.Millisecond);
            bool parentSelected = false;

            //Replace all the chromosomes with child chromosomes.
            for (int scheduleIdx = 0; scheduleIdx < oldPopulation.Count; scheduleIdx++)
            {
                Schedule parent1 = new Schedule();
                Schedule parent2 = new Schedule();

                //Loop through chromosomes twice to pick 2 parents.
                for (int parentIdx = 0; parentIdx < 2; parentIdx++)
                {
                    parentSelected = false;
                    int scheduleIdx2 = 0;

                    while (parentSelected == false && scheduleIdx2 < oldPopulation.Count)
                    {
                        scheduleIdx2++;
                        Schedule candidate = oldPopulation[r.Next(0, oldPopulation.Count - 1)];
                        //int fitnessToBeat = r.Next(0, highestFitness);

                        if (candidate.Fitness >= highestFitness)
                        {
                            if (parentIdx == 0)
                            {
                                parent1 = candidate;
                                parentSelected = true;
                            }
                            else
                            {
                                //Avoid picking same as parent 1.
                                if (candidate.ScheduleString != parent1.ScheduleString)
                                {
                                    parent2 = candidate;
                                    parentSelected = true;
                                }
                            }
                        }
                    }
                }

                //If we've got to the end of the
                //chromosomes without picking parent 2, choose a random parent.
                if (!parentSelected)
                {
                    parent2 = oldPopulation[r.Next(0, oldPopulation.Count - 1)];
                }

                int flipPos = r.Next(0, parent1.ScheduleString.Length);
                string newSchedule = parent1.ScheduleString.Substring(0, flipPos) +
                    parent2.ScheduleString.Substring(flipPos);
                Schedule child = new Schedule() { ScheduleString = newSchedule };
                newPopulation.Add(child);
            }

            return newPopulation;
        }