Ejemplo n.º 1
0
        public BuildAgents_II(List <Domain> lNotGroundedDomains, List <Domain> domains, List <Problem> problems)
        {
            HashSet <Predicate> lAllGoals        = new HashSet <Predicate>();
            HashSet <Predicate> lAllDividedGoals = new HashSet <Predicate>();
            HashSet <Constant>  publicConst      = new HashSet <Constant>();

            for (int i = 0; i < domains.Count; i++)
            {
                foreach (Constant cons in domains.ElementAt(i).PublicConstants)
                {
                    publicConst.Add(cons);
                }
            }
            for (int i = 0; i < domains.Count; i++)
            {
                Problem p = problems.ElementAt(i);
                Domain  d = domains.ElementAt(i);
                Domain  notGroundedDomain = lNotGroundedDomains.ElementAt(i);
                foreach (Constant cons in publicConst)
                {
                    if (!notGroundedDomain.Constants.Contains(cons))
                    {
                        notGroundedDomain.Constants.Add(cons);
                    }
                }
                Dictionary <Predicate, HashSet <Predicate> > lInvariants = null;
                //lInvariants = new Dictionary<Predicate,HashSet<Predicate>>();

                HashSet <GroundedPredicate> publicPredicates = new HashSet <GroundedPredicate>();
                HashSet <GroundedPredicate> Predicates       = new HashSet <GroundedPredicate>();
                d.Predicates = new HashSet <Predicate>();
                foreach (GroundedPredicate gp in d.PublicPredicates)
                {
                    publicPredicates.Add(gp);
                    Predicates.Add(gp);
                    GroundedPredicate ngp = (GroundedPredicate)gp.Negate();
                    publicPredicates.Add(ngp);
                    Predicates.Add(ngp);

                    d.Predicates.Add(gp);
                    d.Predicates.Add(ngp);
                }


                foreach (GroundedPredicate gp in d.PrivatePredicates)
                {
                    Predicates.Add(gp);
                    GroundedPredicate ngp = (GroundedPredicate)gp.Negate();
                    Predicates.Add(ngp);

                    d.Predicates.Add(gp);
                    d.Predicates.Add(ngp);
                }

                State initialState = new State(p);

                /*foreach(GroundedPredicate gp in p.Known)
                 * {
                 *   initialState.AddPredicate(gp);
                 * }*/
                foreach (Predicate gp in p.Known)
                {
                    if (gp is GroundedPredicate)
                    {
                        initialState.AddPredicate(gp);
                    }
                }
                d.groundedAction = d.Actions;

                List <GroundedPredicate> goal = DivideGoal(p.Goal, d.PublicActions.ToList(), d.PrivateActions.ToList(), publicPredicates);
                lAllGoals.UnionWith(p.Goal.GetAllPredicates());

                foreach (Predicate pGoal in goal)
                {
                    lAllDividedGoals.Add(pGoal);
                }

                BuildTables(domains);
                List <Action> projPublicActions = getProjectionPublicActionForAgent("Agent: " + i.ToString());
                notGroundedDomain.Actions = new List <Action>(d.Actions);
                notGroundedDomain.Actions.AddRange(projPublicActions);

                /* bool f = true;
                 * while (f)
                 * {
                 *   f = false;
                 *   foreach (Action act in projPublicActions)
                 *   {
                 *       bool preInclude = true;
                 *       foreach(GroundedPredicate gp in act.HashPrecondition)
                 *       {
                 *           if (!initialState.Contains(gp))
                 *           {
                 *               preInclude = false;
                 *               break;
                 *           }
                 *       }
                 *       if (preInclude)
                 *       {
                 *           foreach (GroundedPredicate gp in act.HashEffects)
                 *           {
                 *               if (!gp.Negation && !initialState.Contains(gp))
                 *               {
                 *                   initialState.AddPredicate(gp);
                 *                   f = true;
                 *               }
                 *           }
                 *       }
                 *   }
                 * }*/
                //lInvariants = notGroundedDomain.IdentifyMutexSAS(p, initialState);

                //lInvariants = new Dictionary<Predicate, HashSet<Predicate>>();
                projPublicActions.AddRange(d.groundedAction);
                lInvariants = d.IdentifyStrongInvariants(projPublicActions);
                Dictionary <Predicate, HashSet <Predicate> > tmp = d.IdentifyInvariants(projPublicActions);
                foreach (var kv in tmp)
                {
                    if (!lInvariants.ContainsKey(kv.Key))
                    {
                        lInvariants.Add(kv.Key, kv.Value);
                    }
                    else
                    {
                        foreach (GroundedPredicate gp in kv.Value)
                        {
                            lInvariants[kv.Key].Add(gp);
                        }
                    }
                }
                foreach (Action act in d.groundedAction)
                {
                    if (!mapActionNameToAgents.ContainsKey(act.Name))
                    {
                        mapActionNameToAgents.Add(act.Name, new HashSet <string>());
                    }
                    mapActionNameToAgents[act.Name].Add("Agent: " + i.ToString());
                }

                /* Agent agent = new Agent(p, notGroundedDomain, d.groundedAction, d.PublicActions.ToList(), d.PrivateActions.ToList(),
                 *  Predicates, publicPredicates, initialState, goal,
                 *  "Agent: " + i.ToString(), projPublicActions, lInvariants); */

                Agent agent = new Agent(p, notGroundedDomain, d.groundedAction, d.PublicActions.ToList(), d.PrivateActions.ToList(),
                                        Predicates, publicPredicates, initialState, goal,
                                        "Agent: " + i.ToString(), projPublicActions, lInvariants);

                agents.Add(agent);
            }
            //There is a problem with goals that are satisfied in the initial state and are constant - not handling for now
            //if (!lAllDividedGoals.Equals(lAllGoals))
            //    throw new Exception("Some goals cannot be obtained by any agent");
        }