Example #1
0
        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);
        }
Example #2
0
        public GroundedPredicate Ground(Dictionary <string, Constant> dBindings)
        {
            GroundedPredicate gpred = GroundedPredicateFactory.Get(Name, m_lParameters, dBindings, Negation);

            if (gpred != null)
            {
                return(gpred);
            }
            gpred = new GroundedPredicate(Name, 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);
                }
            }
            GroundedPredicateFactory.Add(Name, m_lParameters, dBindings, gpred, Negation);
            return(gpred);
        }
Example #3
0
        private static GroundedPredicate GetSafe(int iX, int iY)
        {
            GroundedPredicate gpSafe = new GroundedPredicate("safe");

            gpSafe.AddConstant(new Constant("pos", "p-" + iX));
            gpSafe.AddConstant(new Constant("pos", "p-" + iY));
            return(gpSafe);
        }
Example #4
0
        private static GroundedPredicate GetPredicate(string sName, int iX, int iY)
        {
            GroundedPredicate gpSafe = new GroundedPredicate(sName);

            gpSafe.AddConstant(new Constant("pos", "p-" + iX));
            gpSafe.AddConstant(new Constant("pos", "p-" + iY));
            return(gpSafe);
        }
Example #5
0
        public Predicate ReadFunctionExpression(CompoundExpression exp, Dictionary <string, string> dParameterNameToType, Domain d)
        {
            Constant c     = null;
            string   sName = exp.SubExpressions[0].ToString();

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


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

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

            sName = exp.SubExpressions[1].ToString();
            if (double.TryParse(sName, out dValue))
            {
                c = new Constant("Number", sName);
            }
            else
            {
                throw new ArgumentException("Second argument of increase or decrease must be a number");
            }
            p.AddConstant(c);
            return(p);
        }
Example #6
0
 private void AddEffects(Formula fEffects)
 {
     if (fEffects is PredicateFormula)
     {
         AddEffect(((PredicateFormula)fEffects).Predicate);
     }
     else
     {
         CompoundFormula cf = (CompoundFormula)fEffects;
         if (cf.Operator == "oneof" || cf.Operator == "or")//BUGBUG - should treat or differently
         {
             int iRandomIdx = RandomGenerator.Next(cf.Operands.Count);
             AddEffects(cf.Operands[iRandomIdx]);
             GroundedPredicate pChoice = new GroundedPredicate("Choice");
             pChoice.AddConstant(new Constant("ActionIndex", "a" + (Time - 1)));//time - 1 because this is the action that generated the state, hence its index is i-1
             pChoice.AddConstant(new Constant("ChoiceIndex", "c" + iRandomIdx));
             State s = this;
             while (s != null)
             {
                 s.m_lPredicates.Add(pChoice);
                 s = s.m_sPredecessor;
             }
         }
         else if (cf.Operator == "and")
         {
             foreach (Formula f in cf.Operands)
             {
                 if (f is PredicateFormula)
                 {
                     AddEffect(((PredicateFormula)f).Predicate);
                 }
                 else
                 {
                     AddEffects(f);
                 }
             }
         }
         else if (cf.Operator == "when")
         {
             if (m_sPredecessor.Contains(cf.Operands[0]))
             {
                 AddEffects(cf.Operands[1]);
             }
         }
         else
         {
             throw new NotImplementedException();
         }
     }
 }
Example #7
0
        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);
        }
Example #8
0
        public Predicate PartiallyGround(Dictionary <string, Constant> dBindings)
        {
            GroundedPredicate     gpred = new GroundedPredicate(Name, Negation);
            ParametrizedPredicate ppred = new ParametrizedPredicate(Name, 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(Domain.TRUE_PREDICATE);
                    }
                    else
                    {
                        return(Domain.FALSE_PREDICATE);
                    }
                }
                return(gpred);
            }
            else
            {
                return(ppred);
            }
        }
Example #9
0
        //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);
        }
Example #10
0
        private GroundedPredicate ReadGroundedPredicate(CompoundExpression exp, Domain d)
        {
            GroundedPredicate gp = new GroundedPredicate(exp.Type);
            int      iExpression = 0;
            Constant c           = null;
            string   sName       = "";

            for (iExpression = 0; iExpression < exp.SubExpressions.Count; iExpression++)
            {
                sName = exp.SubExpressions[iExpression].ToString();
                c     = d.GetConstant(sName);
                gp.AddConstant(c);
            }
            return(gp);
        }
Example #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);
            }
            bool bAllConstants = true;

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

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


            PredicateFormula vf = new PredicateFormula(p);

            return(vf);
        }
Example #12
0
        public static List <string> MasterMindHeuristic(PartiallySpecifiedState pssCurrent, Domain domain)
        {
            List <Constant> lColors = new List <Constant>();
            List <Constant> lPegs   = new List <Constant>();
            List <Constant> lValues = new List <Constant>();

            foreach (Constant c in domain.Constants)
            {
                if (c.Type.ToLower() == "color")
                {
                    lColors.Add(c);
                }
                if (c.Type.ToLower() == "peg")
                {
                    lPegs.Add(c);
                }
                if (c.Type.ToLower() == "value")
                {
                    lValues.Add(c);
                }
            }

            //DateTime dtStart = DateTime.Now;

            //BeliefState bs = pssCurrent.m_bsInitialBelief;

            //State s = bs.ChooseState(true);
            //List<Predicate> l = bs.RunSatSolver();


            //Console.WriteLine((DateTime.Now - dtStart).TotalSeconds);


            Permute(lColors);
            List <Constant> lGuessColors = new List <Constant>();

            for (int iPeg = 0; iPeg < lPegs.Count; iPeg++)
            {
                /*
                 * //foreach (GroundedPredicate gp in s.Predicates)
                 * foreach (GroundedPredicate gp in l)
                 * {
                 *  if (gp.ToString().StartsWith("(on p" + iPeg) && gp.Negation == false)
                 *  {
                 *      lGuessColors.Add(gp.Constants[1]);
                 *  }
                 *
                 *
                 * }
                 */
                foreach (Constant cColor in lColors)
                {
                    GroundedPredicate gp = new GroundedPredicate("on");
                    gp.AddConstant(lPegs[iPeg]);
                    gp.AddConstant(cColor);
                    if (!pssCurrent.Observed.Contains(gp.Negate()))
                    {
                        lGuessColors.Add(cColor);
                        break;
                    }
                }
                lColors.Remove(lGuessColors.Last());
            }
            if (lGuessColors.Count == lPegs.Count)
            {
                List <string> lActions = new List <string>();
                string        sGuess   = "guess-all";
                foreach (Constant c in lGuessColors)
                {
                    sGuess += " " + c.Name;
                }
                lActions.Add(sGuess);
                lActions.Add("evaluate-guess");
                foreach (Constant cValue in lValues)
                {
                    lActions.Add("observe-LocationHits " + cValue.Name);
                    lActions.Add("observe-ColorHits " + cValue.Name);
                }
                return(lActions);
            }
            return(null);
        }
Example #13
0
        public static List <string> LargeWumpusHeuristic(PartiallySpecifiedState pssCurrent, Domain d)
        {
            List <string>     lActions = new List <string>();
            GroundedPredicate gpAtX = null, gpAtY = null;

            foreach (GroundedPredicate gp in pssCurrent.Observed)
            {
                if (!gp.Negation)
                {
                    if (gp.Name == "at-x")
                    {
                        gpAtX = gp;
                    }
                    if (gp.Name == "at-y")
                    {
                        gpAtY = gp;
                    }
                }
            }
            string sX = gpAtX.Constants[0].Name;
            string sY = gpAtY.Constants[0].Name;
            int    iX = int.Parse(sX.Split('-')[1]);
            int    iY = int.Parse(sY.Split('-')[1]);

            VisitedLocations.Add(iX * 1000 + iY);

            GroundedPredicate gpAlive = new GroundedPredicate("alive");

            if (pssCurrent.Hidden.Contains(gpAlive))
            {
                lActions.Add("check-alive_" + sX + "_" + sY);
            }
            GroundedPredicate gpStench = new GroundedPredicate("stench");

            gpStench.AddConstant(gpAtX.Constants[0]);
            gpStench.AddConstant(gpAtY.Constants[0]);
            if (pssCurrent.Hidden.Contains(gpStench))
            {
                lActions.Add("smell-wumpus " + sX + " " + sY);
            }
            GroundedPredicate gpBreeze = new GroundedPredicate("breeze");

            gpBreeze.AddConstant(gpAtX.Constants[0]);
            gpBreeze.AddConstant(gpAtY.Constants[0]);
            if (pssCurrent.Hidden.Contains(gpBreeze))
            {
                lActions.Add("feel-breeze " + sX + " " + sY);
            }
            GroundedPredicate gpGold = new GroundedPredicate("gold-at");

            gpGold.AddConstant(gpAtX.Constants[0]);
            gpGold.AddConstant(gpAtY.Constants[0]);
            if (pssCurrent.Hidden.Contains(gpGold))
            {
                lActions.Add("observe-gold " + sX + " " + sY);
            }

            if (lActions.Count == 0)
            {
                List <string> lNotVisited = new List <string>();
                List <string> lSafe       = new List <string>();
                if (iX > 1)
                {
                    if (!VisitedLocations.Contains((iX - 1) * 1000 + iY))
                    {
                        lNotVisited.Add("move-left");
                    }
                    if (pssCurrent.Observed.Contains(GetSafe(iX - 1, iY)))
                    {
                        lSafe.Add("move-left");
                    }
                }
                if (iX < Size)
                {
                    if (!VisitedLocations.Contains((iX + 1) * 1000 + iY))
                    {
                        lNotVisited.Add("move-right");
                    }
                    if (pssCurrent.Observed.Contains(GetSafe(iX + 1, iY)))
                    {
                        lSafe.Add("move-right");
                    }
                }
                if (iY > 1)
                {
                    if (!VisitedLocations.Contains(iX * 1000 + (iY - 1)))
                    {
                        lNotVisited.Add("move-up");
                    }
                    if (pssCurrent.Observed.Contains(GetSafe(iX, iY - 1)))
                    {
                        lSafe.Add("move-up");
                    }
                }
                if (iY < Size)
                {
                    if (!VisitedLocations.Contains(iX * 1000 + (iY + 1)))
                    {
                        lNotVisited.Add("move-down");
                    }
                    if (pssCurrent.Observed.Contains(GetSafe(iX, iY + 1)))
                    {
                        lSafe.Add("move-down");
                    }
                }
                List <string> lSafeAndNotVisited = new List <string>(lSafe.Intersect(lNotVisited));
                if (lSafeAndNotVisited.Count > 0)
                {
                    int idx = RandomGenerator.Next(lSafeAndNotVisited.Count);
                    lActions.Add(lSafeAndNotVisited[idx]);
                }
                else if (lSafe.Count > 0)
                {
                    int idx = RandomGenerator.Next(lSafe.Count);
                    lActions.Add(lSafe[idx]);
                }
                else
                {
                    int idx = RandomGenerator.Next(4);
                    if (idx == 0)
                    {
                        lActions.Add("move-down");
                    }
                    if (idx == 1)
                    {
                        lActions.Add("move-up");
                    }
                    if (idx == 2)
                    {
                        lActions.Add("move-left");
                    }
                    if (idx == 3)
                    {
                        lActions.Add("move-right");
                    }
                }
            }


            return(lActions);
        }
Example #14
0
        private void GetApplicableEffects(Formula fEffects, HashSet <Predicate> lAdd, HashSet <Predicate> lDelete)
        {
            if (fEffects is PredicateFormula)
            {
                Predicate p = ((PredicateFormula)fEffects).Predicate;
                if (p.Negation)
                {
                    lDelete.Add(p);
                }
                else
                {
                    lAdd.Add(p);
                }
            }
            else if (fEffects is ProbabilisticFormula)
            {
                ProbabilisticFormula pf = (ProbabilisticFormula)fEffects;
                double dRand            = RandomGenerator.NextDouble();
                double dInitialRand     = dRand;
                int    iOption          = 0;
                while (iOption < pf.Options.Count && dRand > 0)
                {
                    dRand -= pf.Probabilities[iOption];
                    iOption++;
                }
                if (dRand < 0.01)
                {
                    iOption--;

                    GetApplicableEffects(pf.Options[iOption], lAdd, lDelete);
                }
                else //the no-op option was chosen
                {
                    iOption = -1;
                }
                GroundedPredicate pChoice = new GroundedPredicate("Choice");
                pChoice.AddConstant(new Constant("ActionIndex", "a" + Time));
                pChoice.AddConstant(new Constant("ChoiceIndex", "c" + ChoiceCount + "." + iOption));
                ChoiceCount++;
                State s = this;
                while (s != null)
                {
                    s.m_lPredicates.Add(pChoice);
                    s = s.m_sPredecessor;
                }
            }
            else
            {
                CompoundFormula cf = (CompoundFormula)fEffects;
                if (cf.Operator == "oneof" || cf.Operator == "or")//BUGBUG - should treat or differently
                {
                    int iRandomIdx = RandomGenerator.Next(cf.Operands.Count);
                    GetApplicableEffects(cf.Operands[iRandomIdx], lAdd, lDelete);
                    GroundedPredicate pChoice = new GroundedPredicate("Choice");
                    pChoice.AddConstant(new Constant("ActionIndex", "a" + Time));
                    pChoice.AddConstant(new Constant("ChoiceIndex", "c" + ChoiceCount + "." + iRandomIdx));
                    ChoiceCount++;
                    State s = this;
                    while (s != null)
                    {
                        s.m_lPredicates.Add(pChoice);
                        s = s.m_sPredecessor;
                    }
                }
                else if (cf.Operator == "and")
                {
                    foreach (Formula f in cf.Operands)
                    {
                        GetApplicableEffects(f, lAdd, lDelete);
                    }
                }
                else if (cf.Operator == "when")
                {
                    if (Contains(cf.Operands[0]))
                    {
                        GetApplicableEffects(cf.Operands[1], lAdd, lDelete);
                    }
                }
                else if (cf is ParametrizedFormula)
                {
                    ParametrizedFormula pf = (ParametrizedFormula)cf;
                    foreach (Formula fNew in pf.Ground(Problem.Domain.Constants))
                    {
                        GetApplicableEffects(fNew, lAdd, lDelete);
                    }
                }
                else
                {
                    throw new NotImplementedException();
                }
            }
        }
Example #15
0
        public MemoryStream WriteKnowledgeProblem(HashSet <Predicate> lObserved, HashSet <Predicate> lHidden, int cMinMishaps, int cMishaps)
        {
            MemoryStream msProblem = new MemoryStream();
            StreamWriter sw        = new StreamWriter(msProblem);

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

            string sKP = "", sP = "";

            foreach (GroundedPredicate gp in lObserved)
            {
                if (gp.Name == "Choice")
                {
                    continue;
                }
                if (Domain.AlwaysKnown(gp))
                {
                    sw.WriteLine(gp);
                }
                if (!Domain.AlwaysKnown(gp))
                {
                    Predicate kp = new KnowPredicate(gp);
                    sw.WriteLine(kp);
                }
            }
            foreach (GroundedPredicate gp in lHidden)
            {
                //GroundedPredicate kp = new GroundedPredicate(gp);
                //kp.Name = "NotK" + kp.Name;
                //sw.WriteLine(kp);
            }

            if (cMinMishaps > cMishaps)
            {
                sw.WriteLine("(MishapCount m" + cMishaps + ")");
            }

            sw.WriteLine(")");

            HashSet <Predicate> lGoalPredicates = Goal.GetAllPredicates();


            CompoundFormula cfGoal = new CompoundFormula("and");

            foreach (Predicate p in lGoalPredicates)
            {
                if (Domain.AlwaysKnown(p))
                {
                    cfGoal.AddOperand(p);
                }
                else
                {
                    cfGoal.AddOperand(new KnowPredicate(p));
                }
            }

            CompoundFormula cfAnd = new CompoundFormula(cfGoal);

            if (cMinMishaps > cMishaps && SDRPlanner.Translation != SDRPlanner.Translations.Conformant)
            {
                GroundedPredicate gp = new GroundedPredicate("MishapCount");
                gp.AddConstant(new Constant("mishaps", "m" + cMinMishaps));
                cfAnd.AddOperand(gp);
            }

            sw.WriteLine("(:goal " + cfAnd.Simplify() + ")");
            //sw.WriteLine("))");

            sw.WriteLine(")");
            sw.Flush();


            return(msProblem);
        }