Google OR-Tools v9.11
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-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
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"
19#include "ortools/math_opt/sparse_containers.pb.h"
21
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`).
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
37 double offset = 0.0;
38};
39
40// Inline implementations.
41LinearExpressionProto 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
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_
IntegerValue size
const absl::flat_hash_map< VariableId, double > & terms() const
double get(VariableId id) const
Returns 0.0 by default if no value is set.
int64_t value
An object oriented wrapper for quadratic constraints in ModelStorage.
Definition gurobi_isv.cc:28
std::vector< K > SortedMapKeys(const absl::flat_hash_map< K, V > &in_map)
Definition sorted.h:55
static LinearExpressionData FromProto(const LinearExpressionProto &expr_proto)
LinearExpressionProto Proto() const
Inline implementations.