OR-Tools  9.6
model_reader.cc
Go to the documentation of this file.
1 // Copyright 2010-2022 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 
18 #include "ortools/base/file.h"
19 #include "ortools/base/helpers.h"
20 #include "ortools/base/options.h"
21 #include "ortools/linear_solver/linear_solver.pb.h"
23 #include "ortools/util/file_util.h"
24 
25 namespace operations_research {
26 namespace glop {
27 
28 bool LoadMPModelProtoFromModelOrRequest(const std::string& input_file_path,
29  MPModelProto* model) {
30  MPModelProto model_proto;
31  MPModelRequest request_proto;
32  ReadFileToProto(input_file_path, &model_proto);
33  ReadFileToProto(input_file_path, &request_proto);
34  // If the input proto is in binary format, both ReadFileToProto could return
35  // true. Instead use the actual number of variables found to test the
36  // correct format of the input.
37  const bool is_model_proto = model_proto.variable_size() > 0;
38  const bool is_request_proto = request_proto.model().variable_size() > 0;
39  if (!is_model_proto && !is_request_proto) {
40  LOG(ERROR) << "Failed to parse '" << input_file_path
41  << "' as an MPModelProto or an MPModelRequest.";
42  return false;
43  } else {
44  if (is_model_proto && is_request_proto) {
45  LOG(ERROR) << input_file_path
46  << " is parsing as both MPModelProto and MPModelRequest";
47  return false;
48  }
49  if (is_request_proto) {
50  VLOG(1) << "Read input proto as an MPModelRequest.";
51  model_proto.Swap(request_proto.mutable_model());
52  } else {
53  VLOG(1) << "Read input proto as an MPModelProto.";
54  }
55  }
56  model->Swap(&model_proto);
57  return true;
58 }
59 
60 bool LoadLinearProgramFromModelOrRequest(const std::string& input_file_path,
61  LinearProgram* linear_program) {
62  MPModelProto model_proto;
63  if (LoadMPModelProtoFromModelOrRequest(input_file_path, &model_proto)) {
65  return true;
66  }
67  return false;
68 }
69 
70 } // namespace glop
71 } // namespace operations_research
CpModelProto const * model_proto
GRBmodel * model
void MPModelProtoToLinearProgram(const MPModelProto &input, LinearProgram *output)
Definition: proto_utils.cc:51
bool LoadMPModelProtoFromModelOrRequest(const std::string &input_file_path, MPModelProto *model)
Definition: model_reader.cc:28
bool LoadLinearProgramFromModelOrRequest(const std::string &input_file_path, LinearProgram *linear_program)
Definition: model_reader.cc:60
Collection of objects used to extend the Constraint Solver library.
bool ReadFileToProto(absl::string_view filename, google::protobuf::Message *proto)
Definition: file_util.cc:47
#define VLOG(verboselevel)
Definition: vlog.h:39