18#ifndef OR_TOOLS_MATH_OPT_CPP_OBJECTIVE_H_
19#define OR_TOOLS_MATH_OPT_CPP_OBJECTIVE_H_
26#include "absl/log/check.h"
27#include "absl/strings/string_view.h"
37 "[objective deleted from model]";
52 AuxiliaryObjectiveId
id);
56 inline std::optional<int64_t>
id()
const;
61 inline const ModelStorage*
storage()
const;
74 inline absl::string_view
name()
const;
77 inline double offset()
const;
110 template <
typename H>
112 friend std::ostream&
operator<<(std::ostream& ostr,
118 const ModelStorage* storage_;
147 return storage_->objective_priority(id_);
153 if (
is_primary() || storage_->has_auxiliary_objective(*id_)) {
154 return storage_->objective_name(id_);
162 return storage_->num_quadratic_objective_terms(id_);
166 return storage_->num_linear_objective_terms(id_);
170 CHECK_EQ(variable.
storage(), storage_)
172 return storage_->linear_objective_coefficient(id_, variable.typed_id());
176 const Variable second_variable)
const {
179 CHECK_EQ(second_variable.
storage(), storage_)
181 return storage_->quadratic_objective_coefficient(
186 CHECK_EQ(variable.
storage(), storage_)
188 return storage_->is_linear_objective_coefficient_nonzero(id_,
189 variable.typed_id());
193 const Variable second_variable)
const {
196 CHECK_EQ(second_variable.
storage(), storage_)
198 return storage_->is_quadratic_objective_coefficient_nonzero(
203 return lhs.id_ == rhs.id_ && lhs.storage_ == rhs.storage_;
207 return !(lhs == rhs);
212 return H::combine(std::move(h), objective.id_, objective.storage_);
215Objective::Objective(
const ModelStorage*
const storage,
const ObjectiveId id)
216 : storage_(storage), id_(id) {}
223 const AuxiliaryObjectiveId
id) {
std::string ToString() const
QuadraticExpression AsQuadraticExpression() const
Returns a representation of the objective as a QuadraticExpression.
std::optional< int64_t > id() const
friend bool operator==(const Objective &lhs, const Objective &rhs)
static Objective Primary(const ModelStorage *storage)
Returns an object that refers to the primary objective of the model.
bool is_coefficient_nonzero(Variable variable) const
Returns true if the variable has a nonzero linear coefficient in the model.
friend std::ostream & operator<<(std::ostream &ostr, const Objective &objective)
AuxiliaryObjectiveId IdType
The type used for ids.
double coefficient(Variable variable) const
Returns the linear coefficient for the variable in the model.
static Objective Auxiliary(const ModelStorage *storage, AuxiliaryObjectiveId id)
Returns an object that refers to an auxiliary objective of the model.
ObjectiveId typed_id() const
bool maximize() const
Returns true if the objective is the maximization sense.
const ModelStorage * storage() const
Returns a const-pointer to the underlying storage object for the model.
LinearExpression AsLinearExpression() const
int64_t num_linear_terms() const
Returns the number of linear terms in the objective.
int64_t priority() const
Returns the priority (lower is more important) of the objective.
friend H AbslHashValue(H h, const Objective &objective)
absl::string_view name() const
Returns the name of the objective.
friend bool operator!=(const Objective &lhs, const Objective &rhs)
int64_t num_quadratic_terms() const
Returns the number of quadratic terms in the objective.
double offset() const
Returns the constant offset of the objective.
VariableId typed_id() const
const ModelStorage * storage() const
constexpr absl::string_view kObjectsFromOtherModelStorage
An object oriented wrapper for quadratic constraints in ModelStorage.
std::ostream & operator<<(std::ostream &ostr, const IndicatorConstraint &constraint)
constexpr ObjectiveId kPrimaryObjectiveId
bool operator==(const IndicatorConstraint &lhs, const IndicatorConstraint &rhs)
bool operator!=(const IndicatorConstraint &lhs, const IndicatorConstraint &rhs)
absl::flat_hash_map< Objective, V > ObjectiveMap
constexpr absl::string_view kDeletedObjectiveDefaultDescription
std::optional< AuxiliaryObjectiveId > ObjectiveId
nullopt denotes the primary objective.
H AbslHashValue(H h, const IndicatorConstraint &constraint)