14#ifndef OR_TOOLS_MATH_OPT_STORAGE_ATOMIC_CONSTRAINTS_V2_H_
15#define OR_TOOLS_MATH_OPT_STORAGE_ATOMIC_CONSTRAINTS_V2_H_
19#include "absl/log/check.h"
32template <
typename Constra
intData>
36template <ElementType e>
54 elemental.
SetAttr(DoubleAttr2::kQuadConLinCoef,
AttrKey(con, var), coef);
58 elemental.
SetAttr(SymmetricDoubleAttr3::kQuadConQuadCoef, Key(con, v1, v2),
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);
75 elemental.Slice<0>(DoubleAttr2::kQuadConLinCoef,
id.value())) {
77 const double coef = elemental.GetAttr(DoubleAttr2::kQuadConLinCoef, key);
81 for (
const QuadKey key :
82 elemental.Slice<0>(SymmetricDoubleAttr3::kQuadConQuadCoef,
id.value())) {
86 elemental.GetAttr(SymmetricDoubleAttr3::kQuadConQuadCoef, key);
98 const IndicatorConstraintData& data, Elemental& elemental) {
105 for (
const auto [var, coef] : data.linear_terms.terms()) {
106 elemental.SetAttr(DoubleAttr2::kIndConLinCoef,
AttrKey(con, var), coef);
109 AttrKey(con), data.activate_on_zero);
110 if (data.indicator.has_value()) {
111 elemental.SetAttr(VariableAttr1::kIndConIndicator,
AttrKey(con),
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);
126 result.lower_bound = elemental.GetAttr(DoubleAttr1::kIndConLb, con_key);
127 result.upper_bound = elemental.GetAttr(DoubleAttr1::kIndConUb, con_key);
129 elemental.Slice<0>(DoubleAttr2::kIndConLinCoef,
id.value())) {
131 const double coef = elemental.GetAttr(DoubleAttr2::kIndConLinCoef, key);
132 result.linear_terms.set(var, coef);
134 result.activate_on_zero =
135 elemental.GetAttr(BoolAttr1::kIndConActivateOnZero, con_key);
137 elemental.GetAttr(VariableAttr1::kIndConIndicator, con_key);
139 result.indicator = ind;
A strongly typed element id. Value type.
Policy::CheckResultT SetAttr(AttrType a, AttrKeyFor< AttrType > key, ValueTypeFor< AttrType > value)
ElementId< e > AddElement(const absl::string_view name)
Creates and returns the id of a new element for the element type e.
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
AttrKey(Ints... dims) -> AttrKey< sizeof...(Ints), NoSymmetry >
CTAD for AttrKey(1,2).
ElementId< ElementType::kQuadraticConstraint > QuadraticConstraintId
ElementId< ElementType::kIndicatorConstraint > IndicatorConstraintId
SparseSymmetricMatrix quadratic_terms
SparseCoefficientMap linear_terms