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;
 }
Ejemplo n.º 2
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);
        }
        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);
        }
Ejemplo n.º 4
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);
        }
Ejemplo n.º 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);
        }
Ejemplo n.º 6
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);
            }
        }
Ejemplo n.º 7
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();
         }
     }
 }
        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);
        }
Ejemplo n.º 9
0
 public bool IsUnNone(int iX, int iY)
 {
     if (Belief.Observed == null)
     {
         return(false);
     }
     //Predicate p=new Predicate("cv");
     foreach (Formula f in Belief.Problem.Hidden)
     {
         GroundedPredicate gp = new GroundedPredicate("opened");
         gp.AddConstant(new Constant("pos", "p" + iX + "-" + iY));
         //if(f.Contains
         if (f.Contains(gp))
         {
             return(true);
         }
         //{
         //    string sPos = gp.Constants[0].Name;
         //    sPos = sPos.Replace("p", "");
         //    string[] asPos = sPos.Split('-');
         //    if ((iX == int.Parse(asPos[0])) && (iY == int.Parse(asPos[1]))) return true;
         //}
     }
     return(false);
 }
Ejemplo n.º 10
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);
        }
Ejemplo n.º 11
0
        public 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);
        }
        public bool IsLocationBlocked(int iX, int iY)
        {
            GroundedPredicate gp = new GroundedPredicate("at");

            gp.AddConstant(new Constant("pos", "p" + iX + "-" + iY));
            Action aChecking = Domain.GetAction("checking");

            if (aChecking.Effects.Contains(gp))
            {
                return(false);
            }
            return(true);
        }
        public bool IsPossibleLocation(int iX, int iY)
        {
            GroundedPredicate gp = new GroundedPredicate("at");

            gp.AddConstant(new Constant("pos", "p" + iX + "-" + iY));
            foreach (CompoundFormula cf in Belief.Hidden)
            {
                if (cf.Contains(gp))
                {
                    return(true);
                }
            }
            return(false);
        }
Ejemplo n.º 14
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);
        }
Ejemplo n.º 15
0
        public bool IsGoal(int iX, int iY)
        {
            //iX = -1;
            //iY = -1;
            if (Belief.Observed == null)
            {
                return(false);
            }

            GroundedPredicate gp = new GroundedPredicate("at");

            gp.AddConstant(new Constant("pos", "p" + iX + "-" + iY));
            //if(f.Contains
            if (Belief.Problem.Goal.Contains(gp))
            {
                return(true);
            }


            return(false);
        }
Ejemplo n.º 16
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();
                }
            }
        }
Ejemplo n.º 17
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;
 }
Ejemplo n.º 18
0
        public Action ReplaceNonDeterministicEffectsWithOptions(List<string> lAlwaysKnown, int cMaxOptions)
        {
            Action aNew = Clone();
            List<CompoundFormula> lConditions = new List<CompoundFormula>();
            List<Formula> lObligatory = new List<Formula>();
            SplitEffects(lConditions, lObligatory);

            //currently only handling non-determinism in conditional effects
            CompoundFormula cfEffects = new CompoundFormula("and");
            foreach (Formula f in lObligatory)
            {
                if (f is PredicateFormula)
                    cfEffects.AddOperand(f);
                else
                {
                    CompoundFormula cf = (CompoundFormula)f;
                    if (cf.Operator == "oneof" || cf.Operator == "or")
                    {
                        lConditions.Add(cf);
                    }
                    else
                        cfEffects.AddOperand(cf);

                }
            }

            foreach (CompoundFormula cfCondition in lConditions)
            {
                Formula fDeterministicCondition = null;
                if (cfCondition.ContainsNonDeterministicEffect())
                {
                    //BUGBUG - may cause problems when we have different number of options for each condition - not sure!
                    if (cMaxOptions < cfCondition.GetMaxNonDeterministicOptions())
                        cMaxOptions = cfCondition.GetMaxNonDeterministicOptions();
                    int[] aPermutation = Permutation(cMaxOptions);

                    foreach (int iOption in aPermutation)
                    {
                        fDeterministicCondition = cfCondition.ChooseOption(iOption);
                        GroundedPredicate gpOption = new GroundedPredicate("option");
                        gpOption.AddConstant(new Constant(Domain.OPTION, "opt" + iOption));
                        if (cfCondition.Operator == "when")
                        {
                            ((CompoundFormula)((CompoundFormula)fDeterministicCondition).Operands[0]).AddOperand(gpOption);
                            cfEffects.AddOperand(fDeterministicCondition);
                        }
                        else
                        {
                            CompoundFormula cfWhen = new CompoundFormula("when");
                            cfWhen.AddOperand(gpOption);
                            cfWhen.AddOperand(fDeterministicCondition);
                            cfEffects.AddOperand(cfWhen);

                        }
                    }
                }
                else
                    cfEffects.AddOperand(cfCondition);
            }
            for (int iOption = 0; iOption < cMaxOptions; iOption++)
            {
                GroundedPredicate gpCurrentOption = new GroundedPredicate("option");
                gpCurrentOption.AddConstant(new Constant(Domain.OPTION, "opt" + iOption));
                GroundedPredicate gpNextOption = new GroundedPredicate("option");
                gpNextOption.AddConstant(new Constant(Domain.OPTION, "opt" + (iOption + 1) % cMaxOptions));
                CompoundFormula cfWhen = new CompoundFormula("when");
                cfWhen.AddOperand(gpCurrentOption);
                CompoundFormula cfAnd = new CompoundFormula("and");
                cfAnd.AddOperand(gpCurrentOption.Negate());
                cfAnd.AddOperand(gpNextOption);
                cfWhen.AddOperand(cfAnd);
                cfEffects.AddOperand(cfWhen);
            }
            aNew.Effects = cfEffects;
            if (Observe != null)//assuming that there can't be any observations
                throw new NotImplementedException();
            return aNew;
        }
Ejemplo n.º 19
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;
        }
        public State ChooseState(bool bRemoveNegativePredicates)
        {
            State s = new State(Problem);
            if (Problem.Domain.Name == "mines4")
            {
                string[] a = new string[]{
                         "(obs0-at p1-1)",
                        "(obs0-at p2-1)",
                        "(obs2-at p3-1)",
                        "(mine-at p4-1)",
                        "(obs1-at p4-1)",
                        "(obs2-at p1-2)",
                        "(obs2-at p2-2)",
                        "(obs3-at p3-2)",
                        "(mine-at p4-2)",
                        "(obs1-at p4-2)",
                        "(mine-at p1-3)",
                        "(obs1-at p1-3)",
                        "(mine-at p2-3)",
                        "(obs1-at p2-3)",
                        "(obs2-at p3-3)",
                        "(obs1-at p4-3)",
                        "(obs2-at p1-4)",
                        "(obs2-at p2-4)",
                        "(obs1-at p3-4)",
                        "(obs0-at p4-4)"};
                foreach (GroundedPredicate p in m_lObserved)
                    s.AddPredicate(p);
                foreach (string str in a)
                {
                    string[] a1 = str.Replace("(","").Replace(")","").Split(' ');
                    GroundedPredicate gp = new GroundedPredicate(a1[0]);
                    gp.AddConstant(new Constant("pos", a1[1]));
                    s.AddPredicate(gp);
                }

            }
            else if (SDRPlanner.SimulationStartState == null || UnderlyingEnvironmentState != null)
            {
                if (Problem.Name.Contains("Large"))
                    s = ChooseStateForLargeDomains();
                else
                {
                    List<Predicate> lAssignment = null;
                    Debug.Write("Choosing hidden variables ");
                    while (lAssignment == null)
                    {
                        Debug.Write(".");
                        lAssignment = ChooseHiddenPredicates(m_lHiddenFormulas, true);
                    }
                    Debug.WriteLine("");
                    foreach (Predicate p in lAssignment)
                    {
                        s.AddPredicate(p);
                    }
                    foreach (GroundedPredicate p in m_lObserved)
                    {
                        s.AddPredicate(p);
                    }
                }
            }
            else
            {
                Parser p = new Parser();
                string sNextState = SDRPlanner.SimulationStartState[0];
                SDRPlanner.SimulationStartState.RemoveAt(0);
                CompoundFormula cfInit = p.ParseFormula(sNextState, Problem.Domain);
                foreach (PredicateFormula pf in cfInit.Operands)
                    s.AddPredicate(pf.Predicate);
                foreach (GroundedPredicate gp in Observed)
                    s.AddPredicate(gp);
            }
            if (bRemoveNegativePredicates)
                s.RemoveNegativePredicates();
            if (UnderlyingEnvironmentState == null)
                UnderlyingEnvironmentState = s;
            return s;
        }
 public bool IsPossibleLocation(int iX, int iY)
 {
     GroundedPredicate gp = new GroundedPredicate("at");
     gp.AddConstant(new Constant("pos", "p" + iX + "-" + iY));
     foreach (CompoundFormula cf in Belief.Hidden)
         if (cf.Contains(gp))
             return true;
     return false;
 }
 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 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;
 }
 public bool IsUnNone(int iX, int iY)
 {
     if (Belief.Observed == null)
         return false;
     //Predicate p=new Predicate("cv");
     foreach (Formula f in Belief.Problem.Hidden)
     {
         GroundedPredicate gp = new GroundedPredicate("opened");
         gp.AddConstant(new Constant("pos", "p" + iX + "-" + iY));
         //if(f.Contains
         if (f.Contains(gp)) return true;
         //{
         //    string sPos = gp.Constants[0].Name;
         //    sPos = sPos.Replace("p", "");
         //    string[] asPos = sPos.Split('-');
         //    if ((iX == int.Parse(asPos[0])) && (iY == int.Parse(asPos[1]))) return true;
         //}
     }
     return false;
 }
 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);
         }
     }
     if (Parametrized)
     {
         if (dBindings.ContainsKey(Domain.VALUE_PARAMETER))
             gp.AddConstant(dBindings[Domain.VALUE_PARAMETER]);
         else
             throw new NotImplementedException();
     }
     else
     {
         if (Value)
             gp.AddConstant(new Constant(Domain.VALUE, Domain.TRUE_VALUE));
         else
             gp.AddConstant(new Constant(Domain.VALUE, Domain.FALSE_VALUE));
     }
     return gp;
 }
Ejemplo n.º 26
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
                    {
                        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;
        }
 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;
 }
        public bool IsGoal(int iX, int iY)
        {
            //iX = -1;
            //iY = -1;
            if (Belief.Observed == null)
                return false;

            GroundedPredicate gp = new GroundedPredicate("at");
            gp.AddConstant(new Constant("pos", "p" + iX + "-" + iY));
            //if(f.Contains
            if (Belief.Problem.Goal.Contains(gp)) return true;

            return false;
        }
Ejemplo n.º 29
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);
        }
 private static GroundedPredicate GetPredicate(string sName, int iX, int iY)
 {
     GroundedPredicate gpSafe = new GroundedPredicate(sName);
     gpSafe.AddConstant(new Constant("pos", "p" + iX + "-" + iY));
     return gpSafe;
 }
 public bool IsLocationBlocked(int iX, int iY)
 {
     GroundedPredicate gp = new GroundedPredicate("at");
     gp.AddConstant(new Constant("pos", "p" + iX + "-" + iY));
     Action aChecking = Domain.GetAction("checking");
     if (aChecking.Effects.Contains(gp))
         return false;
     return true;
 }
 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;
 }
Ejemplo n.º 33
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
                    {
                        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);
        }
        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;
        }
        private State ChooseStateForLargeDomains()
        {
            State s = new State(Problem);
            foreach (Predicate pKnown in Problem.Known)
                s.AddPredicate(pKnown);
            if (Problem.Name.Contains("Wumpus"))
            {
                int cWumpuses = LargeWumpusDomain.WumpusCount, cPits = LargeWumpusDomain.PitCount;
                List<int> lPositions = new List<int>();
                for (int i = 0; i < cWumpuses; i++)
                {
                    int pos = GetRandomUniquePosition(lPositions, LargeWumpusDomain.Size);
                    int x = pos % 1000;
                    int y = pos / 1000;
                    GroundedPredicate gpWumpus = new GroundedPredicate("wumpus-at");
                    gpWumpus.AddConstant(new Constant("pos", "p-" + x));
                    gpWumpus.AddConstant(new Constant("pos", "p-" + y));
                    s.AddPredicate(gpWumpus);
                    if (x > 1)
                    {
                        GroundedPredicate gpStench = new GroundedPredicate("stench");
                        gpStench.AddConstant(new Constant("pos", "p-" + (x - 1)));
                        gpStench.AddConstant(new Constant("pos", "p-" + y));
                        s.AddPredicate(gpStench);
                    }
                    if (x < LargeWumpusDomain.Size)
                    {
                        GroundedPredicate gpStench = new GroundedPredicate("stench");
                        gpStench.AddConstant(new Constant("pos", "p-" + (x + 1)));
                        gpStench.AddConstant(new Constant("pos", "p-" + y));
                        s.AddPredicate(gpStench);
                    }
                    if (y > 1)
                    {
                        GroundedPredicate gpStench = new GroundedPredicate("stench");
                        gpStench.AddConstant(new Constant("pos", "p-" + x));
                        gpStench.AddConstant(new Constant("pos", "p-" + (y - 1)));
                        s.AddPredicate(gpStench);
                    }
                    if (y < LargeWumpusDomain.Size)
                    {
                        GroundedPredicate gpStench = new GroundedPredicate("stench");
                        gpStench.AddConstant(new Constant("pos", "p-" + x));
                        gpStench.AddConstant(new Constant("pos", "p-" + (y + 1)));
                        s.AddPredicate(gpStench);
                    }
                }
                for (int i = 0; i < cPits; i++)
                {
                    int pos = GetRandomUniquePosition(lPositions, LargeWumpusDomain.Size);
                    int x = pos % 1000;
                    int y = pos / 1000;
                    GroundedPredicate gpPit = new GroundedPredicate("pit-at");
                    gpPit.AddConstant(new Constant("pos", "p-" + x));
                    gpPit.AddConstant(new Constant("pos", "p-" + y));
                    s.AddPredicate(gpPit);
                    if (x > 1)
                    {
                        GroundedPredicate gpBreeze = new GroundedPredicate("breeze");
                        gpBreeze.AddConstant(new Constant("pos", "p-" + (x - 1)));
                        gpBreeze.AddConstant(new Constant("pos", "p-" + y));
                        s.AddPredicate(gpBreeze);
                    }
                    if (x < LargeWumpusDomain.Size)
                    {
                        GroundedPredicate gpBreeze = new GroundedPredicate("breeze");
                        gpBreeze.AddConstant(new Constant("pos", "p-" + (x + 1)));
                        gpBreeze.AddConstant(new Constant("pos", "p-" + y));
                        s.AddPredicate(gpBreeze);
                    }
                    if (y > 1)
                    {
                        GroundedPredicate gpBreeze = new GroundedPredicate("breeze");
                        gpBreeze.AddConstant(new Constant("pos", "p-" + x));
                        gpBreeze.AddConstant(new Constant("pos", "p-" + (y - 1)));
                        s.AddPredicate(gpBreeze);
                    }
                    if (y < LargeWumpusDomain.Size)
                    {
                        GroundedPredicate gpBreeze = new GroundedPredicate("breeze");
                        gpBreeze.AddConstant(new Constant("pos", "p-" + x));
                        gpBreeze.AddConstant(new Constant("pos", "p-" + (y + 1)));
                        s.AddPredicate(gpBreeze);
                    }
                }

                for (int iX = 1; iX <= LargeWumpusDomain.Size; iX++)
                    for (int iY = 1; iY <= LargeWumpusDomain.Size; iY++)
                    {
                        if (!lPositions.Contains(iY * 1000 + iX))
                        {
                            GroundedPredicate gpSafe = new GroundedPredicate("safe");
                            gpSafe.AddConstant(new Constant("pos", "p-" + iX));
                            gpSafe.AddConstant(new Constant("pos", "p-" + iY));
                            s.AddPredicate(gpSafe);
                        }
                    }

                int pGold = GetRandomUniquePosition(lPositions, LargeWumpusDomain.Size);
                int xGold = pGold % LargeWumpusDomain.Size;
                int yGold = pGold / LargeWumpusDomain.Size;
                GroundedPredicate gpGold = new GroundedPredicate("gold-at");
                gpGold.AddConstant(new Constant("pos", "p-" + xGold));
                gpGold.AddConstant(new Constant("pos", "p-" + yGold));
                s.AddPredicate(gpGold);
            }
            if (Problem.Name.Contains("Mine"))
            {
                bool[,] aBoard = new bool[MineSweeper.Size, MineSweeper.Size];
                List<GroundedPredicate> lPredicates = new List<GroundedPredicate>();
                for (int iMine = 0; iMine < MineSweeper.Size; iMine++)
                {
                    int iX = RandomGenerator.Next(MineSweeper.Size);
                    int iY = RandomGenerator.Next(MineSweeper.Size);
                    GroundedPredicate gp = new GroundedPredicate("mine-at");
                    gp.AddConstant(new Constant("pos", "p" + iX + "-" + iY));
                    aBoard[iX, iY] = true;
                    lPredicates.Add(gp);

                }

                for (int iX = 0; iX < MineSweeper.Size; iX++)
                    for (int iY = 0; iY < MineSweeper.Size; iY++)
                    {
                        int cNeighbors = 0;
                        for (int i = -1; i <= 1; i++)
                            for (int j = -1; j <= 1; j++)
                                if (i != 0 || j != 0)
                                    if (iX + i >= 0 && iX + i < MineSweeper.Size)
                                        if (iY + j >= 0 && iY + j < MineSweeper.Size)
                                            if (aBoard[iX + i, iY + j])
                                                cNeighbors++;
                        GroundedPredicate gp = new GroundedPredicate("mine-count");
                        gp.AddConstant(new Constant("pos", "p" + iX + "-" + iY));
                        gp.AddConstant(new Constant("value", "v" + cNeighbors));
                        lPredicates.Add(gp);
                    }

                foreach (Predicate p in lPredicates)
                    s.AddPredicate(p);

            }
            if (Problem.Name.Contains("Battleship"))
            {
                int[,] aMap = new int[Battleship.Size, Battleship.Size];
                List<GroundedPredicate> lPredicates = new List<GroundedPredicate>();

                int cFailures = 0;
                for (int iShipType = Battleship.ShipTypes; iShipType > 0; iShipType--)
                {
                    int cShips = Battleship.ShipCount[iShipType];

                    int iLength = iShipType;
                    for (int iShip = 0; iShip < cShips; iShip++)
                    {
                        bool bDone = false;
                        int cAttempts = 100;
                        while (!bDone && cAttempts > 0)
                        {
                            cAttempts--;
                            int iX = RandomGenerator.Next(Battleship.Size - iLength);
                            int iY = RandomGenerator.Next(Battleship.Size - iLength);
                            bool bVertical = RandomGenerator.NextDouble() < 0.5;

                            GroundedPredicate gp = null;

                            if (bVertical)
                            {
                                bool bFree = true;
                                for (int i = 0; i < iLength && bFree; i++)
                                    if (aMap[iX, iY + i] > 0)
                                        bFree = false;
                                if (bFree)
                                {
                                    for (int i = 0; i < iLength && bFree; i++)
                                    {
                                        gp = new GroundedPredicate("ship-at");
                                        //gp.AddConstant(new Constant("ship", "s-" + iShip));
                                        gp.AddConstant(new Constant("pos", "p-" + iX));
                                        gp.AddConstant(new Constant("pos", "p-" + (iY + i)));
                                        lPredicates.Add(gp);
                                        aMap[iX, iY + i] = 2;

                                    }
                                    for (int i = -1; i <= iLength; i++)
                                    {
                                        for (int j = -1; j <= 1; j++)
                                            if (iX + j >= 0 && iX + j < Battleship.Size && iY + i >= 0 && iY + i < Battleship.Size)
                                            {
                                                if(aMap[iX + j, iY + i] == 0)
                                                    aMap[iX + j, iY + i] = 1;
                                            }
                                    }
                                    bDone = true;
                                }
                            }
                            else
                            {
                                bool bFree = true;
                                for (int i = 0; i < iLength && bFree; i++)
                                    if (aMap[iX + i, iY] > 0)
                                        bFree = false;
                                if (bFree)
                                {
                                    for (int i = 0; i < iLength && bFree; i++)
                                    {
                                        gp = new GroundedPredicate("ship-at");
                                        //gp.AddConstant(new Constant("ship", "s-" + iShip));
                                        gp.AddConstant(new Constant("pos", "p-" + (iX + i)));
                                        gp.AddConstant(new Constant("pos", "p-" + iY));
                                        lPredicates.Add(gp);
                                        aMap[iX + i, iY] = 2;
                                    }
                                    for (int i = -1; i <= iLength; i++)
                                    {
                                        for (int j = -1; j <= 1; j++)
                                            if (iX + i >= 0 && iX + i < Battleship.Size && iY + j >= 0 && iY + j < Battleship.Size)
                                            {
                                                if(aMap[iX + i, iY + j] == 0)
                                                    aMap[iX + i, iY + j] = 1;

                                            }
                                    }
                                    bDone = true;
                                }
                            }
                        }
                        if (cAttempts == 0)
                            cFailures++;
                    }
                }

                if (cFailures > 0)
                    Console.WriteLine();

                for (int iX = 0; iX < Battleship.Size; iX++)
                {
                    for (int iY = 0; iY < Battleship.Size; iY++)
                    {
                        if (aMap[iX, iY] != 2)
                        {
                            GroundedPredicate gp = new GroundedPredicate("water-at");
                            gp.AddConstant(new Constant("pos", "p-" + iX));
                            gp.AddConstant(new Constant("pos", "p-" + iY));
                            lPredicates.Add(gp);
                            Debug.Write('-');
                        }
                        else
                            Debug.Write('X');

                        /*
                        gp = new GroundedPredicate("hit");
                        gp.AddConstant(new Constant("POS", "p-" + iX));
                        gp.AddConstant(new Constant("POS", "p-" + iY));
                        gp.Negation = true;
                        lPredicates.Add(gp);
                         * */
                    }
                    Debug.Write("\n");
                }
                foreach (Predicate p in lPredicates)
                    s.AddPredicate(p);

            }

            return s;
        }
Ejemplo n.º 36
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();
            }
        }
        public CompoundFormula RemoveNonDeterminism(int iActionIndex, ref int iChoiceIndex, CompoundFormula cfAndChoices)
        {
            CompoundFormula cfNew = null;
            if (Operator == "and")
            {
                cfNew = new CompoundFormula("and");
                foreach (Formula f in Operands)
                {
                    if (f is PredicateFormula)
                    {
                        cfNew.AddOperand(f);
                    }
                    else
                    {
                        CompoundFormula cfOperand = ((CompoundFormula)f).RemoveNonDeterminism(iActionIndex, ref iChoiceIndex, cfAndChoices);
                        cfNew.AddOperand(cfOperand);
                    }
                }
            }
            else if (Operator == "oneof" || Operator == "or")
            {
                cfNew = new CompoundFormula("and");
                CompoundFormula cfChoices = new CompoundFormula(Operator);
                foreach (Formula f in Operands)
                {
                    GroundedPredicate pChoice = new GroundedPredicate("Choice");
                    pChoice.AddConstant(new Constant("ActionIndex", "a" + iActionIndex));
                    pChoice.AddConstant(new Constant("ChoiceIndex", "c" + iChoiceIndex));
                    iChoiceIndex++;

                    CompoundFormula cfWhen = new CompoundFormula("when");
                    cfWhen.AddOperand(pChoice);
                    cfWhen.AddOperand(f);
                    cfNew.AddOperand(cfWhen);

                    cfChoices.AddOperand(pChoice);
                }
                cfAndChoices.AddOperand(cfChoices);
            }
            else if (Operator == "when")
            {
                if(Operands[1] is PredicateFormula)
                    return this;
                CompoundFormula cfSecond = ((CompoundFormula)Operands[1]).RemoveNonDeterminism(iActionIndex, ref iChoiceIndex, cfAndChoices);
                bool bInserted = false;
                cfNew = cfSecond.InsertGiven(Operands[0], out bInserted);
                if (!bInserted)
                {
                    cfNew = new CompoundFormula("when");
                    cfNew.AddOperand(Operands[0]);
                    cfNew.AddOperand(cfSecond);
                }
                /*
                cfNew = new CompoundFormula("when");
                if (cfSecond.Operator == "and")
                {
                    cfNew.AddOperand(Operands[0]);
                    cfNew.AddOperand(cfSecond);
                }
                else if (cfSecond.Operator == "when")
                {
                    CompoundFormula cfAnd = new CompoundFormula("and");
                    cfAnd.AddOperand(Operands[0]);
                    cfAnd.AddOperand(cfSecond.Operands[0]);
                    cfNew.AddOperand(cfAnd);
                    cfNew.AddOperand(cfSecond.Operands[1]);

                }
                 * */
            }
            else if (Operator == "not")
            {
                cfNew = new CompoundFormula("not");
                if (cfNew.Operands[0] is PredicateFormula)
                    cfNew.AddOperand(Operands[0]);
                else
                    cfNew.AddOperand(((CompoundFormula)Operands[0]).RemoveNonDeterminism(iActionIndex, ref iChoiceIndex, cfAndChoices));
            }
            else
                throw new NotImplementedException();
            return cfNew;
        }
Ejemplo n.º 38
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();
     }
 }
Ejemplo n.º 39
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);
        }
Ejemplo n.º 40
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;
        }