Beispiel #1
0
        public Solution_TSP Clone()
        {
            Solution_TSP clone = (Solution_TSP)this.MemberwiseClone();

            clone.solution = (int[])this.solution.Clone();
            return(clone);
        }
Beispiel #2
0
        public void Cross(Solution_TSP r1, double crossProb)
        {
            if (rand.NextDouble() < crossProb)
            {
                Solution_TSP c1 = new Solution_TSP(rand);
                c1.solution = Enumerable.Repeat(-1, Problem_TSP.n).ToArray();
                Solution_TSP c2 = new Solution_TSP(rand);
                c2.solution = Enumerable.Repeat(-1, Problem_TSP.n).ToArray();

                int np = rand.Next((int)Problem_TSP.n);

                for (int i = 0; i < np; i++)
                {
                    int p = rand.Next((int)Problem_TSP.n);
                    c1.solution[p] = this.solution[p];
                    c2.solution[p] = r1.solution[p];
                }

                int p1 = 0, p2 = 0;
                for (int i = 0; i < Problem_TSP.n; i++)
                {
                    if (c1.solution[i] == -1)
                    {
                        while (c1.solution.Contains(this.solution[p1]))
                        {
                            p1++;
                        }
                        c1.solution[i] = this.solution[p1];
                    }
                    if (c2.solution[i] == -1)
                    {
                        while (c2.solution.Contains(r1.solution[p2]))
                        {
                            p2++;
                        }
                        c2.solution[i] = r1.solution[p2];
                    }
                }

                this.solution = c1.solution;
                r1.solution   = c2.solution;
            }
        }
Beispiel #3
0
        public GA_TSP(int popSize, double mutProb, double crossProb, Random rand)
        {
            pop            = new Solution_TSP[popSize];
            npop           = new Solution_TSP[popSize];
            this.mutProb   = mutProb;
            this.crossProb = crossProb;
            this.popSize   = popSize;
            this.rand      = rand;

            for (int i = 0; i < popSize; i++)
            {
                pop[i] = new Solution_TSP(rand);
                //Initialization
                pop[i].initialize();
                //Mutation
                pop[i].Mutate(mutProb);
                //Evaluation
                pop[i].Evaluate();
                cumFitness += pop[i].fitness;
            }
        }