Google OR-Tools v9.11
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
pseudo_costs.h
Go to the documentation of this file.
1// Copyright 2010-2024 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 <limits>
18#include <string>
19#include <vector>
20
21#include "absl/log/check.h"
24#include "ortools/sat/integer.h"
26#include "ortools/sat/model.h"
28#include "ortools/sat/sat_parameters.pb.h"
29#include "ortools/sat/util.h"
31
32namespace operations_research {
33namespace sat {
34
35// Pseudo cost of a variable is measured as average observed change in the
36// objective bounds per unit change in the variable bounds.
38 public:
39 explicit PseudoCosts(Model* model);
40
41 // This must be called before we are about to branch.
42 // It will record the current objective bounds.
43 void BeforeTakingDecision(Literal decision);
44
45 // Updates the pseudo costs for the given decision given to
46 // BeforeTakingDecision().
47 void AfterTakingDecision(bool conflict = false);
48
49 // Advanced usage. Internal functions used by Before/AfterTakingDecision(),
50 // that are exposed for strong branching.
51 double ObjectiveIncrease(bool conflict);
52 bool SaveLpInfo();
53 void SaveBoundChanges(Literal decision, absl::Span<const double> lp_values);
54
55 // Returns the variable with best reliable pseudo cost that is not fixed.
56 IntegerVariable GetBestDecisionVar();
57
58 // Returns the pseudo cost of given variable. Currently used for testing only.
59 double GetCost(IntegerVariable var) const {
60 CHECK_LT(var, pseudo_costs_.size());
61 return pseudo_costs_[var].CurrentAverage();
62 }
63
64 // Visible for testing.
65 // Returns the number of recordings of given variable.
66 int GetNumRecords(IntegerVariable var) const {
67 CHECK_LT(var, pseudo_costs_.size());
68 return pseudo_costs_[var].NumRecords();
69 }
70
71 // Combines the score of the two branch into one score.
72 double CombineScores(double down_branch, double up_branch) const;
73
74 // Alternative pseudo-cost. This relies on the LP more heavily and is more in
75 // line with what a MIP solver would do. Returns all the info about taking a
76 // branch around the current lp_value of var.
78 bool is_fixed = false;
79 bool is_reliable = false;
80 bool is_integer = false;
81 double down_fractionality = 0.0;
82 double score = 0.0;
83 double down_score = 0.0;
84 double up_score = 0.0;
86 };
87 BranchingInfo EvaluateVar(IntegerVariable var,
88 absl::Span<const double> lp_values);
89
90 // Experimental alternative pseudo cost based on the explanation for bound
91 // increases.
92 void UpdateBoolPseudoCosts(absl::Span<const Literal> reason,
93 IntegerValue objective_increase);
94 double BoolPseudoCost(Literal lit, double lp_value) const;
95
96 // Visible for testing.
97 // Returns the bound delta associated with this decision.
99 IntegerVariable var = kNoIntegerVariable;
100 IntegerValue lower_bound_change = IntegerValue(0);
101 double lp_increase = 0.0;
102 };
103 std::vector<VariableBoundChange> GetBoundChanges(
104 Literal decision, absl::Span<const double> lp_values);
105
106 private:
107 // Returns the current objective info.
108 struct ObjectiveInfo {
109 std::string DebugString() const;
110
111 IntegerValue lb = kMinIntegerValue;
112 IntegerValue ub = kMaxIntegerValue;
113 double lp_bound = -std::numeric_limits<double>::infinity();
114 bool lp_at_optimal = false;
115 };
116 ObjectiveInfo GetCurrentObjectiveInfo();
117
118 // Model object.
119 const SatParameters& parameters_;
120 IntegerTrail* integer_trail_;
121 IntegerEncoder* encoder_;
122 ModelLpValues* lp_values_;
124 IntegerVariable objective_var_ = kNoIntegerVariable;
125
126 // Saved info by BeforeTakingDecision().
127 ObjectiveInfo saved_info_;
128 std::vector<VariableBoundChange> bound_changes_;
129
130 // Current IntegerVariable pseudo costs.
131 std::vector<IntegerVariable> relevant_variables_;
135
136 // This version is mainly based on the lp relaxation.
138 average_unit_objective_increase_;
139
140 // This version is based on objective increase explanation.
142};
143
144} // namespace sat
145} // namespace operations_research
146
147#endif // OR_TOOLS_SAT_PSEUDO_COSTS_H_
A class that stores the collection of all LP constraints in a model.
void AfterTakingDecision(bool conflict=false)
double CombineScores(double down_branch, double up_branch) const
Combines the score of the two branch into one score.
int GetNumRecords(IntegerVariable var) const
void UpdateBoolPseudoCosts(absl::Span< const Literal > reason, IntegerValue objective_increase)
void BeforeTakingDecision(Literal decision)
IntegerVariable GetBestDecisionVar()
Returns the variable with best reliable pseudo cost that is not fixed.
BranchingInfo EvaluateVar(IntegerVariable var, absl::Span< const double > lp_values)
double BoolPseudoCost(Literal lit, double lp_value) const
std::vector< VariableBoundChange > GetBoundChanges(Literal decision, absl::Span< const double > lp_values)
double ObjectiveIncrease(bool conflict)
void SaveBoundChanges(Literal decision, absl::Span< const double > lp_values)
double GetCost(IntegerVariable var) const
Returns the pseudo cost of given variable. Currently used for testing only.
IntVar * var
GRBmodel * model
int lit
constexpr IntegerValue kMaxIntegerValue(std::numeric_limits< IntegerValue::ValueType >::max() - 1)
constexpr IntegerValue kMinIntegerValue(-kMaxIntegerValue.value())
const IntegerVariable kNoIntegerVariable(-1)
In SWIG mode, we don't want anything besides these top-level includes.