Beispiel #1
0
        private void ReadGoal(Problem p, Domain d, Expression eGoal)
        {
            Formula fGoal = ReadGroundedFormula((CompoundExpression)eGoal, d);

            p.Goal = fGoal;
        }
Beispiel #2
0
 private void ReadMetric(Problem p, Domain d, CompoundExpression eSub)
 {
     p.AddMetric(eSub.ToString());
 }
 public KnowledgeState(Problem p)
     : base(p)
 {
 }
Beispiel #4
0
        public Problem ParseProblem(string sProblemFile, Domain d)
        {
            StreamReader       sr  = new StreamReader(sProblemFile);
            CompoundExpression exp = (CompoundExpression)ToExpression(sr);

            sr.Close();
            Problem            p    = null;
            CompoundExpression eSub = null;

            foreach (Expression e in exp.SubExpressions)
            {
                eSub = (CompoundExpression)e;
                if (eSub.Type == "problem")
                {
                    p = new Problem(eSub.SubExpressions.First().ToString(), d);
                }
                if (eSub.Type == ":domain")
                {
                    if (eSub.SubExpressions.First().ToString() != d.Name)
                    {
                        throw new InvalidDataException("Domain and problem files don't match!");
                    }
                }
                if (eSub.Type == ":objects")
                {
                    ReadConstants(eSub, d);
                }
                if (eSub.Type == ":init")
                {
                    CompoundExpression eAnd = (CompoundExpression)eSub.SubExpressions.First();
                    if (eAnd.Type == "and")
                    {
                        ReadInitState(p, d, eAnd);
                    }
                    else
                    {
                        ReadInitState(p, d, eSub);
                    }
                    //throw new InvalidDataException("Expecting 'and', got " + eAnd.Type);
                }
                if (eSub.Type == ":goal")
                {
                    ReadGoal(p, d, eSub.SubExpressions[0]);
                }
                if (eSub.Type == ":metric")
                {
                    ReadMetric(p, d, eSub);
                }
            }
            //p.AddReasoningActions(); not needed as long as we use FF to do the planning for us
            d.ComputeAlwaysKnown();
            p.CompleteKnownState();


            List <Predicate> lConstantPredicates = new List <Predicate>();

            foreach (Predicate pKnown in p.Known)
            {
                if (d.AlwaysConstant(pKnown))
                {
                    lConstantPredicates.Add(pKnown);
                }
            }
            //Problem here - sometimes ~p and p are both effects (remove p and immediately add it), and the current code forbids that - not sure what to do...
            //d.RemoveUniversalQuantifiers(lConstantPredicates);
            //p.RemoveUniversalQuantifiers();


            d.ComputePrivateActions();

            return(p);
        }
        public static List <Agent> CreateProjAgents(List <Agent> m_agents, List <Domain> lDomains, List <Problem> lProblems)
        {
            agents = m_agents;
            map    = new Dictionary <string, int>();
            int j = 0;

            foreach (Agent agent in agents)
            {
                map.Add(agent.name, j);
                j++;
            }
            List <Action>   allProjectionAction = new List <Action>();
            List <Landmark> lGoal = new List <Landmark>();
            int             index = 0;

            Distributed_Landmarks_Detection.Reset(agents);
            List <Predicate>  predicates       = new List <Predicate>();
            Domain            dPublic          = new Domain("PublicDomain", "");
            Problem           pPublic          = new Problem("PublicProblem", dPublic);
            State             publicStartState = new State(pPublic);
            GroundedPredicate newPrePredicate  = null;

            newPrePredicate = new GroundedPredicate(Domain.ARTIFICIAL_PREDICATE + "StartState");
            publicStartState.AddPredicate(newPrePredicate);
            predicates.Add(newPrePredicate);
            //mapActionNameToAction=new Dictionary<string,Action>();

            for (int i = 0; i < agents.Count; i++)
            {
                Agent agent = agents[i];
                agent.mapActionNameToAction = new Dictionary <string, Action>();
                foreach (Action act in agent.publicActions)
                {
                    if (!agent.mapActionNameToAction.ContainsKey(act.Name))
                    {
                        agent.mapActionNameToAction.Add(act.Name, act);
                    }
                }
                agent.initLandmarksDetect();
                agent.saveInfo();
                State startState = new State(lProblems[i]);
                startState.AddPredicate(newPrePredicate);
                List <Action> currentlProjAction = agent.getAdvancedProjectionPublicAction(index, predicates, startState);
                agent.m_actions      = currentlProjAction;
                agent.privateActions = new List <Action>();
                agent.publicActions  = currentlProjAction;
                // agent.PublicPredicates = new HashSet<GroundedPredicate>();

                //allProjectionAction.AddRange(currentlProjAction);

                foreach (Action act in currentlProjAction)
                {
                    act.agent = agent.name;
                    //if (!agent.mapActionNameToAction.ContainsKey(act.Name))
                    //  agent.mapActionNameToAction.Add(act.Name, act);
                }
                foreach (Predicate pred in agent.PublicPredicates)
                {
                    predicates.Add(pred);
                    //   agent.PublicPredicates.Add((GroundedPredicate)pred);
                }
                agent.Predicates = new HashSet <GroundedPredicate>();
                foreach (GroundedPredicate pgp in predicates)
                {
                    agent.Predicates.Add(pgp);
                }

                foreach (GroundedPredicate pgp in agent.GetPublicStartState())
                {
                    startState.AddPredicate(pgp);
                }
                index += 1000;
                lGoal.AddRange(agent.GetPublicGoals());

                /**** I implmented this in GetPublicStartState function in Agent  ****/

                List <GroundedPredicate> lInitialPrivate = agent.GetPrivateStartState();
                foreach (KeyValuePair <GroundedPredicate, HashSet <GroundedPredicate> > pArtificial in agent.ArtificialToPrivate)
                {
                    bool bAllTrue = true;
                    foreach (GroundedPredicate gpReal in pArtificial.Value)
                    {
                        if (!lInitialPrivate.Contains(gpReal))
                        {
                            bAllTrue = false;
                        }
                    }
                    if (bAllTrue)
                    {
                        if (!startState.Contains(pArtificial.Key))
                        {
                            startState.AddPredicate(pArtificial.Key);
                        }
                    }
                }
                agent.startState = startState;
            }
            return(agents);

            /* dPublic.Actions = allProjectionAction;
             * foreach (Domain d in lDomains)
             * {
             *   foreach (ParametrizedPredicate pp in d.Predicates)
             *       dPublic.Predicates.Add(pp);
             *   foreach (string sType in d.Types)
             *       if (!dPublic.Types.Contains(sType))
             *           dPublic.Types.Add(sType);
             *   foreach (KeyValuePair<string, string> pair in d.TypeHierarchy)
             *       dPublic.TypeHierarchy[pair.Key] = pair.Value;
             *   foreach (Constant c in d.Constants)
             *       dPublic.Constants.Add(c);
             * }
             * foreach (Predicate p in predicates)
             * {
             *   if (p.Name.StartsWith(Domain.ARTIFICIAL_PREDICATE))
             *   {
             *       ParametrizedPredicate pp = new ParametrizedPredicate(p.Name);
             *       dPublic.Predicates.Add(pp);
             *   }
             *
             * }
             *
             * foreach (Predicate pInit in publicStartState.Predicates)
             *   pPublic.AddKnown(pInit);
             */

            /*CompoundFormula goalf = new CompoundFormula("and");
             * foreach (Landmark l in lGoal)
             * {
             *  CompoundFormula disjGoal = new CompoundFormula("or");
             *  foreach (GroundedPredicate gp in l.facts.Keys)
             *  {
             *      disjGoal.AddOperand(gp);
             *  }
             *  goalf.AddOperand(disjGoal);
             * }
             * pPublic.Goal = goalf;
             *
             * bool ans;
             *
             * highLevelplan = ExternalPlanners.FFPlan(dPublic, pPublic, publicStartState, goalf, dPublic.Actions, 20 * 60000, out ans);
             * //highLevelplan = ExternalPlanners.FDPlan(dPublic, pPublic, publicStartState, goalf, dPublic.Actions, 20 * 60000, out ans);
             * if (highLevelplan == null)
             *  return null;
             * //    return ManualSolve(pPublic, dPublic);
             * string fault;
             * List<string> finalPlan;
             * if (Grounding(out finalPlan, out fault))
             *  return finalPlan;
             * //throw new NotImplementedException();
             * return null;*/
        }