2025-01-10 11:35:44 +01:00
|
|
|
// Copyright 2010-2025 Google LLC
|
2014-07-08 09:27:02 +00:00
|
|
|
// 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
|
2015-06-16 10:08:44 +02:00
|
|
|
// the linear_solver.proto format.
|
2014-07-08 09:27:02 +00:00
|
|
|
|
|
|
|
|
#include <stdio.h>
|
2019-04-18 19:20:39 +02:00
|
|
|
|
2025-02-25 16:03:40 +01:00
|
|
|
#include <cstdlib>
|
2014-07-08 09:27:02 +00:00
|
|
|
#include <string>
|
2023-01-31 20:46:43 +01:00
|
|
|
#include <vector>
|
2014-07-08 09:27:02 +00:00
|
|
|
|
2025-02-25 16:03:40 +01:00
|
|
|
#include "absl/base/log_severity.h"
|
|
|
|
|
#include "absl/flags/flag.h"
|
|
|
|
|
#include "absl/log/check.h"
|
|
|
|
|
#include "absl/log/globals.h"
|
2025-03-04 21:10:09 +01:00
|
|
|
#include "absl/log/log.h"
|
2020-05-06 18:35:18 +02:00
|
|
|
#include "absl/status/status.h"
|
2018-10-31 16:18:18 +01:00
|
|
|
#include "absl/strings/match.h"
|
2025-02-25 16:03:40 +01:00
|
|
|
#include "absl/strings/str_format.h"
|
2014-07-08 09:27:02 +00:00
|
|
|
#include "google/protobuf/text_format.h"
|
2023-01-31 20:46:43 +01:00
|
|
|
#include "ortools/base/helpers.h"
|
2022-02-25 09:47:52 +01:00
|
|
|
#include "ortools/base/init_google.h"
|
2025-02-25 16:03:40 +01:00
|
|
|
#include "ortools/base/options.h"
|
2018-06-08 16:40:43 +02:00
|
|
|
#include "ortools/base/timer.h"
|
2017-04-26 17:30:25 +02:00
|
|
|
#include "ortools/glop/lp_solver.h"
|
|
|
|
|
#include "ortools/glop/parameters.pb.h"
|
2025-02-25 16:03:40 +01:00
|
|
|
#include "ortools/lp_data/lp_data.h"
|
|
|
|
|
#include "ortools/lp_data/lp_types.h"
|
2017-04-26 17:30:25 +02:00
|
|
|
#include "ortools/lp_data/mps_reader.h"
|
|
|
|
|
#include "ortools/lp_data/proto_utils.h"
|
2018-10-31 16:18:18 +01:00
|
|
|
#include "ortools/util/file_util.h"
|
2017-04-26 17:30:25 +02:00
|
|
|
#include "ortools/util/proto_tools.h"
|
2014-07-08 09:27:02 +00:00
|
|
|
|
2020-10-23 11:50:14 +02:00
|
|
|
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)");
|
2014-07-08 09:27:02 +00:00
|
|
|
|
2018-06-08 16:40:43 +02:00
|
|
|
using google::protobuf::TextFormat;
|
2014-07-08 09:27:02 +00:00
|
|
|
using operations_research::FullProtocolMessageAsString;
|
2018-10-31 16:18:18 +01:00
|
|
|
using operations_research::ReadFileToProto;
|
2014-07-08 09:27:02 +00:00
|
|
|
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;
|
2018-06-08 16:40:43 +02:00
|
|
|
using operations_research::glop::MPSReader;
|
2014-07-08 09:27:02 +00:00
|
|
|
using operations_research::glop::ProblemStatus;
|
|
|
|
|
using operations_research::glop::ToDouble;
|
|
|
|
|
|
|
|
|
|
// Parse glop parameters from the flags --params_file and --params.
|
2020-10-29 14:25:39 +01:00
|
|
|
void ReadGlopParameters(GlopParameters* parameters) {
|
2020-10-21 00:21:54 +02:00
|
|
|
if (!absl::GetFlag(FLAGS_params_file).empty()) {
|
2014-07-08 09:27:02 +00:00
|
|
|
std::string params;
|
2023-02-01 14:14:30 +01:00
|
|
|
CHECK_OK(file::GetContents(absl::GetFlag(FLAGS_params_file), ¶ms,
|
2023-06-21 17:31:06 +02:00
|
|
|
file::Defaults()));
|
2019-04-18 19:20:39 +02:00
|
|
|
CHECK(TextFormat::ParseFromString(params, parameters)) << params;
|
2014-07-08 09:27:02 +00:00
|
|
|
}
|
2020-10-21 00:21:54 +02:00
|
|
|
if (!absl::GetFlag(FLAGS_params).empty()) {
|
|
|
|
|
CHECK(TextFormat::MergeFromString(absl::GetFlag(FLAGS_params), parameters))
|
|
|
|
|
<< absl::GetFlag(FLAGS_params);
|
2014-07-08 09:27:02 +00:00
|
|
|
}
|
2020-10-21 00:21:54 +02:00
|
|
|
if (absl::GetFlag(FLAGS_mps_verbose_result)) {
|
2021-01-14 10:48:19 +01:00
|
|
|
absl::PrintF("GlopParameters {\n%s}\n",
|
|
|
|
|
FullProtocolMessageAsString(*parameters, 1));
|
2014-07-08 09:27:02 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-29 14:25:39 +01:00
|
|
|
int main(int argc, char* argv[]) {
|
2022-02-25 09:47:52 +01:00
|
|
|
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);
|
2025-02-25 16:03:40 +01:00
|
|
|
absl::SetStderrThreshold(absl::LogSeverityAtLeast::kInfo);
|
2014-07-08 09:27:02 +00:00
|
|
|
|
|
|
|
|
GlopParameters parameters;
|
|
|
|
|
ReadGlopParameters(¶meters);
|
|
|
|
|
|
|
|
|
|
LinearProgram linear_program;
|
|
|
|
|
std::vector<std::string> file_list;
|
|
|
|
|
// Replace this with your favorite match function.
|
2020-10-21 00:21:54 +02:00
|
|
|
file_list.push_back(absl::GetFlag(FLAGS_input));
|
2014-07-08 09:27:02 +00:00
|
|
|
for (int i = 0; i < file_list.size(); ++i) {
|
2020-10-29 14:25:39 +01:00
|
|
|
const std::string& file_name = file_list[i];
|
2014-07-08 09:27:02 +00:00
|
|
|
MPSReader mps_reader;
|
2015-06-16 10:08:44 +02:00
|
|
|
operations_research::MPModelProto model_proto;
|
2018-10-31 16:18:18 +01:00
|
|
|
if (absl::EndsWith(file_name, ".mps") ||
|
|
|
|
|
absl::EndsWith(file_name, ".mps.gz")) {
|
2020-05-06 18:35:18 +02:00
|
|
|
const absl::Status parse_status =
|
2019-04-18 19:20:39 +02:00
|
|
|
mps_reader.ParseFile(file_name, &linear_program);
|
|
|
|
|
if (!parse_status.ok()) {
|
|
|
|
|
LOG(INFO) << "Parse error for " << file_name << ": " << parse_status;
|
2014-07-08 09:27:02 +00:00
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
2025-01-06 13:20:57 +01:00
|
|
|
const absl::Status status = ReadFileToProto(file_name, &model_proto);
|
|
|
|
|
if (!status.ok()) {
|
|
|
|
|
LOG(INFO) << status;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2014-07-08 09:27:02 +00:00
|
|
|
MPModelProtoToLinearProgram(model_proto, &linear_program);
|
|
|
|
|
}
|
2020-10-21 00:21:54 +02:00
|
|
|
if (absl::GetFlag(FLAGS_mps_dump_problem)) {
|
2021-01-14 10:48:19 +01:00
|
|
|
absl::PrintF("%s", linear_program.Dump());
|
2014-07-08 09:27:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Create the solver with the correct parameters.
|
|
|
|
|
LPSolver solver;
|
|
|
|
|
solver.SetParameters(parameters);
|
|
|
|
|
ProblemStatus solve_status = ProblemStatus::INIT;
|
|
|
|
|
|
2015-07-31 15:35:59 +02:00
|
|
|
std::string status_string;
|
2014-07-08 09:27:02 +00:00
|
|
|
double objective_value;
|
|
|
|
|
double solving_time_in_sec = 0;
|
2020-10-21 00:21:54 +02:00
|
|
|
if (absl::GetFlag(FLAGS_mps_solve)) {
|
2014-07-08 09:27:02 +00:00
|
|
|
ScopedWallTime timer(&solving_time_in_sec);
|
|
|
|
|
solve_status = solver.Solve(linear_program);
|
2015-07-31 15:35:59 +02:00
|
|
|
status_string = GetProblemStatusString(solve_status);
|
2014-07-08 09:27:02 +00:00
|
|
|
objective_value = ToDouble(solver.GetObjectiveValue());
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-21 00:21:54 +02:00
|
|
|
if (absl::GetFlag(FLAGS_mps_terse_result)) {
|
|
|
|
|
if (absl::GetFlag(FLAGS_mps_display_full_path)) {
|
2021-01-14 10:48:19 +01:00
|
|
|
absl::PrintF("%s,", file_name);
|
2014-07-08 09:27:02 +00:00
|
|
|
}
|
2021-01-14 10:48:19 +01:00
|
|
|
absl::PrintF("%s,", linear_program.name());
|
2020-10-21 00:21:54 +02:00
|
|
|
if (absl::GetFlag(FLAGS_mps_solve)) {
|
2021-01-14 10:48:19 +01:00
|
|
|
absl::PrintF("%15.15e,%s,%-6.4g,", objective_value, status_string,
|
|
|
|
|
solving_time_in_sec);
|
2014-07-08 09:27:02 +00:00
|
|
|
}
|
2021-01-14 10:48:19 +01:00
|
|
|
absl::PrintF("%s,%s\n", linear_program.GetProblemStats(),
|
|
|
|
|
linear_program.GetNonZeroStats());
|
2014-07-08 09:27:02 +00:00
|
|
|
}
|
|
|
|
|
|
2020-10-21 00:21:54 +02:00
|
|
|
if (absl::GetFlag(FLAGS_mps_verbose_result)) {
|
|
|
|
|
if (absl::GetFlag(FLAGS_mps_display_full_path)) {
|
2021-01-14 10:48:19 +01:00
|
|
|
absl::PrintF("%-45s: %s\n", "File path", file_name);
|
2014-07-08 09:27:02 +00:00
|
|
|
}
|
2021-01-14 10:48:19 +01:00
|
|
|
absl::PrintF("%-45s: %s\n", "Problem name", linear_program.name());
|
2020-10-21 00:21:54 +02:00
|
|
|
if (absl::GetFlag(FLAGS_mps_solve)) {
|
2021-01-14 10:48:19 +01:00
|
|
|
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);
|
2014-07-08 09:27:02 +00:00
|
|
|
}
|
2021-01-14 10:48:19 +01:00
|
|
|
absl::PrintF("%s%s", linear_program.GetPrettyProblemStats(),
|
|
|
|
|
linear_program.GetPrettyNonZeroStats());
|
2014-07-08 09:27:02 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
|
}
|