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"
19 #include "ortools/math_opt/core/indexed_model.h"
20 #include "ortools/math_opt/cpp/key_types.h"
21 #include "ortools/math_opt/cpp/variable_and_expressions.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 
64   LinearExpression AsLinearExpression() const;
65 
66  private:
67   IndexedModel* model_;
68 };
69 
70 ////////////////////////////////////////////////////////////////////////////////
71 // Inline function implementations
72 ////////////////////////////////////////////////////////////////////////////////
73 
Objective(IndexedModel * const model)74 Objective::Objective(IndexedModel* const model) : model_(model) {}
75 
model()76 IndexedModel* Objective::model() const { return model_; }
77 
set_linear_coefficient(const Variable variable,const double value)78 void Objective::set_linear_coefficient(const Variable variable,
79                                        const double value) const {
80   CHECK_EQ(variable.model(), model_) << internal::kObjectsFromOtherIndexedModel;
81   model_->set_linear_objective_coefficient(variable.typed_id(), value);
82 }
is_linear_coefficient_nonzero(const Variable variable)83 bool Objective::is_linear_coefficient_nonzero(const Variable variable) const {
84   CHECK_EQ(variable.model(), model_) << internal::kObjectsFromOtherIndexedModel;
85   return model_->is_linear_objective_coefficient_nonzero(variable.typed_id());
86 }
linear_coefficient(const Variable variable)87 double Objective::linear_coefficient(const Variable variable) const {
88   CHECK_EQ(variable.model(), model_) << internal::kObjectsFromOtherIndexedModel;
89   return model_->linear_objective_coefficient(variable.typed_id());
90 }
91 
set_offset(const double value)92 void Objective::set_offset(const double value) const {
93   model_->set_objective_offset(value);
94 }
offset()95 double Objective::offset() const { return model_->objective_offset(); }
96 
clear()97 void Objective::clear() const { model_->clear_objective(); }
is_maximize()98 bool Objective::is_maximize() const { return model_->is_maximize(); }
set_is_maximize(const bool is_maximize)99 void Objective::set_is_maximize(const bool is_maximize) const {
100   model_->set_is_maximize(is_maximize);
101 }
set_maximize()102 void Objective::set_maximize() const { model_->set_maximize(); }
set_minimize()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_
109