Пример #1
0
        private CompoundFormula AddKnowledgePredicatesToFormula(Formula f, HashSet <Predicate> lKnowPredicates)
        {
            CompoundFormula     cf          = new CompoundFormula("and");
            HashSet <Predicate> lPredicates = new HashSet <Predicate>();

            f.GetAllPredicates(lPredicates);
            foreach (Predicate p in lPredicates)
            {
                if (lKnowPredicates.Contains(p))
                {
                    KnowPredicate kp = new KnowPredicate(p);
                    cf.AddOperand(new PredicateFormula(kp));
                }
            }
            if (f is CompoundFormula && ((CompoundFormula)f).Operator == "and")
            {
                foreach (Formula fSub in ((CompoundFormula)f).Operands)
                {
                    cf.AddOperand(fSub);
                }
            }
            else
            {
                cf.AddOperand(f);
            }
            return(cf);
        }
Пример #2
0
        private Action CreateReasoningAction(Predicate pEffect, HashSet <Predicate> lPredicates)
        {
            KnowPredicate kpEffect = new KnowPredicate(pEffect);

            if (Predicates.Contains(kpEffect))
            {
                return(null);
            }
            Action a = new KnowledgeAction("Reasoning_" + pEffect.ToString());

            a.Preconditions = new CompoundFormula("and");
            foreach (Predicate pOther in lPredicates)
            {
                if (pOther != pEffect)
                {
                    KnowPredicate kp = new KnowPredicate(pOther);
                    if (!Predicates.Contains(kp))
                    {
                        return(null);
                    }
                    ((CompoundFormula)a.Preconditions).AddOperand(new PredicateFormula(kp));
                }
            }
            CompoundFormula cfEffects = new CompoundFormula("and");

            cfEffects.AddOperand(new PredicateFormula(kpEffect));
            a.SetEffects(cfEffects);
            return(a);
        }
Пример #3
0
        private void AddReasoningAction(HashSet <Predicate> lPreconditions, HashSet <Predicate> lEffects, Dictionary <List <Predicate>, List <Predicate> > dActions)
        {
            List <Predicate> lKnowPreconditions = new List <Predicate>();

            foreach (GroundedPredicate p in lPreconditions)
            {
                KnowPredicate pKnow = new KnowPredicate(p);
                lKnowPreconditions.Add(pKnow);
                lKnowPreconditions.Add(p);
            }
            List <Predicate> lKnowEffects = new List <Predicate>();

            foreach (GroundedPredicate p in lEffects)
            {
                KnowPredicate pKnow = new KnowPredicate(p);
                lKnowEffects.Add(pKnow);
            }
            if (dActions.ContainsKey(lKnowPreconditions))
            {
                if (dActions.Comparer.Equals(lKnowEffects, dActions[lKnowPreconditions]))
                {
                    return;
                }
                throw new NotImplementedException();
            }
            dActions[lKnowPreconditions] = lKnowEffects;
        }
Пример #4
0
        public override Formula GetKnowledgeFormula(List <string> lAlwaysKnown, bool bKnowWhether, HashSet <Predicate> lNegativePreconditions)
        {
            CompoundFormula cf = new CompoundFormula("and");

            if (Predicate.Name == Domain.OPTION_PREDICATE)
            {
                return(null);//we never know an option value
            }
            if (lAlwaysKnown.Contains(Predicate.Name))
            {
                cf.AddOperand(this);
            }
            else
            {
                if (bKnowWhether)
                {
                    cf.AddOperand(new PredicateFormula(new KnowWhetherPredicate(Predicate)));
                }
                else
                {
                    //cf.AddOperand(this);

                    Predicate pNot = new KnowPredicate(Predicate.Negate());
                    cf.AddOperand(pNot.Negate());
                    cf.AddOperand(new KnowPredicate(Predicate));// added by sagi 15.6 removed again, guy is right
                    //return new PredicateFormula(new KnowPredicate(Predicate)); sagi: removed for adding box moving, not only knowing..
                }
            }
            if (lNegativePreconditions != null && lNegativePreconditions.Contains(Predicate.Canonical()))
            {
                Predicate pNot = Predicate.Clone();
                pNot.Name = "Not" + pNot.Name;
                if (Predicate.Negation)
                {
                    cf.AddOperand(pNot);
                }
                else
                {
                    cf.AddOperand(pNot.Negate());
                }
            }
            return(cf);
        }
Пример #5
0
 public override Formula Ground(Dictionary <string, Constant> dBindings)
 {
     if (Predicate is ParameterizedPredicate)
     {
         ParameterizedPredicate ppred = (ParameterizedPredicate)Predicate;
         GroundedPredicate      gpred = ppred.Ground(dBindings);
         return(new PredicateFormula(gpred));
     }
     if (Predicate is KnowPredicate)
     {
         KnowPredicate     kp    = (KnowPredicate)Predicate;
         GroundedPredicate gpred = kp.Ground(dBindings);
         return(new PredicateFormula(gpred));
     }
     if (Predicate is KnowGivenPredicate)
     {
         throw new NotImplementedException();
     }
     return(this);
 }
Пример #6
0
        public MemoryStream WriteKnowledgeProblem(HashSet <Predicate> lObserved, HashSet <Predicate> lAllValues)
        {
            MemoryStream msProblem = new MemoryStream();
            StreamWriter sw        = new StreamWriter(msProblem);

            sw.WriteLine("(define (problem K" + Name + ")");
            sw.WriteLine("(:domain K" + Domain.Name + ")");
            sw.WriteLine(";;" + SDRPlanner.Translation);
            sw.WriteLine("(:init"); //ff doesn't like the and (and");


            foreach (GroundedPredicate gp in lObserved)
            {
                if (gp.Name == "Choice")
                {
                    continue;
                }
                sw.WriteLine(gp);
                if (!Domain.AlwaysKnown(gp))
                {
                    Predicate kp = new KnowPredicate(gp);
                    sw.WriteLine(kp);
                }
            }
            HashSet <Predicate> lHidden = new HashSet <Predicate>(lAllValues.Except(lObserved));



            foreach (GroundedPredicate gp in lHidden)
            {
                sw.WriteLine(gp);
            }



            sw.WriteLine(")");

            HashSet <Predicate> lGoalPredicates = Goal.GetAllPredicates();


            CompoundFormula cfGoal = new CompoundFormula("and");

            foreach (Predicate p in lGoalPredicates)
            {
                if (Domain.AlwaysKnown(p))
                {
                    cfGoal.AddOperand(p);
                }
                else
                {
                    cfGoal.AddOperand(new KnowPredicate(p));
                }
            }

            CompoundFormula cfAnd = new CompoundFormula(cfGoal);

            sw.WriteLine("(:goal " + cfAnd.Simplify() + ")");
            //sw.WriteLine("))");

            sw.WriteLine(")");
            sw.Flush();


            return(msProblem);
        }
Пример #7
0
        public MemoryStream WriteKnowledgeProblem(HashSet <Predicate> lObserved, HashSet <Predicate> lHidden, int cMinMishaps, int cMishaps, bool bRemoveNegativePreconditions)
        {
            MemoryStream msProblem = new MemoryStream();
            StreamWriter sw        = new StreamWriter(msProblem);

            sw.WriteLine("(define (problem K" + Name + ")");
            sw.WriteLine("(:domain K" + Domain.Name + ")");
            sw.WriteLine(";;" + SDRPlanner.Translation);
            sw.WriteLine("(:init"); //ff doesn't like the and (and");

            string sKP = "", sP = "";

            HashSet <string> lNegativePreconditons = new HashSet <string>();

            if (bRemoveNegativePreconditions)
            {
                foreach (Predicate p in Domain.IdentifyNegativePreconditions())
                {
                    lNegativePreconditons.Add(p.Name);
                }
            }

            foreach (GroundedPredicate gp in lObserved)
            {
                if (gp.Name == "Choice")
                {
                    continue;
                }
                if (Domain.AlwaysKnown(gp))
                {
                    sw.WriteLine(gp);
                }
                if (!Domain.AlwaysKnown(gp))
                {
                    Predicate kp = new KnowPredicate(gp);
                    sw.WriteLine(kp);
                }
                if (gp.Negation && bRemoveNegativePreconditions && lNegativePreconditons.Contains(gp.Name))
                {
                    Predicate np = gp.Negate().Clone();
                    np.Name = "Not" + gp.Name;
                    sw.WriteLine(np);
                }
            }
            if (bRemoveNegativePreconditions)
            {
                foreach (GroundedPredicate gp in lHidden)
                {
                    Predicate nkp = gp.Clone();
                    nkp.Name = "NotK" + gp.Name;
                    sw.WriteLine(nkp);
                    Predicate nknp = gp.Clone();
                    nknp.Name = "NotKN" + gp.Name;
                    sw.WriteLine(nknp);
                }
            }

            if (cMinMishaps > cMishaps)
            {
                sw.WriteLine("(MishapCount m" + cMishaps + ")");
            }

            if (SDRPlanner.AddActionCosts)
            {
                sw.WriteLine("(= (total-cost) 0)");
            }

            sw.WriteLine(")");

            HashSet <Predicate> lGoalPredicates    = Goal.GetAllPredicates();


            CompoundFormula cfGoal = new CompoundFormula("and");

            foreach (Predicate p in lGoalPredicates)
            {
                if (Domain.AlwaysKnown(p))
                {
                    cfGoal.AddOperand(p);
                }
                else
                {
                    cfGoal.AddOperand(new KnowPredicate(p));
                }
            }

            CompoundFormula cfAnd = new CompoundFormula(cfGoal);

            if (cMinMishaps > cMishaps && SDRPlanner.Translation != SDRPlanner.Translations.Conformant)
            {
                GroundedPredicate gp = new GroundedPredicate("MishapCount");
                gp.AddConstant(new Constant("mishaps", "m" + cMinMishaps));
                cfAnd.AddOperand(gp);
            }

            sw.WriteLine("(:goal " + cfAnd.Simplify() + ")");
            if (MetricStatement != null)
            {
                sw.WriteLine(MetricStatement);
            }

            sw.WriteLine(")");
            sw.Flush();


            return(msProblem);
        }