Skip to content

Commit 9e80878

Browse files
committed
Enable bintest in CMake, update bintests
1 parent 48608ea commit 9e80878

13 files changed

+1062
-40
lines changed

cmake/cpp.cmake

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,8 @@ function(ortools_cxx_binary)
379379
message(STATUS "Configuring binary ${BINARY_NAME} ...DONE")
380380
endfunction()
381381

382+
find_package(Python3 COMPONENTS Interpreter)
383+
382384
# ortools_cxx_bintest()
383385
# CMake function to generate and build C++ test.
384386
# Parameters:
@@ -410,16 +412,21 @@ function(ortools_cxx_bintest)
410412
if(NOT BINTEST_SCRIPT)
411413
message(FATAL_ERROR "no SCRIPT provided")
412414
endif()
413-
message(STATUS "Configuring binary test ${BINTEST_NAME} ...")
415+
if(NOT Python3_Interpreter_FOUND)
416+
message(WARNING "No python3 interpreter found, the bintest ${BINTEST_NAME} is disable")
417+
return()
418+
endif()
419+
420+
message(STATUS "Configuring bintest ${BINTEST_NAME} ...")
414421
add_test(
415422
NAME ${BINTEST_NAME}
416-
COMMAND python3 -m tools.testing.bintest_script_launcher ${BINTEST_SCRIPT}
423+
COMMAND ${Python3_EXECUTABLE} -m tools.testing.bintest_script_launcher ${BINTEST_SCRIPT}
417424
WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
418425
)
419426
set_tests_properties(${BINTEST_NAME} PROPERTIES
420427
ENVIRONMENT "${BINTEST_ENVIRONMENT}"
421428
)
422-
message(STATUS "Configuring binary test ${BINTEST_NAME} ...DONE")
429+
message(STATUS "Configuring bintest ${BINTEST_NAME} ...DONE")
423430
endfunction()
424431

425432
##################

examples/cpp/BUILD.bazel

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,7 @@ bintest(
354354
srcs = [":shift_minimization_sat_test.bintest"],
355355
named_data = {
356356
"shift_minimization_sat": ":shift_minimization_sat",
357-
"shift_minimization.dat": ":shift_minimization.dat",
357+
"shift_minimization.dat": "testdata/shift_minimization.dat",
358358
},
359359
)
360360

@@ -386,7 +386,7 @@ bintest(
386386
srcs = [":weighted_tardiness_sat_test.bintest"],
387387
named_data = {
388388
"weighted_tardiness_sat": ":weighted_tardiness_sat",
389-
"wt40.txt": ":wt40.txt",
389+
"wt40.txt": "testdata/wt40.txt",
390390
},
391391
)
392392

@@ -519,6 +519,26 @@ cc_binary(
519519
],
520520
)
521521

522+
bintest(
523+
name = "pdptw_test",
524+
size = "medium",
525+
srcs = [":pdptw_test.bintest"],
526+
named_data = {
527+
"pdptw": ":pdptw",
528+
"lc102.txt": "testdata/lc102.txt",
529+
},
530+
)
531+
532+
bintest(
533+
name = "pdptw_non_homogenous_fleet_test",
534+
size = "medium",
535+
srcs = [":pdptw_non_homogenous_fleet_test.bintest"],
536+
named_data = {
537+
"pdptw": ":pdptw",
538+
"lc102.txt": "testdata/lc102.txt",
539+
},
540+
)
541+
522542
# Routing examples.
523543
cc_binary(
524544
name = "random_tsp",
@@ -662,10 +682,30 @@ cc_binary(
662682
"//ortools/graph:linear_assignment",
663683
"@abseil-cpp//absl/container:flat_hash_map",
664684
"@abseil-cpp//absl/flags:flag",
665-
"@abseil-cpp//absl/strings:str_format",
685+
"@abseil-cpp//absl/strings:string_view",
666686
],
667687
)
668688

689+
bintest(
690+
name = "dimacs_assignment_min_cost_test",
691+
size = "small",
692+
srcs = [":dimacs_assignment_min_cost_test.bintest"],
693+
named_data = {
694+
"dimacs_assignment": ":dimacs_assignment",
695+
"dimacs_example.txt": "testdata/dimacs_example.txt",
696+
},
697+
)
698+
699+
bintest(
700+
name = "dimacs_assignment_max_cost_test",
701+
size = "small",
702+
srcs = [":dimacs_assignment_max_cost_test.bintest"],
703+
named_data = {
704+
"dimacs_assignment": ":dimacs_assignment",
705+
"dimacs_example.txt": "testdata/dimacs_example.txt",
706+
},
707+
)
708+
669709
# MPS driver for LP and MIP.
670710
cc_binary(
671711
name = "mps_driver",

examples/cpp/CMakeBazel.txt

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ ortools_cxx_binary(
152152
ortools_cxx_bintest(
153153
NAME bzl_cc_example_shift_minimization_sat_test
154154
SCRIPT ${CMAKE_CURRENT_SOURCE_DIR}/shift_minimization_sat_test.bintest
155-
ENVIRONMENT BINTEST_shift_minimization_sat=$<TARGET_FILE:bzl_cc_example_shift_minimization_sat> BINTEST_shift_minimization.dat=${CMAKE_CURRENT_SOURCE_DIR}/shift_minimization.dat
155+
ENVIRONMENT BINTEST_shift_minimization_sat=$<TARGET_FILE:bzl_cc_example_shift_minimization_sat> BINTEST_shift_minimization.dat=${CMAKE_CURRENT_SOURCE_DIR}/testdata/shift_minimization.dat
156156
)
157157

158158
ortools_cxx_binary(
@@ -163,7 +163,7 @@ ortools_cxx_binary(
163163
ortools_cxx_bintest(
164164
NAME bzl_cc_example_weighted_tardiness_sat_test
165165
SCRIPT ${CMAKE_CURRENT_SOURCE_DIR}/weighted_tardiness_sat_test.bintest
166-
ENVIRONMENT BINTEST_weighted_tardiness_sat=$<TARGET_FILE:bzl_cc_example_weighted_tardiness_sat> BINTEST_wt40.txt=${CMAKE_CURRENT_SOURCE_DIR}/wt40.txt
166+
ENVIRONMENT BINTEST_weighted_tardiness_sat=$<TARGET_FILE:bzl_cc_example_weighted_tardiness_sat> BINTEST_wt40.txt=${CMAKE_CURRENT_SOURCE_DIR}/testdata/wt40.txt
167167
)
168168

169169
ortools_cxx_binary(
@@ -215,6 +215,18 @@ ortools_cxx_binary(
215215
SOURCES pdptw.cc
216216
)
217217

218+
ortools_cxx_bintest(
219+
NAME bzl_cc_example_pdptw_test
220+
SCRIPT ${CMAKE_CURRENT_SOURCE_DIR}/pdptw_test.bintest
221+
ENVIRONMENT BINTEST_pdptw=$<TARGET_FILE:bzl_cc_example_pdptw> BINTEST_lc102.txt=${CMAKE_CURRENT_SOURCE_DIR}/testdata/lc102.txt
222+
)
223+
224+
ortools_cxx_bintest(
225+
NAME bzl_cc_example_pdptw_non_homogenous_fleet_test
226+
SCRIPT ${CMAKE_CURRENT_SOURCE_DIR}/pdptw_non_homogenous_fleet_test.bintest
227+
ENVIRONMENT BINTEST_pdptw=$<TARGET_FILE:bzl_cc_example_pdptw> BINTEST_lc102.txt=${CMAKE_CURRENT_SOURCE_DIR}/testdata/lc102.txt
228+
)
229+
218230
ortools_cxx_binary(
219231
NAME bzl_cc_example_random_tsp
220232
SOURCES random_tsp.cc
@@ -282,6 +294,18 @@ ortools_cxx_binary(
282294
LINK_LIBRARIES bzl_cc_example_parse_dimacs_assignment bzl_cc_example_print_dimacs_assignment
283295
)
284296

297+
ortools_cxx_bintest(
298+
NAME bzl_cc_example_dimacs_assignment_min_cost_test
299+
SCRIPT ${CMAKE_CURRENT_SOURCE_DIR}/dimacs_assignment_min_cost_test.bintest
300+
ENVIRONMENT BINTEST_dimacs_assignment=$<TARGET_FILE:bzl_cc_example_dimacs_assignment> BINTEST_dimacs_example.txt=${CMAKE_CURRENT_SOURCE_DIR}/testdata/dimacs_example.txt
301+
)
302+
303+
ortools_cxx_bintest(
304+
NAME bzl_cc_example_dimacs_assignment_max_cost_test
305+
SCRIPT ${CMAKE_CURRENT_SOURCE_DIR}/dimacs_assignment_max_cost_test.bintest
306+
ENVIRONMENT BINTEST_dimacs_assignment=$<TARGET_FILE:bzl_cc_example_dimacs_assignment> BINTEST_dimacs_example.txt=${CMAKE_CURRENT_SOURCE_DIR}/testdata/dimacs_example.txt
307+
)
308+
285309
ortools_cxx_binary(
286310
NAME bzl_cc_example_mps_driver
287311
SOURCES mps_driver.cc

examples/cpp/dimacs_assignment.cc

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
#include "absl/container/flat_hash_map.h"
2222
#include "absl/flags/flag.h"
23-
#include "absl/strings/str_format.h"
23+
#include "absl/strings/string_view.h"
2424
#include "examples/cpp/parse_dimacs_assignment.h"
2525
#include "examples/cpp/print_dimacs_assignment.h"
2626
#include "ortools/algorithms/hungarian.h"
@@ -156,25 +156,18 @@ int SolveDimacsAssignment(int argc, char* argv[]) {
156156
}
157157
} // namespace operations_research
158158

159-
static const char* const kUsageTemplate = "usage: %s <filename>";
160-
161159
using ::operations_research::ArcIndex;
162160
using ::operations_research::NodeIndex;
163161
using ::operations_research::SolveDimacsAssignment;
164162

165163
int main(int argc, char* argv[]) {
166-
std::string usage;
167-
if (argc < 1) {
168-
usage = absl::StrFormat(kUsageTemplate, "solve_dimacs_assignment");
169-
} else {
170-
usage = absl::StrFormat(kUsageTemplate, argv[0]);
171-
}
172-
InitGoogle(usage.c_str(), &argc, &argv, true);
164+
InitGoogle(argv[0], &argc, &argv, true);
173165

174166
if (argc < 2) {
175-
LOG(FATAL) << usage;
167+
LOG(FATAL) << "Missing input file.";
176168
}
177169

170+
absl::SetStderrThreshold(absl::LogSeverityAtLeast::kInfo);
178171
if (absl::GetFlag(FLAGS_assignment_static_graph)) {
179172
return SolveDimacsAssignment<::util::StaticGraph<NodeIndex, ArcIndex>>(
180173
argc, argv);
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
RUN: $(dimacs_assignment) $(dimacs_example.txt) --alsologtostderr --assignment_maximize_cost 2>&1
1+
RUN: $(dimacs_assignment) $(dimacs_example.txt) --assignment_maximize_cost 2>&1
22
CHECK: "Cost of optimum assignment: -110"
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
RUN: $(dimacs_assignment) $(dimacs_example.txt) --alsologtostderr 2>&1
1+
RUN: $(dimacs_assignment) $(dimacs_example.txt) 2>&1
22
CHECK: "Cost of optimum assignment: 84"

examples/cpp/fap_parser.cc

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "absl/strings/numbers.h"
2222
#include "absl/strings/str_split.h"
2323
#include "absl/strings/string_view.h"
24+
#include "absl/types/span.h"
2425
#include "ortools/base/helpers.h"
2526
#include "ortools/base/map_util.h"
2627

@@ -94,7 +95,7 @@ void DomainParser::Parse() {
9495
}
9596

9697
if (!domain.empty()) {
97-
gtl::InsertOrUpdate(&domains_, key, domain);
98+
domains_.insert_or_assign(key, domain);
9899
}
99100
}
100101
}
@@ -198,7 +199,7 @@ void ParametersParser::Parse() {
198199
}
199200

200201
// TODO(user): Make FindComponents linear instead of quadratic.
201-
void FindComponents(const std::vector<FapConstraint>& constraints,
202+
void FindComponents(absl::Span<const FapConstraint> constraints,
202203
const absl::btree_map<int, FapVariable>& variables,
203204
const int maximum_variable_id,
204205
absl::flat_hash_map<int, FapComponent>* components) {
@@ -216,20 +217,20 @@ void FindComponents(const std::vector<FapConstraint>& constraints,
216217
// Create a new one.
217218
FapComponent component;
218219
const int component_index = constraint_index;
219-
gtl::InsertOrUpdate(&(component.variables), variable_id1, variable1);
220-
gtl::InsertOrUpdate(&(component.variables), variable_id2, variable2);
220+
(component.variables).insert_or_assign(variable_id1, variable1);
221+
(component.variables).insert_or_assign(variable_id2, variable2);
221222
in_component[variable_id1] = component_index;
222223
in_component[variable_id2] = component_index;
223224
component.constraints.push_back(constraint);
224-
gtl::InsertOrUpdate(components, component_index, component);
225+
components->insert_or_assign(component_index, component);
225226
} else if (in_component[variable_id1] >= 0 &&
226227
in_component[variable_id2] < 0) {
227228
// If variable1 belongs to an existing component, variable2 should
228229
// also be included in the same component.
229230
const int component_index = in_component[variable_id1];
230231
CHECK(components->contains(component_index));
231-
gtl::InsertOrUpdate(&((*components)[component_index].variables),
232-
variable_id2, variable2);
232+
((*components)[component_index].variables)
233+
.insert_or_assign(variable_id2, variable2);
233234
in_component[variable_id2] = component_index;
234235
(*components)[component_index].constraints.push_back(constraint);
235236
} else if (in_component[variable_id1] < 0 &&
@@ -238,8 +239,8 @@ void FindComponents(const std::vector<FapConstraint>& constraints,
238239
// also be included in the same component.
239240
const int component_index = in_component[variable_id2];
240241
CHECK(components->contains(component_index));
241-
gtl::InsertOrUpdate(&((*components)[component_index].variables),
242-
variable_id1, variable1);
242+
((*components)[component_index].variables)
243+
.insert_or_assign(variable_id1, variable1);
243244
in_component[variable_id1] = component_index;
244245
(*components)[component_index].constraints.push_back(constraint);
245246
} else {

examples/cpp/fap_parser.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include "absl/container/btree_map.h"
2424
#include "absl/container/flat_hash_map.h"
2525
#include "absl/strings/string_view.h"
26+
#include "absl/types/span.h"
2627

2728
namespace operations_research {
2829

@@ -214,7 +215,7 @@ class ParametersParser {
214215
};
215216

216217
// Function that finds the disjoint sub-graphs of the graph of the instance.
217-
void FindComponents(const std::vector<FapConstraint>& constraints,
218+
void FindComponents(absl::Span<const FapConstraint> constraints,
218219
const absl::btree_map<int, FapVariable>& variables,
219220
int maximum_variable_id,
220221
absl::flat_hash_map<int, FapComponent>* components);

examples/cpp/pdptw.cc

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -127,17 +127,18 @@ double ComputeScalingFactorFromCallback(const C& callback, int size) {
127127
return max_scaled_distance / max_value;
128128
}
129129

130-
void SetupModel(const LiLimParser& parser, const RoutingIndexManager& manager,
131-
RoutingModel* model,
130+
void SetupModel(const routing::LiLimParser& parser,
131+
const RoutingIndexManager& manager, RoutingModel* model,
132132
RoutingSearchParameters* search_parameters) {
133133
const int64_t kPenalty = 100000000;
134134
const int64_t kFixedCost = 100000;
135135
const int num_nodes = parser.NumberOfNodes();
136136
const int64_t horizon =
137-
absl::c_max_element(
138-
parser.time_windows(),
139-
[](const SimpleTimeWindow<int64_t>& a,
140-
const SimpleTimeWindow<int64_t>& b) { return a.end < b.end; })
137+
absl::c_max_element(parser.time_windows(),
138+
[](const routing::SimpleTimeWindow<int64_t>& a,
139+
const routing::SimpleTimeWindow<int64_t>& b) {
140+
return a.end < b.end;
141+
})
141142
->end;
142143
const double scaling_factor = ComputeScalingFactorFromCallback(
143144
[&parser](int64_t i, int64_t j) -> double {
@@ -199,7 +200,8 @@ void SetupModel(const LiLimParser& parser, const RoutingIndexManager& manager,
199200
model->AddPickupAndDelivery(index, delivery_index);
200201
}
201202
IntVar* const cumul = time_dimension.CumulVar(index);
202-
const SimpleTimeWindow<int64_t>& window = parser.time_windows()[node];
203+
const routing::SimpleTimeWindow<int64_t>& window =
204+
parser.time_windows()[node];
203205
cumul->SetMin(MathUtil::Round<int64_t>(scaling_factor * window.start));
204206
cumul->SetMax(MathUtil::Round<int64_t>(scaling_factor * window.end));
205207
}
@@ -244,7 +246,8 @@ void SetupModel(const LiLimParser& parser, const RoutingIndexManager& manager,
244246
std::string VerboseOutput(const RoutingModel& model,
245247
const RoutingIndexManager& manager,
246248
const Assignment& assignment,
247-
const LiLimParser& parser, double scaling_factor) {
249+
const routing::LiLimParser& parser,
250+
double scaling_factor) {
248251
std::string output;
249252
const RoutingDimension& time_dimension = model.GetDimensionOrDie("time");
250253
const RoutingDimension& load_dimension = model.GetDimensionOrDie("demand");
@@ -298,7 +301,7 @@ std::string VerboseOutput(const RoutingModel& model,
298301
bool LoadAndSolve(absl::string_view pdp_file,
299302
const RoutingModelParameters& model_parameters,
300303
RoutingSearchParameters& search_parameters) {
301-
LiLimParser parser;
304+
routing::LiLimParser parser;
302305
if (!parser.LoadFile(pdp_file)) {
303306
return false;
304307
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
c Simple example file demonstrating the DIMACS data format, and for testing.
2+
c Source: Invented from scratch by [email protected] on 2019-05-22.
3+
c Lines starting with 'c' (like this one) are comments.
4+
c
5+
c Graph description: (number of nodes) (number of arcs)
6+
p asn 6 9
7+
c Note that the problems are 'perfect assignment' problems, where
8+
c there are as many 'left' nodes as 'right' nodes, and we want to assign
9+
c each 'left' node to exactly one 'right' node.
10+
c
11+
c "Left" nodes.
12+
n 1
13+
n 2
14+
n 3
15+
c
16+
c Arcs: left node, right node, cost of assigning left to right.
17+
a 1 4 12
18+
a 1 5 53
19+
a 1 6 36
20+
a 2 4 14
21+
a 2 5 37
22+
a 2 6 46
23+
a 3 4 11
24+
a 3 5 52
25+
a 3 6 35

0 commit comments

Comments
 (0)