Beispiel #1
0
        public static TSPGenotype randomGenotype(TSP problem)
        {
            TSPGenotype g = new TSPGenotype(problem);

            g.genotype = problem.randomSolutionInt();
            return(g);
        }
Beispiel #2
0
        public static TSPGenotype crossover(TSPGenotype parent1, TSPGenotype parent2)
        {
            Random r   = new Random();
            int    len = parent1.genotype.Length;

            List <int> genotype2 = new List <int>(parent2.genotype);

            TSPGenotype child     = new TSPGenotype(parent1);
            int         position1 = r.Next(len);
            int         position2 = r.Next(len);

            while (position1 == position2)
            {
                position2 = r.Next(len);
            }



            if (position1 > position2)
            {
                int temp = position1;
                position1 = position2;
                position2 = temp;
            }

            for (int i = position1; i <= position2; i++)
            {
                genotype2.Remove(child.genotype[i]);
            }



            for (int i = 0; i < position1; i++)
            {
                child.genotype[i] = genotype2[0];
                genotype2.RemoveAt(0);
            }

            for (int i = position2 + 1; i < len; i++)
            {
                child.genotype[i] = genotype2[0];
                genotype2.RemoveAt(0);
            }

            return(child);
        }
        public static void testDifferentAlgorithms(TSP[] problems, int populationSize, int generationNum, int numOfLaunch = 10)
        {
            string line;
            var    file = new System.IO.StreamWriter(@"/Users/aggami/Library/Mobile Documents/com~apple~CloudDocs/Studia/Semestr 6/SIiIW/" + "testAlgorytmow.txt");

            file.AutoFlush = true;

            string startLine = "ProblemName; ;Best;Worst;Avg;Std;Best;Worst;Avg;Std;Best;Worst;Avg;Std;";

            file.WriteLine(startLine);
            RandomSolution r = new RandomSolution();


            foreach (TSP problem in problems)
            {
                Console.WriteLine(problem.Name);
                line = problem.Name + "; ;";

                Console.WriteLine("Random");
                r.run(populationSize * generationNum, problem);
                line += r.Best + "; " + r.Worst + "; " + r.Avg + "; " + r.Dev + "; ";

                float   best      = Single.MaxValue;
                float   worst     = 0;
                float   avg       = 0;
                float   div       = 0;
                float[] solutions = new float[problem.Dimention];

                Console.WriteLine("Greedy");
                for (int i = 0; i < problem.Dimention; i++)
                {
                    float temp = new TSPGenotype(TSPGenotype.solutionToGenotype(GreedyAlgorithm.solveGreedy(problem, i)), problem).fitness();
                    solutions[i] = temp;
                    if (temp > worst)
                    {
                        worst = temp;
                    }
                    if (temp < best)
                    {
                        best = temp;
                    }
                    avg += temp;
                }

                avg = avg / problem.Dimention;

                for (int i = 0; i < problem.Dimention; i++)
                {
                    div += (solutions[i] - avg) * (solutions[i] - avg);
                }
                div = div / problem.Dimention;
                div = Convert.ToSingle(Math.Sqrt(div));

                line += best + "; " + worst + "; " + avg + "; " + div + "; ";

                best  = Single.MaxValue;
                worst = 0;
                avg   = 0;
                div   = 0;

                Console.WriteLine("Ewolucyjny");
                for (int i = 0; i < 10; i++)
                {
                    AlgorytmEwolucyjny alg = new AlgorytmEwolucyjny(problem, populationSize, generationNum, "TNM");
                    alg.CrossoverProb = 0.9;
                    alg.MutationProb  = 0.4;
                    if (i == 0)
                    {
                        file.WriteLine(alg.toString());
                    }
                    float temp = alg.runAlgorithm().fitness();
                    solutions[i] = temp;
                    if (temp > worst)
                    {
                        worst = temp;
                    }
                    if (temp < best)
                    {
                        best = temp;
                    }
                    avg += temp;
                }

                avg = avg / problem.Dimention;

                for (int i = 0; i < problem.Dimention; i++)
                {
                    div += (solutions[i] - avg) * (solutions[i] - avg);
                }
                div = div / problem.Dimention;
                div = Convert.ToSingle(Math.Sqrt(div));

                line += best + "; " + worst + "; " + avg + "; " + div + "; ";


                file.WriteLine(line);
            }
        }
Beispiel #4
0
 public TSPGenotype(TSPGenotype parent)
 {
     this.problem  = parent.problem;
     this.genotype = (int[])parent.genotype.Clone();
 }