Recommended polarity callback.

This commit is contained in:
chriseth 2022-08-29 12:14:43 +02:00
parent f132e10155
commit 690ddb54f1
5 changed files with 52 additions and 13 deletions

View File

@ -109,11 +109,11 @@ void BooleanLPSolver::declareVariable(string const& _name, SortPointer const& _s
pair<CheckResult, vector<string>> BooleanLPSolver::check(vector<Expression> const&)
{
#ifdef DEBUG
//#ifdef DEBUG
cerr << "Solving boolean constraint system" << endl;
cerr << toString() << endl;
cerr << "--------------" << endl;
#endif
//#endif
if (state().infeasible)
{
@ -156,7 +156,15 @@ pair<CheckResult, vector<string>> BooleanLPSolver::check(vector<Expression> cons
// Is it even a problem if the indices overlap?
for (auto&& [name, index]: state().variables)
if (state().isBooleanVariable.at(index) || isConditionalConstraint(index))
resizeAndSet(booleanVariables, index, name);
{
string innerName = name;
if (isConditionalConstraint(index))
{
innerName = toString(state().conditionalConstraints.at(index));
cerr << " - set name to " << innerName << endl;
}
resizeAndSet(booleanVariables, index, innerName);
}
else
lpSolver.setVariableName(index, name);
@ -224,6 +232,15 @@ pair<CheckResult, vector<string>> BooleanLPSolver::check(vector<Expression> cons
#endif
};
auto constraintIndication = [&](size_t _variable)
{
#if LPIncremental
lpSolver.recommendedPolarity(_variable);
#else
return std::nullopt;
#endif
};
auto optionalModel = CDCL{move(booleanVariables), clauses, theorySolver, backtrackNotify}.solve();
if (!optionalModel)
{

View File

@ -33,10 +33,12 @@ CDCL::CDCL(
vector<string> _variables,
vector<Clause> const& _clauses,
std::function<std::optional<Clause>(size_t, std::map<size_t, bool> const&)> _theorySolver,
std::function<void(size_t)> _backtrackNotify
std::function<void(size_t)> _backtrackNotify,
std::function<std::optional<bool>(size_t)> _constraintIndication
):
m_theorySolver(_theorySolver),
m_backtrackNotify(_backtrackNotify),
m_constraintIndication(_constraintIndication),
m_variables(move(_variables)),
m_order(VarOrderLt(m_activity))
{
@ -114,16 +116,24 @@ bool CDCL::solve_loop(const uint32_t max_conflicts, CDCL::Model& model, int& sol
{
if (auto variable = nextDecisionVariable())
{
// cout << "c Level " << currentDecisionLevel() << " - ";
// cout << ((m_assignments.size() * 100) / m_variables.size()) << "% of variables assigned." << endl;
cerr << "c Level " << currentDecisionLevel() << " - ";
cerr << ((m_assignments.size() * 100) / m_variables.size()) << "% of variables assigned." << endl;
m_decisionPoints.emplace_back(m_assignmentTrail.size());
// cout << "Deciding on " << m_variables.at(*variable) << " @" << currentDecisionLevel() << endl;
cerr << "Deciding on " << m_variables.at(*variable) << " @" << currentDecisionLevel() << endl;
// Polarity caching below
bool positive = false;
auto const& found = m_assignments_cache.find(*variable);
if (found != m_assignments_cache.end()) positive = found->second;
enqueue(Literal{positive, *variable}, nullptr);
optional<bool> guess;
if (m_constraintIndication)
guess = m_constraintIndication(*variable);
if (!guess)
{
// Polarity caching below
bool positive = false;
auto const& found = m_assignments_cache.find(*variable);
if (found != m_assignments_cache.end()) positive = found->second;
guess = positive;
}
enqueue(Literal{*guess, *variable}, nullptr);
}
else
{

View File

@ -60,7 +60,8 @@ public:
std::vector<std::string> _variables,
std::vector<Clause> const& _clauses,
std::function<std::optional<Clause>(size_t, std::map<size_t, bool> const&)> _theorySolver = {},
std::function<void(size_t)> _backtrackNotify = {}
std::function<void(size_t)> _backtrackNotify = {},
std::function<std::optional<bool>(size_t)> _constraintIndication = {}
);
std::optional<Model> solve();
@ -105,6 +106,7 @@ private:
/// or a conflict clause, i.e. a clauses that is false in the theory with the given assignments.
std::function<std::optional<Clause>(size_t, std::map<size_t, bool>)> m_theorySolver;
std::function<void(size_t)> m_backtrackNotify;
std::function<std::optional<bool>(size_t)> m_constraintIndication;
std::vector<std::string> m_variables;
/// includes the learnt clauses

View File

@ -263,7 +263,15 @@ void LPSolver::setVariableName(size_t, string)
}
#endif
optional<bool> LPSolver::recommendedPolarity(size_t _reason) const
{
if (!reasonToBounds.count(_reason))
return {};
return {};
// TODO: We cannot actually have a negative polarity for a reason / constraint!
// We can recommend not to activate it, though...
}
pair<LPResult, ReasonSet> LPSolver::check()
{

View File

@ -167,6 +167,8 @@ public:
void setVariableName(size_t _variable, std::string _name);
std::optional<bool> recommendedPolarity(size_t _reason) const;
std::pair<LPResult, ReasonSet> check();
std::string toString() const;