Exemple #1
0
        /**
         * Determine if the clause represents a tautology, of which the following
         * are examples:<br>
         *
         * <pre>
         * {..., True, ...}
         * {..., ~False, ...}
         * {..., P, ..., ~P, ...}
         * </pre>
         *
         * @return true if the clause represents a tautology, false otherwise.
         */
        public bool?isTautology()
        {
            if (cachedIsTautologyResult == null)
            {
                foreach (Literal l in literals)
                {
                    if (l.isAlwaysTrue())
                    {
                        // {..., True, ...} is a tautology.
                        // {..., ~False, ...} is a tautology
                        cachedIsTautologyResult = true;
                    }
                }
                // If we still don't know
                if (cachedIsTautologyResult == null)
                {
                    if (SetOps.intersection(cachedPositiveSymbols, cachedNegativeSymbols).Size() > 0)
                    {
                        // We have:
                        // P | ~P
                        // which is always true.
                        cachedIsTautologyResult = true;
                    }
                    else
                    {
                        cachedIsTautologyResult = false;
                    }
                }
            }

            return(cachedIsTautologyResult);
        }
Exemple #2
0
        public List <Symbol> getImpureSymbolsIn(Sentence sentence)
        {
            List <Symbol> allNegatives = getNegativeSymbolsIn(sentence);
            List <Symbol> allPositives = getPositiveSymbolsIn(sentence);

            return(SetOps.intersection(allPositives, allNegatives));
        }
Exemple #3
0
        public void testIntersection()
        {
            ISet <int> intersection = SetOps.intersection(s1, s2);

            Assert.AreEqual(1, intersection.Size());
            Assert.AreEqual(4, s1.Size());
            Assert.AreEqual(3, s2.Size());

            s1.Remove(1);
            Assert.AreEqual(1, intersection.Size());
            Assert.AreEqual(3, s1.Size());
            Assert.AreEqual(3, s2.Size());
        }
Exemple #4
0
        public void testIntersection()
        {
            List <int> intersection = SetOps.intersection(s1, s2);

            Assert.AreEqual(1, intersection.Count);
            Assert.AreEqual(4, s1.Count);
            Assert.AreEqual(3, s2.Count);

            s1.Remove(1);
            Assert.AreEqual(1, intersection.Count);
            Assert.AreEqual(3, s1.Count);
            Assert.AreEqual(3, s2.Count);
        }
Exemple #5
0
            public ClauseSymbols(Sentence clause1, Sentence clause2)
            {
                SymbolClassifier classifier = new SymbolClassifier();

                clause1Symbols         = classifier.getSymbolsIn(clause1);
                clause1PositiveSymbols = classifier.getPositiveSymbolsIn(clause1);
                clause1NegativeSymbols = classifier.getNegativeSymbolsIn(clause1);

                clause2Symbols         = classifier.getSymbolsIn(clause2);
                clause2PositiveSymbols = classifier.getPositiveSymbolsIn(clause2);
                clause2NegativeSymbols = classifier.getNegativeSymbolsIn(clause2);

                positiveInClause1NegativeInClause2 = SetOps.intersection(
                    clause1PositiveSymbols, clause2NegativeSymbols);
                negativeInClause1PositiveInClause2 = SetOps.intersection(
                    clause1NegativeSymbols, clause2PositiveSymbols);
            }
        public void testPLResolveWithTwoLiteralsMatching()
        {
            Clause        one      = Util.first(ConvertToConjunctionOfClauses.convert(parser.parse("~P21 | B11")).getClauses());
            Clause        two      = Util.first(ConvertToConjunctionOfClauses.convert(parser.parse("~B11 | P21 | P12")).getClauses());
            ISet <Clause> expected = ConvertToConjunctionOfClauses.convert(parser.parse("(P12 | P21 | ~P21) & (B11 | P12 | ~B11)")).getClauses();

            ISet <Clause> resolvents = resolution.plResolve(one, two);

            int numberExpectedResolvents = 2;

            if (resolution.isDiscardTautologies())
            {
                numberExpectedResolvents = 0; // due to being tautologies
            }
            Assert.AreEqual(numberExpectedResolvents, resolvents.Size());
            Assert.AreEqual(numberExpectedResolvents, SetOps.intersection(expected, resolvents).Size());
        }
Exemple #7
0
        //
        // PRIVATE METHODS
        //

        private List <Sentence> filterOutClausesWithTwoComplementaryLiterals(
            List <Sentence> clauses)
        {
            List <Sentence>  filtered   = new List <Sentence>();
            SymbolClassifier classifier = new SymbolClassifier();

            foreach (Sentence clause in clauses)
            {
                List <Symbol> positiveSymbols = classifier
                                                .getPositiveSymbolsIn(clause);
                List <Symbol> negativeSymbols = classifier
                                                .getNegativeSymbolsIn(clause);
                if ((SetOps.intersection(positiveSymbols, negativeSymbols).Count == 0))
                {
                    filtered.Add(clause);
                }
            }
            return(filtered);
        }
Exemple #8
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);
            }
        }
Exemple #9
0
        protected void resolvePositiveWithNegative(Clause c1, Clause c2, ISet <Clause> resolvents)
        {
            // Calculate the complementary positive literals from c1 with
            // the negative literals from c2
            ISet <PropositionSymbol> complementary = SetOps.intersection(c1.getPositiveSymbols(), c2.getNegativeSymbols());

            // Construct a resolvent clause for each complement found
            foreach (PropositionSymbol complement in complementary)
            {
                ICollection <Literal> resolventLiterals = CollectionFactory.CreateQueue <Literal>();
                // Retrieve the literals from c1 that are not the complement
                foreach (Literal c1l in c1.getLiterals())
                {
                    if (c1l.isNegativeLiteral() ||
                        !c1l.getAtomicSentence().Equals(complement))
                    {
                        resolventLiterals.Add(c1l);
                    }
                }
                // Retrieve the literals from c2 that are not the complement
                foreach (Literal c2l in c2.getLiterals())
                {
                    if (c2l.isPositiveLiteral() ||
                        !c2l.getAtomicSentence().Equals(complement))
                    {
                        resolventLiterals.Add(c2l);
                    }
                }
                // Construct the resolvent clause
                Clause resolvent = new Clause(resolventLiterals);
                // Discard tautological clauses if this optimization is turned on.
                if (!(isDiscardTautologies() && resolvent.isTautology().Value))
                {
                    resolvents.Add(resolvent);
                }
            }
        }
Exemple #10
0
        private ICollection <IAction> plan = CollectionFactory.CreateFifoQueue <IAction>(); // FIFOQueue

        /**
         * function HYBRID-WUMPUS-AGENT(percept) returns an action<br>
         *
         * @param percept
         *            a list, [stench, breeze, glitter, bump, scream]
         *
         * @return an action the agent should take.
         */
        public override IAction Execute(IPercept percept)
        {
            // TELL(KB, MAKE-PERCEPT-SENTENCE(percept, t))
            kb.makePerceptSentence((AgentPercept)percept, t);
            // TELL the KB the temporal "physics" sentences for time t
            kb.tellTemporalPhysicsSentences(t);

            AgentPosition current = kb.askCurrentPosition(t);

            // safe <- {[x, y] : ASK(KB, OK<sup>t</sup><sub>x,y</sub>) = true}
            ISet <Room> safe = kb.askSafeRooms(t);

            // if ASK(KB, Glitter<sup>t</sup>) = true then
            if (kb.askGlitter(t))
            {
                // plan <- [Grab] + PLAN-ROUTE(current, {[1,1]}, safe) + [Climb]
                ISet <Room> goals = CollectionFactory.CreateSet <Room>();
                goals.Add(new Room(1, 1));

                plan.Add(new Grab());
                plan.AddAll(planRoute(current, goals, safe));
                plan.Add(new Climb());
            }

            // if plan is empty then
            // unvisited <- {[x, y] : ASK(KB, L<sup>t'</sup><sub>x,y</sub>) = false
            // for all t' &le; t}
            ISet <Room> unvisited = kb.askUnvisitedRooms(t);

            if (plan.IsEmpty())
            {
                // plan <- PLAN-ROUTE(current, unvisited &cap; safe, safe)
                plan.AddAll(planRoute(current, SetOps.intersection(unvisited, safe), safe));
            }

            // if plan is empty and ASK(KB, HaveArrow<sup>t</sup>) = true then
            if (plan.IsEmpty() && kb.askHaveArrow(t))
            {
                // possible_wumpus <- {[x, y] : ASK(KB, ~W<sub>x,y</sub>) = false}
                ISet <Room> possibleWumpus = kb.askPossibleWumpusRooms(t);
                // plan <- PLAN-SHOT(current, possible_wumpus, safe)
                plan.AddAll(planShot(current, possibleWumpus, safe));
            }

            // if plan is empty then //no choice but to take a risk
            if (plan.IsEmpty())
            {
                // not_unsafe <- {[x, y] : ASK(KB, ~OK<sup>t</sup><sub>x,y</sub>) =
                // false}
                ISet <Room> notUnsafe = kb.askNotUnsafeRooms(t);
                // plan <- PLAN-ROUTE(current, unvisited &cap; not_unsafe, safe)
                plan.AddAll(planRoute(current, SetOps.intersection(unvisited, notUnsafe), safe));
            }

            // if plan is empty then
            if (plan.IsEmpty())
            {
                // plan PLAN-ROUTE(current, {[1,1]}, safe) + [Climb]
                ISet <Room> start = CollectionFactory.CreateSet <Room>();
                start.Add(new Room(1, 1));
                plan.AddAll(planRoute(current, start, safe));
                plan.Add(new Climb());
            }
            // action <- POP(plan)
            IAction action = plan.Pop();

            // TELL(KB, MAKE-ACTION-SENTENCE(action, t))
            kb.makeActionSentence(action, t);
            // t <- t+1
            t = t + 1;
            // return action
            return(action);
        }