Exemple #1
0
        public string InterpretDPLLResult(DPLLResultHolder result)
        {
            if (result != null)
            {
                if (!result.SAT)
                {
                    return("No hamiltonian path exist.");
                }

                var variableNames = result.Model.VariablesDict.Keys.ToList();

                variableNames.Sort((x, y) => x.CompareTo(y));

                StringBuilder stringBuilder = new StringBuilder();

                foreach (var variableName in variableNames)
                {
                    if (result.Model.VariablesDict[variableName][0].Value)
                    {
                        string[] tokens = variableName.Split('-');

                        if (tokens.Length != 3) // The "result" is not a result of hamiltonian path problem converted to SAT problem
                        {
                            throw new ArgumentException();
                        }

                        stringBuilder.Append(String.Format("{0}. position is vertex {1}\n", tokens[1], tokens[2]));
                    }
                }

                return(stringBuilder.ToString());
            }

            return(null);
        }
Exemple #2
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);
                    }
                }
            }
        }
Exemple #3
0
        // Returns a string explaining the result of the DPLL algorithm
        public string InterpretDPLLResult(DPLLResultHolder result)
        {
            if (result != null)
            {
                if (!result.SAT)
                {
                    return("Such independent set does not exist.");
                }

                StringBuilder stringBuilder = new StringBuilder();
                stringBuilder.Append(String.Format("The following vertices can be chosen for the independent set of size atleast {0}:\n", K));

                var variableNames = result.Model.VariablesDict.Keys.ToList();

                variableNames.Sort((x, y) =>
                {
                    if (x[0] != 'X' && y[0] != 'X')
                    {
                        return(Convert.ToInt32(x).CompareTo(Convert.ToInt32(y)));
                    }
                    else
                    {
                        return(x.CompareTo(y));
                    }
                });

                foreach (var variableName in variableNames)
                {
                    if (!variableName.Contains('X') && result.Model.VariablesDict[variableName][0].Value) // Helper variables start with 'X', we do not want to output them
                    {
                        stringBuilder.Append(String.Format("{0}\n", variableName));
                    }
                }

                return(stringBuilder.ToString());
            }

            return(null);
        }