Example #1
0
        public void Difference()
        {
            int[]           oddNumbers = new int[] { 1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25 };
            int[]           digits     = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
            HashedSet <int> setOdds    = new HashedSet <int>(oddNumbers);
            HashedSet <int> setDigits  = new HashedSet <int>(digits);

            setOdds.ExceptWith(setDigits);

            int[] expectedArray1 = new int[] { 11, 13, 15, 17, 19, 21, 23, 25 };

            int[] actualArray = setOdds.ToArray();
            Array.Sort(actualArray);
            CollectionAssert.AreEqual(expectedArray1, actualArray);

            setOdds = new HashedSet <int>(oddNumbers);

            setDigits.ExceptWith(setOdds);

            int[] expectedArray2 = new int[] { 2, 4, 6, 8 };

            actualArray = setDigits.ToArray();
            Array.Sort(actualArray);
            CollectionAssert.AreEqual(expectedArray2, actualArray);

            setOdds.ExceptWith(setOdds);
            Assert.AreEqual(0, setOdds.Count);
        }
        public IInferenceResult Ask(FOLKnowledgeBase kb, ISentence aQuery)
        {
            //
            // Get the background knowledge - are assuming this is satisfiable
            // as using Set of Support strategy.
            ISet <Clause> bgClauses = new HashedSet <Clause>(kb.GetAllClauses());

            bgClauses.ExceptWith(SubsumptionElimination.FindSubsumedClauses(bgClauses));
            IList <Chain> background = CreateChainsFromClauses(bgClauses);

            // Collect the information necessary for constructing
            // an answer (supports use of answer literals).
            var ansHandler = new AnswerHandler(kb, aQuery, this.MaxQueryTime);

            var ifps = new IndexedFarParents(ansHandler
                                             .GetSetOfSupport(), background);

            // Iterative deepening to be used
            for (int maxDepth = 1; maxDepth < int.MaxValue; maxDepth++)
            {
                // Track the depth actually reached
                ansHandler.ResetMaxDepthReached();

                if (null != tracer)
                {
                    tracer.Reset();
                }

                foreach (Chain nearParent in ansHandler.GetSetOfSupport())
                {
                    this.RecursiveDls(maxDepth, 0, nearParent, ifps, ansHandler);
                    if (ansHandler.IsComplete())
                    {
                        return(ansHandler);
                    }
                }
                // This means the search tree
                // has bottomed out (i.e. finite).
                // Return what I know based on exploring everything.
                if (ansHandler.GetMaxDepthReached() < maxDepth)
                {
                    return(ansHandler);
                }
            }

            return(ansHandler);
        }
Example #3
0
        public IInferenceResult Ask(FOLKnowledgeBase KB, ISentence alpha) 
        {
            ISet<Clause> sos = new HashedSet<Clause>();
            ISet<Clause> usable = new HashedSet<Clause>();

            // Usable set will be the set of clauses in the KB,
            // are assuming this is satisfiable as using the
            // ISet of Support strategy.
            //foreach (var standardizedC in KB.GetAllClauses().Select(c => KB.StandardizeApart(c)))
            foreach (var standardizedC in KB.GetAllClauses())
            {
                standardizedC.SetStandardizedApartCheckNotRequired();
                usable.UnionWith(standardizedC.GetFactors());
            }

            // Ensure reflexivity axiom is added to usable if using paramodulation.
            if (this.UseParamodulation) 
            {
                // Reflexivity Axiom: x = x
                var reflexivityAxiom = new TermEquality(new Variable("x"), new Variable("x"));
                var reflexivityClause = new Clause();
                reflexivityClause.AddLiteral(new Literal(reflexivityAxiom));
                reflexivityClause = KB.StandardizeApart(reflexivityClause);
                reflexivityClause.SetStandardizedApartCheckNotRequired();
                usable.Add(reflexivityClause);
            }

            ISentence notAlpha = new NotSentence(alpha);
            // Want to use an answer literal to pull
            // query variables where necessary
            var answerLiteral = KB.CreateAnswerLiteral(notAlpha);
            var answerLiteralVariables = KB
                    .CollectAllVariables(answerLiteral.AtomicSentence);
            var answerClause = new Clause();

            if (answerLiteralVariables.Count > 0) {
                ISentence notAlphaWithAnswer = new ConnectedSentence(Connectors.Or,
                        notAlpha, answerLiteral.AtomicSentence);
               // foreach (var standardizedC in
              //      KB.ConvertToClauses(notAlphaWithAnswer).Select(KB.StandardizeApart))
                  foreach (var standardizedC in
                      KB.ConvertToClauses(notAlphaWithAnswer))
                   {
                       Clause c = KB.StandardizeApart(standardizedC);
                       c.SetProofStep(new ProofStepGoal(c));
                       c.SetStandardizedApartCheckNotRequired();
                       sos.UnionWith(c.GetFactors());
                   }

                answerClause.AddLiteral(answerLiteral);
            } 
            else 
            {
                //foreach (var standardizedC in
                //    KB.ConvertToClauses(notAlpha).Select(KB.StandardizeApart)) 
                foreach (var standardizedC in
                    KB.ConvertToClauses(notAlpha))
                {
                    Clause c = KB.StandardizeApart(standardizedC);
                    c.SetProofStep(new ProofStepGoal(c));
                    c.SetStandardizedApartCheckNotRequired();
                    sos.UnionWith(standardizedC.GetFactors());
                }
            }

            // Ensure all subsumed clauses are removed
            usable.ExceptWith(SubsumptionElimination.FindSubsumedClauses(usable));
            sos.ExceptWith(SubsumptionElimination.FindSubsumedClauses(sos));

            var ansHandler = new OTTERAnswerHandler(answerLiteral,
                    answerLiteralVariables, answerClause, this.MaxQueryTime);

            var idxdClauses = new IndexedClauses(LightestClauseHeuristic, sos, usable);

            return this.Otter(ansHandler, idxdClauses, sos, usable);
        }
        public void Difference()
        {
            int[] oddNumbers = new int[] { 1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25 };
            int[] digits = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
            HashedSet<int> setOdds = new HashedSet<int>(oddNumbers);
            HashedSet<int> setDigits = new HashedSet<int>(digits);

            setOdds.ExceptWith(setDigits);

            int[] expectedArray1 = new int[] { 11, 13, 15, 17, 19, 21, 23, 25 };

            int[] actualArray = setOdds.ToArray();
            Array.Sort(actualArray);
            CollectionAssert.AreEqual(expectedArray1, actualArray);

            setOdds = new HashedSet<int>(oddNumbers);

            setDigits.ExceptWith(setOdds);

            int[] expectedArray2 = new int[] { 2, 4, 6, 8 };

            actualArray = setDigits.ToArray();
            Array.Sort(actualArray);
            CollectionAssert.AreEqual(expectedArray2, actualArray);

            setOdds.ExceptWith(setOdds);
            Assert.AreEqual(0, setOdds.Count);
        }