private void UndoOne() { Lit l = Lit.LitFromVal(this.trail.Peek()); int x = l.Var; this.assigns[x] = Assign.UNDEF; this.reason[x] = null; this.level[x] = -1; this.trail.Pop(); }
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); }