Exemple #1
0
 public Population Concat(Population genom2)
 {
     Population combined = new Population(this.genom);
     for (int i = 0; i < genom2.GetCount(); i++)
     {
         combined.Add(genom2.Get(i));
     }
     return combined;
 }
Exemple #2
0
 public void CombinedPopulationCount()
 {
     int pop = 10;
     Population p1 = new Population();
     p1.NewPopulation(pop);
     Population p2 = new Population();
     p2.NewPopulation(pop);
     Population p3 = p1.Concat(p2);
     Assert.AreEqual(p3.GetCount(), p1.GetCount() + p2.GetCount());
 }
Exemple #3
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);
        }
Exemple #4
0
        public void InitialPopulationBoundaries()
        {
            Algorithm nsga = new Nsga2();
            int pop = 10;
            TestFunctions funcs = TestFunctions.GetTestFunctions();
            Population p = new Population();
            p.NewPopulation(pop);
            int mistakes = 0;
            for (int i = 0; i < p.GetCount(); i++)
            {
                for (int j = 0; j < p.Get(i).DecisionVariables.Count; j++)
                {
                    if (p.Get(i).DecisionVariables[0] < funcs.GetLowerThreshold()
                        || p.Get(i).DecisionVariables[j] > funcs.GetUpperThreshold())
                    {
                        mistakes++;

                    }
                }
            }
            Assert.AreEqual(mistakes, 0);
        }
Exemple #5
0
 internal void Copy(Population list)
 {
     for (int i = 0; i < list.GetCount(); i++)
     {
         this.Add(list.Get(i));
     }
 }
Exemple #6
0
        public void SelectionCountTest()
        {
            int pop = 10;
            Population genom = new Population();
            genom.NewPopulation(pop);
            Population genom2 = new Population();
            genom2.NewPopulation(pop);
            Population combined = genom.Concat(genom2);

            genom = combined.Selection();
            Assert.AreEqual(combined.GetCount() / 2, genom.GetCount());
        }
Exemple #7
0
        public void FastNonDominatedSort(Population genom)
        {
            genom.EvaluateObjectiveFunctions();
            List<Solution> population = new List<Solution>();
            for (int j = 0; j < genom.GetCount(); j++)
            {
                population.Add(genom.Get(j));
            }
            rankings = new Ranking();
            foreach (Solution p in population)
            {
                p.DominationCount = 0;

                foreach (Solution q in population)
                {
                    if (!p.Equals(q))
                    {
                        if (p.Dominates(q))
                        {
                            p.DominatedSolutions.Add(q);
                        }
                        else
                        {
                            if (q.Dominates(p))
                            {
                                p.DominationCount++;
                            }
                        }
                    }
                }
                if (p.DominationCount == 0)
                {
                    rankings.AddIndividual(0, p);
                }
            }
            int i = 0;
            List<Solution> front = rankings.GetFront(i);
            while (front.Count > 0)
            {
                foreach (Solution p in front)
                {
                    foreach (Solution q in p.DominatedSolutions)
                    {
                        q.DominationCount--;
                        if (q.DominationCount == 0)
                        {
                            rankings.AddIndividual(i + 1, q);
                        }
                    }
                }
                i++;
                front = rankings.GetFront(i);
            }
            for (int j = 0; j < rankings.GetFrontCount(); j++ )
            {
                front = rankings.GetFront(j);
                foreach (Solution s in front)
                {
                    s.Fitness = j;
                }
                if (front.Count == 0)
                {
                    rankings.Remove(rankings.GetFrontCount() - 1);
                }
            }
        }