Google OR-Tools
v9.12
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
linear_expression_data.h
Go to the documentation of this file.
1
// Copyright 2010-2025 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
14
#ifndef OR_TOOLS_MATH_OPT_STORAGE_LINEAR_EXPRESSION_DATA_H_
15
#define OR_TOOLS_MATH_OPT_STORAGE_LINEAR_EXPRESSION_DATA_H_
16
17
#include "absl/container/flat_hash_map.h"
18
#include "
ortools/math_opt/core/sorted.h
"
19
#include "ortools/math_opt/sparse_containers.pb.h"
20
#include "
ortools/math_opt/storage/sparse_coefficient_map.h
"
21
22
namespace
operations_research::math_opt
{
23
24
// Represents a linear expression in "raw ID" form.
25
//
26
// The data storage is not interesting, this struct exists to provide helpers
27
// that go to/from the proto representation (via member functions) and the C++
28
// model representations (via raw functions in `constraints/util/model_util.h`).
29
struct
LinearExpressionData
{
30
inline
LinearExpressionProto
Proto
()
const
;
31
// This method assumes that `expr_proto` is in a valid state; see the inline
32
// comments for `LinearExpressionProto` for details.
33
inline
static
LinearExpressionData
FromProto
(
34
const
LinearExpressionProto& expr_proto);
35
36
SparseCoefficientMap
coeffs
;
37
double
offset
= 0.0;
38
};
39
40
// Inline implementations.
41
LinearExpressionProto
LinearExpressionData::Proto
()
const
{
42
LinearExpressionProto proto_expr;
43
proto_expr.set_offset(
offset
);
44
{
45
const
int
num_terms =
static_cast<
int
>
(
coeffs
.terms().size());
46
proto_expr.mutable_ids()->Reserve(num_terms);
47
proto_expr.mutable_coefficients()->Reserve(num_terms);
48
}
49
for
(
const
VariableId
id
:
SortedMapKeys
(
coeffs
.terms())) {
50
proto_expr.add_ids(
id
.value());
51
proto_expr.add_coefficients(
coeffs
.get(
id
));
52
}
53
return
proto_expr;
54
}
55
56
LinearExpressionData
LinearExpressionData::FromProto
(
57
const
LinearExpressionProto& expr_proto) {
58
LinearExpressionData
expr_data{.offset = expr_proto.offset()};
59
for
(
int
i = 0; i < expr_proto.ids_size(); ++i) {
60
expr_data.coeffs.set(VariableId(expr_proto.ids(i)),
61
expr_proto.coefficients(i));
62
}
63
return
expr_data;
64
}
65
66
}
// namespace operations_research::math_opt
67
68
#endif
// OR_TOOLS_MATH_OPT_STORAGE_LINEAR_EXPRESSION_DATA_H_
operations_research::math_opt::SparseCoefficientMap
Definition
sparse_coefficient_map.h:33
operations_research::math_opt
An object oriented wrapper for quadratic constraints in ModelStorage.
Definition
gurobi_isv.cc:28
operations_research::math_opt::SortedMapKeys
std::vector< K > SortedMapKeys(const absl::flat_hash_map< K, V > &in_map)
Definition
sorted.h:55
sorted.h
sparse_coefficient_map.h
operations_research::math_opt::LinearExpressionData
Definition
linear_expression_data.h:29
operations_research::math_opt::LinearExpressionData::FromProto
static LinearExpressionData FromProto(const LinearExpressionProto &expr_proto)
Definition
linear_expression_data.h:56
operations_research::math_opt::LinearExpressionData::offset
double offset
Definition
linear_expression_data.h:37
operations_research::math_opt::LinearExpressionData::Proto
LinearExpressionProto Proto() const
Inline implementations.
Definition
linear_expression_data.h:41
operations_research::math_opt::LinearExpressionData::coeffs
SparseCoefficientMap coeffs
Definition
linear_expression_data.h:36
ortools
math_opt
storage
linear_expression_data.h
Generated by
1.13.2