extend() public method

public extend ( Symbol symbol, bool b ) : Model
symbol AIMA.Core.Logic.Propositional.Parsing.Ast.Symbol
b bool
return Model
Exemplo n.º 1
0
        public bool ttCheckAll(Sentence kbSentence, Sentence querySentence,
                List<Symbol> symbols, Model model)
        {
            if (symbols.Count==0)
            {
                if (model.isTrue(kbSentence))
                {
                    // System.Console.WriteLine("#");
                    return model.isTrue(querySentence);
                }
                else
                {
                    // System.Console.WriteLine("0");
                    return true;
                }
            }
            else
            {
                Symbol symbol = (Symbol)Util.first(symbols);
                List<Symbol> rest = Util.rest(symbols);

                Model trueModel = model.extend(new Symbol(symbol.getValue()), true);
                Model falseModel = model.extend(new Symbol(symbol.getValue()),
                        false);
                return (ttCheckAll(kbSentence, querySentence, rest, trueModel) && (ttCheckAll(
                        kbSentence, querySentence, rest, falseModel)));
            }
        }
Exemplo n.º 2
0
        public bool ttCheckAll(Sentence kbSentence, Sentence querySentence,
                               List <Symbol> symbols, Model model)
        {
            if (symbols.Count == 0)
            {
                if (model.isTrue(kbSentence))
                {
                    // System.Console.WriteLine("#");
                    return(model.isTrue(querySentence));
                }
                else
                {
                    // System.Console.WriteLine("0");
                    return(true);
                }
            }
            else
            {
                Symbol        symbol = (Symbol)Util.first(symbols);
                List <Symbol> rest   = Util.rest(symbols);

                Model trueModel  = model.extend(new Symbol(symbol.getValue()), true);
                Model falseModel = model.extend(new Symbol(symbol.getValue()),
                                                false);
                return(ttCheckAll(kbSentence, querySentence, rest, trueModel) && (ttCheckAll(
                                                                                      kbSentence, querySentence, rest, falseModel)));
            }
        }
Exemplo n.º 3
0
        //
        // PRIVATE METHODS
        //

        private bool dpll(List <Sentence> clauses, List <Symbol> symbols, Model model)
        {
            // List<Sentence> clauseList = asList(clauses);
            List <Sentence> clauseList = clauses;

            // System.Console.WriteLine("clauses are " + clauses.ToString());
            // if all clauses are true return true;
            if (areAllClausesTrue(model, clauseList))
            {
                // System.Console.WriteLine(model.ToString());
                return(true);
            }
            // if even one clause is false return false
            if (isEvenOneClauseFalse(model, clauseList))
            {
                // System.Console.WriteLine(model.ToString());
                return(false);
            }
            // System.Console.WriteLine("At least one clause is unknown");
            // try to find a unit clause
            SymbolValuePair svp = findPureSymbolValuePair(clauseList, model,
                                                          symbols);

            if (svp.notNull())
            {
                Symbol[] copy = new Symbol[symbols.Count];
                symbols.CopyTo(copy);
                List <Symbol> newSymbols = new List <Symbol>(copy);
                newSymbols.Remove(new Symbol(svp.symbol.getValue()));
                Model newModel = model.extend(new Symbol(svp.symbol.getValue()),
                                              svp.value);
                return(dpll(clauses, newSymbols, newModel));
            }

            SymbolValuePair svp2 = findUnitClause(clauseList, model, symbols);

            if (svp2.notNull())
            {
                Symbol[] copy = new Symbol[symbols.Count];
                symbols.CopyTo(copy);
                List <Symbol> newSymbols = new List <Symbol>(copy);
                newSymbols.Remove(new Symbol(svp2.symbol.getValue()));
                Model newModel = model.extend(new Symbol(svp2.symbol.getValue()),
                                              svp2.value);
                return(dpll(clauses, newSymbols, newModel));
            }

            Symbol symbol = (Symbol)symbols[0];

            // System.Console.WriteLine("default behaviour selecting " + symbol);
            Symbol[] symbolsArr = new Symbol[symbols.Count];
            symbols.CopyTo(symbolsArr);
            List <Symbol> newSymbols2 = symbolsArr.ToList <Symbol>();

            newSymbols2.RemoveAt(0);
            return(dpll(clauses, newSymbols2, model.extend(symbol, true)) || dpll(
                       clauses, newSymbols2, model.extend(symbol, false)));
        }
Exemplo n.º 4
0
 public void testDPLLReturnsFalseWhenOneClauseFalseInModel()
 {
     Model model = new Model();
     model = model.extend(new Symbol("A"), true).extend(new Symbol("B"),
             false);
     Sentence sentence = (Sentence)parser.parse("((A OR B) AND (A => B))");
     bool satisfiable = dpll.dpllSatisfiable(sentence, model);
     Assert.AreEqual(false, satisfiable);
 }
Exemplo n.º 5
0
 public void testSentenceStatusWhenPFalseAndQFalse()
 {
     String p = "P";
     String q = "Q";
     m = m.extend(new Symbol(p), false);
     m = m.extend(new Symbol(q), false);
     Assert.AreEqual(true, m.isFalse(andSentence));
     Assert.AreEqual(true, m.isFalse(orSentence));
     Assert.AreEqual(true, m.isTrue(impliedSentence));
     Assert.AreEqual(true, m.isTrue(biConditionalSentence));
 }
Exemplo n.º 6
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);
 }
Exemplo n.º 7
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);
 }
Exemplo n.º 8
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);
        }
Exemplo n.º 9
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;
        }
Exemplo n.º 10
0
        public void testModelEvaluation()
        {
            kb.tell("(NOT P11)");
            kb.tell("(B11 <=> (P12 OR P21))");
            kb.tell("(B21 <=> ((P11 OR P22) OR P31))");
            kb.tell("(NOT B11)");
            kb.tell("(B21)");

            Model model = new Model();
            model = model.extend(new Symbol("B11"), false);
            model = model.extend(new Symbol("B21"), true);
            model = model.extend(new Symbol("P11"), false);
            model = model.extend(new Symbol("P12"), false);
            model = model.extend(new Symbol("P21"), false);
            model = model.extend(new Symbol("P22"), false);
            model = model.extend(new Symbol("P31"), true);

            Sentence kbs = kb.asSentence();
            Assert.AreEqual(true, model.isTrue(kbs));
        }
Exemplo n.º 11
0
        //
        // PRIVATE METHODS
        //

        private bool dpll(List<Sentence> clauses, List<Symbol> symbols, Model model)
        {
            // List<Sentence> clauseList = asList(clauses);
            List<Sentence> clauseList = clauses;
            // System.Console.WriteLine("clauses are " + clauses.ToString());
            // if all clauses are true return true;
            if (areAllClausesTrue(model, clauseList))
            {
                // System.Console.WriteLine(model.ToString());
                return true;
            }
            // if even one clause is false return false
            if (isEvenOneClauseFalse(model, clauseList))
            {
                // System.Console.WriteLine(model.ToString());
                return false;
            }
            // System.Console.WriteLine("At least one clause is unknown");
            // try to find a unit clause
            SymbolValuePair svp = findPureSymbolValuePair(clauseList, model,
                    symbols);
            if (svp.notNull())
            {
                Symbol[] copy = new Symbol[symbols.Count];
                symbols.CopyTo(copy);
                List<Symbol> newSymbols = new List<Symbol>(copy);
                newSymbols.Remove(new Symbol(svp.symbol.getValue()));
                Model newModel = model.extend(new Symbol(svp.symbol.getValue()),
                        svp.value);
                return dpll(clauses, newSymbols, newModel);
            }

            SymbolValuePair svp2 = findUnitClause(clauseList, model, symbols);
            if (svp2.notNull())
            {
                Symbol[] copy = new Symbol[symbols.Count];
                symbols.CopyTo(copy);
                List<Symbol> newSymbols = new List<Symbol>(copy);
                newSymbols.Remove(new Symbol(svp2.symbol.getValue()));
                Model newModel = model.extend(new Symbol(svp2.symbol.getValue()),
                        svp2.value);
                return dpll(clauses, newSymbols, newModel);
            }

            Symbol symbol = (Symbol)symbols[0];
            // System.Console.WriteLine("default behaviour selecting " + symbol);
            Symbol[] symbolsArr = new Symbol[symbols.Count];
            symbols.CopyTo(symbolsArr);
            List<Symbol> newSymbols2 = symbolsArr.ToList<Symbol>();
            newSymbols2.RemoveAt(0);
            return (dpll(clauses, newSymbols2, model.extend(symbol, true)) || dpll(
                    clauses, newSymbols2, model.extend(symbol, false)));
        }
Exemplo n.º 12
0
 public void testComplexSentence()
 {
     String p = "P";
     String q = "Q";
     m = m.extend(new Symbol(p), true);
     m = m.extend(new Symbol(q), false);
     Sentence sent = (Sentence)parser.parse("((P OR Q) AND  (P => Q))");
     Assert.IsFalse(m.isTrue(sent));
     Assert.IsTrue(m.isFalse(sent));
     Sentence sent2 = (Sentence)parser.parse("((P OR Q) AND  (Q))");
     Assert.IsFalse(m.isTrue(sent2));
     Assert.IsTrue(m.isFalse(sent2));
 }
Exemplo n.º 13
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);
        }