Google OR-Tools v9.11
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
proto_utils.cc
Go to the documentation of this file.
1// Copyright 2010-2024 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 "absl/log/check.h"
17#include "ortools/linear_solver/linear_solver.pb.h"
22
23namespace operations_research {
24namespace glop {
25
26// Converts a LinearProgram to a MPModelProto.
28 MPModelProto* output) {
29 output->Clear();
30 output->set_name(input.name());
31 output->set_maximize(input.IsMaximizationProblem());
32 output->set_objective_offset(input.objective_offset());
33 for (ColIndex col(0); col < input.num_variables(); ++col) {
34 MPVariableProto* variable = output->add_variable();
35 variable->set_lower_bound(input.variable_lower_bounds()[col]);
36 variable->set_upper_bound(input.variable_upper_bounds()[col]);
37 variable->set_name(input.GetVariableName(col));
38 variable->set_is_integer(input.IsVariableInteger(col));
39 variable->set_objective_coefficient(input.objective_coefficients()[col]);
40 }
41 // We need the matrix transpose because a LinearProgram stores the data
42 // column-wise but the MPModelProto uses a row-wise format.
43 SparseMatrix transpose;
44 transpose.PopulateFromTranspose(input.GetSparseMatrix());
45 for (RowIndex row(0); row < input.num_constraints(); ++row) {
46 MPConstraintProto* constraint = output->add_constraint();
47 constraint->set_lower_bound(input.constraint_lower_bounds()[row]);
48 constraint->set_upper_bound(input.constraint_upper_bounds()[row]);
49 constraint->set_name(input.GetConstraintName(row));
50 for (const SparseColumn::Entry e : transpose.column(RowToColIndex(row))) {
51 constraint->add_var_index(e.row().value());
52 constraint->add_coefficient(e.coefficient());
53 }
54 }
55}
56
57// Converts a MPModelProto to a LinearProgram.
58void MPModelProtoToLinearProgram(const MPModelProto& input,
59 LinearProgram* output) {
60 output->Clear();
61 output->SetName(input.name());
62 output->SetMaximizationProblem(input.maximize());
63 output->SetObjectiveOffset(input.objective_offset());
64 // TODO(user): clean up loops to use natural range iteration.
65 for (int i = 0; i < input.variable_size(); ++i) {
66 const MPVariableProto& var = input.variable(i);
67 const ColIndex col = output->CreateNewVariable();
68 output->SetVariableName(col, var.name());
69 output->SetVariableBounds(col, var.lower_bound(), var.upper_bound());
70 output->SetObjectiveCoefficient(col, var.objective_coefficient());
71 if (var.is_integer()) {
73 }
74 }
75 for (int j = 0; j < input.constraint_size(); ++j) {
76 const MPConstraintProto& cst = input.constraint(j);
77 const RowIndex row = output->CreateNewConstraint();
78 output->SetConstraintName(row, cst.name());
79 output->SetConstraintBounds(row, cst.lower_bound(), cst.upper_bound());
80 // TODO(user): implement strong proto validation in the
81 // linear solver server and re-use it here.
82 CHECK_EQ(cst.var_index_size(), cst.coefficient_size());
83 for (int k = 0; k < cst.var_index_size(); ++k) {
84 output->SetCoefficient(row, ColIndex(cst.var_index(k)),
85 cst.coefficient(k));
86 }
87 }
88 output->CleanUp();
89}
90
91} // namespace glop
92} // namespace operations_research
void SetName(absl::string_view name)
Name setter and getter.
Definition lp_data.h:82
void Clear()
Clears, i.e. reset the object to its initial value.
Definition lp_data.cc:143
void SetConstraintName(RowIndex row, absl::string_view name)
Definition lp_data.cc:254
@ INTEGER
The variable must only take integer values.
void SetObjectiveOffset(Fractional objective_offset)
Definition lp_data.cc:340
void SetObjectiveCoefficient(ColIndex col, Fractional value)
Definition lp_data.cc:335
void SetVariableBounds(ColIndex col, Fractional lower_bound, Fractional upper_bound)
Definition lp_data.cc:258
void SetVariableType(ColIndex col, VariableType type)
Set the type of the variable.
Definition lp_data.cc:245
void SetConstraintBounds(RowIndex row, Fractional lower_bound, Fractional upper_bound)
Definition lp_data.cc:318
void SetCoefficient(RowIndex row, ColIndex col, Fractional value)
Defines the coefficient for col / row.
Definition lp_data.cc:326
void SetVariableName(ColIndex col, absl::string_view name)
Definition lp_data.cc:241
void SetMaximizationProblem(bool maximize)
Definition lp_data.cc:352
const SparseColumn & column(ColIndex col) const
Access the underlying sparse columns.
Definition sparse.h:194
void PopulateFromTranspose(const Matrix &input)
Instantiate needed templates.
Definition sparse.cc:190
IntVar * var
ColIndex col
Definition markowitz.cc:187
RowIndex row
Definition markowitz.cc:186
void LinearProgramToMPModelProto(const LinearProgram &input, MPModelProto *output)
Converts a LinearProgram to a MPModelProto.
ColIndex RowToColIndex(RowIndex row)
Get the ColIndex corresponding to the column # row.
Definition lp_types.h:54
void MPModelProtoToLinearProgram(const MPModelProto &input, LinearProgram *output)
Converts a MPModelProto to a LinearProgram.
In SWIG mode, we don't want anything besides these top-level includes.
static int input(yyscan_t yyscanner)