OR-Tools  8.1
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 
16 namespace operations_research {
17 namespace 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.
51 void 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
var
IntVar * var
Definition: expr_array.cc:1858
operations_research::glop::LinearProgram::SetVariableType
void SetVariableType(ColIndex col, VariableType type)
Definition: lp_data.cc:234
proto_utils.h
operations_research::glop::LinearProgramToMPModelProto
void LinearProgramToMPModelProto(const LinearProgram &input, MPModelProto *output)
Definition: proto_utils.cc:20
operations_research::glop::LinearProgram::SetConstraintBounds
void SetConstraintBounds(RowIndex row, Fractional lower_bound, Fractional upper_bound)
Definition: lp_data.cc:307
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::LinearProgram::SetMaximizationProblem
void SetMaximizationProblem(bool maximize)
Definition: lp_data.cc:341
operations_research::glop::MPModelProtoToLinearProgram
void MPModelProtoToLinearProgram(const MPModelProto &input, LinearProgram *output)
Definition: proto_utils.cc:51
operations_research::glop::LinearProgram::CreateNewVariable
ColIndex CreateNewVariable()
Definition: lp_data.cc:160
operations_research::glop::LinearProgram::CreateNewConstraint
RowIndex CreateNewConstraint()
Definition: lp_data.cc:189
operations_research::glop::LinearProgram::Clear
void Clear()
Definition: lp_data.cc:132
operations_research::glop::RowToColIndex
ColIndex RowToColIndex(RowIndex row)
Definition: lp_types.h:48
operations_research::glop::LinearProgram::SetVariableBounds
void SetVariableBounds(ColIndex col, Fractional lower_bound, Fractional upper_bound)
Definition: lp_data.cc:247
operations_research::glop::SparseMatrix
Definition: sparse.h:61
CHECK_EQ
#define CHECK_EQ(val1, val2)
Definition: base/logging.h:697
operations_research::glop::LinearProgram::CleanUp
void CleanUp()
Definition: lp_data.cc:345
operations_research::glop::LinearProgram::SetCoefficient
void SetCoefficient(RowIndex row, ColIndex col, Fractional value)
Definition: lp_data.cc:315
operations_research::glop::LinearProgram::SetConstraintName
void SetConstraintName(RowIndex row, absl::string_view name)
Definition: lp_data.cc:243
operations_research::glop::SparseMatrix::column
const SparseColumn & column(ColIndex col) const
Definition: sparse.h:180
operations_research::glop::SparseMatrix::PopulateFromTranspose
void PopulateFromTranspose(const Matrix &input)
Definition: sparse.cc:181
operations_research::glop::SparseVector< RowIndex, SparseColumnIterator >::Entry
typename Iterator::Entry Entry
Definition: sparse_vector.h:91
operations_research::glop::LinearProgram::SetObjectiveOffset
void SetObjectiveOffset(Fractional objective_offset)
Definition: lp_data.cc:329
operations_research::glop::LinearProgram::VariableType::INTEGER
@ INTEGER
col
ColIndex col
Definition: markowitz.cc:176
operations_research::glop::LinearProgram::SetName
void SetName(const std::string &name)
Definition: lp_data.h:74
input
static int input(yyscan_t yyscanner)
operations_research::glop::LinearProgram::SetVariableName
void SetVariableName(ColIndex col, absl::string_view name)
Definition: lp_data.cc:230
row
RowIndex row
Definition: markowitz.cc:175
operations_research::glop::LinearProgram
Definition: lp_data.h:55
operations_research::glop::LinearProgram::SetObjectiveCoefficient
void SetObjectiveCoefficient(ColIndex col, Fractional value)
Definition: lp_data.cc:324