AIMA.Core.Logic.FOL.Inference.IndexedFarParents.attemptReduction C# (CSharp) Method

attemptReduction() public method

public attemptReduction ( Chain nearParent, int farParentIndex ) : Chain
nearParent AIMA.Core.Logic.FOL.KB.Data.Chain
farParentIndex int
return AIMA.Core.Logic.FOL.KB.Data.Chain
        public Chain attemptReduction(Chain nearParent, int farParentIndex)
        {
            Chain nnpc = null;

            Literal nearLiteral = nearParent.getHead();

            Dictionary<String, List<Chain>> candidateHeads = null;
            if (nearLiteral.isPositiveLiteral())
            {
                candidateHeads = negHeads;
            }
            else
            {
                candidateHeads = posHeads;
            }

            AtomicSentence nearAtom = nearLiteral.getAtomicSentence();
            String nearestKey = nearAtom.getSymbolicName();
            List<Chain> farParents = candidateHeads[nearestKey];
            if (null != farParents)
            {
                Chain farParent = farParents[farParentIndex];
                standardizeApart(farParent);
                Literal farLiteral = farParent.getHead();
                AtomicSentence farAtom = farLiteral.getAtomicSentence();
                Dictionary<Variable, Term> subst = unifier.unify(nearAtom, farAtom);

                // If I was able to unify with one
                // of the far heads
                if (null != subst)
                {
                    // Want to always apply reduction uniformly
                    Chain topChain = farParent;
                    Literal botLit = nearLiteral;
                    Chain botChain = nearParent;

                    // Need to apply subst to all of the
                    // literals in the reduction
                    List<Literal> reduction = new List<Literal>();
                    foreach (Literal l in topChain.getTail())
                    {
                        AtomicSentence atom = (AtomicSentence)substVisitor.subst(
                                subst, l.getAtomicSentence());
                        reduction.Add(l.newInstance(atom));
                    }
                    reduction.Add(new ReducedLiteral((AtomicSentence)substVisitor
                            .subst(subst, botLit.getAtomicSentence()), botLit
                            .isNegativeLiteral()));
                    foreach (Literal l in botChain.getTail())
                    {
                        AtomicSentence atom = (AtomicSentence)substVisitor.subst(
                                subst, l.getAtomicSentence());
                        reduction.Add(l.newInstance(atom));
                    }

                    nnpc = new Chain(reduction);
                    nnpc.setProofStep(new ProofStepChainReduction(nnpc, nearParent,
                            farParent, subst));
                }
            }

            return nnpc;
        }

Usage Example

Ejemplo n.º 1
0
        // Recursive Depth Limited Search
        private void recursiveDLS(int maxDepth, int currentDepth, Chain nearParent,
                IndexedFarParents indexedFarParents, AnswerHandler ansHandler)
        {

            // Keep track of the maximum depth reached.
            ansHandler.updateMaxDepthReached(currentDepth);

            if (currentDepth == maxDepth)
            {
                return;
            }

            int noCandidateFarParents = indexedFarParents
                    .getNumberCandidateFarParents(nearParent);
            if (null != tracer)
            {
                tracer.increment(currentDepth, noCandidateFarParents);
            }
            indexedFarParents.standardizeApart(nearParent);
            for (int farParentIdx = 0; farParentIdx < noCandidateFarParents; farParentIdx++)
            {
                // If have a complete answer, don't keep
                // checking candidate far parents
                if (ansHandler.isComplete())
                {
                    break;
                }

                // Reduction
                Chain nextNearParent = indexedFarParents.attemptReduction(
                        nearParent, farParentIdx);

                if (null == nextNearParent)
                {
                    // Unable to remove the head via reduction
                    continue;
                }

                // Handle Canceling and Dropping
                bool cancelled = false;
                bool dropped = false;
                do
                {
                    cancelled = false;
                    Chain nextParent = null;
                    while (nextNearParent != (nextParent = tryCancellation(nextNearParent)))
                    {
                        nextNearParent = nextParent;
                        cancelled = true;
                    }

                    dropped = false;
                    while (nextNearParent != (nextParent = tryDropping(nextNearParent)))
                    {
                        nextNearParent = nextParent;
                        dropped = true;
                    }
                } while (dropped || cancelled);

                // Check if have answer before
                // going to the next level
                if (!ansHandler.isAnswer(nextNearParent))
                {
                    // Keep track of the current # of
                    // far parents that are possible for the next near parent.
                    int noNextFarParents = indexedFarParents
                            .getNumberFarParents(nextNearParent);
                    // Add to indexed far parents
                    nextNearParent = indexedFarParents.addToIndex(nextNearParent);

                    // Check the next level
                    recursiveDLS(maxDepth, currentDepth + 1, nextNearParent,
                            indexedFarParents, ansHandler);

                    // Reset the number of far parents possible
                    // when recursing back up.
                    indexedFarParents.resetNumberFarParentsTo(nextNearParent,
                            noNextFarParents);
                }
            }
        }
All Usage Examples Of AIMA.Core.Logic.FOL.Inference.IndexedFarParents::attemptReduction