forked from google/or-tools
-
Notifications
You must be signed in to change notification settings - Fork 2
/
mps_driver.cc
165 lines (151 loc) · 6.37 KB
/
mps_driver.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
// Copyright 2010-2024 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.
// Driver for reading and solving files in the MPS format and in
// the linear_solver.proto format.
#include <stdio.h>
#include <string>
#include <vector>
#include "absl/status/status.h"
#include "absl/strings/match.h"
#include "google/protobuf/descriptor.h"
#include "google/protobuf/message.h"
#include "google/protobuf/text_format.h"
#include "ortools/base/commandlineflags.h"
#include "ortools/base/file.h"
#include "ortools/base/helpers.h"
#include "ortools/base/init_google.h"
#include "ortools/base/logging.h"
#include "ortools/base/timer.h"
#include "ortools/glop/lp_solver.h"
#include "ortools/glop/parameters.pb.h"
#include "ortools/lp_data/lp_print_utils.h"
#include "ortools/lp_data/mps_reader.h"
#include "ortools/lp_data/proto_utils.h"
#include "ortools/util/file_util.h"
#include "ortools/util/proto_tools.h"
ABSL_FLAG(bool, mps_dump_problem, false, "Dumps problem in readable form.");
ABSL_FLAG(bool, mps_solve, true, "Solves problem.");
ABSL_FLAG(bool, mps_terse_result, false,
"Displays the result in form of a single CSV line.");
ABSL_FLAG(bool, mps_verbose_result, true,
"Displays the result in verbose form.");
ABSL_FLAG(bool, mps_display_full_path, true,
"Displays the full path of the input file in the result line.");
ABSL_FLAG(std::string, input, "", "File pattern for problems to be optimized.");
ABSL_FLAG(std::string, params_file, "",
"Path to a GlopParameters file in text format.");
ABSL_FLAG(std::string, params, "",
"GlopParameters in text format. If --params_file was "
"also specified, the --params will be merged onto "
"them (i.e. in case of conflicts, --params wins)");
using google::protobuf::TextFormat;
using operations_research::FullProtocolMessageAsString;
using operations_research::ReadFileToProto;
using operations_research::glop::GetProblemStatusString;
using operations_research::glop::GlopParameters;
using operations_research::glop::LinearProgram;
using operations_research::glop::LPSolver;
using operations_research::glop::MPModelProtoToLinearProgram;
using operations_research::glop::MPSReader;
using operations_research::glop::ProblemStatus;
using operations_research::glop::ToDouble;
// Parse glop parameters from the flags --params_file and --params.
void ReadGlopParameters(GlopParameters* parameters) {
if (!absl::GetFlag(FLAGS_params_file).empty()) {
std::string params;
CHECK_OK(file::GetContents(absl::GetFlag(FLAGS_params_file), ¶ms,
file::Defaults()));
CHECK(TextFormat::ParseFromString(params, parameters)) << params;
}
if (!absl::GetFlag(FLAGS_params).empty()) {
CHECK(TextFormat::MergeFromString(absl::GetFlag(FLAGS_params), parameters))
<< absl::GetFlag(FLAGS_params);
}
if (absl::GetFlag(FLAGS_mps_verbose_result)) {
absl::PrintF("GlopParameters {\n%s}\n",
FullProtocolMessageAsString(*parameters, 1));
}
}
int main(int argc, char* argv[]) {
InitGoogle(
"Runs Glop on a given pattern of files given by --input. "
"The files must be in Mps or linear_solver.proto format and can be "
"compressed with gzip.",
&argc, &argv, true);
absl::SetFlag(&FLAGS_stderrthreshold, 0);
GlopParameters parameters;
ReadGlopParameters(¶meters);
LinearProgram linear_program;
std::vector<std::string> file_list;
// Replace this with your favorite match function.
file_list.push_back(absl::GetFlag(FLAGS_input));
for (int i = 0; i < file_list.size(); ++i) {
const std::string& file_name = file_list[i];
MPSReader mps_reader;
operations_research::MPModelProto model_proto;
if (absl::EndsWith(file_name, ".mps") ||
absl::EndsWith(file_name, ".mps.gz")) {
const absl::Status parse_status =
mps_reader.ParseFile(file_name, &linear_program);
if (!parse_status.ok()) {
LOG(INFO) << "Parse error for " << file_name << ": " << parse_status;
continue;
}
} else {
ReadFileToProto(file_name, &model_proto);
MPModelProtoToLinearProgram(model_proto, &linear_program);
}
if (absl::GetFlag(FLAGS_mps_dump_problem)) {
absl::PrintF("%s", linear_program.Dump());
}
// Create the solver with the correct parameters.
LPSolver solver;
solver.SetParameters(parameters);
ProblemStatus solve_status = ProblemStatus::INIT;
std::string status_string;
double objective_value;
double solving_time_in_sec = 0;
if (absl::GetFlag(FLAGS_mps_solve)) {
ScopedWallTime timer(&solving_time_in_sec);
solve_status = solver.Solve(linear_program);
status_string = GetProblemStatusString(solve_status);
objective_value = ToDouble(solver.GetObjectiveValue());
}
if (absl::GetFlag(FLAGS_mps_terse_result)) {
if (absl::GetFlag(FLAGS_mps_display_full_path)) {
absl::PrintF("%s,", file_name);
}
absl::PrintF("%s,", linear_program.name());
if (absl::GetFlag(FLAGS_mps_solve)) {
absl::PrintF("%15.15e,%s,%-6.4g,", objective_value, status_string,
solving_time_in_sec);
}
absl::PrintF("%s,%s\n", linear_program.GetProblemStats(),
linear_program.GetNonZeroStats());
}
if (absl::GetFlag(FLAGS_mps_verbose_result)) {
if (absl::GetFlag(FLAGS_mps_display_full_path)) {
absl::PrintF("%-45s: %s\n", "File path", file_name);
}
absl::PrintF("%-45s: %s\n", "Problem name", linear_program.name());
if (absl::GetFlag(FLAGS_mps_solve)) {
absl::PrintF("%-45s: %15.15e\n", "Objective value", objective_value);
absl::PrintF("%-45s: %s\n", "Problem status", status_string);
absl::PrintF("%-45s: %-6.4g\n", "Solving time", solving_time_in_sec);
}
absl::PrintF("%s%s", linear_program.GetPrettyProblemStats(),
linear_program.GetPrettyNonZeroStats());
}
}
return EXIT_SUCCESS;
}