Ejemplo n.º 1
0
        private void ReadConstants(CompoundExpression exp, Domain d)
        {
            string        sType = "?", sExp = "";
            List <string> lUndefined = new List <string>();

            for (int iExpression = 0; iExpression < exp.SubExpressions.Count; iExpression++)
            {
                sExp = exp.SubExpressions[iExpression].ToString().Trim();
                if (sExp == "-")
                {
                    sType = exp.SubExpressions[iExpression + 1].ToString();
                    iExpression++;
                    foreach (string sName in lUndefined)
                    {
                        d.AddConstant(new Constant(sType, sName));
                    }
                    lUndefined.Clear();
                }
                else if (!sExp.StartsWith(":"))
                {
                    lUndefined.Add(sExp);
                }
            }
            if (lUndefined.Count > 0)
            {
                //supporting objects with undefined types as type "OBJ"
                foreach (string sName in lUndefined)
                {
                    d.AddConstant(new Constant("OBJ", sName));
                }
                //throw new NotImplementedException();
            }
        }
Ejemplo n.º 2
0
        public Problem ParseProblem(string sProblemFile, Domain d)
        {
            StreamReader       sr  = new StreamReader(sProblemFile);
            CompoundExpression exp = (CompoundExpression)ToExpression(sr);

            sr.Close();
            return(ParseProblem(exp, d));
        }
Ejemplo n.º 3
0
 private void ReadPredicates(CompoundExpression exp, Domain d)
 {
     foreach (Expression e in exp.SubExpressions)
     {
         Predicate p = ReadPredicate((CompoundExpression)e, d);
         d.AddPredicate(p);
     }
 }
Ejemplo n.º 4
0
        public CompoundFormula ParseFormula(string sFile, Domain d)
        {
            string             sPath = sFile.Substring(0, sFile.LastIndexOf(@"\") + 1);
            StreamReader       sr    = new StreamReader(sFile);
            CompoundExpression exp   = (CompoundExpression)ToExpression(sr);

            sr.Close();
            Formula cf = ReadFormula(exp, null, false, d);

            return((CompoundFormula)cf);
        }
Ejemplo n.º 5
0
 private void ReadFunctions(CompoundExpression eFunctions, Domain d)
 {
     foreach (Expression eSub in eFunctions.SubExpressions)
     {
         if (eSub.ToString() != ":functions")
         {
             CompoundExpression eFunction = (CompoundExpression)eSub;
             //BUGBUG - only reading non parametrized functions for now
             d.AddFunction("(" + eFunction.Type + ")");
         }
     }
 }
Ejemplo n.º 6
0
        public Predicate ReadFunctionExpression(CompoundExpression exp, Dictionary <string, string> dParameterNameToType, Domain d)
        {
            Constant c     = null;
            string   sName = exp.SubExpressions[0].ToString();

            if (exp.Type == "=")
            {
                string sParam1 = exp.SubExpressions[0].ToString();
                string sParam2 = exp.SubExpressions[1].ToString();
                if (!dParameterNameToType.ContainsKey(sParam1))
                {
                    throw new ArgumentException("First argument of = must be a parameter");
                }
                ParametrizedPredicate pp = new ParametrizedPredicate("=");
                pp.AddParameter(new Parameter(dParameterNameToType[sParam1], sParam1));
                if (dParameterNameToType.ContainsKey(sParam2))
                {
                    pp.AddParameter(new Parameter(dParameterNameToType[sParam2], sParam2));
                }
                else
                {
                    pp.AddParameter(new Constant(d.ConstantNameToType[sParam2], sParam2));
                }
                return(pp);
            }


            GroundedPredicate p      = new GroundedPredicate(exp.Type);
            double            dValue = 0.0;

            if (d.Functions.Contains(sName))
            {
                c = new Constant("Function", sName);
            }
            else
            {
                throw new ArgumentException("First argument of increase or decrease must be a function");
            }
            p.AddConstant(c);

            sName = exp.SubExpressions[1].ToString();
            if (double.TryParse(sName, out dValue))
            {
                c = new Constant("Number", sName);
            }
            else
            {
                throw new ArgumentException("Second argument of increase or decrease must be a number");
            }
            p.AddConstant(c);
            return(p);
        }
Ejemplo n.º 7
0
        private Action ReadAction(CompoundExpression exp, Domain d)
        {
            string sName = exp.SubExpressions[0].ToString();

            sName = sName.Replace("_", "-");
            Action pa          = null;
            int    iExpression = 0;

            for (iExpression = 1; iExpression < exp.SubExpressions.Count; iExpression++)
            {
                if (exp.SubExpressions[iExpression].ToString() == ":parameters")
                {
                    CompoundExpression ceParams = (CompoundExpression)exp.SubExpressions[iExpression + 1];
                    if (ceParams.Type != "N/A")
                    {
                        pa = new ParametrizedAction(sName);
                        ReadParameters((CompoundExpression)exp.SubExpressions[iExpression + 1], (ParametrizedAction)pa);
                    }
                    iExpression++;
                }
                else if (exp.SubExpressions[iExpression].ToString() == ":effect")
                {
                    if (pa == null)
                    {
                        pa = new Action(sName);
                    }
                    ReadEffect((CompoundExpression)exp.SubExpressions[iExpression + 1], pa, d, pa is ParametrizedAction);
                    iExpression++;
                }
                else if (exp.SubExpressions[iExpression].ToString() == ":precondition")
                {
                    if (pa == null)
                    {
                        pa = new Action(sName);
                    }
                    ReadPrecondition((CompoundExpression)exp.SubExpressions[iExpression + 1], pa, d, pa is ParametrizedAction);
                    iExpression++;
                }
                else if (exp.SubExpressions[iExpression].ToString() == ":observe")
                {
                    if (pa == null)
                    {
                        pa = new Action(sName);
                    }
                    ReadObserve((CompoundExpression)exp.SubExpressions[iExpression + 1], pa, d, pa is ParametrizedAction);
                    iExpression++;
                }
            }
            return(pa);
        }
Ejemplo n.º 8
0
        private GroundedPredicate ReadGroundedPredicate(CompoundExpression exp, Domain d)
        {
            GroundedPredicate gp = new GroundedPredicate(exp.Type);
            int      iExpression = 0;
            Constant c           = null;
            string   sName       = "";

            for (iExpression = 0; iExpression < exp.SubExpressions.Count; iExpression++)
            {
                sName = exp.SubExpressions[iExpression].ToString();
                c     = d.GetConstant(sName);
                gp.AddConstant(c);
            }
            return(gp);
        }
Ejemplo n.º 9
0
        private void ReadObserve(CompoundExpression exp, Action pa, Domain d, bool bParametrized)
        {
            string  sOperator = exp.Type;
            Formula f         = null;

            if (pa is ParametrizedAction)
            {
                f = ReadFormula(exp, ((ParametrizedAction)pa).ParameterNameToType, bParametrized, d);
            }
            else
            {
                f = ReadFormula(exp, d.ConstantNameToType, bParametrized, d);
            }
            pa.Observe = f;
        }
Ejemplo n.º 10
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);
            }
        }
Ejemplo n.º 11
0
        private Domain ParseDomain(CompoundExpression exp, string sPath)
        {
            Domain d = null;

            foreach (Expression e in exp.SubExpressions)
            {
                if (e is CompoundExpression)
                {
                    CompoundExpression eSub = (CompoundExpression)e;
                    if (eSub.Type == "domain")
                    {
                        d = new Domain(eSub.SubExpressions.First().ToString(), sPath);
                    }
                    else if (eSub.Type == ":requirements")
                    {
                    }
                    else if (eSub.Type == ":types")
                    {
                        foreach (Expression eType in ((CompoundExpression)eSub).SubExpressions)
                        {
                            d.Types.Add(eType.ToString());
                        }
                    }
                    else if (eSub.Type == ":constants")
                    {
                        ReadConstants(eSub, d);
                    }
                    else if (eSub.Type == ":functions")
                    {
                        ReadFunctions(eSub, d);
                    }
                    else if (eSub.Type == ":predicates")
                    {
                        ReadPredicates(eSub, d);
                    }
                    else if (eSub.Type == ":action")
                    {
                        d.AddAction(ReadAction(eSub, d));
                    }
                }
            }

            return(d);
        }
Ejemplo n.º 12
0
        private void ReadInitState(Problem p, Domain d, CompoundExpression eInitState)
        {
            foreach (Expression e in eInitState.SubExpressions)
            {
                CompoundExpression eSub = (CompoundExpression)e;
                if (d.IsFunctionExpression(eSub.Type))
                {
                    p.AddKnown(ReadFunctionExpression(eSub, null, d));
                }
                else if (d.ContainsPredicate(eSub.Type))
                {
                    p.AddKnown(ReadGroundedPredicate(eSub, d));
                }
                else
                {
                    if (eSub.Type != "unknown")
                    {
                        Formula f = ReadGroundedFormula(eSub, d);
                        if (f is CompoundFormula)
                        {
                            p.AddHidden((CompoundFormula)f);
                        }
                        if (f is PredicateFormula)//this happens in (not (p)) statments
                        {
                            p.AddKnown(((PredicateFormula)f).Predicate);
                        }
                    }
                    else
                    {
                        //causing a problem - add operand does some basic reasoning - adding p and ~p results in true for or statements.
                        //skipping unknown for now...

                        Predicate       pUnknown = ReadGroundedPredicate((CompoundExpression)eSub.SubExpressions[0], d);
                        CompoundFormula cfOr     = new CompoundFormula("or");
                        cfOr.SimpleAddOperand(pUnknown);
                        cfOr.SimpleAddOperand(pUnknown.Negate());
                        p.AddHidden(cfOr);
                    }
                }
            }
            p.ComputeRelevanceClosure();
        }
Ejemplo n.º 13
0
        private Expression ToExpression(Stack <string> sStack)
        {
            string sToken = sStack.Pop();

            if (sToken == "(")
            {
                bool bDone             = false;
                CompoundExpression exp = new CompoundExpression();
                sToken = sStack.Pop();
                if (sToken == ")")
                {
                    exp.Type = "N/A";
                    bDone    = true;
                }
                else
                {
                    exp.Type = sToken;
                }
                while (!bDone)
                {
                    if (sStack.Count == 0)
                    {
                        throw new InvalidDataException("Exp " + exp.Type + " was not closed");
                    }
                    sToken = sStack.Pop();
                    if (sToken == ")")
                    {
                        bDone = true;
                    }
                    else
                    {
                        sStack.Push(sToken);
                        exp.SubExpressions.Add(ToExpression(sStack));
                    }
                }
                return(exp);
            }
            else
            {
                return(new StringExpression(sToken));
            }
        }
Ejemplo n.º 14
0
        public void ParseDomainAndProblem(string sFileName, out Domain d, out Problem p)
        {
            string       sPath = sFileName.Substring(0, sFileName.LastIndexOf(@"\") + 1);
            StreamReader sr    = new StreamReader(sFileName);

            /*
             * Stack<string> s = ToStack(new StreamReader(sDomainFile));
             * Stack<string> s2 = ToStackII(new StreamReader(sDomainFile));
             *
             * for (int i = 0; i < s.Count; i++)
             *  if (s.ElementAt(i) != s2.ElementAt(i))
             *      Debug.WriteLine(s.ElementAt(i) + " =/= " + s2.ElementAt(i));
             */
            Stack <string>     s        = ToStack(sr);
            CompoundExpression eDomain  = (CompoundExpression)ToExpression(s);
            CompoundExpression eProblem = (CompoundExpression)ToExpression(s);

            sr.Close();
            d = ParseDomain(eDomain, sPath);
            p = ParseProblem(eProblem, d);
        }
Ejemplo n.º 15
0
        public Domain ParseDomain(string sDomainFile)
        {
            string       sPath = sDomainFile.Substring(0, sDomainFile.LastIndexOf(@"\") + 1);
            StreamReader sr    = new StreamReader(sDomainFile);

            /*
             * Stack<string> s = ToStack(new StreamReader(sDomainFile));
             * Stack<string> s2 = ToStackII(new StreamReader(sDomainFile));
             *
             * for (int i = 0; i < s.Count; i++)
             *  if (s.ElementAt(i) != s2.ElementAt(i))
             *      Debug.WriteLine(s.ElementAt(i) + " =/= " + s2.ElementAt(i));
             */


            CompoundExpression exp = (CompoundExpression)ToExpression(sr);

            sr.Close();

            return(ParseDomain(exp, sPath));
        }
Ejemplo n.º 16
0
        private Predicate ReadPredicate(CompoundExpression exp, Domain d)
        {
            ParametrizedPredicate pp            = new ParametrizedPredicate(exp.Type);
            int              iExpression        = 0;
            Parameter        p                  = null;
            string           sName              = "";
            List <Parameter> lUntypedParameters = new List <Parameter>();

            for (iExpression = 0; iExpression < exp.SubExpressions.Count; iExpression++)
            {
                sName = exp.SubExpressions[iExpression].ToString();
                if (sName == "-")
                {
                    string sType = exp.SubExpressions[iExpression + 1].ToString();
                    foreach (Parameter pUntyped in lUntypedParameters)
                    {
                        pUntyped.Type = sType;
                    }
                    lUntypedParameters.Clear();
                    iExpression++;//skip the "-" and the type
                }
                else
                {
                    p = new Parameter("", sName);
                    lUntypedParameters.Add(p);
                    pp.AddParameter(p);
                }
            }
            if (d.Types.Count == 1)
            {
                foreach (Parameter pUntyped in lUntypedParameters)
                {
                    pUntyped.Type = d.Types[0];
                }
            }
            return(pp);
        }
Ejemplo n.º 17
0
        private void ReadParameters(CompoundExpression exp, ParametrizedAction pa)
        {
            //unfortunately, expressions have a weird non standard structure with no type - (?i - pos ?j - pos )
            //so we must have a special case
            List <string> lTokens  = exp.ToTokenList();
            List <string> lNames   = new List <string>();
            string        sType    = "";
            int           iCurrent = 0;

            while (iCurrent < lTokens.Count)
            {
                if (lTokens[iCurrent] == "-")
                {
                    sType = lTokens[iCurrent + 1];
                    foreach (string sName in lNames)
                    {
                        pa.AddParameter(new Parameter(sType, sName));
                    }
                    lNames    = new List <string>();
                    sType     = "";
                    iCurrent += 2;
                }
                else
                {
                    lNames.Add(lTokens[iCurrent]);
                    iCurrent++;
                }
            }
            if (lNames.Count != 0) //allowing no types specified
            {
                foreach (string sName in lNames)
                {
                    pa.AddParameter(new Parameter("OBJ", sName));
                }
            }
        }
Ejemplo n.º 18
0
 private bool IsUniversalQuantifier(CompoundExpression exp)
 {
     return(exp.Type.ToLower() == "forall" || exp.Type.ToLower() == "exists");
 }
Ejemplo n.º 19
0
        private void ReadFunctions(CompoundExpression eFunctions, Domain d)
        {
            foreach (Expression eSub in eFunctions.SubExpressions)
            {
                if (eSub.ToString() != ":functions")
                {
                    CompoundExpression eFunction = (CompoundExpression)eSub;
                    //BUGBUG - only reading non parametrized functions for now
                    d.AddFunction("(" + eFunction.Type + ")");
                }

            }
        }
Ejemplo n.º 20
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);
                }
            }
        }
Ejemplo n.º 21
0
 private void ReadPredicates(CompoundExpression exp, Domain d)
 {
     foreach (Expression e in exp.SubExpressions)
     {
         Predicate p = ReadPredicate((CompoundExpression)e, d);
         d.AddPredicate(p);
     }
 }
Ejemplo n.º 22
0
        private Domain ParseDomain(CompoundExpression exp, string sPath)
        {
            Domain d = null;
            foreach (Expression e in exp.SubExpressions)
            {
                if (e is CompoundExpression)
                {
                    CompoundExpression eSub = (CompoundExpression)e;
                    if (eSub.Type == "domain")
                        d = new Domain(eSub.SubExpressions.First().ToString(), sPath);
                    else if (eSub.Type == ":requirements")
                    {
                    }
                    else if (eSub.Type == ":types")
                    {
                        foreach (Expression eType in ((CompoundExpression)eSub).SubExpressions)
                            d.Types.Add(eType.ToString());
                    }
                    else if (eSub.Type == ":constants")
                    {
                        ReadConstants(eSub, d);
                    }
                    else if (eSub.Type == ":functions")
                    {
                        ReadFunctions(eSub, d);
                    }
                    else if (eSub.Type == ":predicates")
                    {
                        ReadPredicates(eSub, d);
                    }
                    else if (eSub.Type == ":action")
                    {
                        d.AddAction(ReadAction(eSub, d));
                    }
                }
            }

            return d;
        }
Ejemplo n.º 23
0
        private Formula ReadPredicate(CompoundExpression exp, Dictionary<string, string> dParameterNameToType, bool bParametrized, Domain d)
        {
            Predicate p = null;
            int iExpression = 0;
            string sName = "";

            if (bParametrized)
                p = new ParametrizedPredicate(exp.Type);
            else
                p = new GroundedPredicate(exp.Type);
            bool bAllConstants = true;
            for (iExpression = 0; iExpression < exp.SubExpressions.Count; iExpression++)
            {
                sName = exp.SubExpressions[iExpression].ToString();
                if (bParametrized)
                {
                    Argument a = null;
                    if (sName.StartsWith("?"))
                    {
                        a = new Parameter(dParameterNameToType[sName], sName);
                        bAllConstants = false;
                    }
                    else
                    {
                        a = new Constant(d.ConstantNameToType[sName], sName);
                    }
                    ((ParametrizedPredicate)p).AddParameter(a);
                }
                else
                {
                    try
                    {
                        Constant c = new Constant(d.ConstantNameToType[sName], sName);
                        ((GroundedPredicate)p).AddConstant(c);
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine();
                    }
                }
            }
            if (bParametrized)
                if (!MatchParametersToPredicateDeclaration((ParametrizedPredicate)p, d))
                    throw new NotImplementedException();

            if (bParametrized && bAllConstants)
            {
                GroundedPredicate gp = new GroundedPredicate(p.Name);
                foreach (Constant c in ((ParametrizedPredicate)p).Parameters)
                    gp.AddConstant(c);
                p = gp;
            }

            PredicateFormula vf = new PredicateFormula(p);
            return vf;
        }
Ejemplo n.º 24
0
 private void ReadConstants(CompoundExpression exp, Domain d)
 {
     string sType = "?", sExp = "";
     List<string> lUndefined = new List<string>();
     for( int iExpression  = 0 ; iExpression < exp.SubExpressions.Count ; iExpression++)
     {
         sExp = exp.SubExpressions[iExpression].ToString().Trim();
         if ( sExp == "-")
         {
             sType = exp.SubExpressions[iExpression + 1].ToString();
             iExpression++;
             foreach (string sName in lUndefined)
                 d.AddConstant(new Constant(sType, sName));
             lUndefined.Clear();
         }
         else if( !sExp.StartsWith(":"))
         {
             lUndefined.Add(sExp);
         }
     }
     if (lUndefined.Count > 0)
     {
         //supporting objects with undefined types as type "OBJ"
         foreach(string sName in lUndefined)
             d.AddConstant(new Constant("OBJ", sName));
         //throw new NotImplementedException();
     }
 }
Ejemplo n.º 25
0
 private void ReadPrecondition(CompoundExpression exp, Action pa, Domain d, bool bParametrized)
 {
     string sOperator = exp.Type;
     Formula f = null;
     if (pa is ParametrizedAction)
         f = ReadFormula(exp, ((ParametrizedAction)pa).ParameterNameToType, bParametrized, d);
     else
         f = ReadFormula(exp, d.ConstantNameToType, bParametrized, d);
     pa.Preconditions = f;
 }
Ejemplo n.º 26
0
 private void ReadMetric(Problem p, Domain d, CompoundExpression eSub)
 {
     p.AddMetric(eSub.ToString());
 }
Ejemplo n.º 27
0
        private void ReadInitState(Problem p, Domain d, CompoundExpression eInitState)
        {
            foreach (Expression e in eInitState.SubExpressions)
            {
                CompoundExpression eSub = (CompoundExpression)e;
                if(d.IsFunctionExpression(eSub.Type))
                {
                    p.AddKnown(ReadFunctionExpression(eSub, null, d));
                }
                else if (d.ContainsPredicate(eSub.Type))
                {
                    p.AddKnown(ReadGroundedPredicate(eSub, d));
                }
                else
                {
                    if (eSub.Type != "unknown")
                    {
                        Formula f = ReadGroundedFormula(eSub, d);
                        if (f is CompoundFormula)
                            p.AddHidden((CompoundFormula)f);
                        if (f is PredicateFormula)//this happens in (not (p)) statments
                            p.AddKnown(((PredicateFormula)f).Predicate);
                    }
                    else
                    {
                        //causing a problem - add operand does some basic reasoning - adding p and ~p results in true for or statements.
                        //skipping unknown for now...

                        Predicate pUnknown = ReadGroundedPredicate((CompoundExpression)eSub.SubExpressions[0], d);
                        CompoundFormula cfOr = new CompoundFormula("or");
                        cfOr.SimpleAddOperand(pUnknown);
                        cfOr.SimpleAddOperand(pUnknown.Negate());
                        p.AddHidden(cfOr);

                    }
                }
            }
            p.ComputeRelevanceClosure();
        }
Ejemplo n.º 28
0
 private void ReadParameters(CompoundExpression exp, ParametrizedAction pa)
 {
     //unfortunately, expressions have a weird non standard structure with no type - (?i - pos ?j - pos )
     //so we must have a special case
     List<string> lTokens = exp.ToTokenList();
     List<string> lNames = new List<string>();
     string sType = "";
     int iCurrent = 0;
     while (iCurrent < lTokens.Count)
     {
         if (lTokens[iCurrent] == "-")
         {
             sType = lTokens[iCurrent + 1];
             foreach (string sName in lNames)
                 pa.AddParameter(new Parameter(sType, sName));
             lNames = new List<string>();
             sType = "";
             iCurrent += 2;
         }
         else
         {
             lNames.Add(lTokens[iCurrent]);
             iCurrent++;
         }
     }
     if (lNames.Count != 0) //allowing no types specified
     {
         foreach (string sName in lNames)
             pa.AddParameter(new Parameter("OBJ", sName));
     }
 }
Ejemplo n.º 29
0
 private bool IsUniversalQuantifier(CompoundExpression exp)
 {
     return exp.Type.ToLower() == "forall" || exp.Type.ToLower() == "exists";
 }
Ejemplo n.º 30
0
        private Problem ParseProblem(CompoundExpression exp, Domain d)
        {
            Problem            p    = null;
            CompoundExpression eSub = null;

            foreach (Expression e in exp.SubExpressions)
            {
                eSub = (CompoundExpression)e;
                if (eSub.Type == "problem")
                {
                    p = new Problem(eSub.SubExpressions.First().ToString(), d);
                }
                if (eSub.Type == ":domain")
                {
                    if (eSub.SubExpressions.First().ToString() != d.Name)
                    {
                        throw new InvalidDataException("Domain and problem files don't match!");
                    }
                }
                if (eSub.Type == ":objects")
                {
                    ReadConstants(eSub, d);
                }
                if (eSub.Type == ":init")
                {
                    CompoundExpression eAnd = (CompoundExpression)eSub.SubExpressions.First();
                    if (eAnd.Type == "and")
                    {
                        ReadInitState(p, d, eAnd);
                    }
                    else
                    {
                        ReadInitState(p, d, eSub);
                    }
                    //throw new InvalidDataException("Expecting 'and', got " + eAnd.Type);
                }
                if (eSub.Type == ":goal")
                {
                    ReadGoal(p, d, eSub.SubExpressions[0]);
                }
                if (eSub.Type == ":metric")
                {
                    ReadMetric(p, d, eSub);
                }
            }
            //p.AddReasoningActions(); not needed as long as we use FF to do the planning for us
            d.ComputeAlwaysKnown();
            p.CompleteKnownState();


            List <Predicate> lConstantPredicates = new List <Predicate>();

            foreach (Predicate pKnown in p.Known)
            {
                if (d.AlwaysConstant(pKnown))
                {
                    lConstantPredicates.Add(pKnown);
                }
            }
            //d.RemoveUniversalQuantifiers(lConstantPredicates);
            //p.RemoveUniversalQuantifiers();

            return(p);
        }
Ejemplo n.º 31
0
        private Formula ReadPredicate(CompoundExpression exp, Dictionary <string, string> dParameterNameToType, bool bParametrized, Domain d)
        {
            Predicate p           = null;
            int       iExpression = 0;
            string    sName       = "";

            if (bParametrized)
            {
                p = new ParametrizedPredicate(exp.Type);
            }
            else
            {
                p = new GroundedPredicate(exp.Type);
            }
            bool bAllConstants = true;

            for (iExpression = 0; iExpression < exp.SubExpressions.Count; iExpression++)
            {
                sName = exp.SubExpressions[iExpression].ToString();
                if (bParametrized)
                {
                    Argument a = null;
                    if (sName.StartsWith("?"))
                    {
                        a             = new Parameter(dParameterNameToType[sName], sName);
                        bAllConstants = false;
                    }
                    else
                    {
                        a = new Constant(d.ConstantNameToType[sName], sName);
                    }
                    ((ParametrizedPredicate)p).AddParameter(a);
                }
                else
                {
                    try
                    {
                        Constant c = new Constant(d.ConstantNameToType[sName], sName);
                        ((GroundedPredicate)p).AddConstant(c);
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine();
                    }
                }
            }
            if (bParametrized)
            {
                if (!MatchParametersToPredicateDeclaration((ParametrizedPredicate)p, d))
                {
                    throw new NotImplementedException();
                }
            }

            if (bParametrized && bAllConstants)
            {
                GroundedPredicate gp = new GroundedPredicate(p.Name);
                foreach (Constant c in ((ParametrizedPredicate)p).Parameters)
                {
                    gp.AddConstant(c);
                }
                p = gp;
            }


            PredicateFormula vf = new PredicateFormula(p);

            return(vf);
        }
Ejemplo n.º 32
0
 private Action ReadAction(CompoundExpression exp, Domain d)
 {
     string sName = exp.SubExpressions[0].ToString();
     sName = sName.Replace("_", "-");
     Action pa = null;
     int iExpression = 0;
     for (iExpression = 1; iExpression < exp.SubExpressions.Count; iExpression++)
     {
         if (exp.SubExpressions[iExpression].ToString() == ":parameters")
         {
             CompoundExpression ceParams = (CompoundExpression)exp.SubExpressions[iExpression + 1];
             if (ceParams.Type != "N/A")
             {
                 pa = new ParametrizedAction(sName);
                 ReadParameters((CompoundExpression)exp.SubExpressions[iExpression + 1], (ParametrizedAction)pa);
             }
             iExpression++;
         }
         else if (exp.SubExpressions[iExpression].ToString() == ":effect")
         {
             if (pa == null)
                 pa = new Action(sName);
             ReadEffect((CompoundExpression)exp.SubExpressions[iExpression + 1], pa, d, pa is ParametrizedAction);
             iExpression++;
         }
         else if (exp.SubExpressions[iExpression].ToString() == ":precondition")
         {
             if (pa == null)
                 pa = new Action(sName);
             ReadPrecondition((CompoundExpression)exp.SubExpressions[iExpression + 1], pa, d, pa is ParametrizedAction);
             iExpression++;
         }
         else if (exp.SubExpressions[iExpression].ToString() == ":observe")
         {
             if (pa == null)
                 pa = new Action(sName);
             ReadObserve((CompoundExpression)exp.SubExpressions[iExpression + 1], pa, d, pa is ParametrizedAction);
             iExpression++;
         }
     }
     return pa;
 }
Ejemplo n.º 33
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;
         }
     }
 }
Ejemplo n.º 34
0
        private Expression ToExpression(Stack<string> sStack)
        {
            string sToken = sStack.Pop();
            if (sToken == "(")
            {
                bool bDone = false;
                CompoundExpression exp = new CompoundExpression();
                sToken = sStack.Pop();
                if (sToken == ")")
                {
                    exp.Type = "N/A";
                    bDone = true;
                }
                else
                    exp.Type = sToken;
                while (!bDone)
                {
                    if (sStack.Count == 0)
                        throw new InvalidDataException("Exp " + exp.Type + " was not closed");
                    sToken = sStack.Pop();
                    if (sToken == ")")
                        bDone = true;
                    else
                    {
                        sStack.Push(sToken);
                        exp.SubExpressions.Add(ToExpression(sStack));
                    }

                }
                return exp;
            }
            else
            {
                return new StringExpression(sToken);
            }
        }
Ejemplo n.º 35
0
 private void ReadMetric(Problem p, Domain d, CompoundExpression eSub)
 {
     p.AddMetric(eSub.ToString());
 }
Ejemplo n.º 36
0
 private GroundedPredicate ReadGroundedPredicate(CompoundExpression exp, Domain d)
 {
     GroundedPredicate gp = new GroundedPredicate(exp.Type);
     int iExpression = 0;
     Constant c = null;
     string sName = "";
     for (iExpression = 0; iExpression < exp.SubExpressions.Count; iExpression++)
     {
         sName = exp.SubExpressions[iExpression].ToString();
         c = d.GetConstant(sName);
         gp.AddConstant(c);
     }
     return gp;
 }
Ejemplo n.º 37
0
        private Problem ParseProblem(CompoundExpression exp, Domain d)
        {
            Problem p = null;
            CompoundExpression eSub = null;
            foreach (Expression e in exp.SubExpressions)
            {
                eSub = (CompoundExpression)e;
                if (eSub.Type == "problem")
                {
                    p = new Problem(eSub.SubExpressions.First().ToString(), d);
                }
                if (eSub.Type == ":domain")
                {
                    if (eSub.SubExpressions.First().ToString() != d.Name)
                        throw new InvalidDataException("Domain and problem files don't match!");
                }
                if (eSub.Type == ":objects")
                {
                    ReadConstants(eSub, d);
                }
                if (eSub.Type == ":init")
                {
                    CompoundExpression eAnd = (CompoundExpression)eSub.SubExpressions.First();
                    if (eAnd.Type == "and")
                        ReadInitState(p, d, eAnd);
                    else
                        ReadInitState(p, d, eSub);
                    //throw new InvalidDataException("Expecting 'and', got " + eAnd.Type);
                }
                if (eSub.Type == ":goal")
                    ReadGoal(p, d, eSub.SubExpressions[0]);
                if (eSub.Type == ":metric")
                    ReadMetric(p, d, eSub);
            }
            //p.AddReasoningActions(); not needed as long as we use FF to do the planning for us
            d.ComputeAlwaysKnown();
            p.CompleteKnownState();

            List<Predicate> lConstantPredicates = new List<Predicate>();
            foreach (Predicate pKnown in p.Known)
            {
                if (d.AlwaysConstant(pKnown))
                    lConstantPredicates.Add(pKnown);
            }
            //d.RemoveUniversalQuantifiers(lConstantPredicates);
            //p.RemoveUniversalQuantifiers();

            return p;
        }
Ejemplo n.º 38
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;
     }
 }
Ejemplo n.º 39
0
        public Predicate ReadFunctionExpression(CompoundExpression exp, Dictionary<string, string> dParameterNameToType, Domain d)
        {
            Constant c = null;
            string sName = exp.SubExpressions[0].ToString();

            if (exp.Type == "=")
            {
                string sParam1 = exp.SubExpressions[0].ToString();
                string sParam2 = exp.SubExpressions[1].ToString();
                if (!dParameterNameToType.ContainsKey(sParam1))
                    throw new ArgumentException("First argument of = must be a parameter");
                ParametrizedPredicate pp = new ParametrizedPredicate("=");
                pp.AddParameter(new Parameter(dParameterNameToType[sParam1], sParam1));
                if (dParameterNameToType.ContainsKey(sParam2))
                    pp.AddParameter(new Parameter(dParameterNameToType[sParam2], sParam2));
                else
                    pp.AddParameter(new Constant(d.ConstantNameToType[sParam2], sParam2));
                return pp;

            }

            GroundedPredicate p = new GroundedPredicate(exp.Type);
            double dValue = 0.0;
            if (d.Functions.Contains(sName))
                c = new Constant("Function", sName);
            else
                throw new ArgumentException("First argument of increase or decrease must be a function");
            p.AddConstant(c);

            sName = exp.SubExpressions[1].ToString();
            if (double.TryParse(sName, out dValue))
                c = new Constant("Number", sName);
            else
                throw new ArgumentException("Second argument of increase or decrease must be a number");
            p.AddConstant(c);
            return p;
        }
Ejemplo n.º 40
0
 private Predicate ReadPredicate(CompoundExpression exp, Domain d)
 {
     ParametrizedPredicate pp = new ParametrizedPredicate(exp.Type);
     int iExpression = 0;
     Parameter p = null;
     string sName = "";
     List<Parameter> lUntypedParameters = new List<Parameter>();
     for (iExpression = 0; iExpression < exp.SubExpressions.Count; iExpression++)
     {
         sName = exp.SubExpressions[iExpression].ToString();
         if (sName == "-")
         {
             string sType = exp.SubExpressions[iExpression + 1].ToString();
             foreach (Parameter pUntyped in lUntypedParameters)
                 pUntyped.Type = sType;
             lUntypedParameters.Clear();
             iExpression++;//skip the "-" and the type
         }
         else
         {
             p = new Parameter("", sName);
             lUntypedParameters.Add(p);
             pp.AddParameter(p);
         }
     }
     if (d.Types.Count == 1)
     {
         foreach (Parameter pUntyped in lUntypedParameters)
             pUntyped.Type = d.Types[0];
     }
     return pp;
 }