Пример #1
0
    private Policy BestRemoval(LogicalForm l)
    {
        // first, we find what supports l.
        if (!activeRules.ContainsKey(l))
        {
            // this means l was not inferred.
            // What should we do here?
            return(new Remove(l));
        }

        foreach (Rule r in activeRules[l].Item2)
        {
            HashSet <LogicalForm>   top = r.GetTop();
            HashSet <LogicalForm>[] bot = r.GetBottom();

            // all the rules where l is on bottom
            // we can either side-step or remove a top one
            Policy bestRemoval = null;
            Policy bestShift   = null;

            if (top.Count > 0)
            {
                bestRemoval = BestRemoval(top.First());

                // determine best top to remove
                foreach (LogicalForm parent in top)
                {
                    Policy contender = BestRemoval(parent);
                    if (CompareLikelihood(bestRemoval, contender) < 0)
                    {
                        bestRemoval = contender;
                    }
                }
            }

            // THIS IS ALL THE SIDE-STEP STUFF
            for (int i = 0; i < bot.Length; i++)
            {
                HashSet <LogicalForm> currentTier = bot[i];

                foreach (LogicalForm sibling in currentTier)
                {
                    if (!Satisfies(sibling.Negate()))
                    {
                        if (bestShift == null)
                        {
                            bestShift = BestAddition(sibling);
                        }
                        else
                        {
                            int comparitor = CompareLikelihood(bestShift, BestAddition(sibling));
                            if (comparitor < 0)
                            {
                                bestShift = BestAddition(sibling);
                            }
                        }
                    }
                }

                if (bestShift != null)
                {
                    break;
                }
            }

            // bestRemoval = bestRemoval.Concat(new Remove(l));

            bestShift = bestRemoval.GetDual().Concat(bestShift);

            Policy winner = (CompareLikelihood(bestRemoval, bestShift) > 0) ? bestRemoval : bestShift;

            return(winner.Concat(new Remove(l)));
        }

        return(null);
        // TODO collect best results from different rules
    }