OR-Tools  8.1
pseudo_costs.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_SAT_PSEUDO_COSTS_H_
15 #define OR_TOOLS_SAT_PSEUDO_COSTS_H_
16 
17 #include <vector>
18 
19 #include "ortools/sat/integer.h"
20 #include "ortools/sat/util.h"
21 
22 namespace operations_research {
23 namespace sat {
24 
25 // Pseudo cost of a variable is measured as average observed change in the
26 // objective bounds per unit change in the variable bounds.
27 class PseudoCosts {
28  public:
29  // Helper struct to get information relavant for pseudo costs from branching
30  // decisions.
32  IntegerVariable var = kNoIntegerVariable;
33  IntegerValue lower_bound_change = IntegerValue(0);
34  };
35  explicit PseudoCosts(Model* model);
36 
37  // Updates the pseudo costs for the given decision.
38  void UpdateCost(const std::vector<VariableBoundChange>& bound_changes,
39  IntegerValue obj_bound_improvement);
40 
41  // Returns the variable with best reliable pseudo cost that is not fixed.
42  IntegerVariable GetBestDecisionVar();
43 
44  // Returns the pseudo cost of given variable. Currently used for testing only.
45  double GetCost(IntegerVariable var) const {
46  CHECK_LT(var, pseudo_costs_.size());
47  return pseudo_costs_[var].CurrentAverage();
48  }
49 
50  // Returns the number of recordings of given variable. Currently used for
51  // testing only.
52  int GetRecordings(IntegerVariable var) const {
53  CHECK_LT(var, pseudo_costs_.size());
54  return pseudo_costs_[var].NumRecords();
55  }
56 
57  private:
58  // Updates the cost of a given variable.
59  void UpdateCostForVar(IntegerVariable var, double new_cost);
60 
61  // Reference of integer trail to access the current bounds of variables.
62  const IntegerTrail& integer_trail_;
63 
64  const SatParameters& parameters_;
65 
67 };
68 
69 // Returns extracted information to update pseudo costs from the given
70 // branching decision.
71 std::vector<PseudoCosts::VariableBoundChange> GetBoundChanges(
72  LiteralIndex decision, Model* model);
73 
74 } // namespace sat
75 } // namespace operations_research
76 
77 #endif // OR_TOOLS_SAT_PSEUDO_COSTS_H_
operations_research::sat::PseudoCosts::GetCost
double GetCost(IntegerVariable var) const
Definition: pseudo_costs.h:45
var
IntVar * var
Definition: expr_array.cc:1858
operations_research::sat::IntegerTrail
Definition: integer.h:533
operations_research::sat::kNoIntegerVariable
const IntegerVariable kNoIntegerVariable(-1)
CHECK_LT
#define CHECK_LT(val1, val2)
Definition: base/logging.h:700
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
operations_research::sat::PseudoCosts::GetRecordings
int GetRecordings(IntegerVariable var) const
Definition: pseudo_costs.h:52
operations_research::sat::PseudoCosts::PseudoCosts
PseudoCosts(Model *model)
Definition: pseudo_costs.cc:27
operations_research::sat::GetBoundChanges
std::vector< PseudoCosts::VariableBoundChange > GetBoundChanges(LiteralIndex decision, Model *model)
Definition: pseudo_costs.cc:99
operations_research::sat::PseudoCosts::UpdateCost
void UpdateCost(const std::vector< VariableBoundChange > &bound_changes, IntegerValue obj_bound_improvement)
Definition: pseudo_costs.cc:44
operations_research::sat::PseudoCosts::GetBestDecisionVar
IntegerVariable GetBestDecisionVar()
Definition: pseudo_costs.cc:60
operations_research::sat::Model
Class that owns everything related to a particular optimization model.
Definition: sat/model.h:38
operations_research::sat::PseudoCosts::VariableBoundChange::lower_bound_change
IntegerValue lower_bound_change
Definition: pseudo_costs.h:33
model
GRBmodel * model
Definition: gurobi_interface.cc:269
absl::StrongVector
Definition: strong_vector.h:76
util.h
operations_research::sat::PseudoCosts::VariableBoundChange
Definition: pseudo_costs.h:31
operations_research::sat::PseudoCosts
Definition: pseudo_costs.h:27
operations_research::sat::PseudoCosts::VariableBoundChange::var
IntegerVariable var
Definition: pseudo_costs.h:32
integer.h