forked from google/or-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
solve.cc
356 lines (327 loc) · 14.4 KB
/
solve.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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
// Copyright 2010-2022 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.
// Command line interface to the MPSolver class.
// See linear_solver.h and kUsageStr below.
//
// Examples.
//
// 1. To run SCIP for 90 seconds, dumping available information use:
//
// solve --solver=scip \
// --time_limit=90s \
// --logtostderr \
// --linear_solver_enable_verbose_output \
// --input=/tmp/foo.mps \
// --dump_model=/tmp/foo.model \
// --dump_request=/tmp/foo.request \
// --dump_response=/tmp/foo.response \
// >/tmp/foo.out 2>/tmp/foo.err
//
// 2. To run CP_SAT for 10 minutes with 8 workers, you can use
// CP-SAT parameters:
//
// solve --solver=sat \
// --params="max_time_in_seconds:600, num_search_workers:8"
// --logtostderr \
// --input=/tmp/foo.mps \
// 2>/tmp/foo.err
//
// or use the solve binary flags:
//
// solve --solver=sat \
// --time_limit=10m \
// --num_threads=8 \
// --logtostderr \
// --input=/tmp/foo.mps \
// --dump_model=/tmp/foo.model \
// --dump_request=/tmp/foo.request \
// --dump_response=/tmp/foo.response \
// 2>/tmp/foo.err
#include <algorithm>
#include <cstdio>
#include <string>
#include <vector>
#include "absl/flags/flag.h"
#include "absl/flags/parse.h"
#include "absl/flags/usage.h"
#include "absl/strings/match.h"
#include "absl/strings/str_format.h"
#include "absl/time/time.h"
#include "ortools/base/commandlineflags.h"
#include "ortools/base/file.h"
#include "ortools/base/integral_types.h"
#include "ortools/base/logging.h"
#include "ortools/base/timer.h"
#include "ortools/linear_solver/linear_solver.h"
#include "ortools/linear_solver/linear_solver.pb.h"
#include "ortools/lp_data/mps_reader.h"
#include "ortools/lp_data/proto_utils.h"
#include "ortools/sat/cp_model.pb.h"
#include "ortools/sat/cp_model_solver.h"
#include "ortools/util/file_util.h"
#include "ortools/util/sigint.h"
ABSL_FLAG(std::string, input, "", "REQUIRED: Input file name.");
ABSL_FLAG(std::string, solver, "glop",
"The solver to use: bop, cbc, clp, glop, glpk_lp, glpk_mip, "
"gurobi_lp, gurobi_mip, pdlp, scip, knapsack, sat.");
ABSL_FLAG(int, num_threads, 1,
"Number of threads to use by the underlying solver.");
ABSL_FLAG(std::string, params_file, "",
"Solver specific parameters file. "
"If this flag is set, the --params flag is ignored.");
ABSL_FLAG(std::string, params, "", "Solver specific parameters");
ABSL_FLAG(absl::Duration, time_limit, absl::InfiniteDuration(),
"It specifies a limit on the solving time. The duration must be must "
"be positive. It default to an infinite duration meaning that no "
"time limit will be imposed.");
ABSL_FLAG(std::string, output_csv, "",
"If non-empty, write the returned solution in csv format with "
"each line formed by a variable name and its value.");
ABSL_FLAG(std::string, dump_format, "text",
"Format in which to dump protos (if flags --dump_model, "
"--dump_request, or --dump_response are used). Possible values: "
"'text', 'binary', 'json' which correspond to text proto format "
"binary proto format, and json. If 'binary' or 'json' are used, "
"we append '.bin' and '.json' to file names.");
ABSL_FLAG(bool, dump_gzip, false,
"Whether to gzip dumped protos. Appends .gz to their name.");
ABSL_FLAG(std::string, dump_model, "",
"If non-empty, dumps MPModelProto there.");
ABSL_FLAG(std::string, dump_request, "",
"If non-empty, dumps MPModelRequest there.");
ABSL_FLAG(std::string, dump_response, "",
"If non-empty, dumps MPSolutionResponse there.");
ABSL_FLAG(std::string, sol_file, "",
"If non-empty, output the best solution in Miplib .sol format.");
ABSL_DECLARE_FLAG(bool, verify_solution); // Defined in ./linear_solver.cc
ABSL_DECLARE_FLAG(
bool,
linear_solver_enable_verbose_output); // Defined in ./linear_solver.cc
static const char kUsageStr[] =
"Run MPSolver on the given input file. Many formats are supported: \n"
" - a .mps or .mps.gz file,\n"
" - an MPModelProto (binary or text, possibly gzipped),\n"
" - an MPModelRequest (binary or text, possibly gzipped).";
namespace operations_research {
namespace {
MPModelRequest ReadMipModel(const std::string& input) {
MPModelRequest request_proto;
MPModelProto model_proto;
if (absl::EndsWith(input, ".mps") || absl::EndsWith(input, ".mps.gz")) {
QCHECK_OK(glop::MPSReader().ParseFile(input, &model_proto))
<< "Error while parsing the mps file '" << input << "'.";
} else {
ReadFileToProto(input, &model_proto);
ReadFileToProto(input, &request_proto);
}
// If the input is a proto in binary format, both ReadFileToProto could
// return true. Instead use the actual number of variables found to test the
// correct format of the input.
const bool is_model_proto = model_proto.variable_size() > 0;
const bool is_request_proto = request_proto.model().variable_size() > 0;
if (!is_model_proto && !is_request_proto) {
LOG(FATAL) << "Failed to parse '" << input
<< "' as an MPModelProto or an MPModelRequest.";
} else {
CHECK(!(is_model_proto && is_request_proto));
}
if (is_request_proto) {
LOG(INFO) << "Read input proto as an MPModelRequest.";
} else {
LOG(INFO) << "Read input proto as an MPModelProto.";
model_proto.Swap(request_proto.mutable_model());
}
return request_proto;
}
// Returns false if an error was encountered.
// More details should be available in the logs.
bool Run(MPSolver::OptimizationProblemType type) {
MPModelRequest request_proto = ReadMipModel(absl::GetFlag(FLAGS_input));
printf("%-12s: '%s'\n", "File", absl::GetFlag(FLAGS_input).c_str());
// Detect format to dump protos.
operations_research::ProtoWriteFormat write_format;
if (absl::GetFlag(FLAGS_dump_format) == "text") {
write_format = ProtoWriteFormat::kProtoText;
} else if (absl::GetFlag(FLAGS_dump_format) == "binary") {
write_format = ProtoWriteFormat::kProtoBinary;
} else if (absl::GetFlag(FLAGS_dump_format) == "json") {
write_format = ProtoWriteFormat::kJson;
} else {
LOG(FATAL) << "Unsupported --dump_format: "
<< absl::GetFlag(FLAGS_dump_format);
}
// Create the solver, we use the name of the model as the solver name.
MPSolver solver(request_proto.model().name(), type);
const absl::Status set_num_threads_status =
solver.SetNumThreads(absl::GetFlag(FLAGS_num_threads));
if (set_num_threads_status.ok()) {
LOG(INFO) << "Set number of threads to " << absl::GetFlag(FLAGS_num_threads)
<< ".";
} else if (absl::GetFlag(FLAGS_num_threads) != 1) {
LOG(ERROR) << "Failed to set number of threads due to: "
<< set_num_threads_status.message() << ". Using 1 as default.";
}
solver.EnableOutput();
if (!absl::GetFlag(FLAGS_params_file).empty()) {
std::string file_contents;
CHECK_OK(file::GetContents(absl::GetFlag(FLAGS_params_file), &file_contents,
file::Defaults()))
<< "Could not read parameters file.";
CHECK(solver.SetSolverSpecificParametersAsString(file_contents));
} else if (!absl::GetFlag(FLAGS_params).empty()) {
CHECK(
solver.SetSolverSpecificParametersAsString(absl::GetFlag(FLAGS_params)))
<< "Wrong --params format.";
}
absl::PrintF(
"%-12s: %s\n", "Solver",
MPModelRequest::SolverType_Name(
static_cast<MPModelRequest::SolverType>(solver.ProblemType()))
.c_str());
// Load the proto into the solver.
std::string error_message;
// If requested, save the model to file.
if (!absl::GetFlag(FLAGS_dump_model).empty()) {
CHECK(WriteProtoToFile(absl::GetFlag(FLAGS_dump_model),
request_proto.model(), write_format,
absl::GetFlag(FLAGS_dump_gzip)));
}
const MPSolverResponseStatus status =
solver.LoadModelFromProtoWithUniqueNamesOrDie(request_proto.model(),
&error_message);
// Note, the underlying MPSolver treats time limit equal to 0 as no limit.
if (status != MPSOLVER_MODEL_IS_VALID) {
LOG(ERROR) << MPSolverResponseStatus_Name(status) << ": " << error_message;
return false;
}
// Time limits.
if (absl::GetFlag(FLAGS_time_limit) != absl::InfiniteDuration()) {
LOG(INFO) << "Setting a time limit of " << absl::GetFlag(FLAGS_time_limit);
// Overwrite the request time limit.
request_proto.set_solver_time_limit_seconds(
absl::ToDoubleSeconds(absl::GetFlag(FLAGS_time_limit)));
}
if (request_proto.has_solver_time_limit_seconds()) {
solver.SetTimeLimit(
absl::Seconds(request_proto.solver_time_limit_seconds()));
}
absl::PrintF("%-12s: %d x %d\n", "Dimension", solver.NumConstraints(),
solver.NumVariables());
// Register a signal handler to interrupt the solve when the user presses ^C.
// Note that we ignore all previously registered handler here. If SCIP is
// used, this handler will be overridden by the one of SCIP that does the same
// thing.
SigintHandler handler;
handler.Register([&solver] { solver.InterruptSolve(); });
// Solve.
MPSolverParameters param;
MPSolver::ResultStatus solve_status = MPSolver::NOT_SOLVED;
absl::Duration solving_time;
const absl::Time time_before = absl::Now();
solve_status = solver.Solve(param);
solving_time = absl::Now() - time_before;
// If requested, re-create a corresponding MPModelRequest and save it to file.
if (!absl::GetFlag(FLAGS_dump_request).empty()) {
request_proto.set_solver_type(
static_cast<MPModelRequest::SolverType>(solver.ProblemType()));
request_proto.set_solver_time_limit_seconds(solver.time_limit_in_secs());
request_proto.set_solver_specific_parameters(
solver.GetSolverSpecificParametersAsString());
CHECK(WriteProtoToFile(absl::GetFlag(FLAGS_dump_request), request_proto,
write_format, absl::GetFlag(FLAGS_dump_gzip)));
}
const bool has_solution =
solve_status == MPSolver::OPTIMAL || solve_status == MPSolver::FEASIBLE;
if (!absl::GetFlag(FLAGS_sol_file).empty() && has_solution) {
operations_research::MPSolutionResponse response;
solver.FillSolutionResponseProto(&response);
std::string sol_string;
absl::StrAppend(&sol_string, "=obj= ", response.objective_value(), "\n");
for (int i = 0; i < response.variable_value().size(); ++i) {
absl::StrAppend(&sol_string, request_proto.model().variable(i).name(),
" ", response.variable_value(i), "\n");
}
LOG(INFO) << "Writing .sol solution to '" << absl::GetFlag(FLAGS_sol_file)
<< "'.\n";
CHECK_OK(file::SetContents(absl::GetFlag(FLAGS_sol_file), sol_string,
file::Defaults()));
}
// If requested, get the MPSolutionResponse and save it to file.
if (!absl::GetFlag(FLAGS_dump_response).empty() && has_solution) {
operations_research::MPSolutionResponse response;
solver.FillSolutionResponseProto(&response);
CHECK(WriteProtoToFile(absl::GetFlag(FLAGS_dump_response), response,
write_format, absl::GetFlag(FLAGS_dump_gzip)));
}
if (!absl::GetFlag(FLAGS_output_csv).empty() && has_solution) {
operations_research::MPSolutionResponse result;
solver.FillSolutionResponseProto(&result);
std::string csv_file;
for (int i = 0; i < result.variable_value_size(); ++i) {
csv_file +=
absl::StrFormat("%s,%e\n", request_proto.model().variable(i).name(),
result.variable_value(i));
}
CHECK_OK(file::SetContents(absl::GetFlag(FLAGS_output_csv), csv_file,
file::Defaults()));
}
// If --verify_solution is true, we already verified it. If not, we add
// a verification step here.
if (has_solution && !absl::GetFlag(FLAGS_verify_solution)) {
LOG(INFO) << "Verifying the solution";
solver.VerifySolution(/*tolerance=*/param.GetDoubleParam(
MPSolverParameters::PRIMAL_TOLERANCE),
/*log_errors=*/true);
}
absl::PrintF("%-12s: %s\n", "Status",
MPSolverResponseStatus_Name(
static_cast<MPSolverResponseStatus>(solve_status))
.c_str());
absl::PrintF("%-12s: %15.15e\n", "Objective",
has_solution ? solver.Objective().Value() : 0.0);
absl::PrintF("%-12s: %15.15e\n", "BestBound",
has_solution ? solver.Objective().BestBound() : 0.0);
absl::PrintF("%-12s: %d\n", "Iterations", solver.iterations());
// NOTE(user): nodes() for non-MIP solvers crashes in debug mode by design.
if (solver.IsMIP()) {
absl::PrintF("%-12s: %d\n", "Nodes", solver.nodes());
}
absl::PrintF("%-12s: %-6.4g\n", "Time", absl::ToDoubleSeconds(solving_time));
return true;
}
} // namespace
} // namespace operations_research
int main(int argc, char** argv) {
absl::SetFlag(&FLAGS_logtostderr, true);
google::InitGoogleLogging(kUsageStr);
absl::ParseCommandLine(argc, argv);
QCHECK(!absl::GetFlag(FLAGS_input).empty()) << "--input is required";
QCHECK_GE(absl::GetFlag(FLAGS_time_limit), absl::ZeroDuration())
<< "--time_limit must be given a positive duration";
operations_research::MPSolver::OptimizationProblemType type;
CHECK(operations_research::MPSolver::ParseSolverType(
absl::GetFlag(FLAGS_solver), &type))
<< "Unsupported --solver: " << absl::GetFlag(FLAGS_solver);
if (!operations_research::Run(type)) {
// If the solver is SAT and we encountered an error, display it in a format
// interpretable by our scripts.
if (type == operations_research::MPSolver::SAT_INTEGER_PROGRAMMING) {
operations_research::sat::CpSolverResponse response;
response.set_status(
operations_research::sat::CpSolverStatus::MODEL_INVALID);
LOG(INFO) << operations_research::sat::CpSolverResponseStats(response);
}
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}