public void CompleteKnownState()
        {
            List <string> lKnownPredicates = new List <string>();

            foreach (Predicate p in m_lKnown)
            {
                if (!lKnownPredicates.Contains(p.Name))
                {
                    lKnownPredicates.Add(p.Name);
                }
            }
            // List<GroundedPredicate> lGrounded = Domain.GroundAllPredicates(lKnownPredicates);
            HashSet <GroundedPredicate> lGrounded = Domain.GroundAllPredicates();
            HashSet <Predicate>         lUnknown  = new HashSet <Predicate>();

            foreach (Formula f in m_lHidden)
            {
                f.GetAllPredicates(lUnknown);
            }
            foreach (GroundedPredicate gp in lGrounded)
            {
                if (!(Domain.AlwaysConstant(gp) && Domain.AlwaysKnown(gp)))         //not sure why I thouhgt that constant predicates do not apply here. We need them for planning in K domain.
                {
                    if (lUnknown.Contains(gp) || lUnknown.Contains(gp.Negate()) || m_lKnown.Contains(gp) || m_lKnown.Contains(gp.Negate()))
                    {
                        //do nothing
                    }
                    else
                    {
                        m_lKnown.Add(gp.Negate());
                    }
                }
            }
        }
 public override Formula RemoveUniversalQuantifiers(HashSet <Constant> lConstants, List <Predicate> lConstantPredicates, Domain d)
 {
     if (d != null && lConstantPredicates != null && d.AlwaysConstant(Predicate) && d.AlwaysKnown(Predicate) && !(Predicate is ParametrizedPredicate))
     {
         Predicate p = Predicate;
         if (p.Negation)
         {
             p = p.Negate();
         }
         bool bContains = lConstantPredicates.Contains(p);
         //assuming that list does not contain negations
         if ((bContains && !Predicate.Negation) || (!bContains && Predicate.Negation))
         {
             return(new PredicateFormula(new GroundedPredicate(Domain.TRUE_PREDICATE)));
         }
         else
         {
             return(new PredicateFormula(new GroundedPredicate(Domain.FALSE_PREDICATE)));
         }
     }
     return(this);
 }
        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);
        }
        /*
         *      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();
        }