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 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();
        }