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