OR-Tools  9.3
model_builder_helper.cc
Go to the documentation of this file.
1// Copyright 2010-2021 Google LLC
2// Licensed under the Apache License, Version 2.0 (the "License");
3// you may not use this file except in compliance with the License.
4// You may obtain a copy of the License at
5//
6// http://www.apache.org/licenses/LICENSE-2.0
7//
8// Unless required by applicable law or agreed to in writing, software
9// distributed under the License is distributed on an "AS IS" BASIS,
10// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11// See the License for the specific language governing permissions and
12// limitations under the License.
13
15
16#include <functional>
17#include <string>
18#include <utility>
19
21#include "ortools/linear_solver/linear_solver.pb.h"
24
25namespace operations_research {
26
28 const MPModelProto& input_model, const MPModelExportOptions& options) {
29 return operations_research::ExportModelAsMpsFormat(input_model, options)
30 .value_or("");
31}
32
34 const MPModelProto& input_model, const MPModelExportOptions& options) {
35 return operations_research::ExportModelAsLpFormat(input_model, options)
36 .value_or("");
37}
38
39// See comment in the header file why we need to wrap absl::Status code with
40// code having simpler APIs.
42 const std::string& mps_string) {
43 absl::StatusOr<MPModelProto> model_or =
45 if (model_or.ok()) return model_or.value();
46 MPModelProto model;
47 model.set_name("Invalid model");
48 return model;
49}
50
52 const std::string& mps_file) {
53 absl::StatusOr<MPModelProto> model_or =
55 if (model_or.ok()) return model_or.value();
56 MPModelProto model;
57 model.set_name("Invalid model");
58 return model;
59}
60
62 const std::string& lp_string) {
63 absl::StatusOr<MPModelProto> model_proto = ModelProtoFromLpFormat(lp_string);
64 if (model_proto.ok()) return model_proto.value();
65 MPModelProto model;
66 model.set_name("Invalid model");
67 return model;
68}
69
70MPModelProto ModelBuilderHelper::ImportFromLpFile(const std::string& lp_file) {
71 absl::StatusOr<MPModelProto> model_proto = ModelProtoFromLpFormat(lp_file);
72 if (model_proto.ok()) return model_proto.value();
73 MPModelProto model;
74 model.set_name("Invalid model");
75 return model;
76}
77
78MPSolutionResponse ModelSolverHelper::Solve(const MPModelRequest& request) {
79 MPSolutionResponse response;
80 // TODO(user): Make compatible with solvers that don't support interrupt.
81 // TODO(user): Register the log callback.
82 MPSolver::SolveWithProto(request, &response, &interrupt_solve_);
83 return response;
84}
85
87 std::function<void(const std::string&)> log_callback) {
88 log_callback_ = std::move(log_callback);
89}
90
92 LogCallback* log_callback) {
93 log_callback_ = [log_callback](const std::string& message) {
94 log_callback->NewMessage(message);
95 };
96}
97
99 interrupt_solve_ = true;
100 return true;
101}
102
103} // namespace operations_research
virtual void NewMessage(const std::string &message)=0
static void SolveWithProto(const MPModelRequest &model_request, MPSolutionResponse *response, std::atomic< bool > *interrupt=nullptr)
Solves the model encoded by a MPModelRequest protocol buffer and fills the solution encoded as a MPSo...
static operations_research::MPModelProto ImportFromLpFile(const std::string &lp_file)
static operations_research::MPModelProto ImportFromMpsString(const std::string &mps_string)
static operations_research::MPModelProto ImportFromMpsFile(const std::string &mps_file)
static std::string ExportModelProtoToMpsString(const operations_research::MPModelProto &input_model, const operations_research::MPModelExportOptions &options=MPModelExportOptions())
static operations_research::MPModelProto ImportFromLpString(const std::string &lp_string)
static std::string ExportModelProtoToLpString(const operations_research::MPModelProto &input_model, const operations_research::MPModelExportOptions &options=MPModelExportOptions())
void SetLogCallbackFromDirectorClass(LogCallback *log_callback)
void SetLogCallback(std::function< void(const std::string &)> log_callback)
operations_research::MPSolutionResponse Solve(const operations_research::MPModelRequest &request)
CpModelProto const * model_proto
SharedResponseManager * response
GRBmodel * model
A C++ wrapper that provides a simple and unified interface to several linear programming and mixed in...
absl::StatusOr< MPModelProto > MpsDataToMPModelProto(const std::string &mps_data)
Definition: mps_reader.cc:1170
Collection of objects used to extend the Constraint Solver library.
absl::StatusOr< std::string > ExportModelAsMpsFormat(const MPModelProto &model, const MPModelExportOptions &options)
Outputs the current model (variables, constraints, objective) as a string encoded in MPS file format,...
absl::StatusOr< MPModelProto > ModelProtoFromLpFormat(absl::string_view model)
Definition: lp_parser.cc:450
absl::StatusOr< std::string > ExportModelAsLpFormat(const MPModelProto &model, const MPModelExportOptions &options)
Outputs the current model (variables, constraints, objective) as a string encoded in the so-called "C...
std::string message
Definition: trace.cc:398