Пример #1
0
 public BranchAndBoundSolver(City[] cities, ProblemAndSolver.TSPSolution bssf, string[] results, int timeLimit)
 {
     this.cities    = cities;
     this.bssf      = bssf;
     this.results   = results;
     this.timeLimit = timeLimit;
 }
Пример #2
0
        public Sequence(ProblemAndSolver.TSPSolution best)
        {
            route = new List <City>();
            foreach (City city in best.Route)
            {
                route.Add(city);
            }
            Rescore();

            /*
             * var list = best.Route;
             * route = new int[list.Count];
             * City[] cities = parent.GetCities();
             * int j = 0;
             * foreach (City c in list)
             * {
             *  for (int i = 0; i < cities.Length; i++)
             *  {
             *      if (cities[i].Equals(c))
             *      {
             *          route[j] = i;
             *      }
             *  }
             *  j++;
             * }
             * rescore();
             */
        }
Пример #3
0
        public ProblemAndSolver.TSPSolution Solve()
        {
            ArrayList route      = new ArrayList();
            int       numUpdates = -1;
            var       timer      = new Stopwatch();

            timer.Start();
            for (int startCity = 0; startCity < cities.Length; startCity++)
            {
                route.Clear();
                int currentCity = startCity;
                do
                {
                    route.Add(cities[currentCity]);
                    if (route.Count == cities.Length)
                    {
                        //go back to first city
                        double pathCost = cities[currentCity].costToGetTo(cities[startCity]);
                        if (double.IsPositiveInfinity(pathCost))
                        {
                            break;
                        }
                        ProblemAndSolver.TSPSolution solution = new ProblemAndSolver.TSPSolution(route);
                        if (solution.costOfRoute() < costOfBssf())
                        {
                            bssf = solution;
                            numUpdates++;
                            break;
                        }
                    }

                    double shortestRoute = double.PositiveInfinity;
                    int    nearestCity   = -1;
                    for (int i = 0; i < cities.Length; i++)
                    {
                        double pathCost = cities[currentCity].costToGetTo(cities[i]);
                        if (pathCost < shortestRoute && !route.Contains(cities[i]))
                        {
                            shortestRoute = pathCost;
                            nearestCity   = i;
                        }
                    }
                    if (nearestCity == -1)
                    {
                        //unable to find a path out of the current city to a new city
                        break;
                    }

                    currentCity = nearestCity;
                } while (true);
            }

            timer.Stop();

            results[ProblemAndSolver.COST]  = costOfBssf().ToString();                                     // load results array
            results[ProblemAndSolver.TIME]  = timer.Elapsed.ToString();
            results[ProblemAndSolver.COUNT] = numUpdates.ToString();
            return(bssf);
        }
Пример #4
0
        private double getCost(ArrayList route)
        {
            ProblemAndSolver.TSPSolution r = new ProblemAndSolver.TSPSolution(route);

            return r.costOfRoute();
        }
Пример #5
0
 public GreedySolver(City[] cities, ProblemAndSolver.TSPSolution bssf, string[] results)
 {
     this.cities  = cities;
     this.bssf    = bssf;
     this.results = results;
 }
Пример #6
0
 /// <summary>
 /// Makes states for all the nodes in the graph that aren't in state's route and stores them in a priority queue
 /// Time O(n^3) Space O(n^3)
 /// </summary>
 /// <param name="state">parentState. The state to expand</param>
 public void ExpandState(State parentState)
 {
     //if the state's bound >= bssf: increment prunedNodes & return
     if (parentState.bound > costOfBssf())
     {
         prunedNodes++;
         return;
     }
     //If all the nodes in the graph are in the route (compare sizes): wrap up the route by traveling to the first node and checking the result against bssf. Update bssf if needed.
     if (parentState.route.Count >= cities.Count())
     {
         //Time O(n)
         double costToReturn = TravelInMatrix(parentState.matrix, parentState.lastCity, 0);
         if (!double.IsPositiveInfinity(costToReturn))
         {
             parentState.bound += costToReturn;
             parentState.route.Add(cities[0]);
             if (parentState.bound < costOfBssf())
             {
                 bssf = new ProblemAndSolver.TSPSolution(parentState.route);
                 numUpdates++;
             }
         }
         return;
     }
     //Else:
     //For each node in the graph that isn't in the route: time O(n^3) space O(n^3)
     for (int i = 0; i < cities.Count(); i++)
     {
         City city = cities[i];
         if (double.IsPositiveInfinity(cities[parentState.lastCity].costToGetTo(city)) || parentState.route.Contains(i))
         {
             continue;
         }
         //Copy the parent node state
         //Time O(n^2) size O(n^2)
         State childState = parentState.Copy();
         nodesCreated++;
         childState.route.Add(cities[i]);
         childState.lastCity = i;
         //Travel in the matrix to the new node and set the appropriate cells to infinity (TravelInMatrix). time O(n)
         double travelCost = TravelInMatrix(childState.matrix, parentState.lastCity, i);
         if (double.IsPositiveInfinity(travelCost))
         {
             continue;
         }
         childState.bound += travelCost;
         //Reduce the matrix and update the bound. time O(n^2)
         childState.bound += ReduceMatrix(childState.matrix);
         //If the bound is lower than bssf's:
         if (childState.bound < costOfBssf())
         {
             //add the state to the priority queue. time O(logn)
             queue.Insert(childState);
         }
         else
         {
             prunedNodes++;
         }
     }
 }