OR-Tools  9.2
objective.h
Go to the documentation of this file.
1 // Copyright 2010-2021 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 // An object oriented wrapper for the linear objective in IndexedModel.
15 #ifndef OR_TOOLS_MATH_OPT_CPP_OBJECTIVE_H_
16 #define OR_TOOLS_MATH_OPT_CPP_OBJECTIVE_H_
17 
18 #include "ortools/base/logging.h"
22 
23 namespace operations_research {
24 namespace math_opt {
25 
26 // The objective of an optimization problem for an IndexedModel.
27 //
28 // Objective is a value type and is typically passed by copy.
29 class Objective {
30  public:
31  inline explicit Objective(IndexedModel* model);
32 
33  inline IndexedModel* model() const;
34 
35  // Setting a value to 0.0 will delete the variable from the underlying sparse
36  // representation (and has no effect if the variable is not present).
37  inline void set_linear_coefficient(Variable variable, double value) const;
38 
39  inline bool is_linear_coefficient_nonzero(Variable variable) const;
40 
41  // Returns 0.0 if this variable has no linear objective coefficient.
42  inline double linear_coefficient(Variable variable) const;
43 
44  inline void set_offset(double value) const;
45  inline double offset() const;
46 
47  // Equivalent to calling set_linear_coefficient(v, 0.0) for every variable
48  // with nonzero objective coefficient.
49  //
50  // Runs in O(#variables with nonzero objective coefficient).
51  inline void clear() const;
52  inline bool is_maximize() const;
53  inline void set_maximize() const;
54  inline void set_minimize() const;
55 
56  // Prefer set_maximize() and set_minimize() above for more readable code.
57  inline void set_is_maximize(bool is_maximize) const;
58 
59  void Maximize(const LinearExpression& objective) const;
60  void Minimize(const LinearExpression& objective) const;
61  void SetObjective(const LinearExpression& objective, bool is_maximize) const;
62  void Add(const LinearExpression& objective_terms) const;
63 
65 
66  private:
67  IndexedModel* model_;
68 };
69 
71 // Inline function implementations
73 
75 
76 IndexedModel* Objective::model() const { return model_; }
77 
79  const double value) const {
81  model_->set_linear_objective_coefficient(variable.typed_id(), value);
82 }
85  return model_->is_linear_objective_coefficient_nonzero(variable.typed_id());
86 }
87 double Objective::linear_coefficient(const Variable variable) const {
89  return model_->linear_objective_coefficient(variable.typed_id());
90 }
91 
92 void Objective::set_offset(const double value) const {
93  model_->set_objective_offset(value);
94 }
95 double Objective::offset() const { return model_->objective_offset(); }
96 
97 void Objective::clear() const { model_->clear_objective(); }
98 bool Objective::is_maximize() const { return model_->is_maximize(); }
99 void Objective::set_is_maximize(const bool is_maximize) const {
100  model_->set_is_maximize(is_maximize);
101 }
102 void Objective::set_maximize() const { model_->set_maximize(); }
103 void Objective::set_minimize() const { model_->set_minimize(); }
104 
105 } // namespace math_opt
106 } // namespace operations_research
107 
108 #endif // OR_TOOLS_MATH_OPT_CPP_OBJECTIVE_H_
constexpr absl::string_view kObjectsFromOtherIndexedModel
Definition: key_types.h:56
void Minimize(const LinearExpression &objective) const
Definition: objective.cc:29
void set_linear_objective_coefficient(VariableId variable, double value)
void Maximize(const LinearExpression &objective) const
Definition: objective.cc:25
GRBmodel * model
void SetObjective(const LinearExpression &objective, bool is_maximize) const
Definition: objective.cc:33
void set_is_maximize(bool is_maximize) const
Definition: objective.h:99
void set_offset(double value) const
Definition: objective.h:92
void Add(const LinearExpression &objective_terms) const
Definition: objective.cc:48
LinearExpression AsLinearExpression() const
Definition: objective.cc:62
void set_linear_coefficient(Variable variable, double value) const
Definition: objective.h:78
double linear_coefficient(Variable variable) const
Definition: objective.h:87
#define CHECK_EQ(val1, val2)
Definition: base/logging.h:702
bool is_linear_objective_coefficient_nonzero(VariableId variable) const
bool is_linear_coefficient_nonzero(Variable variable) const
Definition: objective.h:83
Collection of objects used to extend the Constraint Solver library.
double linear_objective_coefficient(VariableId variable) const
int64_t value