示例#1
0
        // "Smart mutation" - try to swap and improve the distance
        private void mutateSmart(TSPSolution initial)
        {
            PermutationStandard current = new PermutationStandard(initial);

            var start = current.eval();
            var best  = start;

            var bestX = 0;
            var bestY = 0;

            for (int i = 0; i < current.perm.Length; ++i)
            {
                for (int j = 0; j < current.perm.Length; ++j)
                {
                    current.swap(i, j);
                    var temp = current.eval();
                    current.swap(j, i);

                    if (temp < best)
                    {
                        best  = temp;
                        bestX = i;
                        bestY = j;
                    }
                }
            }

            if (best < start)
            {
                current.swap(bestX, bestY);
                current.applyToTSPSolution(initial);
            }
        }
示例#2
0
        // "Dumb mutation" - just swap some elements
        private void mutateDumb(TSPSolution initial)
        {
            PermutationStandard current = new PermutationStandard(initial);

            current.swap(rnd.Next(current.size), rnd.Next(current.size));
            current.applyToTSPSolution(initial);
        }
示例#3
0
        private PermutationStandard initialize(TSPInput input)
        {
            var result = new PermutationStandard(input);

            result.randomize();
            return(result);
        }
示例#4
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);
        }
示例#5
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());
        }
示例#6
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);
        }
示例#7
0
        public PermutationStandard convertToStandard()
        {
            PermutationStandard std = new PermutationStandard(this.input);

            int[] pos = new int[this.size];
            for (int i = size - 1; i >= 0; i--)
            {
                for (int m = i; m < size; m++)
                {
                    if (pos[m] >= this.perm[i] + 1)
                    {
                        pos[m]++;
                    }
                    pos[i] = perm[i] + 1;
                }
            }
            for (int i = 0; i < size; i++)
            {
                std.perm[pos[i] - 1] = i;
            }
            return(std);
        }