示例#1
0
        public Population CreateOffspring()
        {
            GeneticOperators operators = new GeneticOperators();
            Random rand = new Random();
            List<Solution> newIndividuals = null;
            List<Solution> parentGenom = new List<Solution>();
            //foreach (Solution s in genom)
            //{
            //    parentGenom.Add(s);
            //}
            for (int j = 0; j < genom.Count;j++)
            {
                parentGenom.Add(new Solution(genom.ElementAt(j)));
            }
            List<Solution> offsprings = new List<Solution>();
            int i = genom.Count - 1;
            while( i >= 0 )
            {
                if ( (i > 1) && (rand.NextDouble() < 0.9))
                {
                    //do crossover
                    int i1 = 0; ////////////////////////////////////////////////// todo
                    int i2 = 1;
                    newIndividuals = operators.Crossover(parentGenom.ElementAt(i1), parentGenom.ElementAt(i2));
                    parentGenom.RemoveAt(i1);
                    parentGenom.RemoveAt(i2 - 1);
                    i -= 2;
                    foreach (Solution s in newIndividuals)
                    {
                        offsprings.Add(s);
                    }
                }
                else
                {
                    //do mutation
                    int i1 = 0;
                    newIndividuals = operators.Mutation(parentGenom.ElementAt(i1));
                    parentGenom.RemoveAt(i1);
                    i--;
                    foreach (Solution s in newIndividuals)
                    {
                        offsprings.Add(s);
                    }
                }

            }

            return new Population(offsprings);
        }
示例#2
0
        public void CrossoverTest()
        {
            GeneticOperators operators = new GeneticOperators();
            int pop = 10;
            Population genom = new Population();
            genom.NewPopulation(pop);
            List<Solution> newGenom = new List<Solution>();

            for (int i = 0; i < genom.GetCount() - 1; i += 2)
            {
                List<Solution> list = operators.Crossover(genom.Get(i), genom.Get(i + 1));
                foreach (Solution s in list)
                {
                    newGenom.Add(s);
                }
            }
            if (newGenom.Count < genom.GetCount())
            {
                newGenom.Add(genom.Get(genom.GetCount()-1));
            }
            double eps = 0.001;

            int mistakes = 0;
            List<Solution>.Enumerator e = newGenom.GetEnumerator();
            int j = -1;
            while (e.MoveNext())
            {
                j++;
                for (int i = 0; i < e.Current.DecisionVariables.Count; i++)
                {
                    if (Math.Abs(e.Current.DecisionVariables[i] - genom.Get(j).DecisionVariables[i]) < eps)
                    {
                        mistakes++;
                    }
                }
            }
            Assert.AreEqual(0, mistakes);
        }