Ejemplo n.º 1
0
 public Solution ImproveSolution(Solution initialSolution, List<Customer> selected, List<Route> routes)
 {
     if (selected == null)
         return initialSolution;
     var oldDist = initialSolution.TotalDistance();
     Solution bestSolution = initialSolution;
     foreach (var c in selected)
     {
         foreach (var r in routes)
         {
             var transfer = BestInsertionPlaceCzech2001(initialSolution, c, r);
             if(transfer.Value < oldDist)
             {
                 bestSolution = transfer.Key;
                 oldDist = transfer.Value;
             }
         }
     }
     return bestSolution;
 }
Ejemplo n.º 2
0
        private void Button1Click(object sender, EventArgs e)
        {
            var reader = new CsvProblemReader();
            _problem = reader.Read(@"TestData/c1_2_1_v50_c200.csv");
            var sol87Solver = new Solomon87();

            var t = ShapleyDivider.Divide(_problem);

            _p1 = sol87Solver.Solve(t[0]);
            _p2 = sol87Solver.Solve(t[1]);
            _p3 = sol87Solver.Solve(t[2]);

            _p12 = sol87Solver.Solve(ShapleyDivider.Merge(t[0], t[1]));
            _p23 = sol87Solver.Solve(ShapleyDivider.Merge(t[1], t[2]));
            _p31 = sol87Solver.Solve(ShapleyDivider.Merge(t[2], t[0]));

            _p123 = sol87Solver.Solve(_problem);

            textBox1.Text = _p123.PrintToString();
            Draw();
        }
Ejemplo n.º 3
0
        public static List<double> ComputeGains(ref Solution p1, ref Solution p2, ref Solution p3, ref Solution p12, ref Solution p23, ref Solution p31, ref Solution p123,
            int max_iters = 30, Update update = null, VrpSolver solver = null)
        {
            if (solver == null) solver = DefaultSolver;
            /////

            for (var i = 0; i < max_iters; ++i)
            {
                p1 = solver.Solve(p1.Problem, p1);
                p2 = solver.Solve(p2.Problem, p2);
                p3 = solver.Solve(p3.Problem, p3);
                p12 = solver.Solve(p12.Problem, p12);
                p23 = solver.Solve(p23.Problem, p23);
                p31 = solver.Solve(p31.Problem, p31);
                p123 = solver.Solve(p123.Problem, p123);

                var v1 = p1.TotalDistance();
                var v2 = p2.TotalDistance();
                var v3 = p3.TotalDistance();
                var v12 = p12.TotalDistance();
                var v23 = p23.TotalDistance();
                var v31 = p31.TotalDistance();
                var v123 = p123.TotalDistance();

                var phi1 = 1.0 * 3.0 / v1 + 1.0 / 6.0 * (v12 - v2) + 1.0 / 6.0 * (v31 - v3) + 1.0 / 3.0 * (v123 - v23);
                var phi2 = 1.0 * 3.0 / v2 + 1.0 / 6.0 * (v23 - v3) + 1.0 / 6.0 * (v12 - v1) + 1.0 / 3.0 * (v123 - v31);
                var phi3 = 1.0 * 3.0 / v3 + 1.0 / 6.0 * (v31 - v1) + 1.0 / 6.0 * (v23 - v2) + 1.0 / 3.0 * (v123 - v12);

                p1.Phi = v1 - phi1;
                p2.Phi = v2 - phi2;
                p3.Phi = v3 - phi3;

                update(max_iters, i);
            }

            return null;
        }
Ejemplo n.º 4
0
        public KeyValuePair<Solution, double> BestInsertionPlaceCzech2001(Solution solution, Customer customer, Route route)
        {
            // Route customerRoute = solution.Routes[customerRouteIndex];
            // Customer customer = (Customer)customerRoute.RouteList[cusotomerIndex];
            // Route targetRoute = solution.Routes[targetRouteIndex];

            //int bestSolution = -1;
            double bestCost = solution.TotalDistance();

            //solution.Routes[cusotomerIndex] = customerRoute.Copy();
            //solution.Routes[cusotomerIndex].RouteList.RemoveAt(cusotomerIndex);

            var routeIndex = route.Index();
            var newSolution = solution.Copy();
            var oldRouteIdx = customer.Route.Index();
            var oldRoute = newSolution.Routes[oldRouteIdx].Copy();
            oldRoute.RemoveAt(customer.Index());
            newSolution.Routes[oldRouteIdx] = oldRoute;
            Solution bestSolutionCopy = null;

            for (int i = 1; i < route.RouteList.Count; ++i)
            {

                var newRoute = solution.Routes[routeIndex].Copy();
                newRoute.InsertCustomer(customer, i);
                newSolution.Routes[routeIndex] = newRoute;
                double newCost = newSolution.TotalDistance();
                if (newRoute.IsFeasible() && newCost < bestCost)
                {
                    bestSolutionCopy = newSolution.Copy();
                    bestCost = newCost;
                }
            }

            return new KeyValuePair<Solution, double>(bestSolutionCopy, bestCost);
        }
Ejemplo n.º 5
0
        public override Solution Solve(Problem problem, Solution initialSolution)
        {
            var cNumber = (int) (problem.Customers.Count*CNumCoef);

            /* 1. */
            var selected = SelectionStage(initialSolution, cNumber);

            /* 2. */
            var p = .75;
            var probablyOrdered = OrderRoutesWithProb(initialSolution, p);

            /* 3. */
            var newSolution = ImproveSolution(initialSolution, selected, probablyOrdered);

            return newSolution;
        }
Ejemplo n.º 6
0
        public List<Customer> SelectionStage(Solution initialSolution, int cNumber)
        {
            //var deletionList = new List<Customer>(cNumber);

            var iter = 0;
            var neighbourhood = new List<Customer>();
            var routes = new List<Route>(initialSolution.Routes);

            while(neighbourhood.Count < cNumber){

                var customerList = initialSolution.CustomerListCopy();

                /* 1.1. */
                int routeOneIdx = RandomSampleIndex(0, routes.Count - 1);

                /* 1.2. */
                int customerIdx = RandomSampleIndex(1, routes[routeOneIdx].RouteList.Count - 2);
                Route routeOne = initialSolution.Routes[routeOneIdx];
                Customer customer = (Customer)routeOne.RouteList[customerIdx];

                /* 1.3. */
                neighbourhood = NeighbourhoodIrmSearch(customerList, customer);

                /* 1.4. */
                foreach (Customer cust in neighbourhood)
                    customerList.Remove(cust);

                /* 1.5. */
                if (neighbourhood.Count == 1)
                {
                    //routeOneIdx = RandomSampleIndex(0, routes.Count - 1);
                    //routeOne = initialSolution.Routes[routeOneIdx];
                    // implement 1.6.-1.8.
                    /*
                    int bestInsertion = BestInsertionPlaceCzech2001(initialSolution, customer.Route.Index(),
                                                                customer.Index(),
                                                                routeOne.Index());*/
                    //throw new Exception("WL09 LS: Only one neighbour is found");
                }

                //if (neighbourhood.Count < cNumber)
                //   return SelectionStage(initialSolution, cNumber);

                if (iter == 30000)
                    return null;
                iter++;
            }

            return neighbourhood;
        }
Ejemplo n.º 7
0
 public List<Route> OrderRoutesWithProb(Solution solution, double prob)
 {
     var routes = solution.Copy().Routes;
     var rnd = new Random();
     var dice = rnd.NextDouble();
     if(dice > prob)
         return new List<Route>(routes);
     return routes.OrderBy(r => r.RouteList.Count).ToList();
 }
Ejemplo n.º 8
0
        public override Solution Solve(Problem problem, Solution initialSolution = null)
        {
            var solution = new Solution(problem);
            var unroutedCustomers = new List<Customer>(problem.Customers);
            int seedId = SelectSeedCustomer(unroutedCustomers, problem.Depot);
            Customer seed = unroutedCustomers[seedId];
            unroutedCustomers.RemoveAt(seedId);
            var partialRoute = new Route(problem, seed);

            while (unroutedCustomers.Count > 0)
            {
                Route newRoute = partialRoute.Copy();
                var bestU = new List<int>();
                var c1Vals = new List<double>();
                foreach (Customer cust in unroutedCustomers)
                {
                    double minC1 = double.MaxValue;
                    int optimalU = 1;
                    for (int i = 1; i < newRoute.RouteList.Count - 1; ++i)
                    {
                        double c1 = CriterionC1(i, cust, i + 1, newRoute);
                        if (c1 < minC1)
                        {
                            minC1 = c1;
                            optimalU = i;
                        }
                    }
                    bestU.Add(optimalU);
                    c1Vals.Add(minC1);
                }

                int bestCust = 0;
                double minC2 = double.MaxValue;
                for (int i = 0; i < unroutedCustomers.Count; ++i)
                {
                    double c2 = CriterionC2(unroutedCustomers[i], c1Vals[i], newRoute);
                    if (c2 < minC2)
                    {
                        minC2 = c2;
                        bestCust = i;
                    }
                }

                Customer newCustomer = unroutedCustomers[bestCust];
                newRoute.InsertCustomer(newCustomer, bestU[bestCust]);

                if (newRoute.IsFeasible())
                {
                    unroutedCustomers.RemoveAt(bestCust);
                    partialRoute = newRoute;
                }
                else
                {
                    solution.AddRoute(partialRoute);
                    seedId = SelectSeedCustomer(unroutedCustomers, problem.Depot);
                    seed = unroutedCustomers[seedId];
                    unroutedCustomers.RemoveAt(seedId);
                    partialRoute = new Route(problem, seed);
                }

                if (unroutedCustomers.Count == 0)
                    solution.AddRoute(partialRoute);
            }

            return solution;
        }
Ejemplo n.º 9
0
 public Solution Copy()
 {
     var sol = new Solution(Problem);
     foreach (Route route in Routes)
         if(route.RouteList.Count > 2)
            sol.AddRoute(route.Copy());
     return sol;
 }