/// <summary>
        ///  solve the problem.  This is the entry point for the solver when the run button is clicked
        /// right now it just picks a simple solution. 
        /// </summary>
        public void solveProblem()
        {
            int x;
            Route = new ArrayList();
            ArrayList CitiesLeft = new ArrayList();

            for (x = 0; x < Cities.Length; x++)
            {
                CitiesLeft.Add( Cities[Cities.Length - x - 1]);
            }

            City curCity = (City)CitiesLeft[0];
            Route.Add(curCity);
            CitiesLeft.RemoveAt(0);

            while (CitiesLeft.Count > 0)
            {
                double minDist = 999999;
                int minInd = 0;

                for (int i = 0; i < CitiesLeft.Count; i++)
                {
                    City temp = (City)CitiesLeft[i];
                    if (curCity.costToGetTo(temp) < minDist)
                    {
                        minDist = curCity.costToGetTo(temp);
                        minInd = i;
                    }
                }
                Program.MainForm.tbCostOfTour.Text += "Ind " + minInd;
                curCity = (City)CitiesLeft[minInd];
                Route.Add(curCity);
                CitiesLeft.RemoveAt(minInd);
            }

            // call this the best solution so far.  bssf is the route that will be drawn by the Draw method.
            bssf = new TSPSolution(Route);
            // update the cost of the tour.
            Program.MainForm.tbCostOfTour.Text = " " + bssf.costOfRoute();
            Program.MainForm.tbElapsedTime.Text = "initial bssf";
            // do a refresh.
            Program.MainForm.Invalidate();

            //My solving begins here
            //generate first adjacency matrix
            double[,] firstAdj = new double[Cities.Length,Cities.Length];
            for (int i = 0; i < Cities.Length; i++)
            {
                for (int j = 0; j < Cities.Length; j++)
                {
                    if (i == j)
                        firstAdj[i, j] = 999999;
                    else
                        firstAdj[i, j] = Cities[i].costToGetTo(Cities[j]);
                }
            }
            State firstState = new State(firstAdj);
            Agenda myAgenda = new Agenda(Cities.Length);
            myAgenda.Add(firstState);
            //Start timer and go
            DateTime start = DateTime.Now;
            DateTime timeUp = DateTime.Now.AddSeconds(60);

            while ((myAgenda.hasNextState()) && (DateTime.Now <= timeUp))
            {
                State s = myAgenda.nextState;
                if ((s.routeSF.Count == Cities.Length) && s.costSF < bssf.costOfRoute())
                {
                    TSPSolution posS = new TSPSolution(s.routeSF);
                    if (posS.costOfRoute() < bssf.costOfRoute())
                    {
                        bssf = posS;
                        //Prune the agenda
                        myAgenda.Prune(bssf.costOfRoute());
                        // update the cost of the tour.
                        Program.MainForm.tbCostOfTour.Text = " " + bssf.costOfRoute();
                        Program.MainForm.tbElapsedTime.Text = " " + (DateTime.Now - start);
                        // do a refresh.
                        Program.MainForm.Invalidate();
                    }
                }
                else
                {
                        ArrayList newStates = s.Expand(Cities);
                        foreach (State state in newStates)
                        {
                            if (state.lowerBound < bssf.costOfRoute())
                                myAgenda.Add(state);
                        }
                }
            }
            if (DateTime.Now < timeUp)
                Program.MainForm.tbElapsedTime.Text += " true";

            Program.MainForm.tbCostOfTour.Text += " MC " + myAgenda.MaxCount + " NP " + myAgenda.NumPruned;
        }
Exemple #2
0
        /// <summary>
        ///  solve the problem.  This is the entry point for the solver when the run button is clicked
        /// right now it just picks a simple solution.
        /// </summary>
        public void solveProblem()
        {
            int x;

            Route = new ArrayList();
            ArrayList CitiesLeft = new ArrayList();

            for (x = 0; x < Cities.Length; x++)
            {
                CitiesLeft.Add(Cities[Cities.Length - x - 1]);
            }


            City curCity = (City)CitiesLeft[0];

            Route.Add(curCity);
            CitiesLeft.RemoveAt(0);

            while (CitiesLeft.Count > 0)
            {
                double minDist = 999999;
                int    minInd  = 0;

                for (int i = 0; i < CitiesLeft.Count; i++)
                {
                    City temp = (City)CitiesLeft[i];
                    if (curCity.costToGetTo(temp) < minDist)
                    {
                        minDist = curCity.costToGetTo(temp);
                        minInd  = i;
                    }
                }
                Program.MainForm.tbCostOfTour.Text += "Ind " + minInd;
                curCity = (City)CitiesLeft[minInd];
                Route.Add(curCity);
                CitiesLeft.RemoveAt(minInd);
            }


            // call this the best solution so far.  bssf is the route that will be drawn by the Draw method.
            bssf = new TSPSolution(Route);
            // update the cost of the tour.
            Program.MainForm.tbCostOfTour.Text  = " " + bssf.costOfRoute();
            Program.MainForm.tbElapsedTime.Text = "initial bssf";
            // do a refresh.
            Program.MainForm.Invalidate();

            //My solving begins here
            //generate first adjacency matrix
            double[,] firstAdj = new double[Cities.Length, Cities.Length];
            for (int i = 0; i < Cities.Length; i++)
            {
                for (int j = 0; j < Cities.Length; j++)
                {
                    if (i == j)
                    {
                        firstAdj[i, j] = 999999;
                    }
                    else
                    {
                        firstAdj[i, j] = Cities[i].costToGetTo(Cities[j]);
                    }
                }
            }
            State  firstState = new State(firstAdj);
            Agenda myAgenda   = new Agenda(Cities.Length);

            myAgenda.Add(firstState);
            //Start timer and go
            DateTime start  = DateTime.Now;
            DateTime timeUp = DateTime.Now.AddSeconds(60);

            while ((myAgenda.hasNextState()) && (DateTime.Now <= timeUp))
            {
                State s = myAgenda.nextState;
                if ((s.routeSF.Count == Cities.Length) && s.costSF < bssf.costOfRoute())
                {
                    TSPSolution posS = new TSPSolution(s.routeSF);
                    if (posS.costOfRoute() < bssf.costOfRoute())
                    {
                        bssf = posS;
                        //Prune the agenda
                        myAgenda.Prune(bssf.costOfRoute());
                        // update the cost of the tour.
                        Program.MainForm.tbCostOfTour.Text  = " " + bssf.costOfRoute();
                        Program.MainForm.tbElapsedTime.Text = " " + (DateTime.Now - start);
                        // do a refresh.
                        Program.MainForm.Invalidate();
                    }
                }
                else
                {
                    ArrayList newStates = s.Expand(Cities);
                    foreach (State state in newStates)
                    {
                        if (state.lowerBound < bssf.costOfRoute())
                        {
                            myAgenda.Add(state);
                        }
                    }
                }
            }
            if (DateTime.Now < timeUp)
            {
                Program.MainForm.tbElapsedTime.Text += " true";
            }

            Program.MainForm.tbCostOfTour.Text += " MC " + myAgenda.MaxCount + " NP " + myAgenda.NumPruned;
        }