Google OR-Tools v9.11
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
objective.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// IWYU pragma: private, include "ortools/math_opt/cpp/math_opt.h"
15// IWYU pragma: friend "ortools/math_opt/cpp/.*"
16//
17// An object oriented wrapper for objectives in ModelStorage.
18#ifndef OR_TOOLS_MATH_OPT_CPP_OBJECTIVE_H_
19#define OR_TOOLS_MATH_OPT_CPP_OBJECTIVE_H_
20
21#include <cstdint>
22#include <optional>
23#include <ostream>
24#include <string>
25
26#include "absl/log/check.h"
27#include "absl/strings/string_view.h"
33
35
36constexpr absl::string_view kDeletedObjectiveDefaultDescription =
37 "[objective deleted from model]";
38
39// A value type that references an objective (either primary or auxiliary) from
40// ModelStorage. Usually this type is passed by copy.
41//
42// This type implements https://abseil.io/docs/cpp/guides/hash.
43class Objective {
44 public:
45 // The type used for ids.
46 using IdType = AuxiliaryObjectiveId;
47
48 // Returns an object that refers to the primary objective of the model.
49 inline static Objective Primary(const ModelStorage* storage);
50 // Returns an object that refers to an auxiliary objective of the model.
51 inline static Objective Auxiliary(const ModelStorage* storage,
52 AuxiliaryObjectiveId id);
53
54 // Returns the raw integer ID associated with the objective: nullopt for the
55 // primary objective, a nonnegative int64_t for an auxiliary objective.
56 inline std::optional<int64_t> id() const;
57 // Returns the strong int ID associated with the objective: nullopt for the
58 // primary objective, an AuxiliaryObjectiveId for an auxiliary objective.
59 inline ObjectiveId typed_id() const;
60 // Returns a const-pointer to the underlying storage object for the model.
61 inline const ModelStorage* storage() const;
62
63 // Returns true if the ID corresponds to the primary objective, and false if
64 // it is an auxiliary objective.
65 inline bool is_primary() const;
66
67 // Returns true if the objective is the maximization sense.
68 inline bool maximize() const;
69
70 // Returns the priority (lower is more important) of the objective.
71 inline int64_t priority() const;
72
73 // Returns the name of the objective.
74 inline absl::string_view name() const;
75
76 // Returns the constant offset of the objective.
77 inline double offset() const;
78
79 // Returns the number of linear terms in the objective.
80 inline int64_t num_linear_terms() const;
81
82 // Returns the number of quadratic terms in the objective.
83 inline int64_t num_quadratic_terms() const;
84
85 // Returns the linear coefficient for the variable in the model.
86 inline double coefficient(Variable variable) const;
87 // Returns the quadratic coefficient for the pair of variables in the model.
88 inline double coefficient(Variable first_variable,
89 Variable second_variable) const;
90
91 // Returns true if the variable has a nonzero linear coefficient in the model.
92 inline bool is_coefficient_nonzero(Variable variable) const;
93 // Returns true if the pair of variables has a nonzero quadratic coefficient
94 // in the model.
95 inline bool is_coefficient_nonzero(Variable first_variable,
96 Variable second_variable) const;
97
98 // Returns a representation of the objective as a LinearExpression.
99 // NOTE: This will CHECK fail if the objective has quadratic terms.
101 // Returns a representation of the objective as a QuadraticExpression.
103
104 // Returns a detailed string description of the contents of the objective
105 // (not its name, use `<<` for that instead).
106 std::string ToString() const;
107
108 friend inline bool operator==(const Objective& lhs, const Objective& rhs);
109 friend inline bool operator!=(const Objective& lhs, const Objective& rhs);
110 template <typename H>
111 friend H AbslHashValue(H h, const Objective& objective);
112 friend std::ostream& operator<<(std::ostream& ostr,
113 const Objective& objective);
114
115 private:
116 inline Objective(const ModelStorage* storage, ObjectiveId id);
117
118 const ModelStorage* storage_;
119 ObjectiveId id_;
120};
121
122template <typename V>
123using ObjectiveMap = absl::flat_hash_map<Objective, V>;
124
125// Streams the name of the objective, as registered upon objective creation, or
126// a short default if none was provided.
127std::ostream& operator<<(std::ostream& ostr, const Objective& objective);
128
130// Inline function implementations
132
133std::optional<int64_t> Objective::id() const {
134 if (is_primary()) {
135 return std::nullopt;
136 }
137 return id_->value();
138}
139
140ObjectiveId Objective::typed_id() const { return id_; }
141
142const ModelStorage* Objective::storage() const { return storage_; }
143
144bool Objective::is_primary() const { return id_ == kPrimaryObjectiveId; }
145
146int64_t Objective::priority() const {
147 return storage_->objective_priority(id_);
149
150bool Objective::maximize() const { return storage_->is_maximize(id_); }
151
152absl::string_view Objective::name() const {
153 if (is_primary() || storage_->has_auxiliary_objective(*id_)) {
154 return storage_->objective_name(id_);
155 }
157}
158
159double Objective::offset() const { return storage_->objective_offset(id_); }
160
162 return storage_->num_quadratic_objective_terms(id_);
164
165int64_t Objective::num_linear_terms() const {
166 return storage_->num_linear_objective_terms(id_);
168
169double Objective::coefficient(const Variable variable) const {
170 CHECK_EQ(variable.storage(), storage_)
172 return storage_->linear_objective_coefficient(id_, variable.typed_id());
173}
174
175double Objective::coefficient(const Variable first_variable,
176 const Variable second_variable) const {
177 CHECK_EQ(first_variable.storage(), storage_)
179 CHECK_EQ(second_variable.storage(), storage_)
181 return storage_->quadratic_objective_coefficient(
182 id_, first_variable.typed_id(), second_variable.typed_id());
183}
184
185bool Objective::is_coefficient_nonzero(const Variable variable) const {
186 CHECK_EQ(variable.storage(), storage_)
188 return storage_->is_linear_objective_coefficient_nonzero(id_,
189 variable.typed_id());
190}
191
192bool Objective::is_coefficient_nonzero(const Variable first_variable,
193 const Variable second_variable) const {
194 CHECK_EQ(first_variable.storage(), storage_)
196 CHECK_EQ(second_variable.storage(), storage_)
198 return storage_->is_quadratic_objective_coefficient_nonzero(
199 id_, first_variable.typed_id(), second_variable.typed_id());
200}
201
202bool operator==(const Objective& lhs, const Objective& rhs) {
203 return lhs.id_ == rhs.id_ && lhs.storage_ == rhs.storage_;
205
206bool operator!=(const Objective& lhs, const Objective& rhs) {
207 return !(lhs == rhs);
209
210template <typename H>
211H AbslHashValue(H h, const Objective& objective) {
212 return H::combine(std::move(h), objective.id_, objective.storage_);
214
215Objective::Objective(const ModelStorage* const storage, const ObjectiveId id)
216 : storage_(storage), id_(id) {}
217
218Objective Objective::Primary(const ModelStorage* const storage) {
219 return Objective(storage, kPrimaryObjectiveId);
221
222Objective Objective::Auxiliary(const ModelStorage* const storage,
223 const AuxiliaryObjectiveId id) {
224 return Objective(storage, id);
225}
226
227} // namespace operations_research::math_opt
228
229#endif // OR_TOOLS_MATH_OPT_CPP_OBJECTIVE_H_
QuadraticExpression AsQuadraticExpression() const
Returns a representation of the objective as a QuadraticExpression.
Definition objective.cc:40
std::optional< int64_t > id() const
Definition objective.h:135
friend bool operator==(const Objective &lhs, const Objective &rhs)
Definition objective.h:204
static Objective Primary(const ModelStorage *storage)
Returns an object that refers to the primary objective of the model.
Definition objective.h:220
bool is_coefficient_nonzero(Variable variable) const
Returns true if the variable has a nonzero linear coefficient in the model.
Definition objective.h:187
friend std::ostream & operator<<(std::ostream &ostr, const Objective &objective)
Definition objective.cc:61
AuxiliaryObjectiveId IdType
The type used for ids.
Definition objective.h:46
double coefficient(Variable variable) const
Returns the linear coefficient for the variable in the model.
Definition objective.h:171
static Objective Auxiliary(const ModelStorage *storage, AuxiliaryObjectiveId id)
Returns an object that refers to an auxiliary objective of the model.
Definition objective.h:224
bool maximize() const
Returns true if the objective is the maximization sense.
Definition objective.h:152
const ModelStorage * storage() const
Returns a const-pointer to the underlying storage object for the model.
Definition objective.h:144
LinearExpression AsLinearExpression() const
Definition objective.cc:29
int64_t num_linear_terms() const
Returns the number of linear terms in the objective.
Definition objective.h:167
int64_t priority() const
Returns the priority (lower is more important) of the objective.
Definition objective.h:148
friend H AbslHashValue(H h, const Objective &objective)
Definition objective.h:213
absl::string_view name() const
Returns the name of the objective.
Definition objective.h:154
friend bool operator!=(const Objective &lhs, const Objective &rhs)
Definition objective.h:208
int64_t num_quadratic_terms() const
Returns the number of quadratic terms in the objective.
Definition objective.h:163
double offset() const
Returns the constant offset of the objective.
Definition objective.h:161
constexpr absl::string_view kObjectsFromOtherModelStorage
Definition key_types.h:156
An object oriented wrapper for quadratic constraints in ModelStorage.
Definition gurobi_isv.cc:28
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
Definition objective.h:123
constexpr absl::string_view kDeletedObjectiveDefaultDescription
Definition objective.h:36
std::optional< AuxiliaryObjectiveId > ObjectiveId
nullopt denotes the primary objective.
H AbslHashValue(H h, const IndicatorConstraint &constraint)
int64_t coefficient