forked from google/or-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrouting_sat.cc
245 lines (229 loc) · 9.81 KB
/
routing_sat.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
// Copyright 2010-2018 Google LLC
// 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.
#include "ortools/constraint_solver/routing.h"
#include "ortools/sat/cp_model.h"
namespace operations_research {
namespace sat {
namespace {
// For now only TSPs are supported.
// TODO(user): Support multi-route models and any type of constraints.
bool RoutingModelCanBeSolvedBySat(const RoutingModel& model) {
return model.vehicles() == 1;
}
// Adds an integer variable to a CpModelProto, returning its index in the proto.
int AddVariable(CpModelProto* cp_model, int64 lb, int64 ub) {
const int index = cp_model->variables_size();
IntegerVariableProto* const var = cp_model->add_variables();
var->add_domain(lb);
var->add_domain(ub);
return index;
}
// Structure to keep track of arcs created.
struct Arc {
int tail;
int head;
friend bool operator==(const Arc& a, const Arc& b) {
return a.tail == b.tail && a.head == b.head;
}
friend bool operator!=(const Arc& a, const Arc& b) { return !(a == b); }
friend bool operator<(const Arc& a, const Arc& b) {
return a.tail == b.tail ? a.head < b.head : a.tail < b.tail;
}
friend std::ostream& operator<<(std::ostream& strm, const Arc& arc) {
return strm << "{" << arc.tail << ", " << arc.head << "}";
}
template <typename H>
friend H AbslHashValue(H h, const Arc& a) {
return H::combine(std::move(h), a.tail, a.head);
}
};
using ArcVarMap = std::map<Arc, int>; // needs to be stable when iterating
// Adds all dimensions to a CpModelProto. Only adds path cumul constraints and
// cumul bounds.
void AddDimensions(const RoutingModel& model, const ArcVarMap& arc_vars,
CpModelProto* cp_model) {
for (const RoutingDimension* dimension : model.GetDimensions()) {
// Only a single vehicle class.
const RoutingModel::TransitCallback2& transit =
dimension->transit_evaluator(0);
std::vector<int> cumuls(dimension->cumuls().size(), -1);
const int64 min_start = dimension->cumuls()[model.Start(0)]->Min();
const int64 max_end = std::min(dimension->cumuls()[model.End(0)]->Max(),
dimension->vehicle_capacities()[0]);
for (int i = 0; i < cumuls.size(); ++i) {
if (model.IsStart(i) || model.IsEnd(i)) continue;
// Reducing bounds supposing the triangular inequality.
const int64 cumul_min =
std::max(sat::kMinIntegerValue.value(),
std::max(dimension->cumuls()[i]->Min(),
CapAdd(transit(model.Start(0), i), min_start)));
const int64 cumul_max =
std::min(sat::kMaxIntegerValue.value(),
std::min(dimension->cumuls()[i]->Max(),
CapSub(max_end, transit(i, model.End(0)))));
cumuls[i] = AddVariable(cp_model, cumul_min, cumul_max);
}
for (const auto arc_var : arc_vars) {
const int tail = arc_var.first.tail;
const int head = arc_var.first.head;
if (tail == head || model.IsStart(tail) || model.IsStart(head)) continue;
// arc[tail][head] -> cumuls[head] >= cumuls[tail] + transit.
// This is a relaxation of the model as it does not consider slack max.
ConstraintProto* ct = cp_model->add_constraints();
ct->add_enforcement_literal(arc_var.second);
LinearConstraintProto* arg = ct->mutable_linear();
arg->add_domain(transit(tail, head));
arg->add_domain(kint64max);
arg->add_vars(cumuls[tail]);
arg->add_coeffs(-1);
arg->add_vars(cumuls[head]);
arg->add_coeffs(1);
}
}
}
// Converts a RoutingModel with a single vehicle to a CpModelProto.
// The mapping between CPModelProto arcs and their corresponding arc variables
// is returned.
ArcVarMap PopulateSingleRouteModelFromRoutingModel(const RoutingModel& model,
CpModelProto* cp_model) {
ArcVarMap arc_vars;
const int num_nodes = model.Nexts().size();
CircuitConstraintProto* circuit =
cp_model->add_constraints()->mutable_circuit();
for (int tail = 0; tail < num_nodes; ++tail) {
std::unique_ptr<IntVarIterator> iter(
model.NextVar(tail)->MakeDomainIterator(false));
for (int head : InitAndGetValues(iter.get())) {
// Vehicle start and end nodes are represented as a single node in the
// CP-SAT model. We choose the start index to represent both. We can also
// skip any head representing a vehicle start as the CP solver will reject
// those.
if (model.IsStart(head)) continue;
if (model.IsEnd(head)) head = model.Start(0);
const int64 cost = tail != head ? model.GetHomogeneousCost(tail, head)
: model.UnperformedPenalty(tail);
if (cost == kint64max) continue;
const int index = AddVariable(cp_model, 0, 1);
circuit->add_literals(index);
circuit->add_tails(tail);
circuit->add_heads(head);
cp_model->mutable_objective()->add_vars(index);
cp_model->mutable_objective()->add_coeffs(cost);
gtl::InsertOrDie(&arc_vars, {tail, head}, index);
}
}
AddDimensions(model, arc_vars, cp_model);
return arc_vars;
}
// Converts a RoutingModel to a CpModelProto.
// The mapping between CPModelProto arcs and their corresponding arc variables
// is returned.
ArcVarMap PopulateModelFromRoutingModel(const RoutingModel& model,
CpModelProto* cp_model) {
if (model.vehicles() == 1) {
return PopulateSingleRouteModelFromRoutingModel(model, cp_model);
}
// TODO(user): Add support for multi-vehicle models.
return {};
}
// Converts a CpSolverResponse to an Assignment containing next variables.
// Note: supports multiple routes.
bool ConvertToSolution(const CpSolverResponse& response,
const RoutingModel& model, const ArcVarMap& arc_vars,
Assignment* solution) {
if (response.status() != CpSolverStatus::OPTIMAL &&
response.status() != CpSolverStatus::FEASIBLE)
return false;
const int depot = model.Start(0);
int vehicle = 0;
for (const auto& arc_var : arc_vars) {
if (response.solution(arc_var.second) != 0) {
const int tail = arc_var.first.tail;
const int head = arc_var.first.head;
if (head == depot) continue;
if (tail != depot) {
solution->Add(model.NextVar(tail))->SetValue(head);
} else {
solution->Add(model.NextVar(model.Start(vehicle)))->SetValue(head);
++vehicle;
}
}
}
// Close open routes.
for (int v = 0; v < model.vehicles(); ++v) {
int current = model.Start(v);
while (solution->Contains(model.NextVar(current))) {
current = solution->Value(model.NextVar(current));
}
solution->Add(model.NextVar(current))->SetValue(model.End(v));
}
return true;
}
void AddSolutionAsHintToModel(const Assignment* solution,
const RoutingModel& model,
const ArcVarMap& arc_vars,
CpModelProto* cp_model) {
if (solution == nullptr) return;
PartialVariableAssignment* const hint = cp_model->mutable_solution_hint();
hint->Clear();
const int depot = model.Start(0);
const int num_nodes = model.Nexts().size();
for (int tail = 0; tail < num_nodes; ++tail) {
const int tail_index = model.IsStart(tail) ? depot : tail;
const int head = solution->Value(model.NextVar(tail));
const int head_index = model.IsEnd(head) ? depot : head;
if (tail_index == depot && head_index == depot) continue;
hint->add_vars(gtl::FindOrDie(arc_vars, {tail_index, head_index}));
hint->add_values(1);
}
}
// Configures a CP-SAT solver and solves the given (routing) model using it.
// Returns the response of the search.
CpSolverResponse SolveRoutingModel(
const CpModelProto& cp_model, absl::Duration remaining_time,
const std::function<void(const CpSolverResponse& response)>& observer) {
// TODO(user): Add CP-SAT parameters to routing parameters.
SatParameters parameters;
parameters.set_linearization_level(2);
parameters.set_max_time_in_seconds(absl::ToDoubleSeconds(remaining_time));
parameters.set_num_search_workers(1);
Model model;
model.Add(NewSatParameters(parameters));
if (observer != nullptr) {
model.Add(NewFeasibleSolutionObserver(observer));
}
// TODO(user): Add an option to dump the CP-SAT model or check if the
// cp_model_dump_file flag in cp_model_solver.cc is good enough.
return SolveCpModel(cp_model, &model);
}
} // namespace
} // namespace sat
// Solves a RoutingModel using the CP-SAT solver. Returns false if no solution
// was found.
bool SolveModelWithSat(const RoutingModel& model,
const RoutingSearchParameters& search_parameters,
const Assignment* initial_solution,
Assignment* solution) {
if (!sat::RoutingModelCanBeSolvedBySat(model)) return false;
sat::CpModelProto cp_model;
cp_model.mutable_objective()->set_scaling_factor(
search_parameters.log_cost_scaling_factor());
cp_model.mutable_objective()->set_offset(search_parameters.log_cost_offset());
const sat::ArcVarMap arc_vars =
sat::PopulateModelFromRoutingModel(model, &cp_model);
sat::AddSolutionAsHintToModel(initial_solution, model, arc_vars, &cp_model);
return sat::ConvertToSolution(
sat::SolveRoutingModel(cp_model, model.RemainingTime(), nullptr), model,
arc_vars, solution);
}
} // namespace operations_research