示例#1
0
        public TSPSolution solve(TSPInput input)
        {
            Console.WriteLine("Hill climbing started");
            current     = initialize(input);
            currentBest = current.convertToTSPSol();
            visualizer.draw(currentBest);
            stop = false;
            int steps = 0;

            while (!stop)
            {
                steps++;
                goOneStep();
                if (steps % 10 == 0)
                {
                    currentBest = current.convertToTSPSol();
                    visualizer.draw(currentBest);
                    Console.WriteLine("Steps: " + steps + " Best distance: " + currentBest.totalDistance);
                }
            }
            Console.WriteLine("Search ended");
            currentBest = current.convertToTSPSol();
            visualizer.draw(currentBest);
            Console.WriteLine("Steps: " + steps + " Best distance: " + currentBest.totalDistance);
            return(currentBest);
        }
示例#2
0
        public static TSPSolution GetNew(TSPSolution parent1, TSPSolution parent2)
        {
            PermutationStandard ps1 = new PermutationStandard(parent1);
            PermutationStandard ps2 = new PermutationStandard(parent2);
            PermutationStandard tmp = new PermutationStandard(parent2);

            // Keep track of elements, which have not been moved yet
            List <bool> moved = new List <bool>(tmp.size);

            for (int i = 0; i < tmp.size; ++i)
            {
                moved.Add(false);
            }

            // starting point of the cycle - first unmoved element
            moved[0] = true;

            while (moved.Contains(false))
            {
                Cycle cycle = new Cycle();

                int start = moved.IndexOf(false);

                // find the cycle
                while (!cycle.IsComplete())
                {
                    int through = ps2.perm[start];
                    int to      = 0;
                    for (int i = 0; i < ps1.perm.Length; ++i)
                    {
                        if (ps1.perm[i] == through)
                        {
                            to = i;
                        }
                    }

                    cycle.Add(start, to);
                    start = to;
                }

                // elements from ps1 where the cycle goes
                foreach (var startingPoint in cycle.GetStartingPoints())
                {
                    tmp.perm[startingPoint] = ps1.perm[startingPoint];
                    moved[startingPoint]    = true;
                }

                // swap ps1 and ps2
                var swap = ps1;
                ps1 = ps2;
                ps2 = swap;
            }

            return(tmp.convertToTSPSol());
        }
示例#3
0
        // Initialization of a TSPSolution
        private TSPSolution getRandomSolution(TSPInput input)
        {
            var perm = new PermutationStandard(input);

            perm.randomize();
            var p = perm.convertToTSPSol();

            if (!p.validate())
            {
                throw new InvalidOperationException();
            }
            return(p);
        }