OR-Tools  8.1
model_reader.cc
Go to the documentation of this file.
1 // Copyright 2010-2018 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 "ortools/base/file.h"
19 #include "ortools/util/file_util.h"
20 
21 namespace operations_research {
22 namespace glop {
23 
24 bool LoadMPModelProtoFromModelOrRequest(const std::string& input_file_path,
25  MPModelProto* model) {
26  MPModelProto model_proto;
27  MPModelRequest request_proto;
28  ReadFileToProto(input_file_path, &model_proto);
29  ReadFileToProto(input_file_path, &request_proto);
30  // If the input proto is in binary format, both ReadFileToProto could return
31  // true. Instead use the actual number of variables found to test the
32  // correct format of the input.
33  const bool is_model_proto = model_proto.variable_size() > 0;
34  const bool is_request_proto = request_proto.model().variable_size() > 0;
35  if (!is_model_proto && !is_request_proto) {
36  LOG(ERROR) << "Failed to parse '" << input_file_path
37  << "' as an MPModelProto or an MPModelRequest.";
38  return false;
39  } else {
40  if (is_model_proto && is_request_proto) {
41  LOG(ERROR) << input_file_path
42  << " is parsing as both MPModelProto and MPModelRequest";
43  return false;
44  }
45  if (is_request_proto) {
46  VLOG(1) << "Read input proto as an MPModelRequest.";
47  model_proto.Swap(request_proto.mutable_model());
48  } else {
49  VLOG(1) << "Read input proto as an MPModelProto.";
50  }
51  }
52  model->Swap(&model_proto);
53  return true;
54 }
55 
56 bool LoadLinearProgramFromModelOrRequest(const std::string& input_file_path,
57  LinearProgram* linear_program) {
58  MPModelProto model_proto;
59  if (LoadMPModelProtoFromModelOrRequest(input_file_path, &model_proto)) {
61  return true;
62  }
63  return false;
64 }
65 
66 } // namespace glop
67 } // namespace operations_research
VLOG
#define VLOG(verboselevel)
Definition: base/logging.h:978
LOG
#define LOG(severity)
Definition: base/logging.h:420
ERROR
const int ERROR
Definition: log_severity.h:32
file_util.h
linear_solver.pb.h
proto_utils.h
model_proto
CpModelProto const * model_proto
Definition: cp_model_solver.cc:2101
operations_research
The vehicle routing library lets one model and solve generic vehicle routing problems ranging from th...
Definition: dense_doubly_linked_list.h:21
operations_research::glop::MPModelProtoToLinearProgram
void MPModelProtoToLinearProgram(const MPModelProto &input, LinearProgram *output)
Definition: proto_utils.cc:51
file.h
operations_research::glop::LoadMPModelProtoFromModelOrRequest
bool LoadMPModelProtoFromModelOrRequest(const std::string &input_file_path, MPModelProto *model)
Definition: model_reader.cc:24
operations_research::ReadFileToProto
bool ReadFileToProto(absl::string_view filename, google::protobuf::Message *proto)
Definition: file_util.cc:37
model_reader.h
operations_research::glop::LoadLinearProgramFromModelOrRequest
bool LoadLinearProgramFromModelOrRequest(const std::string &input_file_path, LinearProgram *linear_program)
Definition: model_reader.cc:56
model
GRBmodel * model
Definition: gurobi_interface.cc:269
operations_research::glop::LinearProgram
Definition: lp_data.h:55