public List <Lit> CalcReason(Solver s, Lit p) { List <Lit> x = new List <Lit>(); for (int i = (p == Lit.LIT_UNDEF ? 0 : 1); i < this.lits.Count; i++) { //x.Add(this.lits[i]); x.Add(Lit.NegativeLit(this.lits[i])); } return(x); }
public bool Search() { while (true) { Clause confl = this.Propogate(); if (confl != null) { //this.PrintLearnts(); if (this.DecisionLevel == 0) { return(false); } int backtrack_level = 0; List <Lit> learnt_clause; this.Analyze(confl, out learnt_clause, out backtrack_level); //System.Console.WriteLine(); if (backtrack_level == 0) { int y = 0; } this.CancelUntil(Math.Max(backtrack_level, 0)); // 0 for root level? this.Record(learnt_clause); } else { if (this.NAssigns == this.NVars) { return(true); } else // Pick new variable { foreach (var x in this.assigns) { if (x.Value == Assign.UNDEF) { this.Assume(Lit.LitFromVal((x.Key * 2) + 1)); break; } } } } } return(true); }
// Page 15 private void Analyze(Clause confl, out List <Lit> out_learnt, out int btlevel) { int counter = 0; Lit p = Lit.LIT_UNDEF; List <Lit> p_reason; List <int> seen = new List <int>(); // May need to optimize this btlevel = 0; out_learnt = new List <Lit>(); out_learnt.Add(Lit.LIT_UNDEF); // Reserve space for lits[0] do { p_reason = confl.CalcReason(this, p); foreach (Lit q in p_reason) { if (!seen.Contains(q.Var)) { seen.Add(q.Var); if (this.level[q.Var] == this.DecisionLevel) { ++counter; } else if (this.level[q.Var] > 0) { //out_learnt.Add(Lit.LitFromVal(q.Val)); out_learnt.Add(Lit.NegativeLit(q)); btlevel = Math.Max(btlevel, this.level[q.Var]); } } } do { p = Lit.LitFromVal(this.trail.Peek()); confl = this.reason[p.Var]; this.UndoOne(); } while (!seen.Contains(p.Var)); --counter; } while(counter > 0); out_learnt[0] = Lit.NegativeLit(p); }
public bool Enqueue(Lit p, Clause from) { if (this.Value(p) != Assign.UNDEF) { if (this.Value(p) == Assign.FALSE) { return(false); } else { return(true); } } else // FIX: use p.Var for everything { int x = p.Var; this.assigns[x] = (p.Sign) ? Assign.FALSE : Assign.TRUE; this.reason[x] = from; this.trail.Push(p.Val); this.AssignLevel(x); this.propQ.Enqueue(p); return(true); } }
private bool Assume(Lit p) { this.trailLim.Push(this.trail.Count); return(this.Enqueue(p, null)); }