SIGN IN SIGN UP
google / or-tools UNCLAIMED

Google's Operations Research tools:

0 0 1 C++
2025-01-10 11:35:44 +01:00
// Copyright 2010-2025 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>
2019-04-18 19:20:39 +02:00
2025-02-25 16:03:40 +01:00
#include <cstdlib>
#include <string>
2023-01-31 20:46:43 +01:00
#include <vector>
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"
dotnet: Remove reference to dotnet release command - Currently not implemented... Add abseil patch - Add patches/absl-config.cmake Makefile: Add abseil-cpp on unix - Force abseil-cpp SHA1 to 45221cc note: Just before the PR #136 which break all CMake Makefile: Add abseil-cpp on windows - Force abseil-cpp SHA1 to 45221cc note: Just before the PR #136 which break all CMake CMake: Add abseil-cpp - Force abseil-cpp SHA1 to 45221cc note: Just before the PR #136 which break all CMake port to absl: C++ Part - Fix warning with the use of ABSL_MUST_USE_RESULT > The macro must appear as the very first part of a function declaration or definition: ... Note: past advice was to place the macro after the argument list. src: dependencies/sources/abseil-cpp-master/absl/base/attributes.h:418 - Rename enum after windows clash - Remove non compact table constraints - Change index type from int64 to int in routing library - Fix file_nonport compilation on windows - Fix another naming conflict with windows (NO_ERROR is a macro) - Cleanup hash containers; work on sat internals - Add optional_boolean sub-proto Sync cpp examples with internal code - reenable issue173 after reducing number of loops port to absl: Python Part - Add back cp_model.INT32_MIN|MAX for examples Update Python examples - Add random_tsp.py - Run words_square example - Run magic_square in python tests port to absl: Java Part - Fix compilation of the new routing parameters in java - Protect some code from SWIG parsing Update Java Examples port to absl: .Net Part Update .Net examples work on sat internals; Add C++ CP-SAT CpModelBuilder API; update sample code and recipes to use the new API; sync with internal code Remove VS 2015 in Appveyor-CI - abseil-cpp does not support VS 2015... improve tables upgrade C++ sat examples to use the new API; work on sat internals update license dates rewrite jobshop_ft06_distance.py to use the CP-SAT solver rename last example revert last commit more work on SAT internals fix
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"
#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"
#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"
#include "ortools/lp_data/mps_reader.h"
#include "ortools/lp_data/proto_utils.h"
dotnet: Remove reference to dotnet release command - Currently not implemented... Add abseil patch - Add patches/absl-config.cmake Makefile: Add abseil-cpp on unix - Force abseil-cpp SHA1 to 45221cc note: Just before the PR #136 which break all CMake Makefile: Add abseil-cpp on windows - Force abseil-cpp SHA1 to 45221cc note: Just before the PR #136 which break all CMake CMake: Add abseil-cpp - Force abseil-cpp SHA1 to 45221cc note: Just before the PR #136 which break all CMake port to absl: C++ Part - Fix warning with the use of ABSL_MUST_USE_RESULT > The macro must appear as the very first part of a function declaration or definition: ... Note: past advice was to place the macro after the argument list. src: dependencies/sources/abseil-cpp-master/absl/base/attributes.h:418 - Rename enum after windows clash - Remove non compact table constraints - Change index type from int64 to int in routing library - Fix file_nonport compilation on windows - Fix another naming conflict with windows (NO_ERROR is a macro) - Cleanup hash containers; work on sat internals - Add optional_boolean sub-proto Sync cpp examples with internal code - reenable issue173 after reducing number of loops port to absl: Python Part - Add back cp_model.INT32_MIN|MAX for examples Update Python examples - Add random_tsp.py - Run words_square example - Run magic_square in python tests port to absl: Java Part - Fix compilation of the new routing parameters in java - Protect some code from SWIG parsing Update Java Examples port to absl: .Net Part Update .Net examples work on sat internals; Add C++ CP-SAT CpModelBuilder API; update sample code and recipes to use the new API; sync with internal code Remove VS 2015 in Appveyor-CI - abseil-cpp does not support VS 2015... improve tables upgrade C++ sat examples to use the new API; work on sat internals update license dates rewrite jobshop_ft06_distance.py to use the CP-SAT solver rename last example revert last commit more work on SAT internals fix
2018-10-31 16:18:18 +01:00
#include "ortools/util/file_util.h"
#include "ortools/util/proto_tools.h"
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)");
2018-06-08 16:40:43 +02:00
using google::protobuf::TextFormat;
using operations_research::FullProtocolMessageAsString;
dotnet: Remove reference to dotnet release command - Currently not implemented... Add abseil patch - Add patches/absl-config.cmake Makefile: Add abseil-cpp on unix - Force abseil-cpp SHA1 to 45221cc note: Just before the PR #136 which break all CMake Makefile: Add abseil-cpp on windows - Force abseil-cpp SHA1 to 45221cc note: Just before the PR #136 which break all CMake CMake: Add abseil-cpp - Force abseil-cpp SHA1 to 45221cc note: Just before the PR #136 which break all CMake port to absl: C++ Part - Fix warning with the use of ABSL_MUST_USE_RESULT > The macro must appear as the very first part of a function declaration or definition: ... Note: past advice was to place the macro after the argument list. src: dependencies/sources/abseil-cpp-master/absl/base/attributes.h:418 - Rename enum after windows clash - Remove non compact table constraints - Change index type from int64 to int in routing library - Fix file_nonport compilation on windows - Fix another naming conflict with windows (NO_ERROR is a macro) - Cleanup hash containers; work on sat internals - Add optional_boolean sub-proto Sync cpp examples with internal code - reenable issue173 after reducing number of loops port to absl: Python Part - Add back cp_model.INT32_MIN|MAX for examples Update Python examples - Add random_tsp.py - Run words_square example - Run magic_square in python tests port to absl: Java Part - Fix compilation of the new routing parameters in java - Protect some code from SWIG parsing Update Java Examples port to absl: .Net Part Update .Net examples work on sat internals; Add C++ CP-SAT CpModelBuilder API; update sample code and recipes to use the new API; sync with internal code Remove VS 2015 in Appveyor-CI - abseil-cpp does not support VS 2015... improve tables upgrade C++ sat examples to use the new API; work on sat internals update license dates rewrite jobshop_ft06_distance.py to use the CP-SAT solver rename last example revert last commit more work on SAT internals fix
2018-10-31 16:18:18 +01:00
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;
2018-06-08 16:40:43 +02:00
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.
2020-10-29 14:25:39 +01:00
void ReadGlopParameters(GlopParameters* parameters) {
if (!absl::GetFlag(FLAGS_params_file).empty()) {
std::string params;
CHECK_OK(file::GetContents(absl::GetFlag(FLAGS_params_file), &params,
file::Defaults()));
2019-04-18 19:20:39 +02:00
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)) {
2021-01-14 10:48:19 +01:00
absl::PrintF("GlopParameters {\n%s}\n",
FullProtocolMessageAsString(*parameters, 1));
}
}
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);
GlopParameters parameters;
ReadGlopParameters(&parameters);
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) {
2020-10-29 14:25:39 +01:00
const std::string& file_name = file_list[i];
MPSReader mps_reader;
operations_research::MPModelProto model_proto;
dotnet: Remove reference to dotnet release command - Currently not implemented... Add abseil patch - Add patches/absl-config.cmake Makefile: Add abseil-cpp on unix - Force abseil-cpp SHA1 to 45221cc note: Just before the PR #136 which break all CMake Makefile: Add abseil-cpp on windows - Force abseil-cpp SHA1 to 45221cc note: Just before the PR #136 which break all CMake CMake: Add abseil-cpp - Force abseil-cpp SHA1 to 45221cc note: Just before the PR #136 which break all CMake port to absl: C++ Part - Fix warning with the use of ABSL_MUST_USE_RESULT > The macro must appear as the very first part of a function declaration or definition: ... Note: past advice was to place the macro after the argument list. src: dependencies/sources/abseil-cpp-master/absl/base/attributes.h:418 - Rename enum after windows clash - Remove non compact table constraints - Change index type from int64 to int in routing library - Fix file_nonport compilation on windows - Fix another naming conflict with windows (NO_ERROR is a macro) - Cleanup hash containers; work on sat internals - Add optional_boolean sub-proto Sync cpp examples with internal code - reenable issue173 after reducing number of loops port to absl: Python Part - Add back cp_model.INT32_MIN|MAX for examples Update Python examples - Add random_tsp.py - Run words_square example - Run magic_square in python tests port to absl: Java Part - Fix compilation of the new routing parameters in java - Protect some code from SWIG parsing Update Java Examples port to absl: .Net Part Update .Net examples work on sat internals; Add C++ CP-SAT CpModelBuilder API; update sample code and recipes to use the new API; sync with internal code Remove VS 2015 in Appveyor-CI - abseil-cpp does not support VS 2015... improve tables upgrade C++ sat examples to use the new API; work on sat internals update license dates rewrite jobshop_ft06_distance.py to use the CP-SAT solver rename last example revert last commit more work on SAT internals fix
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;
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;
}
MPModelProtoToLinearProgram(model_proto, &linear_program);
}
if (absl::GetFlag(FLAGS_mps_dump_problem)) {
2021-01-14 10:48:19 +01:00
absl::PrintF("%s", linear_program.Dump());
}
// 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;
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);
2015-07-31 15:35:59 +02:00
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)) {
2021-01-14 10:48:19 +01:00
absl::PrintF("%s,", file_name);
}
2021-01-14 10:48:19 +01:00
absl::PrintF("%s,", linear_program.name());
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);
}
2021-01-14 10:48:19 +01:00
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)) {
2021-01-14 10:48:19 +01:00
absl::PrintF("%-45s: %s\n", "File path", file_name);
}
2021-01-14 10:48:19 +01:00
absl::PrintF("%-45s: %s\n", "Problem name", linear_program.name());
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);
}
2021-01-14 10:48:19 +01:00
absl::PrintF("%s%s", linear_program.GetPrettyProblemStats(),
linear_program.GetPrettyNonZeroStats());
}
}
return EXIT_SUCCESS;
}