/****************************************************************************************[Solver.C]
MiniSat -- Copyright (c) 2003-2006, Niklas Een, Niklas Sorensson
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
associated documentation files (the "Software"), to deal in the Software without restriction,
including without limitation the rights to use, copy, modify, merge, publish, distribute,
sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or
substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT
OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
**************************************************************************************************/
#include "Solver.h"
#include "Sort.h"
#include <cmath>
//=================================================================================================
// Constructor/Destructor:
Solver::Solver() :
// Parameters: (formerly in 'SearchParams')
var_decay(1 / 0.95), clause_decay(1 / 0.999), random_var_freq(0.02)
, restart_first(100), restart_inc(1.5), learntsize_factor((double)1/(double)3), learntsize_inc(1.1)
// More parameters:
//
, expensive_ccmin (true)
, polarity_mode (polarity_false)
, verbosity (0)
// Statistics: (formerly in 'SolverStats')
//
, starts(0), decisions(0), rnd_decisions(0), propagations(0), conflicts(0)
, clauses_literals(0), learnts_literals(0), max_literals(0), tot_literals(0)
, ok (true)
, cla_inc (1)
, var_inc (1)
, qhead (0)
, simpDB_assigns (-1)
, simpDB_props (0)
, order_heap (VarOrderLt(activity))
, random_seed (91648253)
, progress_estimate(0)
, remove_satisfied (true)
{}
Solver::~Solver()
{
for (int i = 0; i < learnts.size(); i++) free(learnts[i]);
for (int i = 0; i < clauses.size(); i++) free(clauses[i]);
}
//=================================================================================================
// Minor methods:
// Creates a new SAT variable in the solver. If 'decision_var' is cleared, variable will not be
// used as a decision variable (NOTE! This has effects on the meaning of a SATISFIABLE result).
//
Var Solver::newVar(bool sign, bool dvar)
{
int v = nVars();
watches .push(); // (list for positive literal)
watches .push(); // (list for negative literal)
reason .push(NULL);
assigns .push(toInt(l_Undef));
level .push(-1);
activity .push(0);
seen .push(0);
polarity .push((char)sign);
decision_var.push((char)dvar);
insertVarOrder(v);
return v;
}
bool Solver::addClause(vec<Lit>& ps)
{
assert(decisionLevel() == 0);
if (!ok)
return false;
else{
// Check if clause is satisfied and remove false/duplicate literals:
sort(ps);
Lit p; int i, j;
for (i = j = 0, p = lit_Undef; i < ps.size(); i++)
if (value(ps[i]) == l_True || ps[i] == ~p)
return true;
else if (value(ps[i]) != l_False && ps[i] != p)
ps[j++] = p = ps[i];
ps.shrink(i - j);
}
if (ps.size() == 0)
return ok = false;
else if (ps.size() == 1){
assert(value(ps[0]) == l_Undef);
uncheckedEnqueue(ps[0]);
return ok = (propagate() == NULL);
}else{
Clause* c = Clause_new(ps, false);
clauses.push(c);
attachClause(*c);
}
return true;
}
void Solver::attachClause(Clause& c) {
assert(c.size() > 1);
watches[toInt(~c[0])].push(&c);
watches[toInt(~c[1])].push(&c);
if (c.learnt()) learnts_literals += c.size();
else clauses_literals += c.size(); }
void Solver::detachClause(Clause& c) {
assert(c.size() > 1);
assert(find(watches[toInt(~c[0])], &c));
assert(find(watches[toInt(~c[1])], &c));
remove(watches[toInt(~c[0])], &c);
remove(watches[toInt(~c[1])], &c);
if (c.learnt()) learnts_literals -= c.size();
else clauses_literals -= c.size(); }
void Solver::removeClause(Clause& c) {
detachClause(c);
free(&c); }
bool Solver::satisfied(const Clause& c) const {
for (int i = 0; i < c.size(); i++)
if (value(c[i]) == l_True)
return true;
return false; }
// Revert to the state at given level (keeping all assignment at 'level' but not beyond).
//
void Solver::cancelUntil(int level) {
if (decisionLevel() > level){
for (int c = trail.size()-1; c >= trail_lim[level]; c--){
Var x = var(trail[c]);
assigns[x] = toInt(l_Undef);
insertVarOrder(x); }
qhead = trail_lim[level];
trail.shrink(trail.size() - trail_lim[level]);
trail_lim.shrink(trail_lim.size() - level);
} }
//=================================================================================================
// Major methods:
Lit Solver::pickBranchLit(int polarity_mode, double random_var_freq)
{
Var next = var_Undef;
// Random decision:
if (drand(random_seed) < random_var_freq && !order_heap.empty()){
next = order_heap[irand(random_seed,order_heap.size())];
if (toLbool(assigns[next]) == l_Undef && decision_var[next])
rnd_decisions++; }
// Activity based decision:
while (next == var_Undef || toLbool(assigns[next]) != l_Undef || !decision_var[next])
if (order_heap.empty()){
next = var_Undef;
break;
}else
next = order_heap.removeMin();
bool sign = false;
switch (polarity_mode){
case polarity_true: sign = false; break;
case polarity_false: sign = true; break;
case polarity_user: sign = polarity[next]; break;
case polarity_rnd: sign = irand(random_seed, 2); break;
default: assert(false); }
return next == var_Undef ? lit_Undef : Lit(next, sign);
}
/*_________________________________________________________________________________________________
|
| analyze : (confl : Clause*) (out_learnt : vec<Lit>&) (out_btlevel : int&) -> [void]
|
| Description:
| Analyze conflict and produce a reason clause.
|
| Pre-conditions:
| * 'out_learnt' is assumed to be cleared.
| * Current decision level must be greater than root level.
|
| Post-conditions:
| * 'out_learnt[0]' is the asserting literal at level 'out_btlevel'.
|
| Effect:
| Will undo part of the trail, upto but not beyond the assumption of the current decision level.
|________________________________________________________________________________________________@*/
void Solver::analyze(Clause* confl, vec<Lit>& out_learnt, int& out_btlevel)
{
int pathC = 0;
Lit p = lit_Undef;
// Generate conflict clause:
//
out_learnt.push(); // (leave room for the asserting literal)
int index = trail.size() - 1;
out_btlevel = 0;
do{
assert(confl != NULL); // (otherwise should be UIP)
Clause& c = *confl;
if (c.learnt())
claBumpActivity(c);
for (int j = (p == lit_Undef) ? 0 : 1; j < c.size(); j++){
Lit q = c[j];
if (!seen[var(q)] && level[var(q)] > 0){
varBumpActivity(var(q));
seen[var(q)] = 1;
if (level[var(q)] >= decisionLevel())
pathC++;
els