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