OR-Tools  8.0
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, const std::string& name);
34 
35  void SetValue(VariableIndex var, bool value) {
36  recompute_cost_ = true;
37  recompute_is_feasible_ = true;
38  values_[var] = value;
39  }
40 
41  size_t Size() const { return values_.size(); }
42  bool Value(VariableIndex var) const { return values_[var]; }
43  const std::string& name() const { return name_; }
44  void set_name(const std::string& name) { name_ = name; }
45 
46  // Returns the objective cost of the solution.
47  // Note that this code is lazy but not incremental and might run in the
48  // problem size. Use with care during search.
49  int64 GetCost() const {
50  if (recompute_cost_) {
51  cost_ = ComputeCost();
52  }
53  return cost_;
54  }
55 
56  // Returns the objective cost of the solution taking into account the problem
57  // cost scaling and offset. This is mainly useful for displaying the current
58  // problem cost, while internally, the algorithm works directly with the
59  // integer version of the cost returned by GetCost().
60  double GetScaledCost() const {
61  return sat::AddOffsetAndScaleObjectiveValue(*problem_,
62  sat::Coefficient(GetCost()));
63  }
64 
65  // Returns true iff the solution is feasible.
66  // Note that this code is lazy but not incremental and might run in the
67  // problem size. Use with care during search.
68  bool IsFeasible() const {
69  if (recompute_is_feasible_) {
70  is_feasible_ = ComputeIsFeasible();
71  }
72  return is_feasible_;
73  }
74 
75  // For range based iteration, i.e. for (const bool value : solution) {...}.
77  return values_.begin();
78  }
80  return values_.end();
81  }
82 
83  // Returns true when the cost of the argument solution is strictly greater
84  // than the cost of the object.
85  // This is used to sort solutions.
86  bool operator<(const BopSolution& solution) const {
87  return IsFeasible() == solution.IsFeasible()
88  ? GetCost() < solution.GetCost()
89  : IsFeasible() > solution.IsFeasible();
90  }
91 
92  private:
93  bool ComputeIsFeasible() const;
94  int64 ComputeCost() const;
95 
96  const sat::LinearBooleanProblem* problem_;
97  std::string name_;
99 
100  // Those are mutable because they behave as const values for a given solution
101  // but for performance reasons we want to be lazy on their computation,
102  // e.g. not compute the cost each time set_value() is called.
103  mutable bool recompute_cost_;
104  mutable bool recompute_is_feasible_;
105  mutable int64 cost_;
106  mutable bool is_feasible_;
107 
108  // Note that assign/copy are defined to allow usage of
109  // STL collections / algorithms.
110 };
111 
112 } // namespace bop
113 } // namespace operations_research
114 #endif // OR_TOOLS_BOP_BOP_SOLUTION_H_
operations_research::bop::BopSolution::end
gtl::ITIVector< VariableIndex, bool >::const_iterator end() const
Definition: bop_solution.h:79
var
IntVar * var
Definition: expr_array.cc:1858
operations_research::bop::BopSolution::SetValue
void SetValue(VariableIndex var, bool value)
Definition: bop_solution.h:35
gtl::ITIVector::begin
iterator begin()
Definition: int_type_indexed_vector.h:137
gtl::ITIVector::end
iterator end()
Definition: int_type_indexed_vector.h:139
operations_research::bop::BopSolution::set_name
void set_name(const std::string &name)
Definition: bop_solution.h:44
operations_research::bop::BopSolution::Value
bool Value(VariableIndex var) const
Definition: bop_solution.h:42
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
gtl::ITIVector::size
size_type size() const
Definition: int_type_indexed_vector.h:146
operations_research::bop::BopSolution::GetCost
int64 GetCost() const
Definition: bop_solution.h:49
operations_research::bop::BopSolution::operator<
bool operator<(const BopSolution &solution) const
Definition: bop_solution.h:86
bop_types.h
boolean_problem.pb.h
operations_research::sat::AddOffsetAndScaleObjectiveValue
double AddOffsetAndScaleObjectiveValue(const LinearBooleanProblem &problem, Coefficient v)
Definition: boolean_problem.h:39
operations_research::bop::BopSolution::name
const std::string & name() const
Definition: bop_solution.h:43
operations_research::bop::BopSolution::Size
size_t Size() const
Definition: bop_solution.h:41
operations_research::bop::BopSolution::IsFeasible
bool IsFeasible() const
Definition: bop_solution.h:68
operations_research::bop::BopSolution
Definition: bop_solution.h:31
operations_research::bop::BopSolution::GetScaledCost
double GetScaledCost() const
Definition: bop_solution.h:60
operations_research::bop::BopSolution::BopSolution
BopSolution(const sat::LinearBooleanProblem &problem, const std::string &name)
Definition: bop_solution.cc:24
gtl::ITIVector::const_iterator
ParentType::const_iterator const_iterator
Definition: int_type_indexed_vector.h:90
boolean_problem.h
gtl::ITIVector< VariableIndex, bool >
operations_research::bop::BopSolution::begin
gtl::ITIVector< VariableIndex, bool >::const_iterator begin() const
Definition: bop_solution.h:76