OR-Tools  8.2
proto_utils.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
16namespace operations_research {
17namespace glop {
18
19// Converts a LinearProgram to a MPModelProto.
21 MPModelProto* output) {
22 output->Clear();
23 output->set_name(input.name());
24 output->set_maximize(input.IsMaximizationProblem());
25 output->set_objective_offset(input.objective_offset());
26 for (ColIndex col(0); col < input.num_variables(); ++col) {
27 MPVariableProto* variable = output->add_variable();
28 variable->set_lower_bound(input.variable_lower_bounds()[col]);
29 variable->set_upper_bound(input.variable_upper_bounds()[col]);
30 variable->set_name(input.GetVariableName(col));
31 variable->set_is_integer(input.IsVariableInteger(col));
32 variable->set_objective_coefficient(input.objective_coefficients()[col]);
33 }
34 // We need the matrix transpose because a LinearProgram stores the data
35 // column-wise but the MPModelProto uses a row-wise format.
36 SparseMatrix transpose;
37 transpose.PopulateFromTranspose(input.GetSparseMatrix());
38 for (RowIndex row(0); row < input.num_constraints(); ++row) {
39 MPConstraintProto* constraint = output->add_constraint();
40 constraint->set_lower_bound(input.constraint_lower_bounds()[row]);
41 constraint->set_upper_bound(input.constraint_upper_bounds()[row]);
42 constraint->set_name(input.GetConstraintName(row));
43 for (const SparseColumn::Entry e : transpose.column(RowToColIndex(row))) {
44 constraint->add_var_index(e.row().value());
45 constraint->add_coefficient(e.coefficient());
46 }
47 }
48}
49
50// Converts a MPModelProto to a LinearProgram.
51void MPModelProtoToLinearProgram(const MPModelProto& input,
52 LinearProgram* output) {
53 output->Clear();
54 output->SetName(input.name());
55 output->SetMaximizationProblem(input.maximize());
56 output->SetObjectiveOffset(input.objective_offset());
57 // TODO(user,user): clean up loops to use natural range iteration.
58 for (int i = 0; i < input.variable_size(); ++i) {
59 const MPVariableProto& var = input.variable(i);
60 const ColIndex col = output->CreateNewVariable();
61 output->SetVariableName(col, var.name());
62 output->SetVariableBounds(col, var.lower_bound(), var.upper_bound());
63 output->SetObjectiveCoefficient(col, var.objective_coefficient());
64 if (var.is_integer()) {
66 }
67 }
68 for (int j = 0; j < input.constraint_size(); ++j) {
69 const MPConstraintProto& cst = input.constraint(j);
70 const RowIndex row = output->CreateNewConstraint();
71 output->SetConstraintName(row, cst.name());
72 output->SetConstraintBounds(row, cst.lower_bound(), cst.upper_bound());
73 // TODO(user,user,user): implement strong proto validation in the
74 // linear solver server and re-use it here.
75 CHECK_EQ(cst.var_index_size(), cst.coefficient_size());
76 for (int k = 0; k < cst.var_index_size(); ++k) {
77 output->SetCoefficient(row, ColIndex(cst.var_index(k)),
78 cst.coefficient(k));
79 }
80 }
81 output->CleanUp();
82}
83
84} // namespace glop
85} // namespace operations_research
#define CHECK_EQ(val1, val2)
Definition: base/logging.h:697
void SetVariableBounds(ColIndex col, Fractional lower_bound, Fractional upper_bound)
Definition: lp_data.cc:248
void SetConstraintName(RowIndex row, absl::string_view name)
Definition: lp_data.cc:244
void SetObjectiveOffset(Fractional objective_offset)
Definition: lp_data.cc:330
void SetCoefficient(RowIndex row, ColIndex col, Fractional value)
Definition: lp_data.cc:316
void SetVariableName(ColIndex col, absl::string_view name)
Definition: lp_data.cc:231
void SetConstraintBounds(RowIndex row, Fractional lower_bound, Fractional upper_bound)
Definition: lp_data.cc:308
void SetVariableType(ColIndex col, VariableType type)
Definition: lp_data.cc:235
void SetName(const std::string &name)
Definition: lp_data.h:73
void SetObjectiveCoefficient(ColIndex col, Fractional value)
Definition: lp_data.cc:325
void SetMaximizationProblem(bool maximize)
Definition: lp_data.cc:342
void PopulateFromTranspose(const Matrix &input)
Definition: sparse.cc:181
const SparseColumn & column(ColIndex col) const
Definition: sparse.h:180
IntVar * var
Definition: expr_array.cc:1858
ColIndex col
Definition: markowitz.cc:176
RowIndex row
Definition: markowitz.cc:175
void MPModelProtoToLinearProgram(const MPModelProto &input, LinearProgram *output)
Definition: proto_utils.cc:51
void LinearProgramToMPModelProto(const LinearProgram &input, MPModelProto *output)
Definition: proto_utils.cc:20
ColIndex RowToColIndex(RowIndex row)
Definition: lp_types.h:48
The vehicle routing library lets one model and solve generic vehicle routing problems ranging from th...
static int input(yyscan_t yyscanner)