示例#1
0
            public void Mate(int N_bests = 5)
            {
                List <Individual> to_mate = new List <Individual>(fixed_amount);

                for (int i = 0; i < N_bests; i++)
                {
                    to_mate.Add(individuals[i]);
                }

                for (int i = 0; i < fixed_amount - N_bests; i++)
                {
                    int first_competitor  = _src.random_generator.Next(N_bests + 1, fixed_amount),
                        second_competitor = _src.random_generator.Next(N_bests + 1, fixed_amount);

                    Individual winner = individuals[first_competitor].Value < individuals[second_competitor].Value ? individuals[second_competitor] : individuals[first_competitor];
                    to_mate.Add(winner);
                }


                for (int i = 0; i < to_mate.Count; i++)
                {
                    Individual parrent1  = to_mate[i],
                               parrent2  = to_mate[_src.random_generator.Next(0, to_mate.Count)];
                    Individual offspring = OX_crossover(parrent1, parrent2);

                    double mutationRate = Individual.Common(parrent1, parrent2);

                    if (mutationRate > 0.1)
                    {
                        if (mutationRate > 0.6)
                        {
                            if (_src.random_generator.Next(0, 100) > 70)
                            {
                                int start = _src.random_generator.Next(2, offspring.Chromos.Count - 2);
                                int count = _src.random_generator.Next(1, Convert.ToInt32((offspring.Chromos.Count - 1 - start) * (mutationRate)));
                                offspring.ReverseMutation(start, count);
                            }
                            if (_src.random_generator.Next(0, 100) < mutationRate * 100)
                            {
                                for (int j = 0; j < Convert.ToInt32(mutationRate * 3.3); j++)
                                {
                                    int value    = _src.random_generator.Next(1, _src.numberOfVertices);
                                    int position = _src.random_generator.Next(1, _src.numberOfVertices);

                                    offspring.InsertMutation(position, value);
                                }
                            }
                        }
                        else
                        {
                            offspring.SwapMutation(_src.random_generator.Next(2, offspring.Chromos.Count - 2), _src.random_generator.Next(2, offspring.Chromos.Count - 2));
                        }
                    }

                    individuals.Add(offspring);
                }

                Sort_invididuals();
            }