Skip to content

Commit 854a038

Browse files
committed
examples fixup
1 parent dbf78f7 commit 854a038

5 files changed

+88
-76
lines changed

examples/cpp/multi_knapsack_sat.cc

+2-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
// of each items.
2121

2222
#include <cstdint>
23+
#include <cstdlib>
2324
#include <string>
2425
#include <vector>
2526

@@ -32,7 +33,7 @@
3233

3334
ABSL_FLAG(int, size, 16, "scaling factor of the model");
3435
ABSL_FLAG(std::string, params,
35-
"num_workers:8,log_search_progress:true,max_time_in_seconds:10.0",
36+
"num_workers:8,log_search_progress:false,max_time_in_seconds:10.0",
3637
"Sat parameters");
3738
namespace operations_research {
3839
namespace sat {

examples/cpp/pdptw.cc

+11-11
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,7 @@ ABSL_FLAG(std::string, routing_model_parameters, "",
8383
"Text proto RoutingModelParameters (possibly partial) that will "
8484
"override the DefaultRoutingModelParameters()");
8585

86-
namespace operations_research {
87-
namespace routing {
86+
namespace operations_research::routing {
8887
namespace {
8988

9089
// Returns the list of variables to use for the Tabu metaheuristic.
@@ -131,7 +130,7 @@ double ComputeScalingFactorFromCallback(const C& callback, int size) {
131130

132131
void SetupModel(const LiLimParser& parser, const RoutingIndexManager& manager,
133132
RoutingModel* model,
134-
routing::RoutingSearchParameters* search_parameters) {
133+
RoutingSearchParameters* search_parameters) {
135134
const int64_t kPenalty = 100000000;
136135
const int64_t kFixedCost = 100000;
137136
const int num_nodes = parser.NumberOfNodes();
@@ -357,24 +356,25 @@ bool LoadAndSolve(absl::string_view pdp_file,
357356
return false;
358357
}
359358

360-
} // namespace routing
361-
} // namespace operations_research
359+
} // namespace operations_research::routing
360+
361+
namespace o_r = ::operations_research::routing;
362362

363363
int main(int argc, char** argv) {
364364
absl::SetStderrThreshold(absl::LogSeverityAtLeast::kInfo);
365365
InitGoogle(argv[0], &argc, &argv, true);
366-
operations_research::routing::RoutingModelParameters model_parameters =
367-
operations_research::routing::DefaultRoutingModelParameters();
366+
o_r::RoutingModelParameters model_parameters =
367+
o_r::DefaultRoutingModelParameters();
368368
model_parameters.set_reduce_vehicle_cost_model(
369369
absl::GetFlag(FLAGS_reduce_vehicle_cost_model));
370370
CHECK(google::protobuf::TextFormat::MergeFromString(
371371
absl::GetFlag(FLAGS_routing_model_parameters), &model_parameters));
372-
operations_research::routing::RoutingSearchParameters search_parameters =
373-
operations_research::routing::DefaultRoutingSearchParameters();
372+
o_r::RoutingSearchParameters search_parameters =
373+
o_r::DefaultRoutingSearchParameters();
374374
CHECK(google::protobuf::TextFormat::MergeFromString(
375375
absl::GetFlag(FLAGS_routing_search_parameters), &search_parameters));
376-
if (!operations_research::routing::LoadAndSolve(
377-
absl::GetFlag(FLAGS_pdp_file), model_parameters, search_parameters)) {
376+
if (!o_r::LoadAndSolve(absl::GetFlag(FLAGS_pdp_file), model_parameters,
377+
search_parameters)) {
378378
LOG(INFO) << "Error solving " << absl::GetFlag(FLAGS_pdp_file);
379379
}
380380
return EXIT_SUCCESS;

examples/cpp/random_tsp.cc

+11
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,22 @@
2525
// (forbidden arcs).
2626

2727
#include <cstdint>
28+
#include <cstdlib>
2829
#include <memory>
30+
#include <random>
2931
#include <string>
3032

33+
#include "absl/base/log_severity.h"
34+
#include "absl/flags/flag.h"
3135
#include "absl/flags/parse.h"
36+
#include "absl/log/check.h"
37+
#include "absl/log/globals.h"
38+
#include "absl/log/initialize.h"
3239
#include "absl/log/log.h"
3340
#include "absl/random/random.h"
3441
#include "absl/strings/str_cat.h"
3542
#include "google/protobuf/text_format.h"
43+
#include "ortools/constraint_solver/constraint_solver.h"
3644
#include "ortools/routing/index_manager.h"
3745
#include "ortools/routing/parameters.h"
3846
#include "ortools/routing/parameters.pb.h"
@@ -182,6 +190,9 @@ void Tsp() {
182190
} // namespace operations_research::routing
183191

184192
int main(int argc, char** argv) {
193+
absl::InitializeLog();
194+
absl::EnableLogPrefix(false);
195+
absl::SetStderrThreshold(absl::LogSeverityAtLeast::kInfo);
185196
absl::ParseCommandLine(argc, argv);
186197
operations_research::routing::Tsp();
187198
return EXIT_SUCCESS;

examples/java/CapacitatedVehicleRoutingProblemWithTimeWindows.java

+63-63
Original file line numberDiff line numberDiff line change
@@ -30,21 +30,6 @@
3030
import java.util.function.LongUnaryOperator;
3131
import java.util.logging.Logger;
3232

33-
// A pair class
34-
class Pair<K, V> {
35-
final K first;
36-
final V second;
37-
38-
public static <K, V> Pair<K, V> of(K element0, V element1) {
39-
return new Pair<K, V>(element0, element1);
40-
}
41-
42-
public Pair(K element0, V element1) {
43-
this.first = element0;
44-
this.second = element1;
45-
}
46-
}
47-
4833
/**
4934
* Sample showing how to model and solve a capacitated vehicle routing problem with time windows
5035
* using the swig-wrapped version of the vehicle routing library in
@@ -54,82 +39,97 @@ public class CapacitatedVehicleRoutingProblemWithTimeWindows {
5439
private static final Logger logger =
5540
Logger.getLogger(CapacitatedVehicleRoutingProblemWithTimeWindows.class.getName());
5641

42+
// A pair class
43+
static class Pair<K, V> {
44+
final K first;
45+
final V second;
46+
47+
public static <K, V> Pair<K, V> of(K element0, V element1) {
48+
return new Pair<K, V>(element0, element1);
49+
}
50+
51+
public Pair(K element0, V element1) {
52+
this.first = element0;
53+
this.second = element1;
54+
}
55+
}
56+
5757
static class DataModel {
58-
// Locations representing either an order location or a vehicle route
59-
// start/end.
58+
// Locations representing either an order location or a vehicle route
59+
// start/end.
6060
public final List<Pair<Integer, Integer>> locations = new ArrayList<>();
6161

62-
// Quantity to be picked up for each order.
62+
// Quantity to be picked up for each order.
6363
public final List<Integer> orderDemands = new ArrayList<>();
64-
// Time window in which each order must be performed.
64+
// Time window in which each order must be performed.
6565
public final List<Pair<Integer, Integer>> orderTimeWindows = new ArrayList<>();
66-
// Penalty cost "paid" for dropping an order.
66+
// Penalty cost "paid" for dropping an order.
6767
public final List<Integer> orderPenalties = new ArrayList<>();
6868

6969
public final int numberOfVehicles = 20;
7070

71-
// Capacity of the vehicles.
71+
// Capacity of the vehicles.
7272
public final int vehicleCapacity = 50;
7373
public final int numberOfOrders = 100;
74-
// Latest time at which each vehicle must end its tour.
74+
// Latest time at which each vehicle must end its tour.
7575
public final List<Integer> vehicleEndTime = new ArrayList<>();
76-
// Cost per unit of distance of each vehicle.
76+
// Cost per unit of distance of each vehicle.
7777
public final List<Integer> vehicleCostCoefficients = new ArrayList<>();
78-
// Vehicle start and end indices. They have to be implemented as int[] due
79-
// to the available SWIG-ed interface.
78+
// Vehicle start and end indices. They have to be implemented as int[] due
79+
// to the available SWIG-ed interface.
8080
public int[] vehicleStarts;
8181
public int[] vehicleEnds;
8282

83-
// Random number generator to produce data.
84-
private final Random randomGenerator = new Random(0xBEEF);
83+
// Random number generator to produce data.
84+
private final Random randomGenerator = new Random(0xBEEF);
8585

86-
/**
86+
/**
8787
* Creates order data. Location of the order is random, as well as its demand (quantity), time
8888
* window and penalty.
89-
*
90-
* @param xMax maximum x coordinate in which orders are located.
91-
* @param yMax maximum y coordinate in which orders are located.
92-
* @param demandMax maximum quantity of a demand.
93-
* @param timeWindowMax maximum starting time of the order time window.
94-
* @param timeWindowWidth duration of the order time window.
95-
* @param penaltyMin minimum pernalty cost if order is dropped.
96-
* @param penaltyMax maximum pernalty cost if order is dropped.
97-
*/
89+
*
90+
* @param xMax maximum x coordinate in which orders are located.
91+
* @param yMax maximum y coordinate in which orders are located.
92+
* @param demandMax maximum quantity of a demand.
93+
* @param timeWindowMax maximum starting time of the order time window.
94+
* @param timeWindowWidth duration of the order time window.
95+
* @param penaltyMin minimum pernalty cost if order is dropped.
96+
* @param penaltyMax maximum pernalty cost if order is dropped.
97+
*/
9898
private void buildOrders(int xMax, int yMax, int demandMax, int timeWindowMax,
9999
int timeWindowWidth, int penaltyMin, int penaltyMax) {
100-
for (int order = 0; order < numberOfOrders; ++order) {
100+
for (int order = 0; order < numberOfOrders; ++order) {
101101
locations.add(
102102
Pair.of(randomGenerator.nextInt(xMax + 1), randomGenerator.nextInt(yMax + 1)));
103-
orderDemands.add(randomGenerator.nextInt(demandMax + 1));
104-
int timeWindowStart = randomGenerator.nextInt(timeWindowMax + 1);
105-
orderTimeWindows.add(Pair.of(timeWindowStart, timeWindowStart + timeWindowWidth));
106-
orderPenalties.add(randomGenerator.nextInt(penaltyMax - penaltyMin + 1) + penaltyMin);
103+
orderDemands.add(randomGenerator.nextInt(demandMax + 1));
104+
int timeWindowStart = randomGenerator.nextInt(timeWindowMax + 1);
105+
orderTimeWindows.add(Pair.of(timeWindowStart, timeWindowStart + timeWindowWidth));
106+
orderPenalties.add(randomGenerator.nextInt(penaltyMax - penaltyMin + 1) + penaltyMin);
107+
}
107108
}
108-
}
109109

110-
/**
110+
/**
111111
* Creates fleet data. Vehicle starting and ending locations are random, as well as vehicle
112112
* costs per distance unit.
113-
*
114-
* @param xMax maximum x coordinate in which orders are located.
115-
* @param yMax maximum y coordinate in which orders are located.
116-
* @param endTime latest end time of a tour of a vehicle.
113+
*
114+
* @param xMax maximum x coordinate in which orders are located.
115+
* @param yMax maximum y coordinate in which orders are located.
116+
* @param endTime latest end time of a tour of a vehicle.
117117
* @param costCoefficientMax maximum cost per distance unit of a vehicle (minimum is 1),
118-
*/
118+
*/
119119
private void buildFleet(int xMax, int yMax, int endTime, int costCoefficientMax) {
120-
vehicleStarts = new int[numberOfVehicles];
121-
vehicleEnds = new int[numberOfVehicles];
122-
for (int vehicle = 0; vehicle < numberOfVehicles; ++vehicle) {
123-
vehicleStarts[vehicle] = locations.size();
120+
vehicleStarts = new int[numberOfVehicles];
121+
vehicleEnds = new int[numberOfVehicles];
122+
for (int vehicle = 0; vehicle < numberOfVehicles; ++vehicle) {
123+
vehicleStarts[vehicle] = locations.size();
124124
locations.add(
125125
Pair.of(randomGenerator.nextInt(xMax + 1), randomGenerator.nextInt(yMax + 1)));
126-
vehicleEnds[vehicle] = locations.size();
126+
vehicleEnds[vehicle] = locations.size();
127127
locations.add(
128128
Pair.of(randomGenerator.nextInt(xMax + 1), randomGenerator.nextInt(yMax + 1)));
129-
vehicleEndTime.add(endTime);
130-
vehicleCostCoefficients.add(randomGenerator.nextInt(costCoefficientMax) + 1);
129+
vehicleEndTime.add(endTime);
130+
vehicleCostCoefficients.add(randomGenerator.nextInt(costCoefficientMax) + 1);
131+
}
131132
}
132-
}
133133

134134
public DataModel() {
135135
final int xMax = 20;
@@ -174,7 +174,7 @@ public long applyAsLong(long fromIndex, long toIndex) {
174174
};
175175
}
176176

177-
/// @brief Print the solution.
177+
// Print the solution.
178178
static void printSolution(
179179
DataModel data, RoutingModel model, RoutingIndexManager manager, Assignment solution) {
180180
RoutingSearchStatus.Value status = model.status();
@@ -184,7 +184,6 @@ static void printSolution(
184184
logger.warning("No solution found!");
185185
return;
186186
}
187-
String output = "";
188187

189188
// Solution cost.
190189
logger.info("Objective : " + solution.objectiveValue());
@@ -196,7 +195,7 @@ static void printSolution(
196195
}
197196
}
198197
if (dropped.length() > 0) {
199-
output += "Dropped orders:" + dropped + "\n";
198+
logger.info("Dropped orders:" + dropped);
200199
}
201200

202201
// Routes
@@ -221,12 +220,12 @@ static void printSolution(
221220
IntVar time = timeDimension.cumulVar(index);
222221
long nodeIndex = manager.indexToNode(index);
223222
route += nodeIndex + " Load(" + solution.value(load) + ")";
224-
route += " Time(" + solution.min(time) + ", " + solution.max(time) + ") -> ";
223+
route += " Time(" + solution.min(time) + ", " + solution.max(time) + ")";
225224
logger.info(route);
226225
}
227226
}
228227

229-
public static void main(String[] args) throws Exception {
228+
public static void main(String[] args) {
230229
Loader.loadNativeLibraries();
231230

232231
// Instantiate the data problem.
@@ -261,7 +260,6 @@ public long applyAsLong(long fromIndex) {
261260
};
262261
unused = model.addDimension(model.registerUnaryTransitCallback(demandCallback), 0,
263262
data.vehicleCapacity, true, "capacity");
264-
RoutingDimension capacityDimension = model.getMutableDimension("capacity");
265263

266264
// Setting up vehicles
267265
LongBinaryOperator[] callbacks = new LongBinaryOperator[data.numberOfVehicles];
@@ -293,4 +291,6 @@ public long applyAsLong(long fromIndex) {
293291
// Print solution on console.
294292
printSolution(data, model, manager, solution);
295293
}
294+
295+
private CapacitatedVehicleRoutingProblemWithTimeWindows() {}
296296
}

examples/java/LinearProgramming.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ private static void runLinearProgrammingExample(String solverType, boolean print
7979

8080
// Verify that the solution satisfies all constraints (when using solvers
8181
// others than GLOP_LINEAR_PROGRAMMING, this is highly recommended!).
82-
if (!solver.verifySolution(/*tolerance=*/1e-7, /*log_errors=*/true)) {
82+
if (!solver.verifySolution(/* tolerance= */ 1e-7, /* log_errors= */ true)) {
8383
System.err.println("The solution returned by the solver violated the"
8484
+ " problem constraints by at least 1e-7");
8585
return;

0 commit comments

Comments
 (0)