Exemple #1
0
        private void GreedyAlgorithmTSP(string fileName)
        {
            Stopwatch stopwatch = new Stopwatch();


            IGreedyTSPProcessor fileProcessor = new GreedyTSPProcessor(fileName);

            stopwatch.Start();

            List <Vertex> outputPath = fileProcessor.BuildPath();

            stopwatch.Stop();

            Console.WriteLine("\nOutput Path");
            Console.WriteLine("--------------------------------");
            Console.WriteLine("Calculation Time: " + stopwatch.ElapsedMilliseconds + " ms");
            Console.WriteLine("Distance: " + _pathProcessor.Process(outputPath) + " units");
            Console.WriteLine("--------------------------------");

            foreach (var vertex in outputPath)
            {
                Console.WriteLine("\t(" + vertex.x + ", " + vertex.y + ")");
            }


            Console.WriteLine("\nPress the enter button to continue. . .");
            Console.ReadLine();
        }
Exemple #2
0
        private void DepthFirstSearch(Node parentNode, List <int> path)
        {
            List <int> localPath = path;

            if (parentNode != null)
            {
                //look for all possible children
                foreach (var childNode in parentNode.ChildNodes)
                {
                    DepthFirstSearch(childNode, localPath);
                }
            }
            else
            {
                //now that I have a full path, process it and see if its the shortest path
                double distance = _pathProcessor.Process(localPath.ToArray(), _vertices);

                UpdateShortestPath(localPath.ToArray(), distance);

                return;
            }
        }
Exemple #3
0
        //populate Fitness List with parents level of fitness
        private void EvaluateParentFitness()
        {
            FitnessList.Clear();

            List <double> lengthList = new List <double>();

            foreach (var path in ParentGenerationList)
            {
                lengthList.Add(_pathProcessor.Process(path));
            }

            int i = 0;

            foreach (var pathLength in lengthList)
            {
                //higher rank = higher fitness
                int rank = 1;
                foreach (var d in lengthList)
                {
                    if (pathLength < d)
                    {
                        rank++;
                    }
                }

                FitnessList.Add(rank);
            }

            for (int j = 0; j < FitnessList.Count; j++)
            {
                if (FitnessList.FindAll(x => x == j).Count > 1)
                {
                    FitnessList[FitnessList.FindIndex(x => x == j)] += 1;
                }
            }
        }
Exemple #4
0
        private void Permutation(int[] list, int depthRecursion, int depthMax)
        {
            if (depthRecursion != depthMax)
            {
                var i = depthRecursion;

                for (; i <= depthMax; i++)
                {
                    SwitchVertices(ref list[depthRecursion], ref list[i]);

                    Permutation(list, depthRecursion + 1, depthMax);

                    SwitchVertices(ref list[depthRecursion], ref list[i]);
                }
            }
            else
            {
                //calculate distance
                double pathDistance = _pathProcessor.Process(list, _vertices);

                //save shortest path
                UpdateShortestPath(list, pathDistance);
            }
        }
Exemple #5
0
        private void aToolStripMenuItem_Click(object sender, EventArgs e)
        {
            double av0, av1, av2, av3, av4;

            for (int i = 0; i <= 4; i++)
            {
                _geneticAlgorithmProcessor.RunGA1A(Iterations);
                double distance0 = _pathProcessor.Process(_geneticAlgorithmProcessor.ParentGenerationList[0]);
                double distance1 = _pathProcessor.Process(_geneticAlgorithmProcessor.ParentGenerationList[1]);
                double distance2 = _pathProcessor.Process(_geneticAlgorithmProcessor.ParentGenerationList[2]);
                double distance3 = _pathProcessor.Process(_geneticAlgorithmProcessor.ParentGenerationList[3]);
                double distance4 = _pathProcessor.Process(_geneticAlgorithmProcessor.ParentGenerationList[4]);
                double distance5 = _pathProcessor.Process(_geneticAlgorithmProcessor.ParentGenerationList[5]);
                double distance6 = _pathProcessor.Process(_geneticAlgorithmProcessor.ParentGenerationList[6]);
                double distance7 = _pathProcessor.Process(_geneticAlgorithmProcessor.ParentGenerationList[7]);
                double distance8 = _pathProcessor.Process(_geneticAlgorithmProcessor.ParentGenerationList[8]);
                double distance9 = _pathProcessor.Process(_geneticAlgorithmProcessor.ParentGenerationList[9]);

                switch (i)
                {
                case (0):
                    av0 = (distance0 + distance1 + distance2 + distance3 + distance4 + distance5 + distance6 +
                           distance7 + distance8 + distance9) / 10;
                    break;

                case (1):
                    av1 = (distance0 + distance1 + distance2 + distance3 + distance4 + distance5 + distance6 +
                           distance7 + distance8 + distance9) / 10;
                    break;

                case (2):
                    av2 = (distance0 + distance1 + distance2 + distance3 + distance4 + distance5 + distance6 +
                           distance7 + distance8 + distance9) / 10;
                    break;

                case (3):
                    av3 = (distance0 + distance1 + distance2 + distance3 + distance4 + distance5 + distance6 +
                           distance7 + distance8 + distance9) / 10;
                    break;

                case (4):
                    av4 = (distance0 + distance1 + distance2 + distance3 + distance4 + distance5 + distance6 +
                           distance7 + distance8 + distance9) / 10;
                    break;
                }
            }
        }