Beispiel #1
0
        static void IndependentSetProblem()
        {
            PrintIndependentSetUsage();

            IndependentSetProblem problem = new IndependentSetProblem();

            problem.ReadInput(Console.In);

            CNF problemCNF = problem.ConvertToCNF();

            DPLL problemDPLL = new DPLL();
            Task <DPLLResultHolder> parallelSAT = problemDPLL.SatisfiableParallel(problemCNF);

            parallelSAT.Wait();
            Console.WriteLine();
            Console.WriteLine(problem.InterpretDPLLResult(parallelSAT.Result));
        }
Beispiel #2
0
        static void ThreeColorabilityProblem()
        {
            Print3ColorabilityUsage();

            ThreeColorabilityProblem problem = new ThreeColorabilityProblem();

            problem.ReadInput(Console.In);

            CNF problemCNF = problem.ConvertToCNF();

            DPLL problemDPLL = new DPLL();
            Task <DPLLResultHolder> parallelSAT = problemDPLL.SatisfiableParallel(problemCNF);

            parallelSAT.Wait();
            Console.WriteLine();
            Console.WriteLine(problem.InterpretDPLLResult(parallelSAT.Result));
        }
Beispiel #3
0
        static void HamiltonianPathProblem()
        {
            PrintHamiltonianPathUsage();

            HamiltonianPathProblem problem = new HamiltonianPathProblem();

            problem.ReadInput(Console.In);

            CNF problemCNF = problem.ConvertToCNF();

            DPLL problemDPLL = new DPLL();
            Task <DPLLResultHolder> parallelSAT = problemDPLL.SatisfiableParallel(problemCNF);

            parallelSAT.Wait();
            Console.WriteLine();
            Console.WriteLine(problem.InterpretDPLLResult(parallelSAT.Result));
        }
Beispiel #4
0
        // Each thread's "main" function
        private DPLLResultHolder Solve(DPLL parallelDPLL, Queue <CNF> sharedModelQueue, IdleThreadsCounter idleThreadsCounter, CancellationToken token)
        {
            CNF model;

            while (true)
            {
                lock (sharedModelQueue)
                {
                    while (sharedModelQueue.Count == 0)
                    {
                        Monitor.Wait(sharedModelQueue); // Wait if there's nothing to solve
                        token.ThrowIfCancellationRequested();
                    }

                    Interlocked.Decrement(ref idleThreadsCounter.Counter); // This task started working - is no longer idle
                    model = sharedModelQueue.Dequeue();                    // Get a CNF model from the shared queue
                }

                token.ThrowIfCancellationRequested();
                DPLLResultHolder solverThreadResult = parallelDPLL.Satisfiable(model);

                if (solverThreadResult.SAT)
                {
                    return(solverThreadResult);  // The task has found SAT model
                }
                else
                {
                    Interlocked.Increment(ref idleThreadsCounter.Counter);  // The task is idle for now
                }

                lock (sharedModelQueue)
                {
                    // If all threads are idle and the queue is empty -> we're done and result remains unsat
                    if (Interlocked.Read(ref idleThreadsCounter.Counter) == Environment.ProcessorCount && sharedModelQueue.Count == 0)
                    {
                        return(solverThreadResult);
                    }
                }
            }
        }
Beispiel #5
0
        static void SATSolver()
        {
            PrintSATSolverUsage();

            CNF cnf = new CNF();

            cnf.ReadFormula(Console.In);

            // Sequential solution

            /*CNF cnfCopy = new CNF(cnf);
             * DPLL sequentialDPLL = new DPLL();
             * DPLLResultHolder sequentialResult = sequentialDPLL.Satisfiable(cnfCopy);
             * Console.WriteLine();
             * Console.WriteLine(sequentialResult);*/

            // Parallel solution
            DPLL parallelDPLL = new DPLL();
            Task <DPLLResultHolder> parallelSAT = parallelDPLL.SatisfiableParallel(cnf);

            parallelSAT.Wait();
            Console.WriteLine();
            Console.WriteLine(parallelSAT.Result);
        }