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