Google OR-Tools v9.14
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
atomic_constraints_v2.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_ATOMIC_CONSTRAINTS_V2_H_
15#define OR_TOOLS_MATH_OPT_STORAGE_ATOMIC_CONSTRAINTS_V2_H_
16
17#include <string>
18
19#include "absl/log/check.h"
29
31
32template <typename ConstraintData>
33typename ConstraintData::IdType AddAtomicConstraint(const ConstraintData& data,
34 Elemental& elemental);
35
36template <ElementType e>
37typename AtomicConstraintTraits<ElementId<e>>::ConstraintData
39
41// Quadratic constraints
43
44template <>
46 const QuadraticConstraintData& data, Elemental& elemental) {
49 elemental.SetAttr<Elemental::UBPolicy>(DoubleAttr1::kQuadConLb, AttrKey(con),
50 data.lower_bound);
51 elemental.SetAttr<Elemental::UBPolicy>(DoubleAttr1::kQuadConUb, AttrKey(con),
52 data.upper_bound);
53 for (const auto [var, coef] : data.linear_terms.terms()) {
54 elemental.SetAttr(DoubleAttr2::kQuadConLinCoef, AttrKey(con, var), coef);
55 }
56 for (const auto [v1, v2, coef] : data.quadratic_terms.Terms()) {
58 elemental.SetAttr(SymmetricDoubleAttr3::kQuadConQuadCoef, Key(con, v1, v2),
59 coef);
60 }
61 return con;
62}
63
64template <>
65inline QuadraticConstraintData GetAtomicConstraint(QuadraticConstraintId id,
66 const Elemental& elemental) {
68 const auto name = elemental.GetElementName(id);
69 CHECK_OK(name) << "quadratic constraint with id: " << id
70 << " is not in the model";
71 result.name = std::string(*name);
72 result.lower_bound = elemental.GetAttr(DoubleAttr1::kQuadConLb, AttrKey(id));
73 result.upper_bound = elemental.GetAttr(DoubleAttr1::kQuadConUb, AttrKey(id));
74 for (const AttrKey<2> key :
75 elemental.Slice<0>(DoubleAttr2::kQuadConLinCoef, id.value())) {
76 const VariableId var{key[1]};
77 const double coef = elemental.GetAttr(DoubleAttr2::kQuadConLinCoef, key);
78 result.linear_terms.set(var, coef);
79 }
81 for (const QuadKey key :
82 elemental.Slice<0>(SymmetricDoubleAttr3::kQuadConQuadCoef, id.value())) {
83 const VariableId var1{key[1]};
84 const VariableId var2{key[2]};
85 const double coef =
86 elemental.GetAttr(SymmetricDoubleAttr3::kQuadConQuadCoef, key);
87 result.quadratic_terms.set(var1, var2, coef);
88 }
89 return result;
90}
91
93// Indicator constraints
95
96template <>
98 const IndicatorConstraintData& data, Elemental& elemental) {
99 const IndicatorConstraintId con =
100 elemental.AddElement<ElementType::kIndicatorConstraint>(data.name);
101 elemental.SetAttr<Elemental::UBPolicy>(DoubleAttr1::kIndConLb, AttrKey(con),
102 data.lower_bound);
103 elemental.SetAttr<Elemental::UBPolicy>(DoubleAttr1::kIndConUb, AttrKey(con),
104 data.upper_bound);
105 for (const auto [var, coef] : data.linear_terms.terms()) {
106 elemental.SetAttr(DoubleAttr2::kIndConLinCoef, AttrKey(con, var), coef);
107 }
108 elemental.SetAttr<Elemental::UBPolicy>(BoolAttr1::kIndConActivateOnZero,
109 AttrKey(con), data.activate_on_zero);
110 if (data.indicator.has_value()) {
111 elemental.SetAttr(VariableAttr1::kIndConIndicator, AttrKey(con),
112 *data.indicator);
113 }
114 return con;
115}
116
117template <>
118inline IndicatorConstraintData GetAtomicConstraint(IndicatorConstraintId id,
119 const Elemental& elemental) {
120 IndicatorConstraintData result;
121 const auto name = elemental.GetElementName(id);
122 CHECK_OK(name) << "indicator constraint with id: " << id
123 << " is not in the model";
124 result.name = std::string(*name);
125 const AttrKey<1> con_key(id);
126 result.lower_bound = elemental.GetAttr(DoubleAttr1::kIndConLb, con_key);
127 result.upper_bound = elemental.GetAttr(DoubleAttr1::kIndConUb, con_key);
128 for (const AttrKey<2> key :
129 elemental.Slice<0>(DoubleAttr2::kIndConLinCoef, id.value())) {
130 const VariableId var{key[1]};
131 const double coef = elemental.GetAttr(DoubleAttr2::kIndConLinCoef, key);
132 result.linear_terms.set(var, coef);
133 }
134 result.activate_on_zero =
135 elemental.GetAttr(BoolAttr1::kIndConActivateOnZero, con_key);
136 const VariableId ind =
137 elemental.GetAttr(VariableAttr1::kIndConIndicator, con_key);
138 if (ind.IsValid()) {
139 result.indicator = ind;
140 }
141 return result;
142}
143
144} // namespace operations_research::math_opt::internal
145
146#endif // OR_TOOLS_MATH_OPT_STORAGE_ATOMIC_CONSTRAINTS_V2_H_
A strongly typed element id. Value type.
Definition elements.h:64
Policy::CheckResultT SetAttr(AttrType a, AttrKeyFor< AttrType > key, ValueTypeFor< AttrType > value)
Definition elemental.h:448
ElementId< e > AddElement(const absl::string_view name)
Creates and returns the id of a new element for the element type e.
Definition elemental.h:87
bool set(VariableId id, double coeff)
Returns true if the stored value changes.
const absl::flat_hash_map< VariableId, double > & terms() const
std::vector< std::pair< VariableId, double > > Terms(VariableId variable) const
bool set(VariableId first, VariableId second, double value)
AtomicConstraintTraits< ElementId< e > >::ConstraintData GetAtomicConstraint(ElementId< e > id, const Elemental &elemental)
ConstraintData::IdType AddAtomicConstraint(const ConstraintData &data, Elemental &elemental)
AttrKey< AttrTypeDescriptorT< AttrType >::kNumKeyElements, typename AttrTypeDescriptorT< AttrType >::Symmetry > AttrKeyFor
The type of the AttrKey for attribute type AttrType.
ElementId< ElementType::kVariable > VariableId
Definition elements.h:264
AttrKey(Ints... dims) -> AttrKey< sizeof...(Ints), NoSymmetry >
CTAD for AttrKey(1,2).
ElementId< ElementType::kQuadraticConstraint > QuadraticConstraintId
Definition elements.h:267
ElementId< ElementType::kIndicatorConstraint > IndicatorConstraintId
Definition elements.h:268