Пример #1
0
        public override Formula Negate()
        {
            CompoundFormula cfNegate = new ParametrizedFormula(this);

            foreach (Formula fOperand in Operands)
            {
                cfNegate.AddOperand(fOperand.Negate());
            }
            return(cfNegate);
        }
Пример #2
0
        public override Formula PartiallyGround(Dictionary <string, Constant> dBindings)
        {
            ParametrizedFormula cfGrounded = new ParametrizedFormula(this);

            foreach (Formula fSub in Operands)
            {
                Formula fGrounded = fSub.PartiallyGround(dBindings);
                cfGrounded.AddOperand(fGrounded);
            }
            return(cfGrounded);
        }
Пример #3
0
        public override Formula Simplify()
        {
            if (Simplified)
            {
                return(this);
            }
            ParametrizedFormula pf = new ParametrizedFormula(this);

            foreach (Formula f in Operands)
            {
                pf.AddOperand(f.Simplify());
            }
            return(pf);
        }
Пример #4
0
        private Formula ReadGroundedFormula(CompoundExpression exp, Domain d)
        {
            bool bPredicate = true;

            if (IsUniversalQuantifier(exp))
            {
                CompoundExpression          eParameter           = (CompoundExpression)exp.SubExpressions[0];
                CompoundExpression          eBody                = (CompoundExpression)exp.SubExpressions[1];
                string                      sParameter           = eParameter.Type;
                string                      sType                = eParameter.SubExpressions[1].ToString();
                Dictionary <string, string> dParameterNameToType = new Dictionary <string, string>();
                dParameterNameToType[sParameter] = sType;
                ParametrizedFormula cfQuantified = new ParametrizedFormula(exp.Type);
                cfQuantified.Parameters[sParameter] = sType;
                Formula fBody = ReadFormula(eBody, dParameterNameToType, true, d);
                cfQuantified.AddOperand(fBody);
                return(cfQuantified);
            }
            foreach (Expression eSub in exp.SubExpressions)
            {
                if (eSub is CompoundExpression)
                {
                    bPredicate = false;
                    break;
                }
            }
            if (bPredicate)
            {
                return(new PredicateFormula(ReadGroundedPredicate(exp, d)));
            }
            else
            {
                CompoundFormula cf          = new CompoundFormula(exp.Type);
                int             iExpression = 0;
                for (iExpression = 0; iExpression < exp.SubExpressions.Count; iExpression++)
                {
                    Formula f = ReadGroundedFormula((CompoundExpression)exp.SubExpressions[iExpression], d);
                    cf.AddOperand(f);
                }
                if (cf.Operator == "not" && cf.Operands[0] is PredicateFormula)
                {
                    return(new PredicateFormula(((PredicateFormula)cf.Operands[0]).Predicate.Negate()));
                }
                return(cf);
            }
        }
Пример #5
0
        private Formula ReadFormula(CompoundExpression exp, Dictionary <string, string> dParameterNameToType, bool bParamterized, Domain d)
        {
            bool bPredicate = true;

            //Console.WriteLine(exp);
            if (d != null && d.IsFunctionExpression(exp.Type))
            {
                Predicate p = ReadFunctionExpression(exp, dParameterNameToType, d);
                return(new PredicateFormula(p));
            }
            else if (IsUniversalQuantifier(exp))
            {
                CompoundExpression eParameter = (CompoundExpression)exp.SubExpressions[0];
                CompoundExpression eBody      = (CompoundExpression)exp.SubExpressions[1];
                string             sParameter = eParameter.Type;
                string             sType      = eParameter.SubExpressions[1].ToString();
                dParameterNameToType[sParameter] = sType;
                ParametrizedFormula cfQuantified = new ParametrizedFormula(exp.Type);
                cfQuantified.Parameters[sParameter] = sType;
                Formula fBody = ReadFormula(eBody, dParameterNameToType, true, d);
                cfQuantified.AddOperand(fBody);
                return(cfQuantified);
            }
            else if (exp.Type == "probabilistic")
            {
                ProbabilisticFormula pf = new ProbabilisticFormula();
                int iExpression         = 0;
                for (iExpression = 0; iExpression < exp.SubExpressions.Count; iExpression += 2)
                {
                    //if (exp.SubExpressions[iExpression] is StringExpression)
                    //    throw new InvalidDataException();
                    string sProb = exp.SubExpressions[iExpression].ToString();
                    double dProb = 0.0;
                    if (sProb.Contains("/"))
                    {
                        string[] a = sProb.Split('/');
                        dProb = double.Parse(a[0]) / double.Parse(a[1]);
                    }
                    else
                    {
                        dProb = double.Parse(sProb);
                    }
                    Formula f = ReadFormula((CompoundExpression)exp.SubExpressions[iExpression + 1], dParameterNameToType, bParamterized, d);
                    pf.AddOption(f, dProb);
                }
                return(pf);
            }
            else
            {
                foreach (Expression eSub in exp.SubExpressions)
                {
                    if (eSub is CompoundExpression)
                    {
                        bPredicate = false;
                        break;
                    }
                }
                if (bPredicate)
                {
                    return(ReadPredicate(exp, dParameterNameToType, bParamterized, d));
                }
                else
                {
                    CompoundFormula cf          = new CompoundFormula(exp.Type);
                    int             iExpression = 0;
                    for (iExpression = 0; iExpression < exp.SubExpressions.Count; iExpression++)
                    {
                        if (exp.SubExpressions[iExpression] is StringExpression)
                        {
                            throw new InvalidDataException();
                        }
                        Formula f = ReadFormula((CompoundExpression)exp.SubExpressions[iExpression], dParameterNameToType, bParamterized, d);
                        cf.SimpleAddOperand(f);
                    }
                    if (cf.Operator == "not" && cf.Operands[0] is PredicateFormula)
                    {
                        PredicateFormula fNegate = new PredicateFormula(((PredicateFormula)cf.Operands[0]).Predicate.Negate());
                        return(fNegate);
                    }
                    return(cf);
                }
            }
        }
Пример #6
0
 public ParametrizedFormula(ParametrizedFormula cf)
     : this(cf.Operator)
 {
     Parameters = new Dictionary <string, string>(cf.Parameters);
 }
Пример #7
0
        private void GetApplicableEffects(Formula fEffects, HashSet <Predicate> lAdd, HashSet <Predicate> lDelete)
        {
            if (fEffects is PredicateFormula)
            {
                Predicate p = ((PredicateFormula)fEffects).Predicate;
                if (p.Negation)
                {
                    lDelete.Add(p);
                }
                else
                {
                    lAdd.Add(p);
                }
            }
            else if (fEffects is ProbabilisticFormula)
            {
                ProbabilisticFormula pf = (ProbabilisticFormula)fEffects;
                double dRand            = RandomGenerator.NextDouble();
                double dInitialRand     = dRand;
                int    iOption          = 0;
                while (iOption < pf.Options.Count && dRand > 0)
                {
                    dRand -= pf.Probabilities[iOption];
                    iOption++;
                }
                if (dRand < 0.01)
                {
                    iOption--;

                    GetApplicableEffects(pf.Options[iOption], lAdd, lDelete);
                }
                else //the no-op option was chosen
                {
                    iOption = -1;
                }
                GroundedPredicate pChoice = new GroundedPredicate("Choice");
                pChoice.AddConstant(new Constant("ActionIndex", "a" + Time));
                pChoice.AddConstant(new Constant("ChoiceIndex", "c" + ChoiceCount + "." + iOption));
                ChoiceCount++;
                State s = this;
                while (s != null)
                {
                    s.m_lPredicates.Add(pChoice);
                    s = s.m_sPredecessor;
                }
            }
            else
            {
                CompoundFormula cf = (CompoundFormula)fEffects;
                if (cf.Operator == "oneof" || cf.Operator == "or")//BUGBUG - should treat or differently
                {
                    int iRandomIdx = RandomGenerator.Next(cf.Operands.Count);
                    GetApplicableEffects(cf.Operands[iRandomIdx], lAdd, lDelete);
                    GroundedPredicate pChoice = new GroundedPredicate("Choice");
                    pChoice.AddConstant(new Constant("ActionIndex", "a" + Time));
                    pChoice.AddConstant(new Constant("ChoiceIndex", "c" + ChoiceCount + "." + iRandomIdx));
                    ChoiceCount++;
                    State s = this;
                    while (s != null)
                    {
                        s.m_lPredicates.Add(pChoice);
                        s = s.m_sPredecessor;
                    }
                }
                else if (cf.Operator == "and")
                {
                    foreach (Formula f in cf.Operands)
                    {
                        GetApplicableEffects(f, lAdd, lDelete);
                    }
                }
                else if (cf.Operator == "when")
                {
                    if (Contains(cf.Operands[0]))
                    {
                        GetApplicableEffects(cf.Operands[1], lAdd, lDelete);
                    }
                }
                else if (cf is ParametrizedFormula)
                {
                    ParametrizedFormula pf = (ParametrizedFormula)cf;
                    foreach (Formula fNew in pf.Ground(Problem.Domain.Constants))
                    {
                        GetApplicableEffects(fNew, lAdd, lDelete);
                    }
                }
                else
                {
                    throw new NotImplementedException();
                }
            }
        }