Пример #1
0
        private void InitialUnitPropagation(out Conflict conflict)
        {
            conflict = null;
            foreach (var clause in Clauses)
            {
                if (clause.Count == 1)
                {
                    Literal literal = clause[0];

                    if (!literals[literal.index].HasValue)
                    {
                        AssignLiteral(literal);
                        var node = new TrailNode(level, literal, clause);
                        trail.Add(node);
                        UnitPropagation(node, out conflict);
                        if (conflict != null)
                        {
                            return;
                        }
                    }
                }
            }
        }
Пример #2
0
        public override SolverResult Run()
        {
            if (Running)
            {
                throw new InvalidOperationException("Solver is already running");
            }
            Running    = true;
            totalwatch = Stopwatch.StartNew();

            InitialUnitPropagation(out Conflict conflict);

            if (conflict != null)
            {
                Running = false;
                return(SolverResult.Fail(0));
            }

            if (AllAssigned)
            {
                Running = false;
                return(SolverResult.Success(0, literals));
            }

            if (debug)
            {
                Console.WriteLine();
            }
            while (true)
            {
                if (debug && latestConflict != null)
                {
                    Console.WriteLine("- Conflict");
                    Console.WriteLine(trail.DebugMessage);
                }

                iterations++;
                TrailNode node = Travel();
                UnitPropagation(node, out conflict);

                if (debug)
                {
                    StateDebug();
                }

                if (conflict == null && AllAssigned)
                {
                    Running = false;
                    return(SolverResult.Success(iterations, literals));
                }

                if (conflict != null && level == 0)
                {
                    Running = false;
                    return(SolverResult.Fail(iterations));
                }

                if (conflict != null)
                {
                    latestConflict = conflict;
                }

                if (timeout != 0 && totalwatch.ElapsedMilliseconds > timeout)
                {
                    Running = false;
                    return(SolverResult.Timeout(iterations, (int)totalwatch.ElapsedMilliseconds));
                }
            }
        }