Inheritance: BasicTraverser
Exemple #1
0
 public bool dpllSatisfiable(Sentence s, Model m)
 {
     List<Sentence> clauses = new CNFClauseGatherer()
             .getClausesFrom(new CNFTransformer().transform(s));
     List<Symbol> symbols = new SymbolCollector()
             .getSymbolsIn(s);
     // System.Console.WriteLine(" numberOfSymbols = " + symbols.Count);
     return dpll(clauses, symbols, m);
 }
Exemple #2
0
 public void testDPLLFilteringNonTrueClausesGivesNullWhenAllClausesAreKnown()
 {
     Model model = new Model();
     model = model.extend(new Symbol("A"), true).extend(new Symbol("B"),
             true).extend(new Symbol("C"), true);
     Sentence sentence = (Sentence)parser
             .parse("((A AND B) AND (B AND C))");
     List<Sentence> clauseList = new CNFClauseGatherer()
                     .getClausesFrom(new CNFTransformer()
                             .transform(sentence));
     List<Sentence> clausesWithNonTrueValues = dpll
             .clausesWithNonTrueValues(clauseList, model);
     Assert.AreEqual(0, clausesWithNonTrueValues.Count);
 }
Exemple #3
0
 public void testDPLLFiltersClausesTheStatusOfWhichAreKnown()
 {
     Model model = new Model();
     model = model.extend(new Symbol("A"), true).extend(new Symbol("B"),
             true);
     Sentence sentence = (Sentence)parser
             .parse("((A AND B) AND (B AND C))");
     List<Sentence> clauseList = new CNFClauseGatherer()
                     .getClausesFrom(new CNFTransformer()
                             .transform(sentence));
     List<Sentence> clausesWithNonTrueValues = dpll
             .clausesWithNonTrueValues(clauseList, model);
     Assert.AreEqual(1, clausesWithNonTrueValues.Count);
     Sentence nonTrueClause = (Sentence)parser.parse("(B AND C)");
     clausesWithNonTrueValues.Contains(nonTrueClause);
 }
Exemple #4
0
        public Model findModelFor(String logicalSentence, int numberOfFlips,
                double probabilityOfRandomWalk)
        {
            myModel = new Model();
            Sentence s = (Sentence)new PEParser().parse(logicalSentence);
            CNFTransformer transformer = new CNFTransformer();
            CNFClauseGatherer clauseGatherer = new CNFClauseGatherer();
            SymbolCollector sc = new SymbolCollector();

            List<Symbol> symbols = sc.getSymbolsIn(s);
            Random r = new Random();
            for (int i = 0; i < symbols.Count; i++)
            {
                Symbol sym = (Symbol)symbols[i];
                myModel = myModel.extend(sym, Util.randombool());
            }
            List<Sentence> clauses = clauseGatherer.getClausesFrom(transformer
                            .transform(s));

            for (int i = 0; i < numberOfFlips; i++)
            {
                if (getNumberOfClausesSatisfiedIn(clauses, myModel) == clauses.Count)
                {
                    return myModel;
                }
                Sentence clause = clauses[random.Next(clauses.Count)];

                List<Symbol> symbolsInClause = sc
                        .getSymbolsIn(clause);
                if (random.NextDouble() >= probabilityOfRandomWalk)
                {
                    Symbol randomSymbol = symbolsInClause[random
                            .Next(symbolsInClause.Count)];
                    myModel = myModel.flip(randomSymbol);
                }
                else
                {
                    Symbol symbolToFlip = getSymbolWhoseFlipMaximisesSatisfiedClauses(
                            clauses,
                            symbolsInClause, myModel);
                    myModel = myModel.flip(symbolToFlip);
                }

            }
            return null;
        }
Exemple #5
0
        public bool plResolution(KnowledgeBase kb, Sentence alpha)
        {
            Sentence kBAndNotAlpha = new BinarySentence("AND", kb.asSentence(),
                    new UnarySentence(alpha));
            List<Sentence> clauses = new CNFClauseGatherer()
                    .getClausesFrom(new CNFTransformer().transform(kBAndNotAlpha));
            clauses = filterOutClausesWithTwoComplementaryLiterals(clauses);
            List<Sentence> newClauses = new List<Sentence>();
            while (true)
            {
                List<List<Sentence>> pairs = getCombinationPairs(clauses);

                for (int i = 0; i < pairs.Count; i++)
                {
                    List<Sentence> pair = pairs[i];
                    // System.Console.WriteLine("pair number" + i+" of "+pairs.Count);
                    List<Sentence> resolvents = plResolve(pair[0], pair[1]);
                    resolvents = filterOutClausesWithTwoComplementaryLiterals(resolvents);

                    if (resolvents.Contains(new Symbol("EMPTY_CLAUSE")))
                    {
                        return true;
                    }
                    newClauses = SetOps.union(newClauses, resolvents);
                    // System.Console.WriteLine("clauseslist size = " +clauses.Count);

                }
                if (SetOps.intersection(newClauses, clauses).Count == newClauses
                        .Count)
                {// subset test
                    return false;
                }
                clauses = SetOps.union(newClauses, clauses);
                clauses = filterOutClausesWithTwoComplementaryLiterals(clauses);
            }

        }
 public void setUp()
 {
     parser = new PEParser();
     gatherer = new CNFClauseGatherer();
 }
Exemple #7
0
        public void testDPLLFindsPurePositiveSymbolsWhenTheyExist()
        {
            Model model = new Model();
            model = model.extend(new Symbol("A"), true).extend(new Symbol("B"),
                    true);
            Sentence sentence = (Sentence)parser
                    .parse("((A AND B) AND (B AND C))");
            List<Sentence> clauseList = new CNFClauseGatherer()
                            .getClausesFrom(new CNFTransformer()
                                    .transform(sentence));
            List<Symbol> symbolList = new SymbolCollector().getSymbolsIn(sentence);

            DPLL.SymbolValuePair sv = dpll.findPureSymbolValuePair(clauseList,
                    model, symbolList);
            Assert.IsNotNull(sv);
            Assert.AreEqual(new Symbol("C"), sv.symbol);
            Assert.AreEqual(true, sv.value);
        }