Esempio n. 1
0
    static void Solve(int size, int forbidden, int seed)
    {
        RoutingModel routing = new RoutingModel(size, 1);

        // Setting first solution heuristic (cheapest addition).
        routing.SetFirstSolutionStrategy(RoutingModel.ROUTING_PATH_CHEAPEST_ARC);

        // Setting the cost function.
        // Put a permanent callback to the distance accessor here. The callback
        // has the following signature: ResultCallback2<int64, int64, int64>.
        // The two arguments are the from and to node inidices.
        RandomManhattan distances = new RandomManhattan(size, seed);

        routing.SetCost(distances);

        // Forbid node connections (randomly).
        Random randomizer            = new Random();
        long   forbidden_connections = 0;

        while (forbidden_connections < forbidden)
        {
            long from = randomizer.Next(size - 1);
            long to   = randomizer.Next(size - 1) + 1;
            if (routing.NextVar(from).Contains(to))
            {
                Console.WriteLine("Forbidding connection {0} -> {1}", from, to);
                routing.NextVar(from).RemoveValue(to);
                ++forbidden_connections;
            }
        }

        // Add dummy dimension to test API.
        routing.AddDimension(new ConstantCallback(),
                             size + 1,
                             size + 1,
                             true,
                             "dummy");

        // Solve, returns a solution if any (owned by RoutingModel).
        Assignment solution = routing.Solve();

        if (solution != null)
        {
            // Solution cost.
            Console.WriteLine("Cost = {0}", solution.ObjectiveValue());
            // Inspect solution.
            // Only one route here; otherwise iterate from 0 to routing.vehicles() - 1
            int route_number = 0;
            for (long node = routing.Start(route_number);
                 !routing.IsEnd(node);
                 node = solution.Value(routing.NextVar(node)))
            {
                Console.Write("{0} -> ", node);
            }
            Console.WriteLine("0");
        }
    }
Esempio n. 2
0
        public override void Solve()
        {
            var pparser = new Pparser(FpatIn);
            int n, l;

            pparser.Fetch(out n, out l);
            var rgnode = pparser.FetchN <string>(n);

            rgnode.Insert(0, "X");
            var model = new RoutingModel(n, 1);

            model.SetFirstSolutionStrategy(RoutingModel.ROUTING_GLOBAL_CHEAPEST_ARC);
            //model.SetMetaheuristic(RoutingModel.ROUTING_TABU_SEARCH);
            model.SetCost(new NodeEval(rgnode.ToArray()));
            model.SetDepot(0);
            for (int i = 0; i < n; i++)
            {
                var varI = model.NextVar(i);
                for (int j = 0; j < n; j++)
                {
                    if (i == j)
                    {
                        continue;
                    }
                    if (!NodeEval.FMatch(rgnode[i], rgnode[j]))
                    {
                        varI.RemoveValue(j);
                    }
                }
            }
            Console.WriteLine("solving");

            Assignment solution = model.Solve();

            model.UpdateTimeLimit(1000 * 60 * 3);
            if (solution != null)
            {
                // Solution cost.
                Console.WriteLine("Cost = {0}", solution.ObjectiveValue());
                for (var inode = (int)model.Start(0); !model.IsEnd(inode); inode = (int)solution.Value(model.NextVar(inode)))
                {
                    Console.WriteLine(rgnode[inode]);
                }
                Console.WriteLine("0");
            }
        }
Esempio n. 3
0
        /*
         * Number of Vehicles:
         */

        public static void Solve(int vehicles, string pathToXml = null)
        {
            /*
             * Add custom distance function
             */

            var dist = (pathToXml == null) ? new Distance() : new XmlDistance(pathToXml);

            /*
             * Generate constraint model
             */

            // Third argument defines depot, i.e. start-end node for round trip.
            var model = new  RoutingModel(dist.MapSize(), vehicles, 0);

            model.SetCost(dist);

            /*
             * This modification forces all Vehicles to visit at least one city.
             */

            /*for (int i = 0; i < Vehicles; i++) {
             *
             *  IntVar first = model.NextVar(model.Start(i));
             *  first.SetMax(dist.MapSize() - 1);
             * }
             *
             * /*
             * Solve problem and display solution
             */

            Assignment assignment = model.Solve();

            if (assignment != null)
            {
                Console.WriteLine("Total Distance: " + assignment.ObjectiveValue() + "\n");

                for (int i = 0; i < vehicles; i++)
                {
                    /*
                     * Display Round Trip:
                     */

                    Console.WriteLine("Round Trip for Vehicle " + i + "\n");

                    for (long node = model.Start(i); node < model.End(i); node = model.Next(assignment, node))
                    {
                        Console.Write(node + " -> ");
                    }

                    Console.WriteLine(model.Start(i) + "\n");

                    /*
                     * Display individual Section Distances for Verification:
                     */

                    var source = (int)model.Start(i);

                    while (source < model.End(i))
                    {
                        var target = (int)model.Next(assignment, source);

                        if (source < dist.MapSize() && target < dist.MapSize())
                        {
                            Console.WriteLine("From " + source + " travel to " + target + " -> distance = " +
                                              dist.Run(source, target));
                        }
                        else if (source < dist.MapSize())
                        {
                            Console.WriteLine("From " + source + " travel to 0 -> distance = " + dist.Run(source, 0));
                        }

                        source = target;
                    }

                    Console.WriteLine("\n");
                }
            }

            Console.ReadKey();
        }
Esempio n. 4
0
        public static void Solve(int vehicles, Distance distances)
        {
            /*
             * Generate constraint model
             */

            // Third argument defines depot, i.e. start-end node for round trip.
            var model = new RoutingModel(distances.MapSize(), vehicles, 0);

            // Node costs vs. Arc Costs
            model.SetArcCostEvaluatorOfAllVehicles(distances);

            /*
             * A vehicle must not visit more than 7 cities
             */

            //model.AddConstantDimension(1, 7, true, "count");

            /*
             * A vehicle must visit at least 3 cities
             */

            /*model.AddConstantDimension(1, Int32.MaxValue, true, "count");
             * var count = model.GetDimensionOrDie("count");
             * for (int i = 0; i < vehicles; i++) {
             *  count.CumulVar(model.End(i)).SetMin(3);
             * }*/

            /*
             * City 3 and 5 must NOT be visited by the same vehicle
             */

            //model.solver().Add(model.VehicleVar(3) != model.VehicleVar(5));

            /*
             * City 3 must be visited before city 5 (not necessarily by the same vehicle)
             */

            /*model.AddConstantDimension(1, Int32.MaxValue, true, "time");
             * var time = model.GetDimensionOrDie("time");
             * model.solver().Add(time.CumulVar(3) < time.CumulVar(5));*/

            /*
             * City 3 must be visited right after city 5 (not necessarily by the same vhicle)
             */

            /*model.AddConstantDimension(1, Int32.MaxValue, true, "time");
             * var time = model.GetDimensionOrDie("time");
             * model.solver().Add(time.CumulVar(5) + 1 == time.CumulVar(3));*/

            /*
             * Solve problem and display solution
             */

            Assignment assignment = model.Solve();

            if (assignment != null)
            {
                Console.WriteLine("Depot: " + model.GetDepot());
                Console.WriteLine("Total Distance: " + assignment.ObjectiveValue() + "\n");

                for (int i = 0; i < vehicles; i++)
                {
                    /*
                     * Display Trips:
                     */

                    Console.WriteLine("Round Trip for Vehicle " + i + ":");

                    long total  = 0;
                    var  source = model.Start(i);

                    while (!model.IsEnd(source))
                    {
                        var target = model.Next(assignment, source);

                        var from = model.IndexToNode(source);
                        var to   = model.IndexToNode(target);

                        total += distances.Run(from, to);
                        Console.WriteLine(
                            $" - From {distances.ToString(@from),-3} travel to {distances.ToString(to),-3} with distance: {distances.Run(@from, to),-3}");
                        source = target;
                    }

                    Console.WriteLine("Total Distance for Vehicle " + i + ": " + total + "\n");
                }
            }

            Console.ReadKey();
        }
Esempio n. 5
0
        public static void Solve(int vehicles, int capacity, Distance distances, Demands demands)
        {
            /*
             * Generate constraint model
             */

            // Third argument defines depot, i.e. start-end node for round trip.
            var model = new RoutingModel(distances.MapSize(), vehicles, 0);

            // Node costs vs. Arc Costs
            model.SetArcCostEvaluatorOfAllVehicles(distances);

            /*
             * Capacity Constraints
             */

            if (distances.MapSize() != demands.MapSize())
            {
                throw new ArgumentException("Define demand for each city.");
            }

            model.AddDimension(demands, 0, capacity, true, "capacity");

            /*
             * Solve problem and display solution
             */

            Assignment assignment = model.Solve();

            if (assignment != null)
            {
                Console.WriteLine("Depot: " + model.GetDepot());
                Console.WriteLine("Total Distance: " + assignment.ObjectiveValue() + "\n");

                for (int i = 0; i < vehicles; i++)
                {
                    /*
                     * Display Trips:
                     */

                    Console.WriteLine("Round Trip for Vehicle " + i + ":");

                    long total  = 0;
                    var  source = model.Start(i);

                    while (!model.IsEnd(source))
                    {
                        var s = model.IndexToNode(source);
                        var t = model.IndexToNode(model.Next(assignment, source));

                        total += distances.Run(s, t);

                        Console.WriteLine(String.Format(" - From {0,-2} (demand: {1,-1}) travel to {2,-2} (demand: {3,-1}) with distance: {4,-2}",
                                                        distances.ToString(s),
                                                        demands.Demand(s),
                                                        distances.ToString(t),
                                                        demands.Demand(t),
                                                        distances.Run(s, t)));
                        source = model.Next(assignment, source);
                    }

                    Console.WriteLine("Total Distance for Vehicle " + i + ": " + total + "\n");
                }
            }

            Console.ReadKey();
        }