Пример #1
0
  /// <summary>
  ///   Solves the current routing problem.
  /// </summary>
  private void Solve(int number_of_orders, int number_of_vehicles) {
    Console.WriteLine("Creating model with " + number_of_orders +
                      " orders and " + number_of_vehicles + " vehicles.");
    // Finalizing model
    int number_of_locations = locations_.Length;

    RoutingModel model =
        new RoutingModel(number_of_locations, number_of_vehicles,
                         vehicle_starts_, vehicle_ends_);

    // Setting up dimensions
    const int big_number = 100000;
    NodeEvaluator2 manhattan_callback = new Manhattan(locations_, 1);
    model.AddDimension(
        manhattan_callback, big_number, big_number, false, "time");
    NodeEvaluator2 demand_callback = new Demand(order_demands_);
    model.AddDimension(demand_callback, 0, vehicle_capacity_, true, "capacity");

    // Setting up vehicles
    for (int vehicle = 0; vehicle < number_of_vehicles; ++vehicle) {
      int cost_coefficient = vehicle_cost_coefficients_[vehicle];
      NodeEvaluator2 manhattan_cost_callback =
          new Manhattan(locations_, cost_coefficient);
      model.SetVehicleCost(vehicle, manhattan_cost_callback);
      model.CumulVar(model.End(vehicle), "time").SetMax(
          vehicle_end_time_[vehicle]);
    }

    // Setting up orders
    for (int order = 0; order < number_of_orders; ++order) {
      model.CumulVar(order, "time").SetRange(order_time_windows_[order].start_,
                                             order_time_windows_[order].end_);
      int[] orders = {order};
      model.AddDisjunction(orders, order_penalties_[order]);
    }

    // Solving
    RoutingSearchParameters search_parameters =
        RoutingModel.DefaultSearchParameters();
    search_parameters.FirstSolutionStrategy =
        FirstSolutionStrategy.Types.Value.ALL_UNPERFORMED;

    Console.WriteLine("Search");
    Assignment solution = model.SolveWithParameters(search_parameters);

    if (solution != null) {
      String output = "Total cost: " + solution.ObjectiveValue() + "\n";
      // Dropped orders
      String dropped = "";
      for (int order = 0; order < number_of_orders; ++order) {
        if (solution.Value(model.NextVar(order)) == order) {
          dropped += " " + order;
        }
      }
      if (dropped.Length > 0) {
        output += "Dropped orders:" + dropped + "\n";
      }
      // Routes
      for (int vehicle = 0; vehicle < number_of_vehicles; ++vehicle) {
        String route = "Vehicle " + vehicle + ": ";
        long order = model.Start(vehicle);
        if (model.IsEnd(solution.Value(model.NextVar(order)))) {
          route += "Empty";
        } else {
          for (;
               !model.IsEnd(order);
               order = solution.Value(model.NextVar(order))) {
            IntVar local_load = model.CumulVar(order, "capacity");
            IntVar local_time = model.CumulVar(order, "time");
            route += order + " Load(" + solution.Value(local_load) + ") " +
                "Time(" + solution.Min(local_time) + ", " +
                solution.Max(local_time) + ") -> ";
          }
          IntVar load = model.CumulVar(order, "capacity");
          IntVar time = model.CumulVar(order, "time");
          route += order + " Load(" + solution.Value(load) + ") " +
              "Time(" + solution.Min(time) + ", " + solution.Max(time) + ")";
        }
        output += route + "\n";
      }
      Console.WriteLine(output);
    }
  }
Пример #2
0
    static void Solve(int size, int forbidden, int seed)
    {
        RoutingModel routing = new RoutingModel(size, 1);

        // 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).
        RoutingSearchParameters search_parameters =
        RoutingModel.DefaultSearchParameters();
        // Setting first solution heuristic (cheapest addition).
        search_parameters.FirstSolutionStrategy =
        FirstSolutionStrategy.Types.Value.PathCheapestArc;

        Assignment solution = routing.SolveWithParameters(search_parameters);
        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");
        }
    }