Inheritance: PLVisitor
コード例 #1
0
ファイル: TTEntails.cs プロジェクト: PaulMineau/AIMA.Net
        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)));
            }
        }
コード例 #2
0
ファイル: WalkSAT.cs プロジェクト: PaulMineau/AIMA.Net
        private Symbol getSymbolWhoseFlipMaximisesSatisfiedClauses(
                List<Sentence> clauses, List<Symbol> symbols, Model model)
        {
            if (symbols.Count > 0)
            {
                Symbol retVal = symbols[0];
                int maxClausesSatisfied = 0;
                for (int i = 0; i < symbols.Count; i++)
                {
                    Symbol sym = symbols[i];
                    if (getNumberOfClausesSatisfiedIn(clauses, model.flip(sym)) > maxClausesSatisfied)
                    {
                        retVal = sym;
                        maxClausesSatisfied = getNumberOfClausesSatisfiedIn(
                                clauses, model.flip(sym));
                    }
                }
                return retVal;
            }
            else
            {
                return null;
            }

        }
コード例 #3
0
ファイル: DPLL.cs プロジェクト: PaulMineau/AIMA.Net
 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);
 }
コード例 #4
0
ファイル: DPLLTest.cs プロジェクト: PaulMineau/AIMA.Net
 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);
 }
コード例 #5
0
ファイル: ModelTest.cs プロジェクト: PaulMineau/AIMA.Net
 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));
 }
コード例 #6
0
ファイル: ModelTest.cs プロジェクト: PaulMineau/AIMA.Net
 public void setUp()
 {
     parser = new PEParser();
     trueSentence = (Sentence)parser.parse("true");
     falseSentence = (Sentence)parser.parse("false");
     andSentence = (Sentence)parser.parse("(P  AND  Q)");
     orSentence = (Sentence)parser.parse("(P  OR  Q)");
     impliedSentence = (Sentence)parser.parse("(P  =>  Q)");
     biConditionalSentence = (Sentence)parser.parse("(P  <=>  Q)");
     m = new Model();
 }
コード例 #7
0
ファイル: DPLLTest.cs プロジェクト: PaulMineau/AIMA.Net
 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);
 }
コード例 #8
0
ファイル: DPLLTest.cs プロジェクト: PaulMineau/AIMA.Net
 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);
 }
コード例 #9
0
ファイル: WalkSAT.cs プロジェクト: PaulMineau/AIMA.Net
        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;
        }
コード例 #10
0
ファイル: DPLL.cs プロジェクト: PaulMineau/AIMA.Net
        public List<Sentence> clausesWithNonTrueValues(List<Sentence> clauseList,
                Model model)
        {
            List<Sentence> clausesWithNonTrueValues = new List<Sentence>();
            for (int i = 0; i < clauseList.Count; i++)
            {
                Sentence clause = clauseList[i];
                if (!(isClauseTrueInModel(clause, model)))
                {
                    if (!(clausesWithNonTrueValues.Contains(clause)))
                    {// defensive
                        // programming not really necessary
                        clausesWithNonTrueValues.Add(clause);
                    }
                }

            }
            return clausesWithNonTrueValues;
        }
コード例 #11
0
ファイル: DPLL.cs プロジェクト: PaulMineau/AIMA.Net
        private SymbolValuePair findUnitClause(List<Sentence> clauseList, Model model,
                List<Symbol> symbols)
        {
            for (int i = 0; i < clauseList.Count; i++)
            {
                Sentence clause = (Sentence)clauseList[i];
                if ((clause is Symbol)
                        && (!(model.getAssignedSymbols().Contains(clause))))
                {
                    // System.Console.WriteLine("found unit clause - assigning");
                    return new SymbolValuePair(new Symbol(((Symbol)clause)
                            .getValue()), true);
                }

                if (clause is UnarySentence)
                {
                    UnarySentence sentence = (UnarySentence)clause;
                    Sentence negated = sentence.getNegated();
                    if ((negated is Symbol)
                            && (!(model.getAssignedSymbols().Contains(negated))))
                    {
                        // System.Console.WriteLine("found unit clause type 2 -
                        // assigning");
                        return new SymbolValuePair(new Symbol(((Symbol)negated)
                                .getValue()), false);
                    }
                }

            }

            return new SymbolValuePair();// failed to find any unit clause;

        }
コード例 #12
0
ファイル: DPLL.cs プロジェクト: PaulMineau/AIMA.Net
        private bool isClauseTrueInModel(Sentence clause, Model model)
        {
            List<Symbol> positiveSymbols = new SymbolClassifier().getPositiveSymbolsIn(clause);
            List<Symbol> negativeSymbols = new SymbolClassifier().getNegativeSymbolsIn(clause);

            foreach (Symbol symbol in positiveSymbols)
            {
                if ((model.isTrue(symbol)))
                {
                    return true;
                }
            }
            foreach (Symbol symbol in negativeSymbols)
            {
                if ((model.isFalse(symbol)))
                {
                    return true;
                }
            }
            return false;

        }
コード例 #13
0
ファイル: DPLL.cs プロジェクト: PaulMineau/AIMA.Net
        private bool areAllClausesTrue(Model model, List<Sentence> clauseList)
        {

            for (int i = 0; i < clauseList.Count; i++)
            {
                Sentence clause = (Sentence)clauseList[i];
                // System.Console.WriteLine("evaluating " + clause.ToString());
                if (!isClauseTrueInModel(clause, model))
                { // ie if false or
                    // UNKNOWN
                    // System.Console.WriteLine(clause.ToString()+ " is not true");
                    return false;
                }

            }
            return true;
        }
コード例 #14
0
ファイル: DPLL.cs プロジェクト: PaulMineau/AIMA.Net
        private bool isEvenOneClauseFalse(Model model, List<Sentence> clauseList)
        {
            for (int i = 0; i < clauseList.Count; i++)
            {
                Sentence clause = (Sentence)clauseList[i];
                if (model.isFalse(clause))
                {
                    // System.Console.WriteLine(clause.ToString() + " is false");
                    return true;
                }

            }

            return false;
        }
コード例 #15
0
ファイル: Model.cs プロジェクト: PaulMineau/AIMA.Net
	public Model extend(Symbol symbol, bool b) {
		Model m = new Model();
		m.h = this.h;
		m.h.Add(symbol, b);
		return m;
	}
コード例 #16
0
ファイル: DPLLTest.cs プロジェクト: PaulMineau/AIMA.Net
        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);
        }
コード例 #17
0
ファイル: ModelTest.cs プロジェクト: PaulMineau/AIMA.Net
 public void testExtendModel()
 {
     String p = "P";
     m = m.extend(new Symbol(p), true);
     Assert.AreEqual(true, m.getStatus(new Symbol("P")));
 }
コード例 #18
0
ファイル: ModelTest.cs プロジェクト: PaulMineau/AIMA.Net
 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));
 }
コード例 #19
0
ファイル: WalkSAT.cs プロジェクト: PaulMineau/AIMA.Net
 private int getNumberOfClausesSatisfiedIn(List<Sentence> clauses, Model model)
 {
     int retVal = 0;
     foreach(Sentence s in clauses)
     {
         if (model.isTrue(s))
         {
             retVal += 1;
         }
     }
     return retVal;
 }
コード例 #20
0
ファイル: DPLL.cs プロジェクト: PaulMineau/AIMA.Net
        public SymbolValuePair findPureSymbolValuePair(List<Sentence> clauseList,
                Model model, List<Symbol> symbols)
        {
            List<Sentence> _clausesWithNonTrueValues = clausesWithNonTrueValues(clauseList,
                    model);
            Sentence nonTrueClauses = LogicUtils.chainWith("AND",
                    _clausesWithNonTrueValues);
            // System.Console.WriteLine("Unsatisfied clauses = "
            // + clausesWithNonTrueValues.Count);
            List<Symbol> symbolsAlreadyAssigned = new List<Symbol>( model.getAssignedSymbols());

            // debug
            // List symList = asList(symbolsAlreadyAssigned);
            //
            // System.Console.WriteLine(" assignedSymbols = " + symList.Count);
            // if (symList.Count == 52) {
            // System.Console.WriteLine("untrue clauses = " + clausesWithNonTrueValues);
            // System.Console.WriteLine("model= " + model);
            // }

            // debug
            List<Symbol> purePositiveSymbols = SetOps
                    .difference(new SymbolClassifier()
                            .getPurePositiveSymbolsIn(nonTrueClauses),
                            symbolsAlreadyAssigned);

            List<Symbol> pureNegativeSymbols = SetOps
                    .difference(new SymbolClassifier()
                            .getPureNegativeSymbolsIn(nonTrueClauses),
                            symbolsAlreadyAssigned);
            // if none found return "not found
            if ((purePositiveSymbols.Count == 0)
                    && (pureNegativeSymbols.Count == 0))
            {
                return new SymbolValuePair();// automatically set to null values
            }
            else
            {
                if (purePositiveSymbols.Count > 0)
                {
                    Symbol symbol =purePositiveSymbols[0];
                    if (pureNegativeSymbols.Contains(symbol))
                    {
                        throw new ApplicationException("Symbol " + symbol.getValue()
                                + "misclassified");
                    }
                    return new SymbolValuePair(symbol, true);
                }
                else
                {
                    Symbol symbol = new Symbol((pureNegativeSymbols[0])
                            .getValue());
                    if (purePositiveSymbols.Contains(symbol))
                    {
                        throw new ApplicationException("Symbol " + symbol.getValue()
                                + "misclassified");
                    }
                    return new SymbolValuePair(symbol, false);
                }
            }
        }
コード例 #21
0
ファイル: TTEntailsTest.cs プロジェクト: PaulMineau/AIMA.Net
        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));
        }
コード例 #22
0
ファイル: DPLL.cs プロジェクト: PaulMineau/AIMA.Net
        //
        // 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)));
        }