OR-Tools  9.1
objective.cc
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
15
17#include "absl/container/flat_hash_map.h"
21
22namespace operations_research {
23namespace math_opt {
24
25void Objective::Maximize(const LinearExpression& objective) const {
26 SetObjective(objective, true);
27}
28
29void Objective::Minimize(const LinearExpression& objective) const {
30 SetObjective(objective, false);
31}
32
34 bool is_maximize) const {
35 // LinearExpression that have no terms have a null model().
36 if (!objective.raw_terms().empty()) {
37 CHECK_EQ(objective.model(), model_)
39 }
40 model_->clear_objective();
42 model_->set_objective_offset(objective.offset());
43 for (auto [var, coef] : objective.raw_terms()) {
45 }
46}
47
48void Objective::Add(const LinearExpression& objective_terms) const {
49 // LinearExpression that have no terms have a null model().
50 if (!objective_terms.raw_terms().empty()) {
51 CHECK_EQ(objective_terms.model(), model_)
53 }
54 model_->set_objective_offset(objective_terms.offset() +
55 model_->objective_offset());
56 for (auto [var, coef] : objective_terms.raw_terms()) {
59 }
60}
61
63 LinearExpression result = model_->objective_offset();
64 for (const auto& [v, coef] : model_->linear_objective()) {
65 result += Variable(model_, v) * coef;
66 }
67 return result;
68}
69
70} // namespace math_opt
71} // namespace operations_research
#define CHECK_EQ(val1, val2)
Definition: base/logging.h:698
double linear_objective_coefficient(VariableId variable) const
void set_linear_objective_coefficient(VariableId variable, double value)
const absl::flat_hash_map< VariableId, double > & linear_objective() const
const absl::flat_hash_map< VariableId, double > & raw_terms() const
void Add(const LinearExpression &objective_terms) const
Definition: objective.cc:48
LinearExpression AsLinearExpression() const
Definition: objective.cc:62
void Maximize(const LinearExpression &objective) const
Definition: objective.cc:25
void Minimize(const LinearExpression &objective) const
Definition: objective.cc:29
void SetObjective(const LinearExpression &objective, bool is_maximize) const
Definition: objective.cc:33
IntVar * var
Definition: expr_array.cc:1874
int64_t coef
Definition: expr_array.cc:1875
constexpr absl::string_view kObjectsFromOtherIndexedModel
Definition: key_types.h:56
Collection of objects used to extend the Constraint Solver library.