2021-04-01 21:00:53 +02:00
|
|
|
// Copyright 2010-2021 Google LLC
|
2017-12-08 14:52:49 +01:00
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
|
//
|
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
//
|
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
2019-04-16 09:25:34 -07:00
|
|
|
// This file contains all the top-level logic responsible for driving the search
|
|
|
|
|
// of a satisfiability integer problem. What decision we take next, which new
|
|
|
|
|
// Literal associated to an IntegerLiteral we create and when we restart.
|
|
|
|
|
//
|
|
|
|
|
// For an optimization problem, our algorithm solves a sequence of decision
|
|
|
|
|
// problem using this file as an entry point. Note that some heuristics here
|
|
|
|
|
// still use the objective if there is one in order to orient the search towards
|
|
|
|
|
// good feasible solution though.
|
|
|
|
|
|
2017-12-08 14:52:49 +01:00
|
|
|
#ifndef OR_TOOLS_SAT_INTEGER_SEARCH_H_
|
|
|
|
|
#define OR_TOOLS_SAT_INTEGER_SEARCH_H_
|
|
|
|
|
|
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
|
|
#include "ortools/sat/integer.h"
|
2019-02-26 14:42:30 +01:00
|
|
|
#include "ortools/sat/sat_base.h"
|
2017-12-08 14:52:49 +01:00
|
|
|
#include "ortools/sat/sat_solver.h"
|
|
|
|
|
|
|
|
|
|
namespace operations_research {
|
|
|
|
|
namespace sat {
|
|
|
|
|
|
2020-10-18 16:38:25 +02:00
|
|
|
// This is used to hold the next decision the solver will take. It is either
|
|
|
|
|
// a pure Boolean literal decision or correspond to an IntegerLiteral one.
|
|
|
|
|
//
|
|
|
|
|
// At most one of the two options should be set.
|
|
|
|
|
struct BooleanOrIntegerLiteral {
|
|
|
|
|
BooleanOrIntegerLiteral() {}
|
|
|
|
|
explicit BooleanOrIntegerLiteral(LiteralIndex index)
|
|
|
|
|
: boolean_literal_index(index) {}
|
|
|
|
|
explicit BooleanOrIntegerLiteral(IntegerLiteral i_lit)
|
|
|
|
|
: integer_literal(i_lit) {}
|
|
|
|
|
|
|
|
|
|
bool HasValue() const {
|
|
|
|
|
return boolean_literal_index != kNoLiteralIndex ||
|
|
|
|
|
integer_literal.var != kNoIntegerVariable;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
LiteralIndex boolean_literal_index = kNoLiteralIndex;
|
|
|
|
|
IntegerLiteral integer_literal = IntegerLiteral();
|
|
|
|
|
};
|
|
|
|
|
|
2019-04-16 09:25:34 -07:00
|
|
|
// Model struct that contains the search heuristics used to find a feasible
|
|
|
|
|
// solution to an integer problem.
|
|
|
|
|
//
|
|
|
|
|
// This is reset by ConfigureSearchHeuristics() and used by
|
|
|
|
|
// SolveIntegerProblem(), see below.
|
|
|
|
|
struct SearchHeuristics {
|
|
|
|
|
// Decision and restart heuristics. The two vectors must be of the same size
|
|
|
|
|
// and restart_policies[i] will always be used in conjunction with
|
|
|
|
|
// decision_policies[i].
|
2020-10-28 13:42:36 +01:00
|
|
|
std::vector<std::function<BooleanOrIntegerLiteral()>> decision_policies;
|
|
|
|
|
std::vector<std::function<bool()>> restart_policies;
|
2019-04-16 09:25:34 -07:00
|
|
|
|
2020-10-18 16:38:25 +02:00
|
|
|
// Index in the vectors above that indicate the current configuration.
|
2019-04-16 09:25:34 -07:00
|
|
|
int policy_index;
|
2019-07-03 13:10:22 +02:00
|
|
|
|
|
|
|
|
// Two special decision functions that are constructed at loading time.
|
|
|
|
|
// These are used by ConfigureSearchHeuristics() to fill the policies above.
|
2020-10-18 16:38:25 +02:00
|
|
|
std::function<BooleanOrIntegerLiteral()> fixed_search = nullptr;
|
|
|
|
|
std::function<BooleanOrIntegerLiteral()> hint_search = nullptr;
|
2019-04-16 09:25:34 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Given a base "fixed_search" function that should mainly control in which
|
|
|
|
|
// order integer variables are lazily instantiated (and at what value), this
|
|
|
|
|
// uses the current solver parameters to set the SearchHeuristics class in the
|
|
|
|
|
// given model.
|
2020-10-28 13:42:36 +01:00
|
|
|
void ConfigureSearchHeuristics(Model* model);
|
2019-04-16 09:25:34 -07:00
|
|
|
|
|
|
|
|
// Callbacks that will be called when the search goes back to level 0.
|
|
|
|
|
// Callbacks should return false if the propagation fails.
|
|
|
|
|
struct LevelZeroCallbackHelper {
|
2020-10-28 13:42:36 +01:00
|
|
|
std::vector<std::function<bool()>> callbacks;
|
2019-04-16 09:25:34 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Tries to find a feasible solution to the current model.
|
|
|
|
|
//
|
|
|
|
|
// This function continues from the current state of the solver and loop until
|
|
|
|
|
// all variables are instantiated (i.e. the next decision is kNoLiteralIndex) or
|
|
|
|
|
// a search limit is reached. It uses the heuristic from the SearchHeuristics
|
|
|
|
|
// class in the model to decide when to restart and what next decision to take.
|
|
|
|
|
//
|
|
|
|
|
// Each time a restart happen, this increment the policy index modulo the number
|
|
|
|
|
// of heuristics to act as a portfolio search.
|
2020-10-28 13:42:36 +01:00
|
|
|
SatSolver::Status SolveIntegerProblem(Model* model);
|
2019-04-16 09:25:34 -07:00
|
|
|
|
|
|
|
|
// Resets the solver to the given assumptions before calling
|
|
|
|
|
// SolveIntegerProblem().
|
2020-10-22 23:36:58 +02:00
|
|
|
SatSolver::Status ResetAndSolveIntegerProblem(
|
2020-10-28 13:42:36 +01:00
|
|
|
const std::vector<Literal>& assumptions, Model* model);
|
2019-04-16 09:25:34 -07:00
|
|
|
|
2019-04-18 13:29:21 +02:00
|
|
|
// Only used in tests. Move to a test utility file.
|
|
|
|
|
//
|
|
|
|
|
// This configures the model SearchHeuristics with a simple default heuristic
|
|
|
|
|
// and then call ResetAndSolveIntegerProblem() without any assumptions.
|
2020-10-28 13:42:36 +01:00
|
|
|
SatSolver::Status SolveIntegerProblemWithLazyEncoding(Model* model);
|
2019-04-16 09:25:34 -07:00
|
|
|
|
2020-10-18 16:38:25 +02:00
|
|
|
// Returns decision corresponding to var at its lower bound.
|
|
|
|
|
// Returns an invalid literal if the variable is fixed.
|
2020-10-28 13:42:36 +01:00
|
|
|
IntegerLiteral AtMinValue(IntegerVariable var, IntegerTrail* integer_trail);
|
2019-07-03 13:52:09 +02:00
|
|
|
|
2020-10-18 16:38:25 +02:00
|
|
|
// If a variable appear in the objective, branch on its best objective value.
|
2020-10-28 13:42:36 +01:00
|
|
|
IntegerLiteral ChooseBestObjectiveValue(IntegerVariable var, Model* model);
|
2019-02-26 14:42:30 +01:00
|
|
|
|
|
|
|
|
// Returns decision corresponding to var >= lb + max(1, (ub - lb) / 2). It also
|
|
|
|
|
// CHECKs that the variable is not fixed.
|
2020-10-18 16:38:25 +02:00
|
|
|
IntegerLiteral GreaterOrEqualToMiddleValue(IntegerVariable var,
|
2020-10-28 13:42:36 +01:00
|
|
|
IntegerTrail* integer_trail);
|
2019-02-26 14:42:30 +01:00
|
|
|
|
2019-02-28 15:42:50 +01:00
|
|
|
// This method first tries var <= value. If this does not reduce the domain it
|
|
|
|
|
// tries var >= value. If that also does not reduce the domain then returns
|
2020-10-18 16:38:25 +02:00
|
|
|
// an invalid literal.
|
|
|
|
|
IntegerLiteral SplitAroundGivenValue(IntegerVariable var, IntegerValue value,
|
2020-10-28 13:42:36 +01:00
|
|
|
Model* model);
|
2019-02-28 15:42:50 +01:00
|
|
|
|
|
|
|
|
// Returns decision corresponding to var <= round(lp_value). If the variable
|
2020-10-18 16:38:25 +02:00
|
|
|
// does not appear in the LP, this method returns an invalid literal.
|
2020-10-28 13:42:36 +01:00
|
|
|
IntegerLiteral SplitAroundLpValue(IntegerVariable var, Model* model);
|
2019-02-28 15:42:50 +01:00
|
|
|
|
|
|
|
|
// Returns decision corresponding to var <= best_solution[var]. If no solution
|
2020-10-18 16:38:25 +02:00
|
|
|
// has been found, this method returns a literal with kNoIntegerVariable. This
|
|
|
|
|
// was suggested in paper: "Solution-Based Phase Saving for CP" (2018) by Emir
|
|
|
|
|
// Demirovic, Geoffrey Chu, and Peter J. Stuckey.
|
|
|
|
|
IntegerLiteral SplitDomainUsingBestSolutionValue(IntegerVariable var,
|
2020-10-28 13:42:36 +01:00
|
|
|
Model* model);
|
2019-02-28 15:42:50 +01:00
|
|
|
|
2017-12-08 14:52:49 +01:00
|
|
|
// Decision heuristic for SolveIntegerProblemWithLazyEncoding(). Returns a
|
|
|
|
|
// function that will return the literal corresponding to the fact that the
|
|
|
|
|
// first currently non-fixed variable value is <= its min. The function will
|
|
|
|
|
// return kNoLiteralIndex if all the given variables are fixed.
|
|
|
|
|
//
|
|
|
|
|
// Note that this function will create the associated literal if needed.
|
2020-10-18 16:38:25 +02:00
|
|
|
std::function<BooleanOrIntegerLiteral()> FirstUnassignedVarAtItsMinHeuristic(
|
2020-10-28 13:42:36 +01:00
|
|
|
const std::vector<IntegerVariable>& vars, Model* model);
|
2017-12-08 14:52:49 +01:00
|
|
|
|
|
|
|
|
// Decision heuristic for SolveIntegerProblemWithLazyEncoding(). Like
|
|
|
|
|
// FirstUnassignedVarAtItsMinHeuristic() but the function will return the
|
|
|
|
|
// literal corresponding to the fact that the currently non-assigned variable
|
|
|
|
|
// with the lowest min has a value <= this min.
|
2020-10-18 16:38:25 +02:00
|
|
|
std::function<BooleanOrIntegerLiteral()>
|
2020-10-22 23:36:58 +02:00
|
|
|
UnassignedVarWithLowestMinAtItsMinHeuristic(
|
2020-10-28 13:42:36 +01:00
|
|
|
const std::vector<IntegerVariable>& vars, Model* model);
|
2017-12-08 14:52:49 +01:00
|
|
|
|
2018-03-22 17:47:30 +01:00
|
|
|
// Set the first unassigned Literal/Variable to its value.
|
|
|
|
|
//
|
|
|
|
|
// TODO(user): This is currently quadratic as we scan all variables to find the
|
|
|
|
|
// first unassigned one. Fix. Note that this is also the case in many other
|
|
|
|
|
// heuristics and should be fixed.
|
|
|
|
|
struct BooleanOrIntegerVariable {
|
|
|
|
|
BooleanVariable bool_var = kNoBooleanVariable;
|
|
|
|
|
IntegerVariable int_var = kNoIntegerVariable;
|
|
|
|
|
};
|
2020-10-22 23:36:58 +02:00
|
|
|
std::function<BooleanOrIntegerLiteral()> FollowHint(
|
2020-10-28 13:42:36 +01:00
|
|
|
const std::vector<BooleanOrIntegerVariable>& vars,
|
|
|
|
|
const std::vector<IntegerValue>& values, Model* model);
|
2018-03-22 17:47:30 +01:00
|
|
|
|
2017-12-08 14:52:49 +01:00
|
|
|
// Combines search heuristics in order: if the i-th one returns kNoLiteralIndex,
|
|
|
|
|
// ask the (i+1)-th. If every heuristic returned kNoLiteralIndex,
|
|
|
|
|
// returns kNoLiteralIndex.
|
2020-10-18 16:38:25 +02:00
|
|
|
std::function<BooleanOrIntegerLiteral()> SequentialSearch(
|
2020-10-28 13:42:36 +01:00
|
|
|
std::vector<std::function<BooleanOrIntegerLiteral()>> heuristics);
|
2017-12-08 14:52:49 +01:00
|
|
|
|
2019-02-28 17:07:29 +01:00
|
|
|
// Changes the value of the given decision by 'var_selection_heuristic'. We try
|
|
|
|
|
// to see if the decision is "associated" with an IntegerVariable, and if it is
|
|
|
|
|
// the case, we choose the new value by the first 'value_selection_heuristics'
|
2020-10-18 16:38:25 +02:00
|
|
|
// that is applicable. If none of the heuristics are applicable then the given
|
|
|
|
|
// decision by 'var_selection_heuristic' is returned.
|
|
|
|
|
std::function<BooleanOrIntegerLiteral()> SequentialValueSelection(
|
2020-10-28 13:42:36 +01:00
|
|
|
std::vector<std::function<IntegerLiteral(IntegerVariable)>>
|
2019-02-28 17:07:29 +01:00
|
|
|
value_selection_heuristics,
|
2020-10-18 16:38:25 +02:00
|
|
|
std::function<BooleanOrIntegerLiteral()> var_selection_heuristic,
|
2020-10-28 13:42:36 +01:00
|
|
|
Model* model);
|
2019-02-28 17:07:29 +01:00
|
|
|
|
2019-05-15 20:19:00 +02:00
|
|
|
// Changes the value of the given decision by 'var_selection_heuristic'
|
|
|
|
|
// according to various value selection heuristics. Looks at the code to know
|
|
|
|
|
// exactly what heuristic we use.
|
2020-10-18 16:38:25 +02:00
|
|
|
std::function<BooleanOrIntegerLiteral()> IntegerValueSelectionHeuristic(
|
|
|
|
|
std::function<BooleanOrIntegerLiteral()> var_selection_heuristic,
|
2020-10-28 13:42:36 +01:00
|
|
|
Model* model);
|
2019-02-28 17:07:29 +01:00
|
|
|
|
2020-10-18 16:38:25 +02:00
|
|
|
// Returns the BooleanOrIntegerLiteral advised by the underliying SAT solver.
|
2020-10-28 13:42:36 +01:00
|
|
|
std::function<BooleanOrIntegerLiteral()> SatSolverHeuristic(Model* model);
|
2017-12-08 14:52:49 +01:00
|
|
|
|
2019-01-29 09:15:39 -08:00
|
|
|
// Gets the branching variable using pseudo costs and combines it with a value
|
|
|
|
|
// for branching.
|
2020-10-28 13:42:36 +01:00
|
|
|
std::function<BooleanOrIntegerLiteral()> PseudoCost(Model* model);
|
2019-01-29 09:15:39 -08:00
|
|
|
|
2019-05-15 20:19:00 +02:00
|
|
|
// Returns true if the number of variables in the linearized part represent
|
|
|
|
|
// a large enough proportion of all the problem variables.
|
2020-10-28 13:42:36 +01:00
|
|
|
bool LinearizedPartIsLarge(Model* model);
|
2019-02-28 17:07:29 +01:00
|
|
|
|
2017-12-08 14:52:49 +01:00
|
|
|
// A restart policy that restarts every k failures.
|
2020-10-28 13:42:36 +01:00
|
|
|
std::function<bool()> RestartEveryKFailures(int k, SatSolver* solver);
|
2017-12-08 14:52:49 +01:00
|
|
|
|
|
|
|
|
// A restart policy that uses the underlying sat solver's policy.
|
2020-10-28 13:42:36 +01:00
|
|
|
std::function<bool()> SatSolverRestartPolicy(Model* model);
|
2017-12-08 14:52:49 +01:00
|
|
|
|
|
|
|
|
// Concatenates each input_heuristic with a default heuristic that instantiate
|
|
|
|
|
// all the problem's Boolean variables, into a new vector.
|
2020-10-28 13:42:36 +01:00
|
|
|
std::vector<std::function<BooleanOrIntegerLiteral()>> CompleteHeuristics(
|
|
|
|
|
const std::vector<std::function<BooleanOrIntegerLiteral()>>&
|
|
|
|
|
incomplete_heuristics,
|
|
|
|
|
const std::function<BooleanOrIntegerLiteral()>& completion_heuristic);
|
2017-12-08 14:52:49 +01:00
|
|
|
|
2020-11-19 00:17:26 +01:00
|
|
|
// Specialized search that will continuously probe Boolean variables and bounds
|
|
|
|
|
// of integer variables.
|
|
|
|
|
SatSolver::Status ContinuousProbing(
|
|
|
|
|
const std::vector<BooleanVariable>& bool_vars,
|
|
|
|
|
const std::vector<IntegerVariable>& int_vars,
|
|
|
|
|
const std::function<void()>& feasible_solution_observer, Model* model);
|
2020-10-22 23:36:58 +02:00
|
|
|
} // namespace sat
|
|
|
|
|
} // namespace operations_research
|
2017-12-08 14:52:49 +01:00
|
|
|
|
2020-10-22 23:36:58 +02:00
|
|
|
#endif // OR_TOOLS_SAT_INTEGER_SEARCH_H_
|