Beispiel #1
0
        protected void UnitPropagate(UnitClause unit)
        {
            List <UnitClause> newUnitClauses = GetNewUnitClausesFromPropagate(unit);

            if (newUnitClauses == null)
            {
                return;
            }
            foreach (UnitClause clause in newUnitClauses)
            {
                UnitPropagate(clause);
            }
        }
Beispiel #2
0
        protected void PrepareBranch(UnitClause unit)
        {
            unsatisfiable        = false;
            existsNonUnitClauses = false;

            UnitPropagate(unit);

            if (unsatisfiable)
            {
                return;
            }
            satisfiable = !existsNonUnitClauses && CheckPolarity();
            if (satisfiable)
            {
                return;
            }
            unsatisfiable = AllAssigned();
        }
Beispiel #3
0
        protected override void PrepareBranch(UnitClause unit)
        {
            TrailNode node = new TrailNode(unit.Index, 0);

            trail.Add(node);

            unsatisfiable        = false;
            existsNonUnitClauses = false;

            UnitPropagate(unit);

            if (unsatisfiable)
            {
                return;
            }
            satisfiable = !existsNonUnitClauses && CheckPolarity();
            if (satisfiable)
            {
                return;
            }
            unsatisfiable = AllAssigned();
        }
Beispiel #4
0
        private bool CDCL()
        {
            RunInitialUnitPropagation();
            UnitClause decision;

            while (true)
            {
                // if count > 0 -> get decision, prepare
                if (trail.Count > 0)
                {
                    TrailNode last = PopTrail;
                    PrepareBranch(new UnitClause(last.Variable));
                }
                //

                if (unsatisfiable)
                {
                    if (variables.CheckPointLevel == 0)
                    {
                        return(false);
                    }

                    // analyze conflict
                    // addclause
                    // bacjump
                    continue;
                }
                if (satisfiable)
                {
                    return(true);
                }

                // make decision
                decision = new UnitClause(heuristic.GetVariableToBranchOn());
                // false first
                decision.Value *= -1;
            }
        }
Beispiel #5
0
        protected List <UnitClause> GetNewUnitClausesFromPropagate(UnitClause unit)
        {
            if (variables[unit.Index] != 0)
            {
                return(null);
            }

            // Get clauses in which variable appears in
            var clausesToScan = variableAppearances[unit.Index];

            // Assign necessary variable value
            variables[unit.Index] = unit.Sign;
            List <UnitClause> newUnits = new List <UnitClause>();

            int redundant  = 0;
            int removedVar = 0;

            foreach (var clauseIndex in clausesToScan)
            {
                var clause = clauses.List[clauseIndex];
                if (clause == null)
                {
                    continue;
                }

                // Loop clause variables
                for (int j = 0; j < clause.Count; j++)
                {
                    if (unit.Value == clause[j] && clause.Count > 1)
                    {
                        clauses.NullifyClause(clauseIndex);
                        redundant++;
                        break;
                    }
                    if (clauses[clauseIndex, j] == -unit.Value)
                    {
                        clauses.RemoveVarAt(clauseIndex, j);
                        removedVar++;
                        j--;
                    }
                }

                clause = clauses.List[clauseIndex];
                if (clause == null)
                {
                    continue;
                }

                int newSize = clauses.GetClauseSize(clauseIndex);
                // Created new unit clause
                if (newSize == 1)
                {
                    newUnits.Add(new UnitClause(clauses[clauseIndex, 0]));
                }
                else if (newSize == 0)
                {
                    // We have propagated an empty clause
                    unsatisfiable = true;
                }
                else
                {
                    // Some clauses have more than 1 variable, uncertain satisfiability -> must search deeper
                    existsNonUnitClauses = true;
                }
            }

            // Console.WriteLine($"Ran unit propagation on {unit.Value} and redundant: {redundant}, removed vars: {removedVar}");

            return(newUnits);
        }