OR-Tools  9.3
pseudo_costs.h
Go to the documentation of this file.
1// Copyright 2010-2021 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
21#include "ortools/sat/integer.h"
22#include "ortools/sat/model.h"
24#include "ortools/sat/sat_parameters.pb.h"
25#include "ortools/sat/util.h"
27
28namespace operations_research {
29namespace 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.
34 public:
35 // Helper struct to get information relavant 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 private:
64 // Updates the cost of a given variable.
65 void UpdateCostForVar(IntegerVariable var, double new_cost);
66
67 // Reference of integer trail to access the current bounds of variables.
68 const IntegerTrail& integer_trail_;
69
70 const SatParameters& parameters_;
71
73};
74
75// Returns extracted information to update pseudo costs from the given
76// branching decision.
77std::vector<PseudoCosts::VariableBoundChange> GetBoundChanges(
78 LiteralIndex decision, Model* model);
79
80} // namespace sat
81} // namespace operations_research
82
83#endif // OR_TOOLS_SAT_PSEUDO_COSTS_H_
#define CHECK_LT(val1, val2)
Definition: base/logging.h:706
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
void UpdateCost(const std::vector< VariableBoundChange > &bound_changes, IntegerValue obj_bound_improvement)
Definition: pseudo_costs.cc:49
double GetCost(IntegerVariable var) const
Definition: pseudo_costs.h:51
IntVar * var
Definition: expr_array.cc:1874
GRBmodel * model
const IntegerVariable kNoIntegerVariable(-1)
std::vector< PseudoCosts::VariableBoundChange > GetBoundChanges(LiteralIndex decision, Model *model)
Collection of objects used to extend the Constraint Solver library.