public GroundedPredicate Ground(Dictionary <string, Constant> dBindings)
        {
            GroundedPredicate gp = new GroundedPredicate("K" + Knowledge.Name);

            if (Knowledge is ParametrizedPredicate)
            {
                foreach (Argument a in ((ParametrizedPredicate)Knowledge).Parameters)
                {
                    if (a is Parameter)
                    {
                        if (dBindings.ContainsKey(a.Name))
                        {
                            gp.AddConstant(dBindings[a.Name]);
                        }
                        else
                        {
                            throw new NotImplementedException();
                        }
                    }
                    else
                    {
                        gp.AddConstant((Constant)a);
                    }
                }
            }
            else
            {
                foreach (Constant c in ((GroundedPredicate)Knowledge).Constants)
                {
                    gp.AddConstant(c);
                }
            }
            return(gp);
        }
Exemplo n.º 2
0
        public Predicate PartiallyGround(Dictionary <string, Constant> dBindings)
        {
            GroundedPredicate     gpred = new GroundedPredicate(Name);
            ParametrizedPredicate ppred = new ParametrizedPredicate(Name);

            gpred.Negation = Negation;
            ppred.Negation = Negation;
            bool bAllGrounded = true;

            foreach (Argument a in Parameters)
            {
                if (a is Parameter)
                {
                    if (!dBindings.ContainsKey(a.Name))
                    {
                        ppred.AddParameter(a);
                        bAllGrounded = false;
                    }
                    else
                    {
                        ppred.AddParameter(dBindings[a.Name]);
                        gpred.AddConstant(dBindings[a.Name]);
                    }
                }
                else
                {
                    gpred.AddConstant((Constant)a);
                    ppred.AddParameter(a);
                }
            }
            if (bAllGrounded)
            {
                if (gpred.Name == "=")
                {
                    bool bSame = gpred.Constants[0].Equals(gpred.Constants[1]);
                    if (bSame && !Negation || !bSame && Negation)
                    {
                        return(new GroundedPredicate(Domain.TRUE_PREDICATE));
                    }
                    else
                    {
                        return(new GroundedPredicate(Domain.FALSE_PREDICATE));
                    }
                }
                return(gpred);
            }
            else
            {
                return(ppred);
            }
        }
        public override Predicate GenerateGiven(string sTag)
        {
            GroundedPredicate pGiven = new GroundedPredicate("Given" + Name);

            foreach (Constant c in Constants)
            {
                pGiven.AddConstant(c);
            }
            pGiven.AddConstant(new Constant(Domain.TAG, sTag));
            if (Negation)
            {
                return(pGiven.Negate());
            }
            return(pGiven);
        }
        //for MPSR
        public static Predicate GenerateKNot(Constant cTag1, Constant cTag2)
        {
            GroundedPredicate gp = new GroundedPredicate("KNot");
            int iTag1            = int.Parse(cTag1.Name.Substring(3));
            int iTag2            = int.Parse(cTag2.Name.Substring(3));

            if (iTag1 < iTag2)
            {
                gp.AddConstant(cTag1);
                gp.AddConstant(cTag2);
            }
            else
            {
                gp.AddConstant(cTag2);
                gp.AddConstant(cTag1);
            }
            return(gp);
        }
Exemplo n.º 5
0
        public virtual GroundedPredicate Ground(Dictionary <string, Constant> dBindings)
        {
            GroundedPredicate gpred = new GroundedPredicate(Name);

            gpred.Negation = Negation;
            foreach (Argument a in Parameters)
            {
                if (a is Parameter)
                {
                    if (!dBindings.ContainsKey(a.Name))
                    {
                        return(null);
                    }
                    gpred.AddConstant(dBindings[a.Name]);
                }
                else
                {
                    gpred.AddConstant((Constant)a);
                }
            }
            return(gpred);
        }
        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);
        }
        /*
         *      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();
        }
        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();
        }