コード例 #1
0
        private ScheduleDNA MidPointCrossover(ScheduleDNA otherParent)
        {
            ScheduleDNA child = new ScheduleDNA(_slotsGenerator, _fitnessCalculator, _random, shouldInitGenes: false);

            double randomNumber = 0.2; //_random.NextDouble();

            int slotsCountInDay = _slotsCount / 5;

            //ISubjectsRepository subjectsRepository = new SubjectsFakeRepository();
            //List<Subject> subjects = subjectsRepository.GetAll().ToList();
            //subjects.Shuffle();

            //int firstCount = subjects.Count / 2;
            //int secondCount = subjects.Count - firstCount;

            //List<Slot> slots = _slotsGenerator.GetInitializedSlots();

            //var slotsBySubjectId = Genes.Where(x => x.SubjectId != -1).GroupBy(x => x.SubjectId);

            //List<Subject> halfOfSubjects = subjects.Take(firstCount).ToList();

            //slotsBySubjectId = slotsBySubjectId.Where(x => x.Key == halfOfSubjects.First(s => s.Id == x.Key).Id);

            //foreach (var group in slotsBySubjectId)
            //{
            //    foreach (Slot slot in group)
            //    {
            //        slots[slot.Id - 1].SubjectId = slot.SubjectId;
            //    }
            //}

            //child.Genes = slots;



            //Get Monday and Tuesday slots from first parent and get Wedensday, Thursday and Friday slots from second parent.
            if (randomNumber < 0.5)
            {
                for (int i = 0; i < slotsCountInDay * 2; i++)
                {
                    child.Genes.Add(Genes[i]);
                }

                for (int i = slotsCountInDay * 2; i < _slotsCount; i++)
                {
                    child.Genes.Add(otherParent.Genes[i]);
                }
            }

            return(child);
        }
コード例 #2
0
        public void CalculateFitness()
        {
            _fitnessSum = 0;
            ScheduleDNA best = Population[0];

            for (int i = 0; i < Population.Count; i++)
            {
                _fitnessSum += Population[i].CalculateFitness();
                if (Population[i].Fitness > best.Fitness)
                {
                    best = Population[i];
                }

                BestFitness = best.Fitness;
                BestGenes   = new List <Slot>(best.Genes);
            }
        }
コード例 #3
0
        public void NewGeneration()
        {
            if (Population.Count <= 0)
            {
                return;
            }

            CalculateFitness();

            List <ScheduleDNA> newPopulation = new List <ScheduleDNA>();

            for (int i = 0; i < Population.Count; i++)
            {
                ScheduleDNA parent1 = ChooseParent();
                ScheduleDNA parent2 = ChooseParent();

                ScheduleDNA child = parent1.Crossover(parent2);

                newPopulation.Add(child);
            }

            Population = newPopulation;
            Generation++;
        }
コード例 #4
0
 public ScheduleDNA Crossover(ScheduleDNA firstParent, ScheduleDNA secondParent)
 {
     return(null);
 }
コード例 #5
0
 public ScheduleDNA Crossover(ScheduleDNA otherParent)
 {
     return(null);
 }
コード例 #6
0
        public ScheduleDNA Crossover(ScheduleDNA otherParent)
        {
            ScheduleDNA child = MidPointCrossover(otherParent);

            return(child);
        }