Пример #1
0
        public void RunPartAWithoutConstraint()
        {
            Solution best_soln = new Solution(car_number);

            // random initial solution
            //go through all cities
            //add them randomly to a car
            for (int i = 0; i < cities; i++)
            {
                int random = r.Next(car_number - 1);
                best_soln.GetRoute(random).AddLast(i);
            }
            int best_cost = EvaluateCost(best_soln); //eval initial cost

            Console.WriteLine("INITIAL SOLUTION");
            best_soln.PrintSolution();

            // start SA processs
            while (temperature != 0)
            {
                int count = iterations;
                // # of iterations per temperature
                while (count > 0)
                {
                    // create new solution
                    Solution candidate = best_soln.CloneSolution();

                    // operator selection - switch cities between routes
                                         // switch city order within a route
                    int operation = r.Next(1, 3);
                    if (operation % 2 == 0)
                    {
                        // swtich cities between routes
                        candidate = SwitchCityRoute(candidate);
                    }
                    else
                    {
                        // switch city order
                        SwitchCityOrder(candidate);
                    }

                    int current_cost = EvaluateCost(candidate);
                    //candidate.PrintSolution();

                    // figure out if solution is going to be the best
                    if (SolutionAcceptance(best_soln, candidate) == true)
                    {
                        best_soln = candidate; // candidate is new best
                        best_cost = candidate.GetCost();
                    }
                    Console.WriteLine("Temperature: {0}, Count: {1}", temperature, count);
                    best_soln.PrintSolution();
                    // should we store 'all time best'?

                    count--;
                }
                // decrease temperature
                CoolingSchedule();
            }
            // print final best
            Console.WriteLine("Best Solution");
            best_soln.PrintSolution();

            return;
        }