Google OR-Tools v9.14
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-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// 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 final : public ModelStorageItem {
44 public:
45 // The type used for ids.
47
48 // Returns an object that refers to the primary objective of the model.
49 inline static Objective Primary(ModelStorageCPtr storage);
50 // Returns an object that refers to an auxiliary objective of the model.
51 inline static Objective Auxiliary(ModelStorageCPtr storage,
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
61 // Returns true if the ID corresponds to the primary objective, and false if
62 // it is an auxiliary objective.
63 inline bool is_primary() const;
64
65 // Returns true if the objective is the maximization sense.
66 inline bool maximize() const;
67
68 // Returns the priority (lower is more important) of the objective.
69 inline int64_t priority() const;
70
71 // Returns the name of the objective.
72 inline absl::string_view name() const;
73
74 // Returns the constant offset of the objective.
75 inline double offset() const;
76
77 // Returns the number of linear terms in the objective.
78 inline int64_t num_linear_terms() const;
79
80 // Returns the number of quadratic terms in the objective.
81 inline int64_t num_quadratic_terms() const;
82
83 // Returns the linear coefficient for the variable in the model.
84 inline double coefficient(Variable variable) const;
85 // Returns the quadratic coefficient for the pair of variables in the model.
86 inline double coefficient(Variable first_variable,
87 Variable second_variable) const;
88
89 // Returns true if the variable has a nonzero linear coefficient in the model.
90 inline bool is_coefficient_nonzero(Variable variable) const;
91 // Returns true if the pair of variables has a nonzero quadratic coefficient
92 // in the model.
93 inline bool is_coefficient_nonzero(Variable first_variable,
94 Variable second_variable) const;
95
96 // Returns a representation of the objective as a LinearExpression.
97 // NOTE: This will CHECK fail if the objective has quadratic terms.
99 // Returns a representation of the objective as a QuadraticExpression.
101
102 // Returns a detailed string description of the contents of the objective
103 // (not its name, use `<<` for that instead).
104 std::string ToString() const;
105
106 friend inline bool operator==(const Objective& lhs, const Objective& rhs);
107 friend inline bool operator!=(const Objective& lhs, const Objective& rhs);
108 template <typename H>
109 friend H AbslHashValue(H h, const Objective& objective);
110 friend std::ostream& operator<<(std::ostream& ostr,
111 const Objective& objective);
112
113 private:
114 inline Objective(ModelStorageCPtr storage, ObjectiveId id);
115
116 ObjectiveId id_;
117};
118
119template <typename V>
120using ObjectiveMap = absl::flat_hash_map<Objective, V>;
121
122// Streams the name of the objective, as registered upon objective creation, or
123// a short default if none was provided.
124std::ostream& operator<<(std::ostream& ostr, const Objective& objective);
125
127// Inline function implementations
129
130std::optional<int64_t> Objective::id() const {
131 if (is_primary()) {
132 return std::nullopt;
133 }
134 return id_->value();
135}
136
137ObjectiveId Objective::typed_id() const { return id_; }
138
139bool Objective::is_primary() const { return id_ == kPrimaryObjectiveId; }
140
141int64_t Objective::priority() const {
142 return storage()->objective_priority(id_);
144
145bool Objective::maximize() const { return storage()->is_maximize(id_); }
146
147absl::string_view Objective::name() const {
148 if (is_primary() || storage()->has_auxiliary_objective(*id_)) {
149 return storage()->objective_name(id_);
150 }
152}
153
154double Objective::offset() const { return storage()->objective_offset(id_); }
155
157 return storage()->num_quadratic_objective_terms(id_);
159
160int64_t Objective::num_linear_terms() const {
161 return storage()->num_linear_objective_terms(id_);
163
164double Objective::coefficient(const Variable variable) const {
165 CHECK_EQ(variable.storage(), storage())
167 return storage()->linear_objective_coefficient(id_, variable.typed_id());
168}
169
170double Objective::coefficient(const Variable first_variable,
171 const Variable second_variable) const {
172 CHECK_EQ(first_variable.storage(), storage())
174 CHECK_EQ(second_variable.storage(), storage())
176 return storage()->quadratic_objective_coefficient(
177 id_, first_variable.typed_id(), second_variable.typed_id());
178}
179
180bool Objective::is_coefficient_nonzero(const Variable variable) const {
181 CHECK_EQ(variable.storage(), storage())
183 return storage()->is_linear_objective_coefficient_nonzero(
184 id_, variable.typed_id());
185}
186
187bool Objective::is_coefficient_nonzero(const Variable first_variable,
188 const Variable second_variable) const {
189 CHECK_EQ(first_variable.storage(), storage())
191 CHECK_EQ(second_variable.storage(), storage())
193 return storage()->is_quadratic_objective_coefficient_nonzero(
194 id_, first_variable.typed_id(), second_variable.typed_id());
195}
196
197bool operator==(const Objective& lhs, const Objective& rhs) {
198 return lhs.id_ == rhs.id_ && lhs.storage() == rhs.storage();
200
201bool operator!=(const Objective& lhs, const Objective& rhs) {
202 return !(lhs == rhs);
204
205template <typename H>
206H AbslHashValue(H h, const Objective& objective) {
207 return H::combine(std::move(h), objective.id_, objective.storage());
209
210Objective::Objective(const ModelStorageCPtr storage, const ObjectiveId id)
211 : ModelStorageItem(storage), id_(id) {}
212
213Objective Objective::Primary(const ModelStorageCPtr storage) {
214 return Objective(storage, kPrimaryObjectiveId);
218 const AuxiliaryObjectiveId id) {
219 return Objective(storage, id);
220}
221
222} // namespace operations_research::math_opt
223
224#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:132
friend bool operator==(const Objective &lhs, const Objective &rhs)
Definition objective.h:199
bool is_coefficient_nonzero(Variable variable) const
Returns true if the variable has a nonzero linear coefficient in the model.
Definition objective.h:182
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:166
bool maximize() const
Returns true if the objective is the maximization sense.
Definition objective.h:147
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:162
static Objective Primary(ModelStorageCPtr storage)
Returns an object that refers to the primary objective of the model.
Definition objective.h:215
int64_t priority() const
Returns the priority (lower is more important) of the objective.
Definition objective.h:143
friend H AbslHashValue(H h, const Objective &objective)
Definition objective.h:208
absl::string_view name() const
Returns the name of the objective.
Definition objective.h:149
friend bool operator!=(const Objective &lhs, const Objective &rhs)
Definition objective.h:203
int64_t num_quadratic_terms() const
Returns the number of quadratic terms in the objective.
Definition objective.h:158
double offset() const
Returns the constant offset of the objective.
Definition objective.h:156
static Objective Auxiliary(ModelStorageCPtr storage, AuxiliaryObjectiveId id)
Returns an object that refers to an auxiliary objective of the model.
Definition objective.h:219
constexpr absl::string_view kObjectsFromOtherModelStorage
Definition key_types.h:157
An object oriented wrapper for quadratic constraints in ModelStorage.
Definition gurobi_isv.cc:28
absl::Nonnull< const ModelStorage * > ModelStorageCPtr
ElementId< ElementType::kAuxiliaryObjective > AuxiliaryObjectiveId
Definition elements.h:266
constexpr ObjectiveId kPrimaryObjectiveId
bool operator==(const SecondOrderConeConstraint &lhs, const SecondOrderConeConstraint &rhs)
bool operator!=(const SecondOrderConeConstraint &lhs, const SecondOrderConeConstraint &rhs)
absl::flat_hash_map< Objective, V > ObjectiveMap
Definition objective.h:120
std::ostream & operator<<(std::ostream &ostr, const SecondOrderConeConstraint &constraint)
H AbslHashValue(H h, const SecondOrderConeConstraint &constraint)
constexpr absl::string_view kDeletedObjectiveDefaultDescription
Definition objective.h:36
std::optional< AuxiliaryObjectiveId > ObjectiveId
nullopt denotes the primary objective.