Exemple #1
0
 /// <summary>
 ///     return the cost of the best solution so far.
 /// </summary>
 /// <returns></returns>
 public double CostOfBssf()
 {
     if (_bssf != null)
     {
         return(_bssf.CostOfRoute());
     }
     return(-1D);
 }
Exemple #2
0
        public string[] ThreeOptSolveProblem()
        {
            var results = new string[3];

            // get initial bssf using default algorithm
            int i, swap, temp, count = 0;

            int[] perm = new int[_cities.Length];
            _route = new List <City>();
            Random rnd = new Random();

            do
            {
                for (i = 0; i < perm.Length; i++)                                 // create a random permutation template
                {
                    perm[i] = i;
                }
                for (i = 0; i < perm.Length; i++)
                {
                    swap = i;
                    while (swap == i)
                    {
                        swap = rnd.Next(0, _cities.Length);
                    }
                    temp       = perm[i];
                    perm[i]    = perm[swap];
                    perm[swap] = temp;
                }
                _route.Clear();
                for (i = 0; i < _cities.Length; i++)                            // Now build the route using the random permutation
                {
                    _route.Add(_cities[perm[i]]);
                }
                _bssf = new TspSolution(_route);
            } while (CostOfBssf() == double.PositiveInfinity);                // until a valid route is found


            Stopwatch timer = new Stopwatch();

            timer.Start();
            count = 0;
            TspSolution prevSolution = _bssf;

            bool found = false;

            do
            {
                double bestDist = _bssf.CostOfRoute();
                found = false;
                for (i = 0; i < _bssf.Route.Count - 2; i++)
                {
                    for (int j = i + 1; j < _bssf.Route.Count - 1; j++)
                    {
                        for (int k = j + 1; k < _bssf.Route.Count; k++)
                        {
                            TspSolution newRoute1 = threeOptSwapA(_bssf, i, j, k);
                            TspSolution newRoute2 = threeOptSwapB(_bssf, i, j, k);
                            TspSolution newRoute3 = threeOptSwapC(_bssf, i, j, k);
                            TspSolution newRoute4 = twoOptSwap(_bssf, i, j);
                            TspSolution newRoute5 = twoOptSwap(_bssf, j, k);
                            TspSolution newRoute6 = twoOptSwap(_bssf, i, k);

                            double newDist1 = newRoute1.CostOfRoute();
                            double newDist2 = newRoute2.CostOfRoute();
                            double newDist3 = newRoute3.CostOfRoute();
                            double newDist4 = newRoute4.CostOfRoute();
                            double newDist5 = newRoute5.CostOfRoute();
                            double newDist6 = newRoute6.CostOfRoute();


                            if (isCompleteSolution(newRoute1) && newDist1 < bestDist && newDist1 <= newDist2 && newDist1 <= newDist3 && newDist1 <= newDist4 && newDist1 <= newDist5 && newDist1 <= newDist6)
                            {
                                prevSolution = _bssf;
                                _bssf        = newRoute1;
                                found        = true;
                                count++;
                                break;
                            }
                            if (isCompleteSolution(newRoute2) && newDist2 < bestDist && newDist2 <= newDist1 && newDist2 <= newDist3 && newDist2 <= newDist4 && newDist2 <= newDist5 && newDist2 <= newDist6)
                            {
                                prevSolution = _bssf;
                                _bssf        = newRoute2;
                                found        = true;
                                count++;
                                break;
                            }
                            if (isCompleteSolution(newRoute3) && newDist3 < bestDist && newDist3 <= newDist2 && newDist3 <= newDist1 && newDist3 <= newDist4 && newDist3 <= newDist5 && newDist3 <= newDist6)
                            {
                                prevSolution = _bssf;
                                _bssf        = newRoute3;
                                found        = true;
                                count++;
                                break;
                            }
                            if (isCompleteSolution(newRoute4) && newDist4 < bestDist && newDist4 <= newDist2 && newDist4 <= newDist1 && newDist4 <= newDist3 && newDist4 <= newDist5 && newDist4 <= newDist6)
                            {
                                prevSolution = _bssf;
                                _bssf        = newRoute4;
                                found        = true;
                                count++;
                                break;
                            }
                            if (isCompleteSolution(newRoute5) && newDist5 < bestDist && newDist5 <= newDist2 && newDist5 <= newDist1 && newDist5 <= newDist4 && newDist5 <= newDist3 && newDist5 <= newDist6)
                            {
                                prevSolution = _bssf;
                                _bssf        = newRoute5;
                                found        = true;
                                count++;
                                break;
                            }
                            if (isCompleteSolution(newRoute6) && newDist6 < bestDist && newDist6 <= newDist2 && newDist6 <= newDist1 && newDist6 <= newDist4 && newDist6 <= newDist5 && newDist6 <= newDist3)
                            {
                                prevSolution = _bssf;
                                _bssf        = newRoute6;
                                found        = true;
                                count++;
                                break;
                            }
                        }
                        if (found)
                        {
                            break;
                        }
                    }
                    if (found)
                    {
                        break;
                    }
                }
            } while (found);


            timer.Stop();

            results[Cost]  = _bssf.CostOfRoute().ToString();
            results[Time]  = timer.Elapsed.ToString();
            results[Count] = count.ToString();

            return(results);
        }
Exemple #3
0
        public string[] TwoOptSolveProblem()
        {
            var results = new string[3];

            // get initial bssf using default algorithm
            int i, swap, temp, count = 0;

            int[] perm = new int[_cities.Length];
            _route = new List <City>();
            Random rnd = new Random();

            do
            {
                for (i = 0; i < perm.Length; i++)                                 // create a random permutation template
                {
                    perm[i] = i;
                }
                for (i = 0; i < perm.Length; i++)
                {
                    swap = i;
                    while (swap == i)
                    {
                        swap = rnd.Next(0, _cities.Length);
                    }
                    temp       = perm[i];
                    perm[i]    = perm[swap];
                    perm[swap] = temp;
                }
                _route.Clear();
                for (i = 0; i < _cities.Length; i++)                            // Now build the route using the random permutation
                {
                    _route.Add(_cities[perm[i]]);
                }
                _bssf = new TspSolution(_route);
            } while (CostOfBssf() == double.PositiveInfinity);                // until a valid route is found



            Stopwatch timer = new Stopwatch();

            timer.Start();
            count = 0;
            TspSolution prevSolution = _bssf;

            bool found = false;

            do
            {
                double bestDist = _bssf.CostOfRoute();
                for (i = 0; i < _bssf.Route.Count; i++)
                {
                    found = false;
                    for (int k = i + 1; k < _bssf.Route.Count; k++)
                    {
                        TspSolution newRoute = twoOptSwap(_bssf, i, k);
                        double      newDist  = newRoute.CostOfRoute();
                        if (isCompleteSolution(newRoute) && newDist < bestDist)
                        {
                            prevSolution = _bssf;
                            _bssf        = newRoute;
                            found        = true;
                            count++;
                            break;
                        }
                    }
                    if (found)
                    {
                        break;
                    }
                }
            } while (found);


            timer.Stop();

            results[Cost]  = _bssf.CostOfRoute().ToString();
            results[Time]  = timer.Elapsed.ToString();
            results[Count] = count.ToString();

            return(results);
        }