コード例 #1
0
 private CompoundFormula AddKnowledgePredicatesToFormula(Formula f, List<Predicate> lKnowPredicates)
 {
     CompoundFormula cf = new CompoundFormula("and");
     List<Predicate> lPredicates = GetAllPredicates(f);
     foreach (Predicate p in lPredicates)
     {
         if (lKnowPredicates.Contains(p))
             cf.AddOperand(new PredicateFormula(new KnowPredicate(p)));
     }
     cf.AddOperand(f);
     return cf;
 }
コード例 #2
0
        public Action AddKnowledge(List<string> lAlwaysKnown)
        {
            Action aNew = Clone();

            CompoundFormula cfPreconditions = new CompoundFormula("and");
            HashSet<Predicate> lKnowPreconditions = new HashSet<Predicate>();
            if (Preconditions != null)
            {
                Preconditions.GetAllPredicates(lKnowPreconditions);
                cfPreconditions.AddOperand(Preconditions);
                foreach (Predicate p in lKnowPreconditions)
                    if (!lAlwaysKnown.Contains(p.Name))
                        cfPreconditions.AddOperand(new PredicateFormula(new KnowPredicate(p)));
                aNew.Preconditions = cfPreconditions;
            }
            if (Effects != null)
            {
                HashSet<Predicate> lKnowEffects = new HashSet<Predicate>();
                CompoundFormula cfEffects = new CompoundFormula("and");
                Effects.GetAllPredicates(lKnowEffects);
                cfEffects.AddOperand(Effects.Clone());

                foreach (Predicate p in lKnowEffects)
                    if (!lAlwaysKnown.Contains(p.Name))
                        cfEffects.AddOperand(new PredicateFormula(new KnowPredicate(p)));

                aNew.Effects = cfEffects;
            }
            if (Observe != null)
            {
                if (aNew.Effects == null)
                    aNew.Effects = new CompoundFormula("and");
                Predicate pObserve = ((PredicateFormula)Observe).Predicate;
                ((CompoundFormula)aNew.Effects).AddOperand(new KnowPredicate(pObserve));
            }
            return aNew;
        }
コード例 #3
0
        /*

        private bool RegressLandmark(Predicate pCurrentLandmark, List<Action> lLandmarkAchievers, HashSet<Predicate> lInitialState,
            Dictionary<string, List<Predicate>> dObligatory, Dictionary<string, List<Predicate>> dOptional)
        {

            if (lLandmarkAchievers.Count == 0)
                return false;
            //looking for preconditions that are needed for all actions that achieve p
            HashSet<Predicate> lJointPreconditions = GetJointPreconditions(lLandmarkAchievers);
            bool bNewLandmarkFound = false;
            if (lJointPreconditions.Count > 0)
            {
                foreach (GroundedPredicate pPrecondition in lJointPreconditions)
                {
                    if (!lInitialState.Contains(pPrecondition))
                    {
                        string sPreconditionName = GetNameAndTag(pPrecondition);
                        if (!dObligatory.ContainsKey(sPreconditionName))
                            dObligatory[sPreconditionName] = new List<Predicate>();

                        dObligatory[sPreconditionName].Add(pPrecondition);
                        bNewLandmarkFound = true;
                    }
                }

            }

            Dictionary<string, Dictionary<string, List<Predicate>>> dDisjunctions = GetDisjunctivePreconditions(lLandmarkAchievers, lInitialState);
            foreach (string sPredicateType in dDisjunctions.Keys)//go over the different pre types
            {
                Dictionary<string, List<Predicate>> dActionToPrecondition = dDisjunctions[sPredicateType];
                if (dActionToPrecondition.Count == lLandmarkAchievers.Count)//all achivers require a pre of this type
                {
                    if (!dOptional.ContainsKey(sPredicateType))
                        dOptional[sPredicateType] = new List<Predicate>();
                    foreach (KeyValuePair<string, List<Predicate>> pair in dActionToPrecondition)
                    {
                        foreach (Predicate p in pair.Value)
                        {
                            if (!lInitialState.Contains(p))
                            {
                                dOptional[sPredicateType].Add(p);
                            }
                        }
                    }
                }
            }

            return true;
        }

        private List<Formula> RegressLandmark(Formula fCurrentLandmark, Dictionary<Predicate, List<Action>> dLandmarkAchievers, HashSet<Predicate> lInitialState)
        {
            //when landmark is (or p1 p2), and p1 requires (or q1 q2) (or r1 r2) and p2 requires p3 and r3, then the result should be (or q1 q2 q3) (or r1 r2 r3)
            List<Formula> lFormulas = new List<Formula>();
            Dictionary<string, Dictionary<int, List<Predicate>>> dAllObligatory = new Dictionary<string, Dictionary<int, List<Predicate>>>();
            Dictionary<string, Dictionary<int, List<Predicate>>> dAllOptional = new Dictionary<string, Dictionary<int, List<Predicate>>>();
            int cPredicatesToAchieve = 0;
            foreach (GroundedPredicate pLandmark in dLandmarkAchievers.Keys)
            {
                List<Action> lActions = dLandmarkAchievers[pLandmark];
                Dictionary<string, List<Predicate>> dObligatory = new Dictionary<string, List<Predicate>>();
                Dictionary<string, List<Predicate>> dOptional = new Dictionary<string, List<Predicate>>();
                RegressLandmark(pLandmark, dLandmarkAchievers[pLandmark], lInitialState, dObligatory, dOptional);
                string sLandmarkName = GetNameAndTag(pLandmark);
                foreach (string sPreType in dObligatory.Keys)
                {
                    if (!dAllObligatory.ContainsKey(sPreType))
                        dAllObligatory[sPreType] = new Dictionary<int, List<Predicate>>();

                    dAllObligatory[sPreType][cPredicatesToAchieve] = new List<Predicate>();
                    dAllObligatory[sPreType][cPredicatesToAchieve].AddRange(dObligatory[sPreType]);
                }
                foreach (string sPreType in dOptional.Keys)
                {
                    if (!dAllOptional.ContainsKey(sPreType))
                        dAllOptional[sPreType] = new Dictionary<int, List<Predicate>>();

                    dAllOptional[sPreType][cPredicatesToAchieve] = new List<Predicate>();
                    dAllOptional[sPreType][cPredicatesToAchieve].AddRange(dOptional[sPreType]);
                }
                cPredicatesToAchieve++;
            }
            foreach (string sPreType in dAllObligatory.Keys)
            {
                if (dAllObligatory[sPreType].Count == cPredicatesToAchieve)
                {
                    CompoundFormula cfOr = new CompoundFormula("or");
                    foreach (List<Predicate> lPreconditions in dAllObligatory[sPreType].Values)
                    {
                        CompoundFormula cfAnd = new CompoundFormula("and");
                        foreach (Predicate p in lPreconditions)
                            cfAnd.AddOperand(p);
                        cfOr.AddOperand(cfAnd);
                    }
                    Formula fSimplified = cfOr.Simplify();
                    //4 options here:
                    //fSimplified is a single predicate
                    //fSimplified is a simple disjunction
                    //fSimplified is a simple conjunction
                    //fSimplified is a disjunction of simple conjunctions
                    if (fSimplified is PredicateFormula)
                        lFormulas.Add(fSimplified);
                    else
                    {
                        CompoundFormula cf = (CompoundFormula)fSimplified;
                        if (cf.Operator == "and")
                        {
                            foreach (PredicateFormula pf in cf.Operands)
                                lFormulas.Add(pf);
                        }
                        else
                            lFormulas.Add(RemoveConjunctions(cf));

                    }
                }
            }
            foreach (string sPreType in dAllOptional.Keys)
            {
                if (dAllOptional[sPreType].Count == cPredicatesToAchieve)
                {
                    CompoundFormula cfOr = new CompoundFormula("or");
                    foreach (List<Predicate> lPreconditions in dAllOptional[sPreType].Values)
                    {
                        CompoundFormula cfAnd = new CompoundFormula("or");
                        foreach (Predicate p in lPreconditions)
                            cfAnd.AddOperand(p);
                        cfOr.AddOperand(cfAnd);
                    }
                    Formula fSimplified = cfOr.Simplify();
                    //4 options here:
                    //fSimplified is a single predicate
                    //fSimplified is a simple disjunction
                    //fSimplified is a simple conjunction
                    //fSimplified is a disjunction of simple conjunctions
                    if (fSimplified is PredicateFormula)
                        lFormulas.Add(fSimplified);
                    else
                    {
                        CompoundFormula cf = (CompoundFormula)fSimplified;
                        if (cf.Operator == "and")
                        {
                            foreach (PredicateFormula pf in cf.Operands)
                                lFormulas.Add(pf);
                        }
                        else
                            lFormulas.Add(RemoveConjunctions(cf));

                    }
                }
            }

            return lFormulas;
        }

         */
        private CompoundFormula RemoveConjunctions(CompoundFormula cf)
        {
            CompoundFormula cfNew = new CompoundFormula("or");
            if (cf.Operator == "and")
            {
                foreach (PredicateFormula pf in cf.Operands)
                    cfNew.AddOperand(pf);
            }
            else
            {
                foreach (Formula fSub in cf.Operands)
                {
                    if (fSub is PredicateFormula)
                        cfNew.AddOperand(fSub);
                    else
                        cfNew.AddOperand(RemoveConjunctions((CompoundFormula)fSub));
                }
            }
            return cfNew;
        }
コード例 #4
0
        private List<Formula> RegressLandmark(Formula fCurrentLandmark, Dictionary<Predicate, List<Action>> dLandmarkAchievers, HashSet<Predicate> lInitialState)
        {
            //when landmark is (or p1 p2), and p1 requires (or q1 q2) (or r1 r2) and p2 requires p3 and r3, then the result should be (or q1 q2 q3) (or r1 r2 r3)
            List<Formula> lFormulas = new List<Formula>();
            Dictionary<string, CompoundFormula> dAll = null;
            int cPredicatesToAchieve = 0;
            foreach (GroundedPredicate pLandmark in dLandmarkAchievers.Keys)
            {
                List<Action> lActions = dLandmarkAchievers[pLandmark];
                Dictionary<string, CompoundFormula> dRegressedFormulas = new Dictionary<string, CompoundFormula>();
                RegressLandmark(pLandmark, dLandmarkAchievers[pLandmark], lInitialState, dRegressedFormulas);
                string sLandmarkName = GetNameAndTag(pLandmark);
                if (dAll == null)
                    dAll = dRegressedFormulas;
                else
                {
                    Dictionary<string, CompoundFormula> dNew = new Dictionary<string, CompoundFormula>();
                    foreach (string sKey in dAll.Keys)
                    {
                        if (dRegressedFormulas.ContainsKey(sKey))
                        {
                            CompoundFormula cfOr = new CompoundFormula("or");
                            cfOr.AddOperand(dAll[sKey]);
                            cfOr.AddOperand(dRegressedFormulas[sKey]);
                            dNew[sKey] = cfOr;
                        }
                    }
                    dAll = dNew;
                }
            }
            foreach (string sPreType in dAll.Keys)
            {
                    Formula fSimplified = dAll[sPreType].Simplify();
                    //4 options here:
                    //fSimplified is a single predicate
                    //fSimplified is a simple disjunction
                    //fSimplified is a simple conjunction
                    //fSimplified is a disjunction of simple conjunctions
                    if (fSimplified is PredicateFormula)
                        lFormulas.Add(fSimplified);
                    else
                    {
                        CompoundFormula cf = (CompoundFormula)fSimplified;
                        if (cf.Operator == "and")
                        {
                            foreach (PredicateFormula pf in cf.Operands)
                                lFormulas.Add(pf);
                        }
                        else
                            lFormulas.Add(RemoveConjunctions(cf));

                    }

            }

            return lFormulas;
        }
コード例 #5
0
 public override Formula RemoveNegations()
 {
     CompoundFormula cf = new CompoundFormula(Operator);
     foreach (Formula f in Operands)
     {
         Formula fRemoved = f.RemoveNegations();
         if (f != null)
         {
             cf.AddOperand(fRemoved);
         }
     }
     return cf;
 }
コード例 #6
0
 public override Formula Regress(Action a)
 {
     CompoundFormula cfNew = new CompoundFormula(Operator);
     foreach (Formula f in Operands)
     {
         //Formula fRgressed = f.Regress(a, lObserved);
         Formula fRgressed = f.Regress(a);
         //what happens if we get  "TRUE" or "FALSE"?
         cfNew.AddOperand(fRgressed);
     }
     return cfNew.Simplify();
 }
コード例 #7
0
        public override Formula PartiallyGround(Dictionary<string, Constant> dBindings)
        {
            CompoundFormula cfGrounded = new CompoundFormula(Operator);
            foreach (Formula fSub in Operands)
            {
                Formula fGrounded = fSub.PartiallyGround(dBindings);
                if (fGrounded is PredicateFormula)
                {
                    Predicate p = ((PredicateFormula)fGrounded).Predicate;
                    if (p.Name == Domain.TRUE_PREDICATE)
                    {
                        if (Operator == "and")
                            continue;
                        else if (Operator == "or")
                            return fGrounded;
                        else
                            throw new NotImplementedException();
                    }
                    if (p.Name == Domain.FALSE_PREDICATE)
                    {
                        if (Operator == "and")
                            return fGrounded;
                        else if (Operator == "or")
                            continue;
                        else if (Operator == "when")
                        {
                            return null;
                        }
                        else
                            throw new NotImplementedException();
                    }

                }
                cfGrounded.AddOperand(fGrounded);
                if (cfGrounded.IsFalse(null))
                    return new PredicateFormula(new GroundedPredicate(Domain.FALSE_PREDICATE));
            }
            return cfGrounded;
        }
コード例 #8
0
 public override Formula GetKnowledgeFormula(List<string> lAlwaysKnown, bool bKnowWhether)
 {
     CompoundFormula cfK = new CompoundFormula(Operator);
     foreach (Formula f in Operands)
         cfK.AddOperand(f.GetKnowledgeFormula(lAlwaysKnown, bKnowWhether));
     return cfK;
 }
コード例 #9
0
 private Formula ReduceAnd()
 {
     List<Predicate> lObligatory = new List<Predicate>();
     foreach (Formula f in Operands)
     {
         if (f is PredicateFormula)
             lObligatory.Add(((PredicateFormula)f).Predicate);
     }
     if (lObligatory.Count == 0)
         return this;
     CompoundFormula cfAnd = new CompoundFormula("and");
     foreach (Formula f in Operands)
     {
         if (f is PredicateFormula)
             cfAnd.AddOperand(f);
         else
         {
             CompoundFormula cf = (CompoundFormula)f;
             Formula fReduce = cf.Reduce(lObligatory);
             if (fReduce is PredicateFormula)
             {
                 if (((PredicateFormula)fReduce).Predicate.Name == Domain.FALSE_PREDICATE)
                     return fReduce;
                 else if (((PredicateFormula)fReduce).Predicate.Name != Domain.TRUE_PREDICATE)
                     cfAnd.AddOperand(fReduce);
             }
             else
                 cfAnd.AddOperand(fReduce);
         }
     }
     return cfAnd;
 }
コード例 #10
0
        private CompoundFormula InsertGiven(Formula fGiven, out bool bInserted)
        {
            CompoundFormula cfNew = new CompoundFormula(Operator);
            bInserted = false;
            if (Operator == "when")
            {
                CompoundFormula cfAnd = new CompoundFormula("and");
                cfAnd.AddOperand(Operands[0]);
                cfAnd.AddOperand(fGiven);
                cfNew.AddOperand(cfAnd);
                cfNew.AddOperand(Operands[1]);
                bInserted = true;
            }
            else if (Operator == "and")
            {
                foreach (Formula f in Operands)
                {
                    if (f is PredicateFormula)
                        cfNew.AddOperand(f);
                    else
                    {
                        CompoundFormula cf = (CompoundFormula)f;
                        cfNew.AddOperand(cf.InsertGiven(fGiven, out bInserted));
                    }

                }

            }
            else if (Operator == "not")
            {
                return this;//when cannot be inside a negation (I think)
            }
            else
                throw new NotImplementedException();
            return cfNew;
        }
コード例 #11
0
        private List<Formula> GetAllCombinations(List<Formula> lConverted, int idx)
        {
            List<Formula> lCombinations = new List<Formula>();
            if (idx == lConverted.Count - 1)
            {
                if (lConverted[idx] is CompoundFormula)
                {
                    CompoundFormula cf = (CompoundFormula)lConverted[idx];
                    foreach (Formula fSub in cf.Operands)
                        lCombinations.Add(fSub);
                }
                else
                    lCombinations.Add(lConverted[idx]);
            }
            else
            {

                List<Formula> lRec = GetAllCombinations(lConverted, idx + 1);
                if (lConverted[idx] is CompoundFormula)
                {
                    CompoundFormula cf1 = (CompoundFormula)lConverted[idx];
                    foreach (Formula fSub1 in cf1.Operands)
                    {
                        foreach (Formula f2 in lRec)
                        {
                            CompoundFormula cfOr = new CompoundFormula("or");
                            cfOr.AddOperand(fSub1);
                            cfOr.AddOperand(f2);
                            if(!cfOr.IsTrue(null))
                                lCombinations.Add(cfOr);
                        }
                    }

                }
                else
                {
                    foreach (Formula f2 in lRec)
                    {
                        CompoundFormula cfOr = new CompoundFormula("or");
                        cfOr.AddOperand(lConverted[idx]);
                        cfOr.AddOperand(f2);
                        if (!cfOr.IsTrue(null))
                            lCombinations.Add(cfOr);
                    }

                }
            }

            return lCombinations;
        }
コード例 #12
0
 private bool ConditionDeletesPrecondition(out Formula fRest)
 {
     //BUGBUG: simplified and not full implementation
     fRest = null;
     if (Operator != "when")
         return false;
     Formula fPre = Operands[0];
     Formula fDelete = fPre.Negate().Simplify();
     bool bFound = false;
     CompoundFormula cfRest = new CompoundFormula("and");
     if (Operands[1] is CompoundFormula)
     {
         foreach (Formula f in ((CompoundFormula)Operands[1]).Operands)
         {
             if (f.Equals(fDelete))
                 bFound = true;
             else
                 cfRest.AddOperand(f);
         }
     }
     else
     {
         PredicateFormula f = (PredicateFormula)Operands[1];
         if (f.Equals(fDelete))
             bFound = true;
         else
             cfRest.AddOperand(f);
     }
     if (!bFound)
         return false;
     if (cfRest.Operands.Count == 1)
         fRest = cfRest.Operands[0];
     else
         fRest = cfRest;
     return true;
 }
コード例 #13
0
        // for every f->g replace f with g
        public CompoundFormula AddRemoveConditionalEffects(Dictionary<Formula, List<Predicate>> dEffects)
        {
            CompoundFormula cfNew = new CompoundFormula(Operator);
            if (Operator == "and") //bugbug - not updating more complex structures for now
            {
                List<Formula> lReplaceKeys = new List<Formula>();
                List<Formula> lReplaceNegates = new List<Formula>();
                foreach (Formula fOperand in Operands)
                {
                    if (dEffects.ContainsKey(fOperand))
                    {
                        lReplaceKeys.Add(fOperand);
                    }
                }

                if (lReplaceKeys.Count == 0 && lReplaceNegates.Count == 0)
                    return this;

                List<Predicate> lEffects = new List<Predicate>();
                foreach (Formula fReplace in lReplaceKeys)
                {
                    lEffects.AddRange(dEffects[fReplace]);
                }
                foreach (Formula fOperand in Operands)
                {
                    if (fOperand is PredicateFormula)
                    {
                        PredicateFormula pf = (PredicateFormula)fOperand;
                        if (!lReplaceNegates.Contains(fOperand))
                        {
                            if (!lEffects.Contains(pf.Predicate))
                            {
                                if (!lEffects.Contains(pf.Predicate.Negate()))
                                    cfNew.AddOperand(fOperand);
                            }
                        }
                    }
                    else
                    {
                        CompoundFormula cf = (CompoundFormula)fOperand;
                        CompoundFormula cfRevised = cf.AddRemoveConditionalEffects(dEffects);
                        cfNew.AddOperand(cfRevised);
                    }
                }
                foreach (Predicate p in lEffects)
                    cfNew.AddOperand(new PredicateFormula(p));
            }
            else
            {
                foreach (Formula fOperand in Operands)
                {
                    if (fOperand is PredicateFormula)
                    {
                        bool bContainsKey = dEffects.ContainsKey(fOperand);
                        Formula fNegate = fOperand.Negate();
                        bool bContainsNegate = dEffects.ContainsKey(fNegate);
                        if (bContainsKey || bContainsNegate)
                        {
                            CompoundFormula cfAnd = new CompoundFormula("and");

                            List<Predicate> lEffects = null;
                            if (bContainsKey)
                                lEffects = dEffects[fOperand];
                            if (bContainsNegate)
                                lEffects = dEffects[fNegate];
                            if (!lEffects.Contains(((PredicateFormula)fOperand).Predicate.Negate()))
                                cfAnd.AddOperand(fOperand);
                            foreach (Predicate p in lEffects)
                            {
                                cfAnd.AddOperand(new PredicateFormula(p));
                            }
                            if (bContainsNegate)
                                cfAnd = (CompoundFormula)cfAnd.Negate();
                            cfNew.AddOperand(cfAnd);
                        }
                        else
                            cfNew.AddOperand(fOperand);
                    }
                    else
                        cfNew.AddOperand(((CompoundFormula)fOperand).AddRemoveConditionalEffects(dEffects));
                }
            }
            return cfNew;
        }
コード例 #14
0
 //given f->g replace f everywhere with (~f or f and g)
 //possible problem - results in always true statements. When previous formula was f1 xor f2, and f1 was replaced, now the f1 revision is always true and f2 must be false
 private CompoundFormula ApplyConditionII(CompoundFormula cfCondition)
 {
     Formula fPremise = cfCondition.Operands[0];
     CompoundFormula cfConclusion = new CompoundFormula("and");
     cfConclusion.AddOperand(cfCondition.Operands[0]);
     cfConclusion.AddOperand(cfCondition.Operands[1]);
     Formula fNegatePremise = fPremise.Negate();
     CompoundFormula cfOr = new CompoundFormula("or");
     cfOr.AddOperand(fNegatePremise);
     cfOr.AddOperand(cfConclusion.Simplify());
     return (CompoundFormula)Replace(fPremise, cfOr);
 }
コード例 #15
0
 //given f->g replacing f with (f and g)
 private CompoundFormula ApplyCondition(CompoundFormula cfCondition)
 {
     Formula fPremise = cfCondition.Operands[0];
     CompoundFormula cfConclusion = new CompoundFormula("and");
     cfConclusion.AddOperand(cfCondition.Operands[0]);
     cfConclusion.AddOperand(cfCondition.Operands[1]);
     return (CompoundFormula)Replace(fPremise, cfConclusion.Simplify());
 }
コード例 #16
0
 public override Formula CreateRegression(Predicate p, int iChoice)
 {
     CompoundFormula cfNew = new CompoundFormula(Operator);
     foreach (Formula f in Operands)
         cfNew.AddOperand(f.CreateRegression(p, iChoice));
     return cfNew;
 }
コード例 #17
0
 public override Formula GenerateGiven(string sTag, List<string> lAlwaysKnown)
 {
     CompoundFormula cfGiven = new CompoundFormula(Operator);
     foreach (Formula fOperand in Operands)
         cfGiven.AddOperand(fOperand.GenerateGiven(sTag, lAlwaysKnown));
     return cfGiven;
 }
コード例 #18
0
        private Formula RemoveInvariants()
        {
            if(Operator != "and")
                return this;
            Predicate pPositiveAt = null;
            CompoundFormula cfAnd = new CompoundFormula("and");

            foreach (Formula fSub in Operands)
            {
                if (fSub is PredicateFormula)
                {
                    Predicate pSub = ((PredicateFormula)fSub).Predicate;
                    if (pSub.Name == "at")
                    {
                        if (!pSub.Negation)
                        {
                            if (pPositiveAt == null)
                            {
                                pPositiveAt = pSub;
                                cfAnd.AddOperand(pPositiveAt);
                            }
                            else
                                return new PredicateFormula(new GroundedPredicate(Domain.FALSE_PREDICATE));
                        }
                    }
                    else
                        cfAnd.AddOperand(pSub);
                }
                else
                    cfAnd.AddOperand(fSub);
            }
            if (pPositiveAt == null)
                return this;
            return cfAnd;//not simplifying because we always need to return a CompoundFormula
        }
コード例 #19
0
 public override Formula Ground(Dictionary<string, Constant> dBindings)
 {
     CompoundFormula cfGrounded = new CompoundFormula(Operator);
     foreach (Formula fSub in Operands)
     {
         Formula fGrounded = fSub.Ground(dBindings);
         cfGrounded.AddOperand(fGrounded);
     }
     return cfGrounded;
 }
コード例 #20
0
        /*
        public List<CompoundFormula> ToCNF()
        {
            List<CompoundFormula> lClauses = new List<CompoundFormula>();
            if (Operator == "or")
            {
                List<CompoundFormula> lCompoundOperands = new List<CompoundFormula>();
                List<PredicateFormula> lPredicateOperands = new List<PredicateFormula>();
                foreach (Formula f in Operands)
                {
                    if (f is CompoundFormula)
                        lCompoundOperands.Add((CompoundFormula)f);
                    else
                        lPredicateOperands.Add((PredicateFormula)f);
                }
                if (lCompoundOperands.Count == 0)
                {
                    lClauses.Add(this);
                }
                else if (lCompoundOperands.Count == 1)
                {
                    CompoundFormula cfAnd = lCompoundOperands[0];
                    if (cfAnd.Operator != "and")
                        throw new NotImplementedException();
                    foreach (Formula f in cfAnd.Operands)
                    {
                        if (f is PredicateFormula)
                        {
                            PredicateFormula pf = (PredicateFormula)f;
                            CompoundFormula cfOr = new CompoundFormula("or");
                            cfOr.AddOperand(pf);
                            foreach (PredicateFormula pfOrg in lPredicateOperands)
                                cfOr.AddOperand(pfOrg);
                            lClauses.Add(cfOr);
                        }
                        else
                        {
                            CompoundFormula cf = (CompoundFormula)f;
                            if (cf.Operator == "or")
                            {
                                CompoundFormula cfOr = new CompoundFormula("or");
                                foreach (PredicateFormula pf in cf.Operands)
                                {
                                    cfOr.AddOperand(pf);
                                }
                                foreach (PredicateFormula pfOrg in lPredicateOperands)
                                    cfOr.AddOperand(pfOrg);
                                lClauses.Add(cfOr);
                            }

                        }
                    }
                }
                else
                    throw new NotImplementedException();
            }
            else if (Operator == "oneof")
            {
                CompoundFormula cfOr = new CompoundFormula("or");
                List<Predicate> lPredicates = new List<Predicate>();
                foreach (PredicateFormula pf in Operands)
                {
                    lPredicates.Add(pf.Predicate);
                    cfOr.AddOperand(pf);
                }
                lClauses.Add(cfOr);
                for (int i = 0; i < lPredicates.Count - 1; i++)
                {
                    for (int j = i + 1; j < lPredicates.Count; j++)
                    {
                        cfOr = new CompoundFormula("or");
                        cfOr.AddOperand(lPredicates[i].Negate());
                        cfOr.AddOperand(lPredicates[j].Negate());
                        lClauses.Add(cfOr);
                    }
                }
            }
            else if (Operator == "and")
            {
                foreach (Formula f in Operands)
                {
                    if (f is CompoundFormula)
                        lClauses.Add((CompoundFormula)f);
                    else
                    {
                        CompoundFormula cfOr = new CompoundFormula("or");
                        cfOr.AddOperand(f);
                        lClauses.Add(cfOr);
                    }
                }
            }
            else
                throw new NotImplementedException();

            return lClauses;
        }
        */
        private CompoundFormula RemoveOneof(out bool bChanged)
        {
            if (Operator == "oneof")
            {
                CompoundFormula cfAnd = new CompoundFormula("and");
                CompoundFormula cfOr = new CompoundFormula("or");
                List<Formula> lOperands = new List<Formula>();
                foreach (Formula f in Operands)
                {
                    lOperands.Add(f);
                    cfOr.AddOperand(f);
                }
                cfAnd.AddOperand(cfOr);
                for (int i = 0; i < lOperands.Count - 1; i++)
                {
                    for (int j = i + 1; j < lOperands.Count; j++)
                    {
                        cfOr = new CompoundFormula("or");
                        cfOr.AddOperand(lOperands[i].Negate());
                        cfOr.AddOperand(lOperands[j].Negate());
                        cfAnd.AddOperand(cfOr);
                    }
                }
                bChanged = true;
                return cfAnd;
            }
            bChanged = false;
            return this;
        }
コード例 #21
0
 public override Formula ReduceConditions(IEnumerable<Predicate> lKnown)
 {
     CompoundFormula cfNew = new CompoundFormula(Operator);
     if (Operator == "and")// || Operator == "or" || Operator == "oneof")
     {
         foreach (Formula f in Operands)
         {
             if (f is CompoundFormula)
             {
                 Formula fTag = f.ReduceConditions(lKnown);
                 if (fTag != null)
                     cfNew.SimpleAddOperand(fTag);
             }
             else
                 cfNew.SimpleAddOperand(f);
         }
     }
     else if (Operator == "when")
     {
         Formula fReduced = Operands[0].Reduce(lKnown);
         if(fReduced.IsTrue(null))
             return Operands[1];
         if (fReduced.IsFalse(null))
             return null;
         cfNew.AddOperand(fReduced);
         cfNew.AddOperand(Operands[1]);
     }
     else
         throw new NotImplementedException();
     return cfNew;
 }
コード例 #22
0
 private CompoundFormula RemoveRedundancies()
 {
     if (Operator == "or")
     {
         HashSet<Predicate> lPredicates = GetAllPredicates();
         HashSet<Predicate> lObligatory = new HashSet<Predicate>();
         foreach (Predicate p in lPredicates)
         {
             List<Predicate> lNotP = new List<Predicate>();
             lNotP.Add(p.Negate());
             if (IsFalse(lNotP))
                 lObligatory.Add(p);
         }
         if (lObligatory.Count == 0)
         {
             return this;
         }
         CompoundFormula cfAnd = new CompoundFormula("and");
         CompoundFormula cfOr = new CompoundFormula("or");
         foreach (Predicate p in lObligatory)
             cfAnd.AddOperand(p);
         foreach (Formula f in Operands)
             cfOr.AddOperand(f.Reduce(lObligatory));
         cfAnd.AddOperand(cfOr);
         return cfAnd;
     }
     return this;
 }
コード例 #23
0
 public override Formula RemoveImpossibleOptions(IEnumerable<Predicate> lObserved)
 {
     CompoundFormula cfNew = new CompoundFormula(Operator);
     if (Operator == "and")
     {
         foreach (Formula f in Operands)
         {
             Formula fTag = f;
             if (f is CompoundFormula)
             {
                 fTag = ((CompoundFormula)f).RemoveImpossibleOptions(lObserved);
             }
             if (fTag == null)
                 return null;
             cfNew.AddOperand(fTag);
         }
     }
     else if (Operator == "or")
     {
         foreach (Formula f in Operands)
         {
             Formula fTag = f;
             if (f is CompoundFormula)
             {
                 fTag = ((CompoundFormula)f).RemoveImpossibleOptions(lObserved);
             }
             if (fTag != null)
                 cfNew.AddOperand(fTag);
         }
     }
     else if (Operator == "when")
     {
         Formula fTag = ((CompoundFormula)Operands[1]).RemoveImpossibleOptions(lObserved);
         if (fTag != null)
         {
             cfNew.AddOperand(Operands[0]);
             cfNew.AddOperand(fTag);
         }
         else
             return null;
     }
     else
         throw new NotImplementedException();
     return cfNew;
 }
コード例 #24
0
 public override Formula AddTime(int iTime)
 {
     CompoundFormula cf = new CompoundFormula(Operator);
     foreach (Formula f in Operands)
         cf.AddOperand(f.AddTime(iTime));
     return cf;
 }
コード例 #25
0
 public CompoundFormula RemoveNestedConjunction(out bool bChanged)
 {
     bChanged = false;
     CompoundFormula cfAnd = new CompoundFormula("and");
     if (Operator == "or")
     {
         List<CompoundFormula> lCompoundOperands = new List<CompoundFormula>();
         List<PredicateFormula> lPredicateOperands = new List<PredicateFormula>();
         foreach (Formula f in Operands)
         {
             if (f is CompoundFormula)
             {
                 CompoundFormula cf = (CompoundFormula)f;
                 cf = cf.RemoveNestedConjunction(out bChanged);
                 lCompoundOperands.Add(cf);
             }
             else
                 lPredicateOperands.Add((PredicateFormula)f);
         }
         if (lCompoundOperands.Count == 0)
         {
             return this;
         }
         List<List<Formula>> lAllCombinations = GetAllCombinations(lPredicateOperands, lCompoundOperands);
         foreach (List<Formula> lCombination in lAllCombinations)
         {
             CompoundFormula cfOr = new CompoundFormula("or");
             foreach (Formula f in lCombination)
                 cfOr.AddOperand(f);
             foreach (PredicateFormula f in lPredicateOperands)
                 cfOr.AddOperand(f);
             if (!cfOr.IsTrue(new List<Predicate>()))
                 cfAnd.AddOperand(cfOr);
         }
             /*
         else if (lCompoundOperands.Count == 1)
         {
             CompoundFormula cfNestedAnd = lCompoundOperands[0];
             if (cfNestedAnd.Operator != "and")
                 throw new NotImplementedException();
             foreach (Formula f in cfNestedAnd.Operands)
             {
                 CompoundFormula cfOr = new CompoundFormula("or");
                 cfOr.AddOperand(f);
                 foreach (PredicateFormula pfOrg in lPredicateOperands)
                     cfOr.AddOperand(pfOrg);
                 cfAnd.AddOperand(cfOr);
             }
         }
         else
             throw new NotImplementedException();
              * */
         bChanged = true;
     }
     else
     {
         foreach (Formula f in Operands)
         {
             if (f is CompoundFormula)
             {
                 CompoundFormula cf = (CompoundFormula)f;
                 cf = cf.RemoveNestedConjunction(out bChanged);
                 if (cf.Operands.Count > 0)
                 {
                     if (cf.Operator == "and")
                         foreach (Formula fSub in cf.Operands)
                             cfAnd.AddOperand(fSub);
                     else
                         cfAnd.AddOperand(cf);
                 }
             }
             else
                 cfAnd.AddOperand(f);
         }
     }
     return cfAnd;
 }
コード例 #26
0
 public CompoundFormula ApplyConditionsII(List<CompoundFormula> lConditions)
 {
     Dictionary<Formula, Formula> dTranslations = new Dictionary<Formula, Formula>();
     foreach (CompoundFormula cfCondition in lConditions)
     {
         Formula fRest = null;
         if (cfCondition.ConditionDeletesPrecondition(out fRest))
             dTranslations[cfCondition.Operands[0].Simplify()] = fRest;
         else
         {
             CompoundFormula cfAnd = new CompoundFormula("and");
             cfAnd.AddOperand(cfCondition.Operands[0].Simplify());
             cfAnd.AddOperand(cfCondition.Operands[1].Simplify());
             dTranslations[cfCondition.Operands[0].Simplify()] = cfAnd.Simplify();
         }
     }
     if (dTranslations.Count == 0)
         return this;
     return (CompoundFormula)Replace(dTranslations);
 }
コード例 #27
0
 private List<Action> RemoveConditions(List<Action> lActions)
 {
     List<Action> lNoConditions = new List<Action>();
     foreach (Action a in lActions)
     {
         HashSet<Predicate> lMandatory = a.GetMandatoryEffects();
         List<CompoundFormula> lConditions = a.GetConditions();
         if (lConditions.Count == 0)
         {
             lNoConditions.Add(a);
         }
         else
         {
             int iCondition = 0;
             foreach (CompoundFormula cfCondition in lConditions)
             {
                 Action aNew = new Action(a.Name + "." + iCondition);
                 CompoundFormula cfPreconditions = new CompoundFormula("and");
                 cfPreconditions.AddOperand(a.Preconditions);
                 cfPreconditions.AddOperand(cfCondition.Operands[0]);
                 if (!cfPreconditions.IsFalse(null))
                 {
                     aNew.Preconditions = cfPreconditions;
                     CompoundFormula cfEffects = new CompoundFormula("and");
                     cfEffects.AddOperand(cfCondition.Operands[1]);
                     foreach (Predicate p in lMandatory)
                         cfEffects.AddOperand(p);
                     aNew.Effects = cfEffects;
                     lNoConditions.Add(aNew);
                     iCondition++;
                 }
             }
         }
     }
     return lNoConditions;
 }
コード例 #28
0
 public override Formula ApplyKnown(IEnumerable<Predicate> lKnown)
 {
     CompoundFormula cfNew = new CompoundFormula(Operator);
     if (Operator == "and" || Operator == "or" || Operator == "oneof")
     {
         foreach (Formula f in Operands)
         {
             Formula fTag = f.ApplyKnown(lKnown);
             if (fTag != null)
                 cfNew.SimpleAddOperand(fTag);
         }
     }
     else if (Operator == "when")
     {
         if (Operands[0].IsTrue(lKnown))
             return Operands[1];
         if (Operands[0].IsFalse(lKnown))
             return null;
         cfNew.AddOperand(Operands[0].ApplyKnown(lKnown));
         cfNew.AddOperand(Operands[1]);
     }
     else
         throw new NotImplementedException();
     return cfNew;
 }
コード例 #29
0
 private List<Action> RemoveDisjunctions(List<Action> lGrounded)
 {
     List<Action> lNoDisjunctions = new List<Action>();
     foreach (Action a in lGrounded)
     {
             //if (a.Name.Contains("move-KW-tag0"))
           //      Console.WriteLine("*");
         if (a.Preconditions == null || a.Preconditions is PredicateFormula)
             lNoDisjunctions.Add(a);
         else
         {
             HashSet<Predicate> hsMandatory = new HashSet<Predicate>();
             List<List<Formula>> lOptions = new List<List<Formula>>();
             ((CompoundFormula)a.Preconditions).IdentifyDisjunctions(lOptions, hsMandatory);
             CompoundFormula cfAnd = new CompoundFormula("and");
             foreach (Predicate p in hsMandatory)
                 cfAnd.AddOperand(p);
             int iIndex = 0;
             RemoveDisjunctions(a, lOptions, 0, cfAnd, lNoDisjunctions, ref iIndex);
         }
     }
     return lNoDisjunctions;
 }
コード例 #30
0
 public Formula ChooseOption(int iOption)
 {
     if (Operator == "or" || Operator == "oneof")
     {
         //in or there could be more than a single option - not implemented here
         int iActualOption = iOption % Operands.Count;
         return Operands[iActualOption].Clone();
     }
     if (Operator == "and")
     {
         CompoundFormula cfNew = new CompoundFormula("and");
         foreach (Formula fOperand in Operands)
         {
             if (fOperand is CompoundFormula)
             {
                 cfNew.AddOperand(((CompoundFormula)fOperand).ChooseOption(iOption));
             }
             else
                 cfNew.AddOperand(fOperand);
         }
         return cfNew;
     }
     if (Operator == "when")
     {
         CompoundFormula cfNew = new CompoundFormula("when");
         CompoundFormula cfGiven = new CompoundFormula("and");
         cfGiven.AddOperand(Operands[0].Clone());
         cfNew.AddOperand(cfGiven);
         cfNew.AddOperand(((CompoundFormula)Operands[1]).ChooseOption(iOption));
         return cfNew;
     }
     throw new NotFiniteNumberException();
 }