Google OR-Tools v9.11
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
objective.cc
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
15
16#include <optional>
17#include <ostream>
18#include <sstream>
19#include <string>
20
21#include "absl/log/check.h"
22#include "absl/strings/string_view.h"
26
28
30 CHECK_EQ(storage()->num_quadratic_objective_terms(id_), 0)
31 << "The objective function contains quadratic terms and cannot be "
32 "represented as a LinearExpression";
33 LinearExpression objective = offset();
34 for (const auto [raw_var_id, coeff] : storage_->linear_objective(id_)) {
35 objective += coeff * Variable(storage_, raw_var_id);
36 }
37 return objective;
38}
39
41 QuadraticExpression result = offset();
42 for (const auto& [v, coef] : storage_->linear_objective(id_)) {
43 result += coef * Variable(storage(), v);
44 }
45 for (const auto& [v1, v2, coef] : storage_->quadratic_objective_terms(id_)) {
46 result +=
48 }
49 return result;
50}
51
52std::string Objective::ToString() const {
53 if (!is_primary() && !storage()->has_auxiliary_objective(*id_)) {
54 return std::string(kDeletedObjectiveDefaultDescription);
55 }
56 std::stringstream str;
57 str << AsQuadraticExpression();
58 return str.str();
59}
60
61std::ostream& operator<<(std::ostream& ostr, const Objective& objective) {
62 // TODO(b/170992529): handle quoting of invalid characters in the name.
63 const absl::string_view name = objective.name();
64 if (name.empty()) {
65 if (objective.is_primary()) {
66 ostr << "__primary_obj__";
67 } else {
68 ostr << "__aux_obj#" << *objective.id() << "__";
69 }
70 } else {
71 ostr << name;
72 }
73 return ostr;
74}
75
76} // namespace operations_research::math_opt
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
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
absl::string_view name() const
Returns the name of the objective.
Definition objective.h:154
double offset() const
Returns the constant offset of the objective.
Definition objective.h:161
const std::string name
A name for logging purposes.
int64_t coef
An object oriented wrapper for quadratic constraints in ModelStorage.
Definition gurobi_isv.cc:28
std::ostream & operator<<(std::ostream &ostr, const IndicatorConstraint &constraint)
constexpr absl::string_view kDeletedObjectiveDefaultDescription
Definition objective.h:36