OR-Tools  8.2
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"
17#include "ortools/linear_solver/linear_solver.pb.h"
20
21namespace operations_research {
22namespace glop {
23
24bool 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
56bool 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
#define LOG(severity)
Definition: base/logging.h:420
#define VLOG(verboselevel)
Definition: base/logging.h:978
CpModelProto const * model_proto
GRBmodel * model
const int ERROR
Definition: log_severity.h:32
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:24
bool LoadLinearProgramFromModelOrRequest(const std::string &input_file_path, LinearProgram *linear_program)
Definition: model_reader.cc:56
The vehicle routing library lets one model and solve generic vehicle routing problems ranging from th...
bool ReadFileToProto(absl::string_view filename, google::protobuf::Message *proto)
Definition: file_util.cc:43