2025-01-10 11:35:44 +01:00
|
|
|
// Copyright 2010-2025 Google LLC
|
2011-11-16 17:31:41 +00:00
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
|
//
|
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
//
|
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
|
// limitations under the License.
|
2014-07-09 15:18:27 +00:00
|
|
|
|
2011-11-16 17:31:41 +00:00
|
|
|
// Pickup and Delivery Problem with Time Windows.
|
|
|
|
|
// The overall objective is to minimize the length of the routes delivering
|
|
|
|
|
// quantities of goods between pickup and delivery locations, taking into
|
|
|
|
|
// account vehicle capacities and node time windows.
|
|
|
|
|
// Given a set of pairs of pickup and delivery nodes, find the set of routes
|
|
|
|
|
// visiting all the nodes, such that
|
|
|
|
|
// - corresponding pickup and delivery nodes are visited on the same route,
|
|
|
|
|
// - the pickup node is visited before the corresponding delivery node,
|
|
|
|
|
// - the quantity picked up at the pickup node is the same as the quantity
|
|
|
|
|
// delivered at the delivery node,
|
|
|
|
|
// - the total quantity carried by a vehicle at any time is less than its
|
|
|
|
|
// capacity,
|
|
|
|
|
// - each node must be visited within its time window (time range during which
|
|
|
|
|
// the node is accessible).
|
|
|
|
|
// The maximum number of vehicles used (i.e. the number of routes used) is
|
2018-10-31 16:18:18 +01:00
|
|
|
// specified in the data but can be overridden using the --pdp_force_vehicles
|
2011-11-16 17:31:41 +00:00
|
|
|
// flag.
|
|
|
|
|
//
|
|
|
|
|
// A further description of the problem can be found here:
|
2013-12-12 14:43:41 +00:00
|
|
|
// http://en.wikipedia.org/wiki/Vehicle_routing_problem
|
2011-11-16 17:31:41 +00:00
|
|
|
// http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.123.9965&rep=rep1&type=pdf.
|
|
|
|
|
// Reads data in the format defined by Li & Lim
|
2016-01-21 20:00:39 +01:00
|
|
|
// (https://www.sintef.no/projectweb/top/pdptw/li-lim-benchmark/documentation/).
|
2011-11-16 17:31:41 +00:00
|
|
|
|
2022-09-09 16:49:24 +02:00
|
|
|
#include <algorithm>
|
2021-04-23 14:55:51 +02:00
|
|
|
#include <cstdint>
|
2024-11-15 14:42:52 +01:00
|
|
|
#include <cstdlib>
|
|
|
|
|
#include <limits>
|
|
|
|
|
#include <memory>
|
|
|
|
|
#include <optional>
|
2022-09-09 16:49:24 +02:00
|
|
|
#include <string>
|
2018-10-31 16:18:18 +01:00
|
|
|
#include <utility>
|
2011-11-16 17:31:41 +00:00
|
|
|
#include <vector>
|
|
|
|
|
|
2024-11-15 14:42:52 +01:00
|
|
|
#include "absl/algorithm/container.h"
|
2025-02-25 16:03:40 +01:00
|
|
|
#include "absl/base/log_severity.h"
|
2021-01-14 10:48:19 +01:00
|
|
|
#include "absl/flags/flag.h"
|
2024-11-15 14:42:52 +01:00
|
|
|
#include "absl/log/check.h"
|
2025-02-25 16:03:40 +01:00
|
|
|
#include "absl/log/globals.h"
|
2025-03-04 21:10:09 +01:00
|
|
|
#include "absl/log/log.h"
|
2018-10-31 16:18:18 +01:00
|
|
|
#include "absl/strings/str_format.h"
|
2024-11-15 14:42:52 +01:00
|
|
|
#include "absl/strings/string_view.h"
|
2018-10-31 16:18:18 +01:00
|
|
|
#include "google/protobuf/text_format.h"
|
2022-02-25 09:47:52 +01:00
|
|
|
#include "ortools/base/init_google.h"
|
2017-04-26 17:30:25 +02:00
|
|
|
#include "ortools/base/mathutil.h"
|
2019-01-05 12:34:50 +01:00
|
|
|
#include "ortools/base/timer.h"
|
2017-04-26 17:30:25 +02:00
|
|
|
#include "ortools/constraint_solver/routing.h"
|
|
|
|
|
#include "ortools/constraint_solver/routing_enums.pb.h"
|
2018-10-31 16:18:18 +01:00
|
|
|
#include "ortools/constraint_solver/routing_index_manager.h"
|
|
|
|
|
#include "ortools/constraint_solver/routing_parameters.h"
|
|
|
|
|
#include "ortools/constraint_solver/routing_parameters.pb.h"
|
2024-11-17 17:29:23 +01:00
|
|
|
#include "ortools/routing/parsers/lilim_parser.h"
|
|
|
|
|
#include "ortools/routing/parsers/simple_graph.h"
|
2011-11-16 17:31:41 +00:00
|
|
|
|
2020-10-23 11:50:14 +02:00
|
|
|
ABSL_FLAG(std::string, pdp_file, "",
|
|
|
|
|
"File containing the Pickup and Delivery Problem to solve.");
|
|
|
|
|
ABSL_FLAG(int, pdp_force_vehicles, 0,
|
|
|
|
|
"Force the number of vehicles used (maximum number of routes.");
|
|
|
|
|
ABSL_FLAG(bool, reduce_vehicle_cost_model, true,
|
|
|
|
|
"Overrides the homonymous field of "
|
|
|
|
|
"DefaultRoutingModelParameters().");
|
|
|
|
|
ABSL_FLAG(std::string, routing_search_parameters,
|
|
|
|
|
"first_solution_strategy:ALL_UNPERFORMED",
|
|
|
|
|
"Text proto RoutingSearchParameters (possibly partial) that will "
|
|
|
|
|
"override the DefaultRoutingSearchParameters()");
|
2024-11-15 14:42:52 +01:00
|
|
|
ABSL_FLAG(std::string, routing_model_parameters, "",
|
|
|
|
|
"Text proto RoutingModelParameters (possibly partial) that will "
|
|
|
|
|
"override the DefaultRoutingModelParameters()");
|
2011-11-16 17:31:41 +00:00
|
|
|
|
|
|
|
|
namespace operations_research {
|
|
|
|
|
|
2018-10-31 16:18:18 +01:00
|
|
|
// Returns the list of variables to use for the Tabu metaheuristic.
|
|
|
|
|
// The current list is:
|
|
|
|
|
// - Total cost of the solution,
|
|
|
|
|
// - Number of used vehicles,
|
|
|
|
|
// - Total schedule duration.
|
|
|
|
|
// TODO(user): add total waiting time.
|
2020-10-29 14:25:39 +01:00
|
|
|
std::vector<IntVar*> GetTabuVars(std::vector<IntVar*> existing_vars,
|
2024-11-15 14:42:52 +01:00
|
|
|
RoutingModel* routing) {
|
2020-10-29 14:25:39 +01:00
|
|
|
Solver* const solver = routing->solver();
|
|
|
|
|
std::vector<IntVar*> vars(std::move(existing_vars));
|
2018-10-31 16:18:18 +01:00
|
|
|
vars.push_back(routing->CostVar());
|
|
|
|
|
|
2020-10-29 14:25:39 +01:00
|
|
|
IntVar* used_vehicles = solver->MakeIntVar(0, routing->vehicles());
|
|
|
|
|
std::vector<IntVar*> is_used_vars;
|
2018-10-31 16:18:18 +01:00
|
|
|
// Number of vehicle used
|
|
|
|
|
is_used_vars.reserve(routing->vehicles());
|
|
|
|
|
for (int v = 0; v < routing->vehicles(); v++) {
|
|
|
|
|
is_used_vars.push_back(solver->MakeIsDifferentCstVar(
|
|
|
|
|
routing->NextVar(routing->Start(v)), routing->End(v)));
|
|
|
|
|
}
|
|
|
|
|
solver->AddConstraint(
|
|
|
|
|
solver->MakeEquality(solver->MakeSum(is_used_vars), used_vehicles));
|
|
|
|
|
vars.push_back(used_vehicles);
|
|
|
|
|
|
|
|
|
|
return vars;
|
2011-11-16 17:31:41 +00:00
|
|
|
}
|
|
|
|
|
|
2024-11-15 14:42:52 +01:00
|
|
|
// Scaling factor from callback.
|
|
|
|
|
template <typename C>
|
|
|
|
|
double ComputeScalingFactorFromCallback(const C& callback, int size) {
|
|
|
|
|
double max_value = 0;
|
|
|
|
|
for (int i = 0; i < size; ++i) {
|
|
|
|
|
for (int j = 0; j < size; ++j) {
|
|
|
|
|
max_value = std::max(max_value, callback(i, j));
|
2011-11-16 17:31:41 +00:00
|
|
|
}
|
|
|
|
|
}
|
2024-11-15 14:42:52 +01:00
|
|
|
const double max_scaled_total_distance =
|
|
|
|
|
(1LL << (std::numeric_limits<double>::digits - 2)) - 1;
|
|
|
|
|
const double max_scaled_distance = max_scaled_total_distance / size;
|
|
|
|
|
return max_scaled_distance / max_value;
|
2011-11-16 17:31:41 +00:00
|
|
|
}
|
|
|
|
|
|
2024-11-17 17:29:23 +01:00
|
|
|
void SetupModel(const routing::LiLimParser& parser,
|
|
|
|
|
const RoutingIndexManager& manager, RoutingModel* model,
|
|
|
|
|
RoutingSearchParameters* search_parameters) {
|
2024-11-15 14:42:52 +01:00
|
|
|
const int64_t kPenalty = 100000000;
|
|
|
|
|
const int64_t kFixedCost = 100000;
|
|
|
|
|
const int num_nodes = parser.NumberOfNodes();
|
|
|
|
|
const int64_t horizon =
|
2024-11-17 17:29:23 +01:00
|
|
|
absl::c_max_element(parser.time_windows(),
|
|
|
|
|
[](const routing::SimpleTimeWindow<int64_t>& a,
|
|
|
|
|
const routing::SimpleTimeWindow<int64_t>& b) {
|
|
|
|
|
return a.end < b.end;
|
|
|
|
|
})
|
2024-11-15 14:42:52 +01:00
|
|
|
->end;
|
|
|
|
|
const double scaling_factor = ComputeScalingFactorFromCallback(
|
|
|
|
|
[&parser](int64_t i, int64_t j) -> double {
|
|
|
|
|
const int depot = parser.Depot();
|
|
|
|
|
double fixed_cost = 0;
|
|
|
|
|
if (i == depot && j != depot) {
|
|
|
|
|
fixed_cost = kFixedCost;
|
|
|
|
|
} else if (i == j && i != depot) {
|
|
|
|
|
return kPenalty;
|
|
|
|
|
}
|
|
|
|
|
return fixed_cost + parser.GetTravelTime(i, j);
|
|
|
|
|
},
|
|
|
|
|
manager.num_nodes());
|
|
|
|
|
search_parameters->set_log_cost_scaling_factor(1.0 / scaling_factor);
|
|
|
|
|
const int vehicle_cost = model->RegisterTransitCallback(
|
|
|
|
|
[&parser, &manager, scaling_factor](int64_t i, int64_t j) {
|
2025-02-25 16:03:40 +01:00
|
|
|
return MathUtil::Round<int64_t>(
|
2024-11-15 14:42:52 +01:00
|
|
|
scaling_factor *
|
|
|
|
|
parser.GetDistance(manager.IndexToNode(i).value(),
|
|
|
|
|
manager.IndexToNode(j).value()));
|
2020-10-22 23:36:58 +02:00
|
|
|
});
|
2024-11-15 14:42:52 +01:00
|
|
|
model->SetArcCostEvaluatorOfAllVehicles(vehicle_cost);
|
|
|
|
|
model->SetFixedCostOfAllVehicles(
|
2025-02-25 16:03:40 +01:00
|
|
|
MathUtil::Round<int64_t>(kFixedCost * scaling_factor));
|
2024-11-15 14:42:52 +01:00
|
|
|
RoutingTransitCallback2 demand_evaluator =
|
|
|
|
|
[&parser, &manager](int64_t from_index, int64_t /*to_index*/) {
|
|
|
|
|
return parser.demands()[manager.IndexToNode(from_index).value()];
|
|
|
|
|
};
|
|
|
|
|
model->AddDimension(model->RegisterTransitCallback(demand_evaluator), 0,
|
|
|
|
|
parser.capacity(), /*fix_start_cumul_to_zero=*/true,
|
|
|
|
|
"demand");
|
|
|
|
|
RoutingTransitCallback2 time_evaluator = [&parser, &manager, scaling_factor](
|
|
|
|
|
int64_t from_index,
|
2021-04-02 14:58:16 +02:00
|
|
|
int64_t to_index) {
|
2025-02-25 16:03:40 +01:00
|
|
|
int64_t value = MathUtil::Round<int64_t>(
|
2024-11-15 14:42:52 +01:00
|
|
|
scaling_factor *
|
|
|
|
|
parser.GetTravelTime(manager.IndexToNode(from_index).value(),
|
|
|
|
|
manager.IndexToNode(to_index).value()));
|
|
|
|
|
return value;
|
2020-10-22 23:36:58 +02:00
|
|
|
};
|
2024-11-15 14:42:52 +01:00
|
|
|
model->AddDimension(model->RegisterTransitCallback(time_evaluator),
|
|
|
|
|
MathUtil::FastInt64Round(scaling_factor * horizon),
|
|
|
|
|
MathUtil::FastInt64Round(scaling_factor * horizon),
|
|
|
|
|
/*fix_start_cumul_to_zero=*/true, "time");
|
|
|
|
|
const RoutingDimension& time_dimension = model->GetDimensionOrDie("time");
|
|
|
|
|
Solver* const solver = model->solver();
|
2018-10-31 16:18:18 +01:00
|
|
|
for (int node = 0; node < num_nodes; ++node) {
|
2021-04-02 14:58:16 +02:00
|
|
|
const int64_t index =
|
2018-10-31 16:18:18 +01:00
|
|
|
manager.NodeToIndex(RoutingIndexManager::NodeIndex(node));
|
2024-11-15 14:42:52 +01:00
|
|
|
if (const std::optional<int> delivery = parser.GetDelivery(node);
|
|
|
|
|
delivery.has_value()) {
|
|
|
|
|
const int64_t delivery_index =
|
|
|
|
|
manager.NodeToIndex(RoutingIndexManager::NodeIndex(delivery.value()));
|
2016-10-07 17:40:41 +02:00
|
|
|
solver->AddConstraint(solver->MakeEquality(
|
2024-11-15 14:42:52 +01:00
|
|
|
model->VehicleVar(index), model->VehicleVar(delivery_index)));
|
2016-10-07 17:40:41 +02:00
|
|
|
solver->AddConstraint(
|
|
|
|
|
solver->MakeLessOrEqual(time_dimension.CumulVar(index),
|
|
|
|
|
time_dimension.CumulVar(delivery_index)));
|
2024-11-15 14:42:52 +01:00
|
|
|
model->AddPickupAndDelivery(index, delivery_index);
|
2011-11-16 17:31:41 +00:00
|
|
|
}
|
2020-10-29 14:25:39 +01:00
|
|
|
IntVar* const cumul = time_dimension.CumulVar(index);
|
2024-11-17 17:29:23 +01:00
|
|
|
const routing::SimpleTimeWindow<int64_t>& window =
|
|
|
|
|
parser.time_windows()[node];
|
2025-02-25 16:03:40 +01:00
|
|
|
cumul->SetMin(MathUtil::Round<int64_t>(scaling_factor * window.start));
|
|
|
|
|
cumul->SetMax(MathUtil::Round<int64_t>(scaling_factor * window.end));
|
2018-10-31 16:18:18 +01:00
|
|
|
}
|
|
|
|
|
|
2024-11-15 14:42:52 +01:00
|
|
|
if (search_parameters->local_search_metaheuristic() ==
|
2018-10-31 16:18:18 +01:00
|
|
|
LocalSearchMetaheuristic::GENERIC_TABU_SEARCH) {
|
|
|
|
|
// Create variable for the total schedule time of the solution.
|
|
|
|
|
// This will be used as one of the Tabu criteria.
|
|
|
|
|
// This is done here and not in GetTabuVarsCallback as it requires calling
|
|
|
|
|
// AddVariableMinimizedByFinalizer and this method must be called early.
|
2020-10-29 14:25:39 +01:00
|
|
|
std::vector<IntVar*> end_cumuls;
|
|
|
|
|
std::vector<IntVar*> start_cumuls;
|
2024-11-15 14:42:52 +01:00
|
|
|
for (int i = 0; i < model->vehicles(); ++i) {
|
|
|
|
|
end_cumuls.push_back(time_dimension.CumulVar(model->End(i)));
|
|
|
|
|
start_cumuls.push_back(time_dimension.CumulVar(model->Start(i)));
|
2018-10-31 16:18:18 +01:00
|
|
|
}
|
2020-10-29 14:25:39 +01:00
|
|
|
IntVar* total_time = solver->MakeIntVar(0, 99999999, "total");
|
2018-10-31 16:18:18 +01:00
|
|
|
solver->AddConstraint(solver->MakeEquality(
|
|
|
|
|
solver->MakeDifference(solver->MakeSum(end_cumuls),
|
|
|
|
|
solver->MakeSum(start_cumuls)),
|
|
|
|
|
total_time));
|
|
|
|
|
|
2024-11-15 14:42:52 +01:00
|
|
|
model->AddVariableMinimizedByFinalizer(total_time);
|
2018-10-31 16:18:18 +01:00
|
|
|
|
|
|
|
|
RoutingModel::GetTabuVarsCallback tabu_var_callback =
|
2020-10-29 14:25:39 +01:00
|
|
|
[total_time](RoutingModel* model) {
|
2020-10-22 23:36:58 +02:00
|
|
|
return GetTabuVars({total_time}, model);
|
|
|
|
|
};
|
2024-11-15 14:42:52 +01:00
|
|
|
model->SetTabuVarsCallback(tabu_var_callback);
|
2011-11-16 17:31:41 +00:00
|
|
|
}
|
2018-10-31 16:18:18 +01:00
|
|
|
|
2011-11-16 17:31:41 +00:00
|
|
|
// Adding penalty costs to allow skipping orders.
|
2024-11-15 14:42:52 +01:00
|
|
|
for (RoutingIndexManager::NodeIndex order(1); order < model->nodes();
|
2018-10-31 16:18:18 +01:00
|
|
|
++order) {
|
2021-04-02 14:58:16 +02:00
|
|
|
std::vector<int64_t> orders(1, manager.NodeToIndex(order));
|
2024-11-15 14:42:52 +01:00
|
|
|
model->AddDisjunction(orders,
|
2025-02-25 16:03:40 +01:00
|
|
|
MathUtil::Round<int64_t>(scaling_factor * kPenalty));
|
2024-11-15 14:42:52 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Outputs a solution to the current model in a string.
|
|
|
|
|
std::string VerboseOutput(const RoutingModel& model,
|
|
|
|
|
const RoutingIndexManager& manager,
|
|
|
|
|
const Assignment& assignment,
|
2024-11-17 17:29:23 +01:00
|
|
|
const routing::LiLimParser& parser,
|
|
|
|
|
double scaling_factor) {
|
2024-11-15 14:42:52 +01:00
|
|
|
std::string output;
|
|
|
|
|
const RoutingDimension& time_dimension = model.GetDimensionOrDie("time");
|
|
|
|
|
const RoutingDimension& load_dimension = model.GetDimensionOrDie("demand");
|
|
|
|
|
for (int i = 0; i < model.vehicles(); ++i) {
|
|
|
|
|
absl::StrAppendFormat(&output, "Vehicle %d: ", i);
|
|
|
|
|
int64_t index = model.Start(i);
|
|
|
|
|
if (model.IsEnd(assignment.Value(model.NextVar(index)))) {
|
|
|
|
|
output.append("empty");
|
|
|
|
|
} else {
|
|
|
|
|
while (!model.IsEnd(index)) {
|
|
|
|
|
absl::StrAppendFormat(&output, "%d ",
|
|
|
|
|
manager.IndexToNode(index).value());
|
|
|
|
|
const IntVar* vehicle = model.VehicleVar(index);
|
|
|
|
|
absl::StrAppendFormat(&output, "Vehicle(%d) ",
|
|
|
|
|
assignment.Value(vehicle));
|
|
|
|
|
const IntVar* arrival = time_dimension.CumulVar(index);
|
|
|
|
|
absl::StrAppendFormat(
|
|
|
|
|
&output, "Time(%d..%d) ",
|
2025-02-25 16:03:40 +01:00
|
|
|
MathUtil::Round<int64_t>(assignment.Min(arrival) * scaling_factor),
|
|
|
|
|
MathUtil::Round<int64_t>(assignment.Max(arrival) * scaling_factor));
|
2024-11-15 14:42:52 +01:00
|
|
|
const IntVar* load = load_dimension.CumulVar(index);
|
|
|
|
|
absl::StrAppendFormat(&output, "Load(%d..%d) ", assignment.Min(load),
|
|
|
|
|
assignment.Max(load));
|
|
|
|
|
const int64_t next_index = assignment.Value(model.NextVar(index));
|
|
|
|
|
absl::StrAppendFormat(
|
|
|
|
|
&output, "Transit(%f) ",
|
|
|
|
|
parser.GetTravelTime(manager.IndexToNode(index).value(),
|
|
|
|
|
manager.IndexToNode(next_index).value()));
|
|
|
|
|
index = next_index;
|
|
|
|
|
}
|
|
|
|
|
output.append("Route end ");
|
|
|
|
|
const IntVar* vehicle = model.VehicleVar(index);
|
|
|
|
|
absl::StrAppendFormat(&output, "Vehicle(%d) ", assignment.Value(vehicle));
|
|
|
|
|
const IntVar* arrival = time_dimension.CumulVar(index);
|
|
|
|
|
absl::StrAppendFormat(
|
|
|
|
|
&output, "Time(%d..%d) ",
|
2025-02-25 16:03:40 +01:00
|
|
|
MathUtil::Round<int64_t>(assignment.Min(arrival) * scaling_factor),
|
|
|
|
|
MathUtil::Round<int64_t>(assignment.Max(arrival) * scaling_factor));
|
2024-11-15 14:42:52 +01:00
|
|
|
const IntVar* load = load_dimension.CumulVar(index);
|
|
|
|
|
absl::StrAppendFormat(&output, "Load(%d..%d) ", assignment.Min(load),
|
|
|
|
|
assignment.Max(load));
|
|
|
|
|
}
|
|
|
|
|
output.append("\n");
|
|
|
|
|
}
|
|
|
|
|
return output;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Builds and solves a model from a file in the format defined by Li & Lim
|
|
|
|
|
// (https://www.sintef.no/projectweb/top/pdptw/li-lim-benchmark/documentation/).
|
|
|
|
|
bool LoadAndSolve(absl::string_view pdp_file,
|
|
|
|
|
const RoutingModelParameters& model_parameters,
|
|
|
|
|
RoutingSearchParameters& search_parameters) {
|
2024-11-17 17:29:23 +01:00
|
|
|
routing::LiLimParser parser;
|
2024-11-15 14:42:52 +01:00
|
|
|
if (!parser.LoadFile(pdp_file)) {
|
|
|
|
|
return false;
|
2011-11-16 17:31:41 +00:00
|
|
|
}
|
|
|
|
|
|
2024-11-15 14:42:52 +01:00
|
|
|
// Build pickup and delivery model.
|
|
|
|
|
const int num_nodes = parser.NumberOfNodes();
|
|
|
|
|
const int num_vehicles = parser.NumberOfVehicles();
|
|
|
|
|
const RoutingIndexManager::NodeIndex depot =
|
|
|
|
|
RoutingIndexManager::NodeIndex(parser.Depot());
|
|
|
|
|
RoutingIndexManager manager(num_nodes, num_vehicles, depot);
|
|
|
|
|
RoutingModel model(manager, model_parameters);
|
|
|
|
|
SetupModel(parser, manager, &model, &search_parameters);
|
|
|
|
|
|
2011-11-16 17:31:41 +00:00
|
|
|
// Solve pickup and delivery problem.
|
2018-10-31 16:18:18 +01:00
|
|
|
SimpleCycleTimer timer;
|
|
|
|
|
timer.Start();
|
2024-11-15 14:42:52 +01:00
|
|
|
const Assignment* assignment = model.SolveWithParameters(search_parameters);
|
2018-10-31 16:18:18 +01:00
|
|
|
timer.Stop();
|
2024-11-15 14:42:52 +01:00
|
|
|
LOG(INFO) << model.solver()->LocalSearchProfile();
|
2017-04-26 17:30:25 +02:00
|
|
|
if (nullptr != assignment) {
|
2024-11-15 14:42:52 +01:00
|
|
|
const double scaling_factor = search_parameters.log_cost_scaling_factor();
|
|
|
|
|
LOG(INFO) << VerboseOutput(model, manager, *assignment, parser,
|
|
|
|
|
scaling_factor);
|
|
|
|
|
const int64_t cost = assignment->ObjectiveValue();
|
|
|
|
|
LOG(INFO) << absl::StrFormat("Cost: %f", cost * scaling_factor);
|
|
|
|
|
int num_used_vehicles = 0;
|
|
|
|
|
int64_t total_fixed_cost = 0;
|
|
|
|
|
for (int v = 0; v < model.vehicles(); v++) {
|
|
|
|
|
if (model.IsVehicleUsed(*assignment, v)) {
|
|
|
|
|
num_used_vehicles++;
|
|
|
|
|
total_fixed_cost += model.GetFixedCostOfVehicle(v);
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-10-31 16:18:18 +01:00
|
|
|
int skipped_nodes = 0;
|
2024-11-15 14:42:52 +01:00
|
|
|
int64_t total_penalty = 0;
|
|
|
|
|
for (int node = 0; node < model.Size(); node++) {
|
|
|
|
|
if (!model.IsEnd(node) && !model.IsStart(node) &&
|
|
|
|
|
assignment->Value(model.NextVar(node)) == node) {
|
2018-10-31 16:18:18 +01:00
|
|
|
skipped_nodes++;
|
2024-11-15 14:42:52 +01:00
|
|
|
for (RoutingModel::DisjunctionIndex disjunction :
|
|
|
|
|
model.GetDisjunctionIndices(node)) {
|
|
|
|
|
total_penalty += model.GetDisjunctionPenalty(disjunction);
|
|
|
|
|
}
|
2018-10-31 16:18:18 +01:00
|
|
|
}
|
|
|
|
|
}
|
2024-11-15 14:42:52 +01:00
|
|
|
LOG(INFO) << absl::StrFormat(
|
|
|
|
|
"Distance: %.2f",
|
|
|
|
|
(cost - total_fixed_cost - total_penalty) * scaling_factor);
|
2018-10-31 16:18:18 +01:00
|
|
|
LOG(INFO) << "Number of skipped nodes: " << skipped_nodes;
|
|
|
|
|
LOG(INFO) << "Number of used vehicles: " << num_used_vehicles;
|
|
|
|
|
LOG(INFO) << "Time: " << timer.Get();
|
2011-11-16 17:31:41 +00:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-22 23:36:58 +02:00
|
|
|
} // namespace operations_research
|
2011-11-16 17:31:41 +00:00
|
|
|
|
2020-10-29 14:25:39 +01:00
|
|
|
int main(int argc, char** argv) {
|
2025-02-25 16:03:40 +01:00
|
|
|
absl::SetStderrThreshold(absl::LogSeverityAtLeast::kInfo);
|
2022-02-25 09:47:52 +01:00
|
|
|
InitGoogle(argv[0], &argc, &argv, true);
|
2018-10-31 16:18:18 +01:00
|
|
|
operations_research::RoutingModelParameters model_parameters =
|
|
|
|
|
operations_research::DefaultRoutingModelParameters();
|
|
|
|
|
model_parameters.set_reduce_vehicle_cost_model(
|
2020-10-21 00:21:54 +02:00
|
|
|
absl::GetFlag(FLAGS_reduce_vehicle_cost_model));
|
2024-11-15 14:42:52 +01:00
|
|
|
CHECK(google::protobuf::TextFormat::MergeFromString(
|
|
|
|
|
absl::GetFlag(FLAGS_routing_model_parameters), &model_parameters));
|
2018-10-31 16:18:18 +01:00
|
|
|
operations_research::RoutingSearchParameters search_parameters =
|
|
|
|
|
operations_research::DefaultRoutingSearchParameters();
|
|
|
|
|
CHECK(google::protobuf::TextFormat::MergeFromString(
|
2020-10-21 00:21:54 +02:00
|
|
|
absl::GetFlag(FLAGS_routing_search_parameters), &search_parameters));
|
2024-11-17 17:29:23 +01:00
|
|
|
if (!operations_research::LoadAndSolve(absl::GetFlag(FLAGS_pdp_file),
|
|
|
|
|
model_parameters, search_parameters)) {
|
2020-10-21 00:21:54 +02:00
|
|
|
LOG(INFO) << "Error solving " << absl::GetFlag(FLAGS_pdp_file);
|
2011-11-16 17:31:41 +00:00
|
|
|
}
|
2018-11-07 09:52:37 +01:00
|
|
|
return EXIT_SUCCESS;
|
2011-11-16 17:31:41 +00:00
|
|
|
}
|