OR-Tools  9.6
bop_solution.h
Go to the documentation of this file.
1 // Copyright 2010-2022 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 <cstdint>
18 #include <string>
19 
20 #include "absl/strings/string_view.h"
22 #include "ortools/bop/bop_types.h"
24 #include "ortools/sat/boolean_problem.pb.h"
25 
26 namespace operations_research {
27 namespace bop {
28 
29 // A Bop solution is a Boolean assignment for each variable of the problem. The
30 // cost value associated to the solution is the instantiation of the objective
31 // cost of the problem.
32 //
33 // Note that a solution might not be a feasible solution, i.e. might violate
34 // some constraints of the problem. The IsFeasible() method can be used to test
35 // the feasibility.
36 class BopSolution {
37  public:
38  BopSolution(const sat::LinearBooleanProblem& problem, absl::string_view name);
39 
40  void SetValue(VariableIndex var, bool value) {
41  recompute_cost_ = true;
42  recompute_is_feasible_ = true;
43  values_[var] = value;
44  }
45 
46  size_t Size() const { return values_.size(); }
47  bool Value(VariableIndex var) const { return values_[var]; }
48  const std::string& name() const { return name_; }
49  void set_name(absl::string_view name) { name_ = name; }
50 
51  // Returns the objective cost of the solution.
52  // Note that this code is lazy but not incremental and might run in the
53  // problem size. Use with care during search.
54  int64_t GetCost() const {
55  if (recompute_cost_) {
56  cost_ = ComputeCost();
57  }
58  return cost_;
59  }
60 
61  // Returns the objective cost of the solution taking into account the problem
62  // cost scaling and offset. This is mainly useful for displaying the current
63  // problem cost, while internally, the algorithm works directly with the
64  // integer version of the cost returned by GetCost().
65  double GetScaledCost() const {
66  return sat::AddOffsetAndScaleObjectiveValue(*problem_,
68  }
69 
70  // Returns true iff the solution is feasible.
71  // Note that this code is lazy but not incremental and might run in the
72  // problem size. Use with care during search.
73  bool IsFeasible() const {
74  if (recompute_is_feasible_) {
75  is_feasible_ = ComputeIsFeasible();
76  }
77  return is_feasible_;
78  }
79 
80  // For range based iteration, i.e. for (const bool value : solution) {...}.
82  return values_.begin();
83  }
85  return values_.end();
86  }
87 
88  // Returns true when the cost of the argument solution is strictly greater
89  // than the cost of the object.
90  // This is used to sort solutions.
91  bool operator<(const BopSolution& solution) const {
92  return IsFeasible() == solution.IsFeasible()
93  ? GetCost() < solution.GetCost()
94  : IsFeasible() > solution.IsFeasible();
95  }
96 
97  private:
98  bool ComputeIsFeasible() const;
99  int64_t ComputeCost() const;
100 
101  const sat::LinearBooleanProblem* problem_;
102  std::string name_;
104 
105  // Those are mutable because they behave as const values for a given solution
106  // but for performance reasons we want to be lazy on their computation,
107  // e.g. not compute the cost each time set_value() is called.
108  mutable bool recompute_cost_;
109  mutable bool recompute_is_feasible_;
110  mutable int64_t cost_;
111  mutable bool is_feasible_;
112 
113  // Note that assign/copy are defined to allow usage of
114  // STL collections / algorithms.
115 };
116 
117 } // namespace bop
118 } // namespace operations_research
119 #endif // OR_TOOLS_BOP_BOP_SOLUTION_H_
size_type size() const
ParentType::const_iterator const_iterator
Definition: strong_vector.h:90
absl::StrongVector< VariableIndex, bool >::const_iterator end() const
Definition: bop_solution.h:84
void SetValue(VariableIndex var, bool value)
Definition: bop_solution.h:40
bool operator<(const BopSolution &solution) const
Definition: bop_solution.h:91
BopSolution(const sat::LinearBooleanProblem &problem, absl::string_view name)
Definition: bop_solution.cc:31
bool Value(VariableIndex var) const
Definition: bop_solution.h:47
const std::string & name() const
Definition: bop_solution.h:48
absl::StrongVector< VariableIndex, bool >::const_iterator begin() const
Definition: bop_solution.h:81
void set_name(absl::string_view name)
Definition: bop_solution.h:49
int64_t value
IntVar * var
Definition: expr_array.cc:1874
std::tuple< int64_t, int64_t, const double > Coefficient
double AddOffsetAndScaleObjectiveValue(const LinearBooleanProblem &problem, Coefficient v)
Collection of objects used to extend the Constraint Solver library.