14#ifndef OR_TOOLS_MATH_OPT_CONSTRAINTS_SOS_STORAGE_H_
15#define OR_TOOLS_MATH_OPT_CONSTRAINTS_SOS_STORAGE_H_
24#include "absl/container/flat_hash_set.h"
25#include "absl/log/check.h"
27#include "ortools/math_opt/model.pb.h"
28#include "ortools/math_opt/model_update.pb.h"
40template <
typename Constra
intId>
48 std::disjunction_v<std::is_same<ConstraintId, Sos1ConstraintId>,
49 std::is_same<ConstraintId, Sos2ConstraintId>>,
50 "ID type may only be Sos1ConstraintId or Sos2ConstraintId");
55 std::vector<double> weights, std::string
name)
56 : expressions_(
std::move(expressions)), name_(
std::move(
name)) {
57 if (!weights.empty()) {
58 CHECK_EQ(weights.size(), expressions_.size());
59 weights_ = std::move(weights);
73 AssertInbounds(
index);
74 return weights_.has_value() ? (*weights_)[
index] :
index + 1;
77 AssertInbounds(
index);
78 return expressions_[
index];
81 const std::string&
name()
const {
return name_; }
85 void AssertInbounds(
const int index)
const {
87 CHECK_LT(
index, expressions_.size());
91 std::optional<std::vector<double>> weights_;
92 std::vector<LinearExpressionData> expressions_;
117template <
typename Constra
intId>
119 const ProtoType& in_proto) {
120 const int num_expressions = in_proto.expressions_size();
122 data.name_ = in_proto.name();
123 for (
int i = 0; i < num_expressions; ++i) {
124 data.expressions_.push_back(
128 if (!in_proto.weights().empty()) {
129 data.weights_.emplace().reserve(num_expressions);
130 for (
int i = 0; i < num_expressions; ++i) {
131 data.weights_->push_back(in_proto.weights(i));
137template <
typename Constra
intId>
140 ProtoType constraint;
142 for (
int i = 0; i < num_expressions(); ++i) {
143 *constraint.add_expressions() = expression(i).Proto();
145 if (weights_.has_value()) {
146 for (
int i = 0; i < num_expressions(); ++i) {
147 constraint.add_weights(
weight(i));
153template <
typename Constra
intId>
156 absl::flat_hash_set<VariableId> vars;
158 for (
const auto [
var, _] : expression.coeffs.terms()) {
162 return std::vector<VariableId>(vars.begin(), vars.end());
165template <
typename Constra
intId>
168 expression.coeffs.erase(
var);
const LinearExpressionData & expression(const int index) const
double weight(const int index) const
static SosConstraintData FromProto(const ProtoType &in_proto)
SosConstraintProto ProtoType
std::vector< VariableId > RelatedVariables() const
void DeleteVariable(VariableId var)
const std::string & name() const
int64_t num_expressions() const
SosConstraintUpdatesProto UpdatesProtoType
SosConstraintData(std::vector< LinearExpressionData > expressions, std::vector< double > weights, std::string name)
const std::string name
A name for logging purposes.
An object oriented wrapper for quadratic constraints in ModelStorage.
internal::SosConstraintData< Sos1ConstraintId > Sos1ConstraintData
internal::SosConstraintData< Sos2ConstraintId > Sos2ConstraintData
static LinearExpressionData FromProto(const LinearExpressionProto &expr_proto)