Пример #1
0
        public TSPath(TSPath parent, int destination)
        {
            isFinished       = false;
            this.destination = destination;
            this.parent      = parent;
            int sourceCity = parent.destination;

            minDistance   = parent.minDistance + parent.distances[sourceCity, this.destination];
            level         = parent.level + 1;
            cityPathOrder = (int[])parent.cityPathOrder.Clone();
            cityPathOrder[destination] = level;
            if (parent.cityPathOrder[destination] > 0)
            {
                if (level == numCities + 1 && destination == 0)
                {
                    cityPathOrder[0] = 1;//this is to reset the first city to being first not last
                    //minDistance += parent.distances[sourceCity, destination];
                    score      = 0;
                    isFinished = true;
                    return;
                }
                minDistance = double.PositiveInfinity;
                score       = double.PositiveInfinity;
                return;
            }
            if (double.IsPositiveInfinity(parent.distances[sourceCity, destination]))
            {
                minDistance = double.PositiveInfinity;
                score       = double.PositiveInfinity;
                return;
            }
            distances = (double[, ])parent.distances.Clone();
            commonInitialization();
        }
Пример #2
0
        public int CompareTo(Object obj)
        {
            if (obj == null)
            {
                return(1);
            }

            TSPath otherTemperature = obj as TSPath;

            if (otherTemperature != null)
            {
                return(this.score.CompareTo(otherTemperature.score));
            }
            else
            {
                throw new ArgumentException("Object is not a TSPath");
            }
        }
Пример #3
0
 // used to initialize each city
 public TSPath(City[] cities)
 {
     isFinished    = false;
     parent        = null;
     minDistance   = 0;
     level         = 1;
     numCities     = cities.Length;
     distances     = new double[numCities, numCities];
     cityPathOrder = new int[numCities];
     for (int i = 0; i < numCities; i++)
     {
         City refCity = cities[i];
         for (int j = 0; j < numCities; j++)
         {
             double distance = refCity.costToGetTo(cities[j]);
             distances[i, j] = distance;
         }
         distances[i, i]  = double.PositiveInfinity;
         cityPathOrder[i] = 0;
     }
     cityPathOrder[0] = 1;
     commonInitialization();
 }
Пример #4
0
 public TSDistance(TSPath path)
 {
     this.path   = path.cityPathOrder;
     minDistance = path.getDistance();
 }
Пример #5
0
        /// <summary>
        /// performs a Branch and Bound search of the state space of partial tours
        /// stops when time limit expires and uses BSSF as solution
        /// </summary>
        /// <returns>results array for GUI that contains three ints: cost of solution, time spent to find solution, number of solutions found during search (not counting initial BSSF estimate)</returns>
        public string[] bBSolveProblem()
        {
            //make first TSPath and add it to the binary heap
            MinHeap <TSPath> heap = new MinHeap <TSPath>();

            int       timeout   = TIME_LIMIT * 1000;
            Stopwatch stopWatch = new Stopwatch();

            stopWatch.Start();
            heap.Insert(new TSPath(GetCities()));
            MinHeap <TSDistance> minDistances = new MinHeap <TSDistance>();

            minDistances.Insert(new TSDistance(heap.Peek()));
            double bestPossibleSolution = heap.Peek().getDistance();
            int    numSolutions         = 0;

            defaultSolveProblem();
            double bestSolutionDistance = costOfBssf();

            int[] bssfArray = new int[0];
            numStatesCreated = 1;
            numStatesPruned  = 1;
            maxStatesStored  = 1;
            //repeat following until timeout or best solution found
            while (heap.Count != 0 && stopWatch.ElapsedMilliseconds < timeout)
            {
                maxStatesStored = (maxStatesStored > heap.Count ? maxStatesStored : heap.Count);
                TSPath min = heap.ExtractMin();
                if (min.isFinished)
                {
                    numSolutions++;
                    ArrayList Route    = new ArrayList();
                    City[]    cities   = GetCities();
                    int[]     minArray = min.getPath();
                    for (int i = 0; i < minArray.Length; i++)
                    {
                        Route.Add(cities[minArray[i]]);
                    }
                    double minLength = min.getDistance();

                    TSPSolution solution = new TSPSolution(Route);

                    if (solution.costOfRoute() < bestSolutionDistance)
                    {
                        bssfArray            = min.getPath();
                        bestSolutionDistance = solution.costOfRoute();
                        bssf = solution;
                    }
                }
                else
                {
                    if (min.getDistance() > bestSolutionDistance)
                    {
                        numStatesPruned++;
                    }
                    else
                    {
                        foreach (TSPath child in min.getAllChildren())
                        {
                            numStatesCreated++;
                            if (child.getDistance() < bestSolutionDistance)
                            {
                                //minDistances.Insert(new TSDistance(child));

                                heap.Insert(child);
                            }
                            else
                            {
                                numStatesPruned++;
                            }
                        }
                    }
                }
            }
            string[] results = new string[3];

            /*if(numSolutions > 0)
             * {
             *  ArrayList Route = new ArrayList();
             *  City[] cities = GetCities();
             *  for(int i = 0; i < bssfArray.Length; i++)
             *  {
             *      Route.Add(cities[bssfArray[i]]);
             *  }
             *  bssf = new TSPSolution(Route);
             * }*/

            results[COST]  = costOfBssf().ToString();   // load results into array here, replacing these dummy values
            results[TIME]  = (((double)stopWatch.ElapsedMilliseconds) / 1000).ToString();
            results[COUNT] = numSolutions.ToString();


            return(results);
        }