Esempio n. 1
0
        public static int GenerateAssignment(SatProblem origProblem, IWellAnalyzed algorithm, out VariableAssignment assignment)
        {
            assignment = new VariableAssignment();

            var curProblem = origProblem;

            foreach (var problemVariable in origProblem.Variables)
            {
                var subsProblem0 = SubstituteVaraible(curProblem, problemVariable, false);
                var subsProblem1 = SubstituteVaraible(curProblem, problemVariable, true);

                double W0 = algorithm.GetExpectedValue(subsProblem0);
                double W1 = algorithm.GetExpectedValue(subsProblem1);

                if (W0 <= W1)
                {
                    assignment[problemVariable] = true;
                    curProblem = subsProblem1;
                }
                else
                {
                    assignment[problemVariable] = false;
                    curProblem = subsProblem0;
                }
            }

            return(origProblem.GetNumberOfSatisfiedClauses(assignment));
        }
Esempio n. 2
0
        private static SatProblem SubstituteVaraible(SatProblem problem, Variable variable, bool value)
        {
            SatProblem result = new SatProblem();

            foreach (var clause in problem)
            {
                if (clause.TryGetValue(variable, out bool negated))
                {
                    if (negated != value)
                    {
                        result.Add(new Clause());
                    }
                    else if (clause.Count > 1)
                    {
                        Clause newClause = new Clause(clause);
                        newClause.Remove(variable);
                        result.Add(newClause);
                    }
                }
                else
                {
                    result.Add(new Clause(clause));
                }
            }

            return(result);
        }
Esempio n. 3
0
        public double GetExpectedValue(SatProblem problem)
        {
            var algoObjectToUse = this;

            if (problem != _problem)
            {
                algoObjectToUse = new AlgorithmB(problem);
            }

            if (algoObjectToUse._probailities == null)
            {
                algoObjectToUse.GenerateProbabilities();
            }

            double result = 0;

            foreach (var problemClause in problem.Clauses)
            {
                if (problemClause.Count == 0)
                {
                    result++;
                    continue;
                }

                double falsePropability = 1;
                foreach (var literal in problemClause)
                {
                    falsePropability *= literal.Value ? 1 - algoObjectToUse._probailities[literal.Key] : algoObjectToUse._probailities[literal.Key];
                }
                result += 1 - falsePropability;
            }
            return(result);
        }
Esempio n. 4
0
        public int GenerateAssingment(SatProblem problem, out VariableAssignment assignment)
        {
            assignment = new VariableAssignment();
            foreach (var problemVariable in problem.Variables)
            {
                assignment[problemVariable] = _rand.NextDouble() < _probailities[problemVariable];
            }

            return(problem.GetNumberOfSatisfiedClauses(assignment));
        }
Esempio n. 5
0
        public int GenerateAssingment(SatProblem problem, int trys, out VariableAssignment assignment)
        {
            int bestSatisfaction = 0;

            assignment = null;
            for (int i = 0; i < trys; i++)
            {
                int newSatisfaction;
                if ((newSatisfaction = GenerateAssingment(problem, out VariableAssignment newAssignment)) > bestSatisfaction)
                {
                    assignment       = newAssignment;
                    bestSatisfaction = newSatisfaction;
                }
            }
            return(bestSatisfaction);
        }
Esempio n. 6
0
 public AlgorithmB(SatProblem problem)
 {
     _problem = problem;
 }
Esempio n. 7
0
        static void Main(string[] args)
        {
            Variable x1 = new Variable(1);
            Variable x2 = new Variable(2);
            Variable x3 = new Variable(3);


            SatProblem parsedProblem = CnfFileParser.Parse(args[0]);

            SatProblem problem = new SatProblem
            {
                new Clause {
                    { x1, true }, x2
                },
                new Clause {
                    { x2, true }, x3
                }
            };


            problem = parsedProblem;
            Console.WriteLine($"Problem: n={problem.Variables.Count}, m={problem.Clauses.Count}");

            int benchmarkRuns = 1000;

            int trys = 100;


            Console.WriteLine();
            Console.WriteLine("Algorithmus A:");
            AlgorithmA algoA = new AlgorithmA();

            int satisfiedClausesA = algoA.GenerateAssingment(problem, trys, out VariableAssignment assignmentA);

            Console.WriteLine($"Anzahl der Versuche: {trys}");
            Console.WriteLine($"Erfüllte Klauseln: {satisfiedClausesA}");

            TimeSpan algoAExecutionTime = Benchmark(benchmarkRuns, () => algoA.GenerateAssingment(problem, trys, out assignmentA));

            Console.WriteLine($"Average Runtime for {benchmarkRuns} runs: {algoAExecutionTime.TotalMilliseconds:##0.#####} ms");


            Console.WriteLine();
            Console.WriteLine("Algorithmus B:");
            var algoB = new AlgorithmB(parsedProblem);

            algoB.GenerateProbabilities();

            int satisfiedClausesB = algoB.GenerateAssingment(problem, trys, out VariableAssignment assignmentB);

            Console.WriteLine($"Anzahl der Versuche: {trys}");
            Console.WriteLine($"Erfüllte Klauseln: {satisfiedClausesB}");

            TimeSpan algoBExecutionTime = Benchmark(benchmarkRuns, () =>
            {
                algoB.GenerateProbabilities();
                algoB.GenerateAssingment(problem, trys, out assignmentB);
            });

            Console.WriteLine($"Average Runtime for {benchmarkRuns} runs: {algoBExecutionTime.TotalMilliseconds:##0.#####} ms");


            Console.WriteLine();
            Console.WriteLine("Algorithmus DRAND_A:");

            int satisfiedClausesDrandA = ArgorithmDrand.GenerateAssignment(problem, algoA, out VariableAssignment assignmentDrandA);

            Console.WriteLine($"Erfüllte Klauseln: {satisfiedClausesDrandA}");

            TimeSpan algoDerandAExecutionTime = Benchmark(benchmarkRuns, () => ArgorithmDrand.GenerateAssignment(problem, algoA, out assignmentDrandA));

            Console.WriteLine($"Average Runtime for {benchmarkRuns} runs: {algoDerandAExecutionTime.TotalMilliseconds:##0.#####} ms");


            Console.WriteLine();
            Console.WriteLine("Algorithmus DRAND_B:");

            int satisfiedClausesDrandB = ArgorithmDrand.GenerateAssignment(problem, algoB, out VariableAssignment assignmentDrandB);

            Console.WriteLine($"Erfüllte Klauseln: {satisfiedClausesDrandB}");

            TimeSpan algoDerandBExecutionTime = Benchmark(benchmarkRuns, () => ArgorithmDrand.GenerateAssignment(problem, algoB, out assignmentDrandB));

            Console.WriteLine($"Average Runtime for {benchmarkRuns} runs: {algoDerandBExecutionTime.TotalMilliseconds:##0.#####} ms");


            Console.Read();
        }
 public double GetExpectedValue(SatProblem problem)
 {
     return(problem.Select(clause => clause.Count == 0 ? 1 : 1 - 1.0 / (1 << clause.Count)).Sum());
 }
        public static SatProblem Parse(string filePath)
        {
            SatProblem result = new SatProblem();

            using (StreamReader reader = new StreamReader(filePath))
            {
                Variable[] variables = null;
                Clause     clause    = new Clause();

                string line;
                while ((line = reader.ReadLine()) != null)
                {
                    if (line.StartsWith("c"))
                    {
                        continue;
                    }
                    if (line.StartsWith("p"))
                    {
                        var match = ProblemLineRegex.Match(line);
                        if (match.Success && int.TryParse(match.Groups["VariableCount"].Value, out int variableCount))
                        {
                            variables = new Variable[variableCount];
                            for (int i = 0; i < variableCount; i++)
                            {
                                variables[i] = new Variable(i);
                            }
                        }
                        else
                        {
                            throw new Exception($"Could not parse problem line of file {filePath}");
                        }
                    }
                    else if (variables != null)
                    {
                        var match = DataLineRegex.Match(line);
                        if (!match.Success)
                        {
                            continue;
                        }

                        foreach (Capture capture in match.Groups["Date"].Captures)
                        {
                            if (int.TryParse(capture.Value, out int value))
                            {
                                if (value == 0)
                                {
                                    result.Add(clause);
                                    clause = new Clause();
                                }
                                else
                                {
                                    clause.Add(variables[Math.Abs(value) - 1], value < 0);
                                }
                            }
                        }
                    }
                }

                if (clause.Count > 0)
                {
                    result.Add(clause);
                }

                return(result);
            }
        }