Esempio n. 1
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()
        {
            string[] results = new string[3];

            ////// My code //////

            // Initialize variables
            int countHittingBottom = 0;
            int bssfUpdates        = 0;
            int maxUsedSize        = 0;
            int generatedNotes     = 0;
            int pruned             = 0;

            // First get the default
            defaultSolveProblem();
            double bssfCost = bssf.costOfRoute();

            // Then do branch and bound
            BnB           a       = new BnB();
            priorityQueue h       = new array();
            string        timeOut = "";

            a.solve(h, 0, Cities, ref bssfCost, ref countHittingBottom, ref bssf, time_limit, ref timeOut, ref bssfUpdates, ref maxUsedSize, ref pruned, ref generatedNotes);

            // Update text
            results[COST]  = bssfCost.ToString();   // load results into array here, replacing these dummy values
            results[TIME]  = timeOut;
            results[COUNT] = bssfUpdates.ToString();
            // MessageBox.Show("Max states "+maxUsedSize+ ". #BSSF updates: "+bssfUpdates+". States created: "+generatedNotes+". States pruned: "+pruned);

            return(results);
        }
Esempio n. 2
0
        void init(City[] Cities)
        {
            r       = new Random(SEED);
            species = new List <individual>(POPULATION);
            // Initialize to random cities
            for (int j = 0; j < POPULATION; j++)
            {
                species.Add(new individual(Cities.Length));

                // Default solve problem
                int[] perm = new int[Cities.Length];
                //{
                int       i, swap, temp1, count = 0;
                string[]  results = new string[3];
                ArrayList Route   = new ArrayList();
                Random    rnd     = new Random(j);

                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);
                        }
                        temp1      = perm[i];
                        perm[i]    = perm[swap];
                        perm[swap] = temp1;
                    }
                    Route.Clear();
                    for (i = 0; i < Cities.Length; i++)                            // Now build the route using the random permutation
                    {
                        Route.Add(Cities[perm[i]]);
                    }
                    bssf = new ProblemAndSolver.TSPSolution(Route);
                    count++;
                } while (pAS.costOfBssf() == double.PositiveInfinity);                // until a valid route is found
                //}


                species[j].copy(perm);
                //int[] temp1 = ;
                //species.Add(new individual(temp1));
                species[j].cost = determineFitness(ref Cities, species[j].path);
            }

            // Borrowing my generate cost function
            BnB temp = new BnB();

            temp.generateCosts(Cities, out cityBase);
        }