Skip to content

Commit 3622b4f

Browse files
committed
backport fixup from main
1 parent 0d03516 commit 3622b4f

21 files changed

+133
-89
lines changed

go.mod

+2-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ module github.com/google/or-tools
33
go 1.22.2
44

55
require (
6-
github.com/golang/glog v1.2.4
7-
github.com/google/go-cmp v0.6.0
8-
google.golang.org/protobuf v1.34.2
6+
github.com/golang/glog v1.2.4
7+
google.golang.org/protobuf v1.36.5
98
)

go.sum

+2
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,5 @@ github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
44
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
55
google.golang.org/protobuf v1.34.2 h1:6xV6lTsCfpGD21XK49h7MhtcApnLqkfYgPcdHftf6hg=
66
google.golang.org/protobuf v1.34.2/go.mod h1:qYOHts0dSfpeUzUFpOMr/WGzszTmLH+DiWniOlNbLDw=
7+
google.golang.org/protobuf v1.36.5 h1:tPhr+woSbjfYvY6/GPufUoYizxw1cF/yFoxJ2fmpwlM=
8+
google.golang.org/protobuf v1.36.5/go.mod h1:9fA7Ob0pmnwhb644+1+CVWFRbNajQ6iRojtC/QF5bRE=

ortools/algorithms/BUILD.bazel

+1
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,7 @@ cc_test(
430430
deps = [
431431
":n_choose_k",
432432
"//ortools/base:dump_vars",
433+
"//ortools/base:fuzztest",
433434
"//ortools/base:gmock_main",
434435
"//ortools/base:mathutil",
435436
"//ortools/util:flat_matrix",

ortools/constraint_solver/BUILD.bazel

+2-1
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@
1616
load("@com_google_protobuf//bazel:cc_proto_library.bzl", "cc_proto_library")
1717
load("@com_google_protobuf//bazel:java_proto_library.bzl", "java_proto_library")
1818
load("@com_google_protobuf//bazel:proto_library.bzl", "proto_library")
19+
load("@com_google_protobuf//bazel:py_proto_library.bzl", "py_proto_library")
1920
load("@rules_cc//cc:defs.bzl", "cc_library")
2021
load("@rules_python//python:defs.bzl", "py_library")
21-
load("@rules_python//python:proto.bzl", "py_proto_library")
2222

2323
package(default_visibility = ["//visibility:public"])
2424

@@ -188,6 +188,7 @@ cc_library(
188188
":solver_parameters_cc_proto",
189189
":routing_parameters_cc_proto",
190190
"//ortools/base",
191+
"//ortools/base:base_export",
191192
"//ortools/base:bitmap",
192193
"//ortools/base:file",
193194
"//ortools/base:int_type",

ortools/constraint_solver/samples/cp_is_fun_cp.py

-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
"""
2323
# [START import]
2424
from ortools.constraint_solver import pywrapcp
25-
2625
# [END import]
2726

2827

ortools/constraint_solver/samples/nqueens_cp.py

-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
# [START import]
1818
import sys
1919
from ortools.constraint_solver import pywrapcp
20-
2120
# [END import]
2221

2322

ortools/constraint_solver/samples/simple_cp_program.py

-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717

1818
# [START import]
1919
from ortools.constraint_solver import pywrapcp
20-
2120
# [END import]
2221

2322

ortools/routing/parsers/cvrptw_lib.cc

+37-25
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,18 @@
1818
#include <limits>
1919
#include <memory>
2020
#include <random>
21-
#include <set>
2221
#include <string>
2322
#include <utility>
23+
#include <vector>
2424

2525
#include "absl/container/btree_set.h"
26+
#include "absl/random/distributions.h"
27+
#include "absl/random/random.h"
2628
#include "absl/strings/str_format.h"
29+
#include "absl/strings/string_view.h"
30+
#include "absl/types/span.h"
2731
#include "ortools/base/logging.h"
32+
#include "ortools/constraint_solver/constraint_solver.h"
2833
#include "ortools/constraint_solver/routing.h"
2934
#include "ortools/constraint_solver/routing_index_manager.h"
3035

@@ -157,12 +162,17 @@ int64_t StopServiceTimePlusTransition::Compute(NodeIndex from,
157162
: stop_time_ + transition_time_(from, to);
158163
}
159164

160-
void DisplayPlan(
161-
const RoutingIndexManager& manager, const RoutingModel& routing,
162-
const operations_research::Assignment& plan, bool use_same_vehicle_costs,
163-
int64_t max_nodes_per_group, int64_t same_vehicle_cost,
164-
const operations_research::RoutingDimension& capacity_dimension,
165-
const operations_research::RoutingDimension& time_dimension) {
165+
void DisplayPlan(const RoutingIndexManager& manager,
166+
const RoutingModel& routing,
167+
const operations_research::Assignment& plan,
168+
bool use_same_vehicle_costs, int64_t max_nodes_per_group,
169+
int64_t same_vehicle_cost,
170+
absl::Span<const std::string> dimension_names) {
171+
std::vector<const operations_research::RoutingDimension*> dimensions;
172+
for (const std::string& dimension_name : dimension_names) {
173+
dimensions.push_back(&routing.GetDimensionOrDie(dimension_name));
174+
}
175+
166176
// Display plan cost.
167177
std::string plan_output = absl::StrFormat("Cost %d\n", plan.ObjectiveValue());
168178

@@ -207,6 +217,18 @@ void DisplayPlan(
207217
}
208218

209219
// Display actual output for each vehicle.
220+
const auto str_append_variable =
221+
[&plan, &plan_output](const IntVar* var, absl::string_view name) {
222+
if (var == nullptr || !plan.Contains(var)) return;
223+
const int64_t var_min = plan.Min(var);
224+
const int64_t var_max = plan.Max(var);
225+
if (var_min == var_max) {
226+
absl::StrAppendFormat(&plan_output, "%s(%d) ", name, var_min);
227+
} else {
228+
absl::StrAppendFormat(&plan_output, "%s(%d, %d) ", name, var_min,
229+
var_max);
230+
}
231+
};
210232
for (int route_number = 0; route_number < routing.vehicles();
211233
++route_number) {
212234
int64_t order = routing.Start(route_number);
@@ -215,26 +237,16 @@ void DisplayPlan(
215237
plan_output += "Empty\n";
216238
} else {
217239
while (true) {
218-
operations_research::IntVar* const load_var =
219-
capacity_dimension.CumulVar(order);
220-
operations_research::IntVar* const time_var =
221-
time_dimension.CumulVar(order);
222-
operations_research::IntVar* const slack_var =
223-
routing.IsEnd(order) ? nullptr : time_dimension.SlackVar(order);
224-
if (slack_var != nullptr && plan.Contains(slack_var)) {
225-
absl::StrAppendFormat(
226-
&plan_output, "%d Load(%d) Time(%d, %d) Slack(%d, %d)",
227-
manager.IndexToNode(order).value(), plan.Value(load_var),
228-
plan.Min(time_var), plan.Max(time_var), plan.Min(slack_var),
229-
plan.Max(slack_var));
230-
} else {
231-
absl::StrAppendFormat(&plan_output, "%d Load(%d) Time(%d, %d)",
232-
manager.IndexToNode(order).value(),
233-
plan.Value(load_var), plan.Min(time_var),
234-
plan.Max(time_var));
240+
absl::StrAppendFormat(&plan_output, "%d ",
241+
manager.IndexToNode(order).value());
242+
for (const operations_research::RoutingDimension* dimension : dimensions) {
243+
str_append_variable(dimension->CumulVar(order), dimension->name());
244+
operations_research::IntVar* const slack_var =
245+
routing.IsEnd(order) ? nullptr : dimension->SlackVar(order);
246+
str_append_variable(slack_var, dimension->name() + "Slack");
235247
}
236248
if (routing.IsEnd(order)) break;
237-
plan_output += " -> ";
249+
plan_output += "-> ";
238250
order = plan.Value(routing.NextVar(order));
239251
}
240252
plan_output += "\n";

ortools/routing/parsers/cvrptw_lib.h

+4-2
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,10 @@
2020
#include <functional>
2121
#include <memory>
2222
#include <random>
23+
#include <string>
24+
#include <vector>
2325

26+
#include "absl/types/span.h"
2427
#include "ortools/base/strong_vector.h"
2528
#include "ortools/constraint_solver/routing.h"
2629

@@ -128,8 +131,7 @@ void DisplayPlan(
128131
const operations_research::RoutingModel& routing,
129132
const operations_research::Assignment& plan, bool use_same_vehicle_costs,
130133
int64_t max_nodes_per_group, int64_t same_vehicle_cost,
131-
const operations_research::RoutingDimension& capacity_dimension,
132-
const operations_research::RoutingDimension& time_dimension);
134+
absl::Span<const std::string> dimension_names);
133135

134136
} // namespace operations_research
135137

ortools/routing/parsers/solution_serializer.cc

+2-2
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,7 @@ std::string RoutingSolution::SerializeToNEARPLIBSolutionFile() const {
340340

341341
namespace {
342342
RoutingSolution::Route RouteFromVector(
343-
const std::vector<int64_t>& route_int,
343+
absl::Span<const int64_t> route_int,
344344
std::optional<int64_t> depot = std::nullopt);
345345

346346
std::vector<RoutingSolution::Route> RoutesFromVector(
@@ -355,7 +355,7 @@ std::vector<RoutingSolution::Route> RoutesFromVector(
355355
return solution_routes;
356356
}
357357

358-
RoutingSolution::Route RouteFromVector(const std::vector<int64_t>& route_int,
358+
RoutingSolution::Route RouteFromVector(absl::Span<const int64_t> route_int,
359359
std::optional<int64_t> forced_depot) {
360360
// One route in input: from the node indices, create a Route object (not yet
361361
// a RoutingSolution one).

ortools/routing/samples/cvrp_disjoint_tw.cc

+9-6
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,19 @@
2424

2525
#include <algorithm>
2626
#include <cstdint>
27+
#include <cstdlib>
2728
#include <random>
29+
#include <string>
30+
#include <utility>
2831
#include <vector>
2932

33+
#include "absl/flags/flag.h"
3034
#include "absl/random/random.h"
3135
#include "google/protobuf/text_format.h"
32-
#include "ortools/base/commandlineflags.h"
3336
#include "ortools/base/init_google.h"
3437
#include "ortools/base/logging.h"
3538
#include "ortools/base/types.h"
39+
#include "ortools/constraint_solver/constraint_solver.h"
3640
#include "ortools/constraint_solver/routing.h"
3741
#include "ortools/constraint_solver/routing_index_manager.h"
3842
#include "ortools/constraint_solver/routing_parameters.h"
@@ -170,12 +174,13 @@ int main(int argc, char** argv) {
170174
order < manager.num_nodes(); ++order) {
171175
group.push_back(manager.NodeToIndex(order));
172176
if (group.size() == kMaxNodesPerGroup) {
173-
routing.AddSoftSameVehicleConstraint(group, kSameVehicleCost);
177+
routing.AddSoftSameVehicleConstraint(std::move(group),
178+
kSameVehicleCost);
174179
group.clear();
175180
}
176181
}
177182
if (!group.empty()) {
178-
routing.AddSoftSameVehicleConstraint(group, kSameVehicleCost);
183+
routing.AddSoftSameVehicleConstraint(std::move(group), kSameVehicleCost);
179184
}
180185
}
181186

@@ -187,9 +192,7 @@ int main(int argc, char** argv) {
187192
if (solution != nullptr) {
188193
DisplayPlan(manager, routing, *solution,
189194
absl::GetFlag(FLAGS_vrp_use_same_vehicle_costs),
190-
kMaxNodesPerGroup, kSameVehicleCost,
191-
routing.GetDimensionOrDie(kCapacity),
192-
routing.GetDimensionOrDie(kTime));
195+
kMaxNodesPerGroup, kSameVehicleCost, {kCapacity, kTime});
193196
} else {
194197
LOG(INFO) << "No solution found.";
195198
}

ortools/routing/samples/cvrptw.cc

+9-6
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,19 @@
2222
// to be in meters and times in seconds.
2323

2424
#include <cstdint>
25+
#include <cstdlib>
2526
#include <random>
27+
#include <string>
28+
#include <utility>
2629
#include <vector>
2730

31+
#include "absl/flags/flag.h"
2832
#include "absl/random/random.h"
2933
#include "google/protobuf/text_format.h"
30-
#include "ortools/base/commandlineflags.h"
3134
#include "ortools/base/init_google.h"
3235
#include "ortools/base/logging.h"
3336
#include "ortools/base/types.h"
37+
#include "ortools/constraint_solver/constraint_solver.h"
3438
#include "ortools/constraint_solver/routing.h"
3539
#include "ortools/constraint_solver/routing_index_manager.h"
3640
#include "ortools/constraint_solver/routing_parameters.h"
@@ -155,12 +159,13 @@ int main(int argc, char** argv) {
155159
order < manager.num_nodes(); ++order) {
156160
group.push_back(manager.NodeToIndex(order));
157161
if (group.size() == kMaxNodesPerGroup) {
158-
routing.AddSoftSameVehicleConstraint(group, kSameVehicleCost);
162+
routing.AddSoftSameVehicleConstraint(std::move(group),
163+
kSameVehicleCost);
159164
group.clear();
160165
}
161166
}
162167
if (!group.empty()) {
163-
routing.AddSoftSameVehicleConstraint(group, kSameVehicleCost);
168+
routing.AddSoftSameVehicleConstraint(std::move(group), kSameVehicleCost);
164169
}
165170
}
166171

@@ -172,9 +177,7 @@ int main(int argc, char** argv) {
172177
if (solution != nullptr) {
173178
DisplayPlan(manager, routing, *solution,
174179
absl::GetFlag(FLAGS_vrp_use_same_vehicle_costs),
175-
kMaxNodesPerGroup, kSameVehicleCost,
176-
routing.GetDimensionOrDie(kCapacity),
177-
routing.GetDimensionOrDie(kTime));
180+
kMaxNodesPerGroup, kSameVehicleCost, {kCapacity, kTime});
178181
} else {
179182
LOG(INFO) << "No solution found.";
180183
}

ortools/routing/samples/cvrptw_soft_capacity.cc

+9-6
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,19 @@
2121
// distance. Distances are assumed to be in meters and times in seconds.
2222

2323
#include <cstdint>
24+
#include <cstdlib>
2425
#include <random>
26+
#include <string>
27+
#include <utility>
2528
#include <vector>
2629

30+
#include "absl/flags/flag.h"
2731
#include "absl/random/random.h"
2832
#include "google/protobuf/text_format.h"
29-
#include "ortools/base/commandlineflags.h"
3033
#include "ortools/base/init_google.h"
3134
#include "ortools/base/logging.h"
3235
#include "ortools/base/types.h"
36+
#include "ortools/constraint_solver/constraint_solver.h"
3337
#include "ortools/constraint_solver/routing.h"
3438
#include "ortools/constraint_solver/routing_index_manager.h"
3539
#include "ortools/constraint_solver/routing_parameters.h"
@@ -179,12 +183,13 @@ int main(int argc, char** argv) {
179183
order < manager.num_nodes(); ++order) {
180184
group.push_back(manager.NodeToIndex(order));
181185
if (group.size() == kMaxNodesPerGroup) {
182-
routing.AddSoftSameVehicleConstraint(group, kSameVehicleCost);
186+
routing.AddSoftSameVehicleConstraint(std::move(group),
187+
kSameVehicleCost);
183188
group.clear();
184189
}
185190
}
186191
if (!group.empty()) {
187-
routing.AddSoftSameVehicleConstraint(group, kSameVehicleCost);
192+
routing.AddSoftSameVehicleConstraint(std::move(group), kSameVehicleCost);
188193
}
189194
}
190195

@@ -196,9 +201,7 @@ int main(int argc, char** argv) {
196201
if (solution != nullptr) {
197202
DisplayPlan(manager, routing, *solution,
198203
absl::GetFlag(FLAGS_vrp_use_same_vehicle_costs),
199-
kMaxNodesPerGroup, kSameVehicleCost,
200-
routing.GetDimensionOrDie(kCapacity),
201-
routing.GetDimensionOrDie(kTime));
204+
kMaxNodesPerGroup, kSameVehicleCost, {kCapacity, kTime});
202205
} else {
203206
LOG(INFO) << "No solution found.";
204207
}

ortools/routing/samples/cvrptw_with_breaks.cc

+5-4
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,20 @@
2626
// day or two smaller ones which can be taken during a longer period of the day.
2727

2828
#include <cstdint>
29+
#include <cstdlib>
2930
#include <random>
31+
#include <string>
3032
#include <utility>
3133
#include <vector>
3234

35+
#include "absl/flags/flag.h"
3336
#include "absl/random/random.h"
3437
#include "absl/strings/str_cat.h"
3538
#include "google/protobuf/text_format.h"
36-
#include "ortools/base/commandlineflags.h"
3739
#include "ortools/base/init_google.h"
3840
#include "ortools/base/logging.h"
3941
#include "ortools/base/types.h"
42+
#include "ortools/constraint_solver/constraint_solver.h"
4043
#include "ortools/constraint_solver/routing.h"
4144
#include "ortools/constraint_solver/routing_enums.pb.h"
4245
#include "ortools/constraint_solver/routing_index_manager.h"
@@ -227,9 +230,7 @@ int main(int argc, char** argv) {
227230
LOG(INFO) << break_interval.Var()->name() << " unperformed";
228231
}
229232
}
230-
DisplayPlan(manager, routing, *solution, false, 0, 0,
231-
routing.GetDimensionOrDie(kCapacity),
232-
routing.GetDimensionOrDie(kTime));
233+
DisplayPlan(manager, routing, *solution, false, 0, 0, {kCapacity, kTime});
233234
} else {
234235
LOG(INFO) << "No solution found.";
235236
}

0 commit comments

Comments
 (0)