Google OR-Tools v9.11
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
model_reader.cc
Go to the documentation of this file.
1// Copyright 2010-2024 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 <string>
17
19#include "ortools/linear_solver/linear_solver.pb.h"
23
24namespace operations_research {
25namespace glop {
26
27bool LoadMPModelProtoFromModelOrRequest(const std::string& input_file_path,
28 MPModelProto* model) {
29 MPModelProto model_proto;
30 MPModelRequest request_proto;
31 ReadFileToProto(input_file_path, &model_proto).IgnoreError();
32 ReadFileToProto(input_file_path, &request_proto).IgnoreError();
33 // If the input proto is in binary format, both ReadFileToProto could return
34 // true. Instead use the actual number of variables found to test the
35 // correct format of the input.
36 const bool is_model_proto = model_proto.variable_size() > 0;
37 const bool is_request_proto = request_proto.model().variable_size() > 0;
38 if (!is_model_proto && !is_request_proto) {
39 LOG(ERROR) << "Failed to parse '" << input_file_path
40 << "' as an MPModelProto or an MPModelRequest.";
41 return false;
42 } else {
43 if (is_model_proto && is_request_proto) {
44 LOG(ERROR) << input_file_path
45 << " is parsing as both MPModelProto and MPModelRequest";
46 return false;
47 }
48 if (is_request_proto) {
49 VLOG(1) << "Read input proto as an MPModelRequest.";
50 model_proto.Swap(request_proto.mutable_model());
51 } else {
52 VLOG(1) << "Read input proto as an MPModelProto.";
53 }
54 }
55 model->Swap(&model_proto);
56 return true;
57}
58
59bool LoadLinearProgramFromModelOrRequest(const std::string& input_file_path,
60 LinearProgram* linear_program) {
61 MPModelProto model_proto;
62 if (LoadMPModelProtoFromModelOrRequest(input_file_path, &model_proto)) {
63 MPModelProtoToLinearProgram(model_proto, linear_program);
64 return true;
65 }
66 return false;
67}
68
69} // namespace glop
70} // namespace operations_research
GRBmodel * model
bool LoadMPModelProtoFromModelOrRequest(const std::string &input_file_path, MPModelProto *model)
bool LoadLinearProgramFromModelOrRequest(const std::string &input_file_path, LinearProgram *linear_program)
void MPModelProtoToLinearProgram(const MPModelProto &input, LinearProgram *output)
Converts a MPModelProto to a LinearProgram.
In SWIG mode, we don't want anything besides these top-level includes.
absl::Status ReadFileToProto(absl::string_view filename, google::protobuf::Message *proto, bool allow_partial)
Definition file_util.cc:53