forked from google/or-tools
-
Notifications
You must be signed in to change notification settings - Fork 1
/
model_reader.cc
67 lines (61 loc) · 2.47 KB
/
model_reader.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
// Copyright 2010-2021 Google LLC
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "ortools/lp_data/model_reader.h"
#include "ortools/base/file.h"
#include "ortools/linear_solver/linear_solver.pb.h"
#include "ortools/lp_data/proto_utils.h"
#include "ortools/util/file_util.h"
namespace operations_research {
namespace glop {
bool LoadMPModelProtoFromModelOrRequest(const std::string& input_file_path,
MPModelProto* model) {
MPModelProto model_proto;
MPModelRequest request_proto;
ReadFileToProto(input_file_path, &model_proto);
ReadFileToProto(input_file_path, &request_proto);
// If the input proto is in binary format, both ReadFileToProto could return
// true. Instead use the actual number of variables found to test the
// correct format of the input.
const bool is_model_proto = model_proto.variable_size() > 0;
const bool is_request_proto = request_proto.model().variable_size() > 0;
if (!is_model_proto && !is_request_proto) {
LOG(ERROR) << "Failed to parse '" << input_file_path
<< "' as an MPModelProto or an MPModelRequest.";
return false;
} else {
if (is_model_proto && is_request_proto) {
LOG(ERROR) << input_file_path
<< " is parsing as both MPModelProto and MPModelRequest";
return false;
}
if (is_request_proto) {
VLOG(1) << "Read input proto as an MPModelRequest.";
model_proto.Swap(request_proto.mutable_model());
} else {
VLOG(1) << "Read input proto as an MPModelProto.";
}
}
model->Swap(&model_proto);
return true;
}
bool LoadLinearProgramFromModelOrRequest(const std::string& input_file_path,
LinearProgram* linear_program) {
MPModelProto model_proto;
if (LoadMPModelProtoFromModelOrRequest(input_file_path, &model_proto)) {
MPModelProtoToLinearProgram(model_proto, linear_program);
return true;
}
return false;
}
} // namespace glop
} // namespace operations_research