Пример #1
0
        public int[] SA_Algorithm(int[] permutation, string[,] distances, string[] cities, string unit, int count)
        {
            vrp = new VRP();

            double T_end = 0.01;
            double T = 100;
            int    L = count;
            double sum, newSum;

            double e = 2.71828;

            int[] newPermutation   = permutation;
            int[] finalPermutation = permutation;

            int i, j;

            while (T > T_end)
            {
                foreach (int p in permutation)
                {
                    i = new Random().Next(0, permutation.Length - 1);
                    j = new Random().Next(0, permutation.Length - 1);

                    newPermutation = swap(permutation, i, j);

                    sum    = vrp.calculate(permutation, distances, cities, unit);
                    newSum = vrp.calculate(newPermutation, distances, cities, unit);

                    if (newSum > sum)
                    {
                        double r     = new Random().NextDouble();
                        double delta = sum - newSum;

                        if (r >= (Math.Pow(e, ((double)delta / T))))
                        {
                            permutation = newPermutation;
                        }
                    }

                    permutation = newPermutation;
                    if (vrp.calculate(permutation, distances, cities, unit)
                        < vrp.calculate(finalPermutation, distances, cities, unit))
                    {
                        finalPermutation = permutation;
                    }
                }

                T = reduceTemperature(T);
            }

            return(finalPermutation);
        }
Пример #2
0
        static void Main(string[] args)
        {
            readData();

            permutation = new int[count];

            for (int i = 0; i < count; i++)
            {
                permutation[i] = i + 1;
            }

            VRP vrp = new VRP();
            SA  sa  = new SA();

            Random random = new Random();

            randomPermutation = permutation.OrderBy(x => random.Next()).ToArray();

            Console.WriteLine("Podstawowy algorytm: " + vrp.calculate(randomPermutation, distances, cities, unit));

            Console.WriteLine("Ograniczenie pojemnościowe");
            vrp.calculateWithCapacity(permutation, distances, cities, unit);
            Console.WriteLine("Ograniczenie czasowe");
            vrp.calculateWithTimeLimit(permutation, distances, cities, unit, count);

            Console.WriteLine("Algorytm 2/3 odlegości: ");
            greedyAlgorithm();

            Console.WriteLine("SA");
            randomPermutation = sa.SA_Algorithm(randomPermutation, distances, cities, unit, count);

            greedyAlgorithm();
        }