public override Formula RemoveUniversalQuantifiers(HashSet <Constant> lConstants, List <Predicate> lConstantPredicates, Domain d)
        {
            ProbabilisticFormula pf = new ProbabilisticFormula();

            for (int i = 0; i < Options.Count; i++)
            {
                pf.AddOption(Options[i].RemoveUniversalQuantifiers(lConstants, lConstantPredicates, d), Probabilities[i]);
            }
            return(pf);
        }
Beispiel #2
0
        public List <string> Plan(string agent, bool bUseFF, bool bUseFD, Domain d, Problem p, State curentState, Formula goal, List <Action> privateActions, int cMaxMilliseconds, out bool bUnsolvable)
        {
            //Program.KillPlanners();
            List <string> lPlan = null;


            if (privateActions != null)
            {
                d.Actions = privateActions;
            }
            if (goal != null)
            {
                p.Goal = goal;
            }

            string  sFDPath = @"C:\cygwin\home\shlomi\FastDownward\src\";
            Process pFF = null, pFD = null;

            if (bUseFF)
            {
                pFF = RunFF(d.WriteSimpleDomain(), p.WriteSimpleProblem(curentState));
            }
            if (bUseFD)
            {
                pFD = RunFD(agent, sFDPath, d.WriteSimpleDomain(), p.WriteSimpleProblem(curentState));
            }
            bUnsolvable = false;
            bool bFFDone = false, bFDDone = false;

            Process[] process;
            if (bUseFF && !bUseFD)
            {
                process = new Process[] { pFF }
            }
            ;
            else
            if (!bUseFF && bUseFD)
            {
                process = new Process[] { pFD }
            }
            ;
            else
            {
                process = new Process[] { pFF, pFD }
            };

            if (WaitForProcesses(process, cMaxMilliseconds, out bFFDone, out bFDDone))
            {
                if (bFFDone)
                {
                    //  Console.WriteLine("Plan found by FF");
                    Thread.Sleep(200);
                    lPlan = ReadFFPlan(process[0].Id, out bUnsolvable);
                    KillAll(process.ToList());
                }
                else if (bFDDone)
                {
                    Console.WriteLine("Plan found by FD");
                    Thread.Sleep(100);
                    lPlan = ReadPlan(@"C:\cygwin\home\shlomi\");
                    KillAll(process.ToList());
                }
                return(lPlan);
            }
            return(null);
        }
Beispiel #3
0
        public static int MaxMakespanCalculation(List <Agent> agents, List <string> lActionsName, Domain dJoint)
        {
            List <KeyValuePair <string, Action> > sPlan = Runner.GetActions(lActionsName, dJoint, agents);
            State fullInitialState = new State(agents[0].startState);
            Dictionary <string, int> agentsTimeSteps = new Dictionary <string, int>();

            agentsTimeSteps.Add(agents[0].name, 0);
            for (int i = 1; i < agents.Count; i++)
            {
                foreach (var fact in agents[i].startState.m_lPredicates)
                {
                    fullInitialState.AddPredicate(fact);
                }
                agentsTimeSteps.Add(agents[i].name, 0);
            }
            Dictionary <GroundedPredicate, int> gpCost = new Dictionary <GroundedPredicate, int>();

            foreach (GroundedPredicate gp in fullInitialState.m_lPredicates)
            {
                gpCost.Add(gp, 0);
            }


            int  timeSteps = 0;
            bool stop      = false;

            foreach (var action in sPlan)
            {
                int cost = MaxCost(gpCost, action.Value.Preconditions.GetAllPredicates().ToList());
                if (cost < agentsTimeSteps[action.Key])
                {
                    cost = agentsTimeSteps[action.Key];
                }

                HashSet <Predicate> lDeleteList = new HashSet <Predicate>(), lAddList = new HashSet <Predicate>();
                fullInitialState.GetApplicableEffects(action.Value.Effects, lAddList, lDeleteList);
                foreach (GroundedPredicate dellp in lDeleteList)
                {
                    gpCost.Remove((GroundedPredicate)dellp.Negate());
                }
                foreach (GroundedPredicate addp in lAddList)
                {
                    if (!gpCost.ContainsKey(addp))
                    {
                        gpCost.Add(addp, cost + 1);
                    }
                }
                foreach (GroundedPredicate addp in lDeleteList)
                {
                    if (!gpCost.ContainsKey(addp))
                    {
                        gpCost.Add(addp, cost + 1);
                    }
                }

                agentsTimeSteps[action.Key] = cost + 1;
                fullInitialState            = fullInitialState.Apply(action.Value);
            }
            int maxSpan = 0;

            foreach (Agent agent in agents)
            {
                if (agentsTimeSteps[agent.name] > maxSpan)
                {
                    maxSpan = agentsTimeSteps[agent.name];
                }
            }
            return(maxSpan);
        }
        /*
         *      public void WriteTaggedProblemNoState(string sProblemFile, Dictionary<string, List<Predicate>> dTags, IEnumerable<Predicate> lObserved,
         *                                               Dictionary<string, double> dFunctionValues)
         *      {
         *          StreamWriter sw = new StreamWriter(sProblemFile);
         *          sw.WriteLine("(define (problem K" + Name + ")");
         *          sw.WriteLine("(:domain K" + Domain.Name + ")");
         *          sw.WriteLine("(:init"); //ff doesn't like the and (and");
         *
         *          string sKP = "";
         *          if (Domain.TIME_STEPS > 0)
         *              sw.WriteLine("(time0)");
         *          foreach (KeyValuePair<string, double> f in dFunctionValues)
         *          {
         *              sw.WriteLine("(= " + f.Key + " " + f.Value + ")");
         *          }
         *          foreach (GroundedPredicate gp in lObserved)
         *          {
         *              //if (gp.Negation)
         *              //    continue;
         *              if (gp.Name == "Choice" || gp.Name == Domain.OPTION_PREDICATE)
         *                  continue;
         *              if (Domain.AlwaysKnown(gp) && Domain.AlwaysConstant(gp))
         *              {
         *                  sKP = "(K" + gp.Name;
         *                  foreach (Constant c in gp.Constants)
         *                  {
         *                      sKP += " " + c.Name;
         *                  }
         *                  if (gp.Negation)
         *                      sKP += " " + Domain.FALSE_VALUE;
         *                  else
         *                      sKP += " " + Domain.TRUE_VALUE;
         *
         *                  sw.WriteLine(sKP + ")");
         *              }
         *              else
         *              {
         *                  foreach (string sTag in dTags.Keys)
         *                  {
         *                      if (!gp.Negation)
         *                          sw.WriteLine(gp.GenerateGiven(sTag));
         *                      if (!Domain.AlwaysKnown(gp))
         *                          sw.WriteLine(gp.GenerateKnowGiven(sTag, true));
         *                  }
         *              }
         *          }
         *          foreach (KeyValuePair<string, List<Predicate>> p in dTags)
         *          {
         *
         *              foreach (GroundedPredicate gp in p.Value)
         *              {
         *                  if (gp.Negation)
         *                      continue;
         *                  if (gp.Name == "Choice")
         *                      continue;
         *                  if (!gp.Negation)
         *                  {
         *                      sKP = GenerateKnowGivenLine(gp, p.Key, false);
         *                      sw.WriteLine(sKP);
         *                  }
         *                  //sKP = GenerateKnowGivenLine(gp, p.Key, true);
         *                  //sw.WriteLine(sKP);
         *              }
         *
         *              if (SDRPlanner.AddAllKnownToGiven)
         *              {
         *                  foreach (GroundedPredicate gp in lObserved)
         *                  {
         *                      //if (gp.Negation)
         *                      //    continue;
         *                      if (gp.Name == "Choice")
         *                          continue;
         *                      if (!(Domain.AlwaysKnown(gp) && Domain.AlwaysConstant(gp)))
         *                      {
         *                          if (!gp.Negation)
         *                          {
         *                              sKP = GenerateKnowGivenLine(gp, p.Key, false);
         *                              sw.WriteLine(sKP);
         *                          }
         *                      }
         *                      if (!(Domain.AlwaysKnown(gp)) && gp.Name != Domain.OPTION_PREDICATE)
         *                      {
         *                          sKP = GenerateKnowGivenLine(gp, p.Key, true);
         *                          sw.WriteLine(sKP);
         *                      }
         *                  }
         *              }
         *
         *          }
         *
         *          //if (Problem.Domain.HasNonDeterministicActions())
         *          //    sw.WriteLine("(option opt0)");
         *
         *          if (SDRPlanner.SplitConditionalEffects)
         *              sw.WriteLine("(NotInAction)");
         *
         *          sw.WriteLine(")");
         *
         *          CompoundFormula cfGoal = new CompoundFormula("and");
         *
         *          List<Predicate> lGoalPredicates = new List<Predicate>();
         *          Goal.GetAllPredicates(lGoalPredicates);
         *
         *
         *          for (int iTag = 0; iTag < dTags.Count; iTag++)
         *          {
         *              if (SDRPlanner.ConsiderStateNegations && iTag == dTags.Count - 1)
         *                  break;
         *              string sTag = dTags.Keys.ElementAt(iTag);
         *              foreach (Predicate p in lGoalPredicates)
         *              {
         *                  if (!Domain.AlwaysKnown(p) || !Domain.AlwaysConstant(p))
         *                  {
         *                      cfGoal.AddOperand(p.GenerateGiven(sTag));
         *                      if (!Domain.AlwaysKnown(p))
         *                          cfGoal.AddOperand(p.GenerateKnowGiven(sTag, true));
         *                  }
         *              }
         *          }
         *
         *
         *          sw.WriteLine("(:goal " + cfGoal + ")");
         *          //sw.WriteLine("))");
         *          if (MetricStatement != null)
         *          {
         *              sw.WriteLine(MetricStatement);
         *          }
         *          sw.WriteLine(")");
         *          sw.Close();
         *      }
         */

        public void WriteTaggedProblemNoState(string sProblemFile, Dictionary <string, List <Predicate> > dTags, IEnumerable <Predicate> lObserved,
                                              Dictionary <string, double> dFunctionValues)
        {
            StreamWriter sw = new StreamWriter(sProblemFile);

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

            string sKP = "";

            if (Domain.TIME_STEPS > 0)
            {
                sw.WriteLine("(time0)");
            }
            foreach (KeyValuePair <string, double> f in dFunctionValues)
            {
                sw.WriteLine("(= " + f.Key + " " + f.Value + ")");
            }
            foreach (GroundedPredicate gp in lObserved)
            {
                //if (gp.Negation)
                //    continue;
                if (gp.Name == "Choice" || gp.Name == Domain.OPTION_PREDICATE)
                {
                    continue;
                }
                if (Domain.AlwaysKnown(gp) && Domain.AlwaysConstant(gp))
                {
                    sKP = "(" + gp.Name;
                    foreach (Constant c in gp.Constants)
                    {
                        sKP += " " + c.Name;
                    }
                    sw.WriteLine(sKP + ")");
                }
                else
                {
                    foreach (string sTag in dTags.Keys)
                    {
                        if (!gp.Negation)
                        {
                            Predicate pGiven = gp.GenerateGiven(sTag);
                            sw.WriteLine(pGiven);
                        }
                    }
                }
            }
            foreach (KeyValuePair <string, List <Predicate> > p in dTags)
            {
                foreach (GroundedPredicate gp in p.Value)
                {
                    if (gp.Negation)
                    {
                        continue;
                    }
                    if (gp.Name == "Choice")
                    {
                        continue;
                    }
                    if (!gp.Negation)
                    {
                        sw.WriteLine(gp.GenerateGiven(p.Key));
                    }
                    //sKP = GenerateKnowGivenLine(gp, p.Key, true);
                    //sw.WriteLine(sKP);
                }
            }

            //if (Problem.Domain.HasNonDeterministicActions())
            //    sw.WriteLine("(option opt0)");

            //if (SDRPlanner.SplitConditionalEffects)
            sw.WriteLine("(NotInAction)");

            sw.WriteLine(")");

            CompoundFormula cfGoal = new CompoundFormula("and");

            HashSet <Predicate> lGoalPredicates = new HashSet <Predicate>();

            Goal.GetAllPredicates(lGoalPredicates);


            for (int iTag = 0; iTag < dTags.Count; iTag++)
            {
                if (SDRPlanner.ConsiderStateNegations && iTag == dTags.Count - 1)
                {
                    break;//What is that?
                }
                string sTag = dTags.Keys.ElementAt(iTag);
                foreach (Predicate p in lGoalPredicates)
                {
                    if (!Domain.AlwaysKnown(p) || !Domain.AlwaysConstant(p))
                    {
                        cfGoal.AddOperand(p.GenerateGiven(sTag));
                    }
                }
            }

            if (SDRPlanner.ForceTagObservations)
            {
                foreach (string sTag1 in dTags.Keys)
                {
                    foreach (string sTag2 in dTags.Keys)
                    {
                        if (sTag1 != sTag2)
                        {
                            GroundedPredicate gpNot = new GroundedPredicate("Knot");
                            gpNot.AddConstant(new Constant(Domain.TAG, sTag1));
                            gpNot.AddConstant(new Constant(Domain.TAG, sTag2));
                            cfGoal.AddOperand(gpNot);
                        }
                    }
                }
            }

            sw.WriteLine("(:goal " + cfGoal + ")");
            //sw.WriteLine("))");
            if (MetricStatement != null)
            {
                sw.WriteLine(MetricStatement);
            }
            sw.WriteLine(")");
            sw.Close();
        }
Beispiel #5
0
        public List <string> PdbFFPlan(Domain d, Problem p, State curentState, Formula goal, List <Action> privateActions, int cMaxMilliseconds, out bool bUnsolvable)
        {
            RunUtils.KillPlanners();
            List <string> ffLplan = new List <string>();
            List <string> lPlan   = new List <string>();


            if (privateActions != null)
            {
                d.Actions = privateActions;
            }
            if (goal != null)
            {
                p.Goal = goal;
            }

            MemoryStream msDomain    = d.WriteSimpleDomain();
            MemoryStream problem_M_S = p.WriteSimpleProblem(curentState);


            //StreamWriter swDomainFile = new StreamWriter(@"C:\Dropbox-users\shlomi\Dropbox\Dropbox\privacyPreserving\Competition\all\factored\testd.pddl");
            // StreamReader srDomain = new StreamReader(msDomain);
            // swDomainFile.Write(srDomain.ReadToEnd());
            // swDomainFile.Close();

            // StreamWriter swProblemFile = new StreamWriter(@"C:\Dropbox-users\shlomi\Dropbox\Dropbox\privacyPreserving\Competition\all\factored\testp.pddl");
            //  StreamReader srProblem = new StreamReader(problem_M_S);
            // swProblemFile.Write(srProblem.ReadToEnd());
            // swProblemFile.Close();


            problem_M_S.Position = 0;
            msDomain.Position    = 0;



            Process planer = new Process();

            //planer.StartInfo.WorkingDirectory = @"C:\project\Planning 2 new\PDDLTEST\temp";
            planer.StartInfo.FileName = "ff.exe";
            //planer.StartInfo.Arguments += "-o dT.pddl -f pT.pddl";

            FFOutput[planer.Id] = "";

            planer.StartInfo.UseShellExecute        = false;
            planer.StartInfo.RedirectStandardInput  = true;
            planer.StartInfo.RedirectStandardOutput = true;
            planer.OutputDataReceived += new DataReceivedEventHandler(FFOutputHandler);
            planer.Start();
            planer.BeginOutputReadLine();
            StreamReader srOps = new StreamReader(msDomain);


            string domain = srOps.ReadToEnd();

            planer.StandardInput.Write(domain);
            srOps.Close();
            BinaryWriter b = new BinaryWriter(planer.StandardInput.BaseStream);

            b.Write('\0');

            StreamReader srFct   = new StreamReader(problem_M_S);
            string       problem = srFct.ReadToEnd();

            planer.StandardInput.Write(problem);
            srFct.Close();

            b.Write('\0');
            //planer.StandardInput.Flush();

            planer.StandardInput.Close();

            if (cMaxMilliseconds != -1)
            {
                if (!planer.WaitForExit(cMaxMilliseconds))//2 minutes max
                {
                    planer.Kill();
                    bUnsolvable = false;
                    return(null);
                }
            }
            else
            {
                planer.WaitForExit();
            }
            planer.Close();
            //string sOutput = planer.StandardOutput.ReadToEnd();
            // planer.WaitForExit();
            string sOutput = FFOutput[planer.Id];
            //throw new NotImplementedException();
            //Console.WriteLine(sOutput);
            MemoryStream planMs = new MemoryStream();

            if (sOutput.Contains("found legal plan as follows"))
            {
                string sPlan = sOutput.Substring(sOutput.IndexOf("found legal plan as follows"));
                sPlan = sPlan.Replace("found legal plan as follows", "").Trim();
                string[] asPlan     = sPlan.Split('\n');
                string   sFinalPlan = "";
                for (int i = 0; i < asPlan.Length; i++)
                {
                    if (!asPlan[i].Contains(":"))
                    {
                        break;
                    }
                    if (asPlan[i].Contains("time spent:"))
                    {
                        break;
                    }
                    sFinalPlan += asPlan[i].Substring(asPlan[i].IndexOf(':') + 2).Trim() + "\n";
                }
                StreamWriter sw = new StreamWriter(planMs);
                sw.WriteLine(sFinalPlan);
                sw.Close();
                bUnsolvable = false;
            }
            else
            {
                if (sOutput.Contains("goal can be simplified to TRUE"))
                {
                    ffLplan     = new List <string>();
                    bUnsolvable = false;
                    return(ffLplan);
                }
                else if (sOutput.Contains("goal can be simplified to FALSE"))
                {
                    ffLplan     = null;
                    bUnsolvable = true;
                    return(null);
                }
                else
                {
                    ffLplan     = null;
                    bUnsolvable = false;
                    return(null);
                }
            }


            lPlan   = ReadPlan(new MemoryStream(planMs.ToArray()));
            ffLplan = lPlan;
            return(lPlan);
        }
Beispiel #6
0
        private Argument ReadFunctionArgument(Expression exp, Dictionary <string, string> dParameterNameToType, Domain d)
        {
            string sName  = exp.ToString();
            double dValue = 0.0;

            if (double.TryParse(sName, out dValue))
            {
                Constant c = new Constant("Number", sName);
                return(c);
            }
            if (exp is CompoundExpression)
            {
                CompoundExpression ce = (CompoundExpression)exp;
                if (!d.Functions.Contains(ce.Type))
                {
                    throw new ArgumentException("Function arguments can only be numbers, parameters, or functions");
                }
                ParameterizedFunctionPredicate fp = new ParameterizedFunctionPredicate(ce.Type);
                foreach (Expression e in ce.SubExpressions)
                {
                    string sParam = e.ToString();
                    if (dParameterNameToType != null && dParameterNameToType.ContainsKey(sParam))
                    {
                        fp.AddParameter(new Parameter(dParameterNameToType[sParam], sParam));
                    }
                    else if (d.ConstantNameToType.ContainsKey(sParam))
                    {
                        fp.AddParameter(new Constant(d.ConstantNameToType[sParam], sParam));
                    }
                    else
                    {
                        throw new ArgumentException("Unknown function parameter");
                    }
                }
                FunctionParameter fa = new FunctionParameter(fp);
                return(fa);
            }
            if (dParameterNameToType.ContainsKey(sName))
            {
                return(new Parameter(dParameterNameToType[sName], sName));
            }
            throw new ArgumentException("Unknown function parameter");
        }
        public void WriteTaggedProblem(string sProblemFile, Dictionary <string, List <Predicate> > dTags, IEnumerable <Predicate> lObserved,
                                       List <Predicate> lTrueState, Dictionary <string, double> dFunctionValues, bool bOnlyIdentifyStates)
        {
            StreamWriter sw = new StreamWriter(sProblemFile);

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

            string sKP = "", sP = "";

            if (Domain.TIME_STEPS > 0)
            {
                sw.WriteLine("(time0)");
            }
            if (SDRPlanner.SplitConditionalEffects)
            {
                sw.WriteLine("(NotInAction)\n");
            }
            foreach (KeyValuePair <string, double> f in dFunctionValues)
            {
                sw.WriteLine("(= " + f.Key + " " + f.Value + ")");
            }
            foreach (GroundedPredicate gp in lObserved)
            {
                if (gp.Name == "Choice")
                {
                    continue;
                }
                sKP = "(K" + gp.Name;
                sP  = "(" + gp.Name;
                foreach (Constant c in gp.Constants)
                {
                    sKP += " " + c.Name;
                    sP  += " " + c.Name;
                }
                if (gp.Negation)
                {
                    sKP += " " + Domain.FALSE_VALUE;
                }
                else
                {
                    sKP += " " + Domain.TRUE_VALUE;
                }
                if (!Domain.AlwaysKnown(gp))
                {
                    sw.WriteLine(sKP + ")");
                }
                if (!gp.Negation)
                {
                    sw.WriteLine(sP + ")");
                }
            }
            foreach (GroundedPredicate gp in lTrueState)
            {
                if (gp.Name == "Choice")
                {
                    continue;
                }
                if (!gp.Negation)
                {
                    sP = "(" + gp.Name;
                    foreach (Constant c in gp.Constants)
                    {
                        sP += " " + c.Name;
                    }
                    sw.WriteLine(sP + ")");
                }
            }
            foreach (KeyValuePair <string, List <Predicate> > p in dTags)
            {
                foreach (GroundedPredicate gp in p.Value)
                {
                    if (gp.Name == "Choice")
                    {
                        continue;
                    }
                    sKP = GenerateKnowGivenLine(gp, p.Key, false);
                    sw.WriteLine(sKP);
                }

                if (SDRPlanner.AddAllKnownToGiven)
                {
                    foreach (GroundedPredicate gp in lObserved)
                    {
                        if (gp.Name == "Choice")
                        {
                            continue;
                        }
                        if (!Domain.AlwaysKnown(gp))
                        {
                            sKP = GenerateKnowGivenLine(gp, p.Key, false);
                            sw.WriteLine(sKP);
                        }
                    }
                }
            }

            //if (Problem.Domain.HasNonDeterministicActions())
            //    sw.WriteLine("(option opt0)");

            sw.WriteLine(")");

            CompoundFormula cfGoal = new CompoundFormula("and");

            if (!bOnlyIdentifyStates)
            {
                cfGoal.AddOperand(Goal);
                HashSet <Predicate> lGoalPredicates = new HashSet <Predicate>();
                Goal.GetAllPredicates(lGoalPredicates);
                //string sGoal = Problem.Goal.ToString();
                //sw.WriteLine("(:goal " + sGoal + ")");

                //sw.Write("(:goal (and ");
                //sw.Write( sGoal );

                foreach (Predicate p in lGoalPredicates)
                {
                    //Problem.Domain.WriteKnowledgePredicate(sw, p);
                    if (!Domain.AlwaysKnown(p))
                    {
                        cfGoal.AddOperand(new KnowPredicate(p));
                    }
                }
            }
            if (bOnlyIdentifyStates || SDRPlanner.AddTagRefutationToGoal)
            {
                for (int iTag = 1; iTag < dTags.Count; iTag++)
                {
                    GroundedPredicate gp = new GroundedPredicate("KNot");
                    gp.AddConstant(new Constant("TAG_TYPE", "tag" + iTag));
                    cfGoal.AddOperand(gp);
                    //sw.Write(" (KNot t" + iTag + ")");
                }
            }
            if (SDRPlanner.ForceTagObservations)
            {
                foreach (Predicate p in lTrueState)
                {
                    if (Domain.Observable(p))
                    {
                        cfGoal.AddOperand(new KnowPredicate(p));
                    }
                }
            }
            sw.WriteLine("(:goal " + cfGoal + ")");
            //sw.WriteLine("))");
            if (MetricStatement != null)
            {
                sw.WriteLine(MetricStatement);
            }
            sw.WriteLine(")");
            sw.Close();
        }
Beispiel #8
0
 private void ReadMetric(Problem p, Domain d, CompoundExpression eSub)
 {
     p.AddMetric(eSub.ToString());
 }
Beispiel #9
0
        private void ReadGoal(Problem p, Domain d, Expression eGoal)
        {
            Formula fGoal = ReadGroundedFormula((CompoundExpression)eGoal, d);

            p.Goal = fGoal;
        }
Beispiel #10
0
        public Problem ParseProblem(string sProblemFile, Domain d)
        {
            StreamReader       sr  = new StreamReader(sProblemFile);
            CompoundExpression exp = (CompoundExpression)ToExpression(sr);

            sr.Close();
            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);
                }
            }
            //Problem here - sometimes ~p and p are both effects (remove p and immediately add it), and the current code forbids that - not sure what to do...
            //d.RemoveUniversalQuantifiers(lConstantPredicates);
            //p.RemoveUniversalQuantifiers();


            d.ComputePrivateActions();

            return(p);
        }
Beispiel #11
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);
            }
            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);
                    }
                    else
                    {
                        a = new Constant(d.ConstantNameToType[sName], sName);
                    }
                    ((ParametrizedPredicate)p).AddParameter(a);
                }
                else
                {
                    Constant c = new Constant(d.ConstantNameToType[sName], sName);
                    ((GroundedPredicate)p).AddConstant(c);
                }
            }
            if (bParametrized)
            {
                if (!MatchParametersToPredicateDeclaration((ParametrizedPredicate)p, d))
                {
                    throw new NotImplementedException();
                }
            }

            PredicateFormula vf = new PredicateFormula(p);

            return(vf);
        }
Beispiel #12
0
        private Formula ReadFormula(CompoundExpression exp, Dictionary <string, string> dParameterNameToType, bool bParamterized, Domain d)
        {
            bool bPredicate = true;

            //Debug.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
            {
                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);
                }
            }
        }
Beispiel #13
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();

            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")
                    {
                        d.Types.Add("object");
                        List <string>      lTypes = new List <string>();
                        CompoundExpression eTypes = ((CompoundExpression)eSub);
                        for (int i = 0; i < eTypes.SubExpressions.Count; i++)
                        {
                            Expression eType = eTypes.SubExpressions[i];
                            string     sType = eType.ToString().Trim();
                            if (sType == "-")
                            {
                                Expression eParentType = eTypes.SubExpressions[i + 1];
                                i++;
                                string sParentType = eParentType.ToString();
                                foreach (string sSubType in lTypes)
                                {
                                    d.TypeHierarchy[sSubType] = sParentType;
                                }
                                lTypes = new List <string>();
                            }
                            else
                            {
                                d.Types.Add(sType);
                                lTypes.Add(sType);
                            }
                        }
                    }
                    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);
        }
Beispiel #14
0
        public ParameterizedFunctionPredicate ReadFunctionExpression(CompoundExpression exp, Dictionary <string, string> dParameterNameToType, Domain d)
        {
            ParameterizedFunctionPredicate pp = new ParameterizedFunctionPredicate(exp.Type);

            foreach (Expression e in exp.SubExpressions)
            {
                pp.AddParameter(ReadFunctionArgument(e, dParameterNameToType, d));
            }
            return(pp);
        }