Google OR-Tools v9.11
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
bop_solution.h
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
14#ifndef OR_TOOLS_BOP_BOP_SOLUTION_H_
15#define OR_TOOLS_BOP_BOP_SOLUTION_H_
16
17#include <stddef.h>
18
19#include <cstdint>
20#include <string>
21
22#include "absl/strings/string_view.h"
26#include "ortools/sat/boolean_problem.pb.h"
28
29namespace operations_research {
30namespace bop {
31
32// A Bop solution is a Boolean assignment for each variable of the problem. The
33// cost value associated to the solution is the instantiation of the objective
34// cost of the problem.
35//
36// Note that a solution might not be a feasible solution, i.e. might violate
37// some constraints of the problem. The IsFeasible() method can be used to test
38// the feasibility.
40 public:
41 BopSolution(const sat::LinearBooleanProblem& problem, absl::string_view name);
42
43 void SetValue(VariableIndex var, bool value) {
44 recompute_cost_ = true;
45 recompute_is_feasible_ = true;
46 values_[var] = value;
47 }
48
49 size_t Size() const { return values_.size(); }
50 bool Value(VariableIndex var) const { return values_[var]; }
51 const std::string& name() const { return name_; }
52 void set_name(absl::string_view name) { name_ = name; }
53
54 // Returns the objective cost of the solution.
55 // Note that this code is lazy but not incremental and might run in the
56 // problem size. Use with care during search.
57 int64_t GetCost() const {
58 if (recompute_cost_) {
59 cost_ = ComputeCost();
60 }
61 return cost_;
62 }
63
64 // Returns the objective cost of the solution taking into account the problem
65 // cost scaling and offset. This is mainly useful for displaying the current
66 // problem cost, while internally, the algorithm works directly with the
67 // integer version of the cost returned by GetCost().
68 double GetScaledCost() const {
70 sat::Coefficient(GetCost()));
71 }
72
73 // Returns true iff the solution is feasible.
74 // Note that this code is lazy but not incremental and might run in the
75 // problem size. Use with care during search.
76 bool IsFeasible() const {
77 if (recompute_is_feasible_) {
78 is_feasible_ = ComputeIsFeasible();
79 }
80 return is_feasible_;
81 }
82
83 // For range based iteration, i.e. for (const bool value : solution) {...}.
90
91 // Returns true when the cost of the argument solution is strictly greater
92 // than the cost of the object.
93 // This is used to sort solutions.
94 bool operator<(const BopSolution& solution) const {
95 return IsFeasible() == solution.IsFeasible()
96 ? GetCost() < solution.GetCost()
97 : IsFeasible() > solution.IsFeasible();
98 }
99
100 private:
101 bool ComputeIsFeasible() const;
102 int64_t ComputeCost() const;
103
104 const sat::LinearBooleanProblem* problem_;
105 std::string name_;
107
108 // Those are mutable because they behave as const values for a given solution
109 // but for performance reasons we want to be lazy on their computation,
110 // e.g. not compute the cost each time set_value() is called.
111 mutable bool recompute_cost_;
112 mutable bool recompute_is_feasible_;
113 mutable int64_t cost_;
114 mutable bool is_feasible_;
115
116 // Note that assign/copy are defined to allow usage of
117 // STL collections / algorithms.
118};
119
120} // namespace bop
121} // namespace operations_research
122#endif // OR_TOOLS_BOP_BOP_SOLUTION_H_
bool operator<(const BopSolution &solution) const
util_intops::StrongVector< VariableIndex, bool >::const_iterator end() const
bool Value(VariableIndex var) const
const std::string & name() const
void SetValue(VariableIndex var, bool value)
void set_name(absl::string_view name)
BopSolution(const sat::LinearBooleanProblem &problem, absl::string_view name)
util_intops::StrongVector< VariableIndex, bool >::const_iterator begin() const
For range based iteration, i.e. for (const bool value : solution) {...}.
ParentType::const_iterator const_iterator
int64_t value
IntVar * var
double solution
double AddOffsetAndScaleObjectiveValue(const LinearBooleanProblem &problem, Coefficient v)
Adds the offset and returns the scaled version of the given objective value.
In SWIG mode, we don't want anything besides these top-level includes.