public void AddHidden(CompoundFormula cf)
        {
            Domain.AddHidden(cf);

            HashSet <Predicate> hs = cf.GetAllPredicates();

            foreach (GroundedPredicate gp in hs)
            {
                m_lInitiallyUnknown.Add(gp.Canonical());
                GroundedPredicate gpCanonical = (GroundedPredicate)gp.Canonical();
                if (!m_dRelevantPredicates.ContainsKey(gpCanonical))
                {
                    m_dRelevantPredicates[gpCanonical] = new HashSet <GroundedPredicate>();
                }
                foreach (GroundedPredicate gpOther in hs)
                {
                    GroundedPredicate gpOtherCanonical = (GroundedPredicate)gpOther.Canonical();
                    if (gpOtherCanonical != gpCanonical)
                    {
                        m_dRelevantPredicates[gpCanonical].Add(gpOtherCanonical);
                    }
                }
            }

            m_lHidden.Add(cf);
        }
        private int WriteReasoningActionsII(StreamWriter sw, CompoundFormula cfHidden, int cStart)
        {
            HashSet <Predicate> lPredicates = new HashSet <Predicate>();

            cfHidden.GetAllPredicates(lPredicates);
            int cCurrent = cStart;

            foreach (GroundedPredicate pEffect in lPredicates)
            {
                sw.WriteLine("(:action R" + cCurrent);
                sw.Write(":precondition (and");
                foreach (GroundedPredicate pPrecondition in lPredicates)
                {
                    if (pPrecondition != pEffect)
                    {
                        sw.Write(" (K" + pPrecondition.Name);
                        foreach (Constant c in pPrecondition.Constants)
                        {
                            sw.Write(" " + c.Name);
                        }
                        sw.Write(")");
                    }
                }
                sw.WriteLine(")");
                sw.Write(":effect (K" + pEffect.Name);
                foreach (Constant c in pEffect.Constants)
                {
                    sw.Write(" " + c.Name);
                }
                sw.WriteLine(")");
                sw.WriteLine(")");
                cCurrent++;
            }
            return(cCurrent);
        }
Example #3
0
        private int WriteReasoningAxioms(StreamWriter sw, CompoundFormula cfHidden, int cActions)
        {
            HashSet <Predicate> lPredicates = new HashSet <Predicate>();

            cfHidden.GetAllPredicates(lPredicates);
            Dictionary <HashSet <Predicate>, HashSet <Predicate> > dActions = new Dictionary <HashSet <Predicate>, HashSet <Predicate> >();

            if (cfHidden.IsSimpleFormula())
            {
                SimpleAddReasoningActions(cfHidden, lPredicates, dActions, false);
            }
            else
            {
                throw new NotImplementedException();
            }
            foreach (KeyValuePair <HashSet <Predicate>, HashSet <Predicate> > p in dActions)
            {
                if (p.Value.Count == 1)
                {
                    KnowPredicate     kp   = (KnowPredicate)p.Value.First();
                    GroundedPredicate pOrg = (GroundedPredicate)kp.Knowledge;
                    //GroundedPredicate gpNotK = new GroundedPredicate(pOrg);
                    //gpNotK.Name = "NotK" + gpNotK.Name;
                    //p.Key.Add(gpNotK);
                }
                WriteResoningAxiom(sw, p.Key, p.Value, cActions);
                cActions++;
            }
            return(cActions);
        }
Example #4
0
        private List <Action> CreateReasoningActions(CompoundFormula cf)
        {
            List <Action>       lActions    = new List <Action>();
            Action              a           = null;
            HashSet <Predicate> lPredicates = new HashSet <Predicate>();

            cf.GetAllPredicates(lPredicates);
            foreach (Predicate p in lPredicates)
            {
                a = CreateReasoningAction(p, lPredicates);
                if (a != null)
                {
                    lActions.Add(a);
                }
            }
            return(lActions);
        }
Example #5
0
        private int WriteReasoningActions(StreamWriter sw, CompoundFormula cfHidden, int cActions, bool bRequireP)
        {
            HashSet <Predicate> lPredicates = new HashSet <Predicate>();

            cfHidden.GetAllPredicates(lPredicates);
            Dictionary <HashSet <Predicate>, HashSet <Predicate> > dActions = new Dictionary <HashSet <Predicate>, HashSet <Predicate> >();

            if (cfHidden.IsSimpleFormula())
            {
                SimpleAddReasoningActions(cfHidden, lPredicates, dActions, bRequireP);
            }
            else
            {
                throw new NotImplementedException();
            }
            foreach (KeyValuePair <HashSet <Predicate>, HashSet <Predicate> > pair in dActions)
            {
                HashSet <Predicate> lRemoveNotK = new HashSet <Predicate>(pair.Value);
                WriteResoningAction(sw, pair.Key, lRemoveNotK, cActions);
                cActions++;
            }
            return(cActions);
        }
        private int WriteReasoningActions(StreamWriter sw, CompoundFormula cfHidden, int cActions)
        {
            HashSet <Predicate> lPredicates = new HashSet <Predicate>();

            cfHidden.GetAllPredicates(lPredicates);
            Dictionary <List <Predicate>, List <Predicate> > dActions = new Dictionary <List <Predicate>, List <Predicate> >(new PredicateListComparer());

            if (cfHidden.IsSimpleFormula())
            {
                SimpleAddReasoningActions(cfHidden, lPredicates, dActions);
            }
            else
            {
                AddReasoningActions(cfHidden, lPredicates, new HashSet <Predicate>(), dActions);
                dActions = FilterRedundancies(dActions);
                dActions = ReduceActions(dActions);
            }
            foreach (KeyValuePair <List <Predicate>, List <Predicate> > p in dActions)
            {
                WriteResoningAction(sw, p.Key, p.Value, cActions);
                cActions++;
            }
            return(cActions);
        }
        public MemoryStream WriteTaggedProblem(Dictionary <string, List <Predicate> > dTags, IEnumerable <Predicate> lObserved,
                                               List <Predicate> lTrueState, Dictionary <string, double> dFunctionValues, bool bOnlyIdentifyStates)
        {
            MemoryStream msProblem = new MemoryStream();
            StreamWriter sw        = new StreamWriter(msProblem);

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

            string sKP = "", sP = "";

            if (Domain.TIME_STEPS > 0)
            {
                sw.WriteLine("(time0)");
            }
            if (SDRPlanner.SplitConditionalEffects)
            {
                sw.WriteLine("(NotInAction)\n");
            }
            foreach (KeyValuePair <string, double> f in dFunctionValues)
            {
                sw.WriteLine("(= " + f.Key + " " + f.Value + ")");
            }
            foreach (GroundedPredicate gp in lObserved)
            {
                if (gp.Name == "Choice")
                {
                    continue;
                }
                sKP = "(K" + gp.Name;
                sP  = "(" + gp.Name;
                foreach (Constant c in gp.Constants)
                {
                    sKP += " " + c.Name;
                    sP  += " " + c.Name;
                }
                if (gp.Negation)
                {
                    sKP += " " + Domain.FALSE_VALUE;
                }
                else
                {
                    sKP += " " + Domain.TRUE_VALUE;
                }
                if (!Domain.AlwaysKnown(gp))
                {
                    sw.WriteLine(sKP + ")");
                }
                if (!gp.Negation)
                {
                    sw.WriteLine(sP + ")");
                }
            }
            foreach (GroundedPredicate gp in lTrueState)
            {
                if (gp.Name == "Choice")
                {
                    continue;
                }
                if (!gp.Negation)
                {
                    sP = "(" + gp.Name;
                    foreach (Constant c in gp.Constants)
                    {
                        sP += " " + c.Name;
                    }
                    sw.WriteLine(sP + ")");
                }
            }
            foreach (KeyValuePair <string, List <Predicate> > p in dTags)
            {
                foreach (GroundedPredicate gp in p.Value)
                {
                    if (gp.Name == "Choice")
                    {
                        continue;
                    }
                    sKP = GenerateKnowGivenLine(gp, p.Key, false);
                    sw.WriteLine(sKP);
                }

                if (SDRPlanner.AddAllKnownToGiven)
                {
                    foreach (GroundedPredicate gp in lObserved)
                    {
                        if (gp.Name == "Choice")
                        {
                            continue;
                        }
                        if (!Domain.AlwaysKnown(gp))
                        {
                            sKP = GenerateKnowGivenLine(gp, p.Key, false);
                            sw.WriteLine(sKP);
                        }
                    }
                }
            }


            sw.WriteLine(")");
            CompoundFormula cfGoal = new CompoundFormula("and");

            cfGoal.AddOperand(Goal);
            HashSet <Predicate> lGoalPredicates = new HashSet <Predicate>();

            cfGoal.GetAllPredicates(lGoalPredicates);

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


            sw.WriteLine("(:goal " + cfGoal + ")");
            //sw.WriteLine("))");
            if (MetricStatement != null)
            {
                sw.WriteLine(MetricStatement);
            }
            sw.WriteLine(")");
            sw.Flush();


            return(msProblem);
        }
        public static bool Grounding(out List <string> finalPlan, out string fault)
        {
            Console.WriteLine("\nPublic global plan found, searching for private plans");
            List <Dictionary <CompoundFormula, string> >   newPlan = new List <Dictionary <CompoundFormula, string> >();
            List <KeyValuePair <string, CompoundFormula> > lplan   = new List <KeyValuePair <string, CompoundFormula> >();

            foreach (string actname in highLevelplan)
            {
                Action          act    = mapActionNameToAction[actname];
                CompoundFormula effect = new CompoundFormula("and");
                foreach (GroundedPredicate gp in act.HashEffects)
                {
                    if (agents[map[act.agent]].PublicPredicates.Contains(gp))
                    {
                        effect.AddOperand(gp);
                    }
                }
                lplan.Add(new KeyValuePair <string, CompoundFormula>(act.agent, effect));
            }
            int count = 0;

            finalPlan = new List <string>();
            fault     = "";
            int level = 0;

            Dictionary <string, State> agentState = new Dictionary <string, State>();

            foreach (Agent a in agents)
            {
                agentState.Add(a.name, new State(a.startState));
            }
            CompoundFormula goalFormula = new CompoundFormula("and");
            string          agentName;

            foreach (KeyValuePair <string, CompoundFormula> eff in lplan)
            {
                agentName   = eff.Key;
                goalFormula = new CompoundFormula(eff.Value);

                HashSet <GroundedPredicate> mutex = new HashSet <GroundedPredicate>();
                foreach (GroundedPredicate gp in goalFormula.GetAllPredicates())
                {
                    if (agents[map[agentName]].MutuallyExclusive.ContainsKey(gp))
                    {
                        foreach (GroundedPredicate mgp in agents[map[agentName]].MutuallyExclusive[gp])
                        {
                            mutex.Add(mgp);
                        }
                    }
                }

                /*
                 * foreach (GroundedPredicate publicPos in agents[map[agentName]].PublicPredicates)
                 * {
                 *  if (agentState[agentName].m_lPredicates.Contains(publicPos))
                 *  {
                 *      if (!mutex.Contains(publicPos) && !goalFormula.GetAllPredicates().Contains(publicPos.Negate()))
                 *          goalFormula.AddOperand(publicPos);
                 *  }
                 * }
                 * */
                bool             bUnsolvable      = false;
                ExternalPlanners externalPlanners = new ExternalPlanners();
                ffLplan = externalPlanners.FFPlan(agents[map[agentName]].domain, agents[map[agentName]].problem, agentState[agentName], goalFormula, agents[map[agentName]].m_actions, 5 * 60 * 1000, out bUnsolvable);

                //List<string> todo = Plan(d, p, agentState[agentName], goalFormula, agents[map[agentName]].m_actions);
                //planTheard = new Thread(() => FF_Plan(agents[map[agentName]].domain, agents[map[agentName]].problem, agentState[agentName], goalFormula, agents[map[agentName]].m_actions));
                //List<string> todo = Plan(d, p, agentState[agentName], localGoal, agents[map[agentName]].m_actions);
                //planTheard.Start();
                //planTheard.Join(3000);
                //planTheard.Abort();
                if (ffLplan != null)
                {
                    List <string> todo = ffLplan;
                    foreach (string act in todo)
                    {
                        State s = agentState[agentName].ApplyII(agents[map[agentName]].domain.mapActionNameToAction[act]);
                        if (s == null)
                        {
                            throw new Exception();
                        }
                        agentState[agentName] = s;
                        finalPlan.Add(act);
                        foreach (Agent a in agents)
                        {
                            if (!a.name.Equals(agentName))
                            {
                                agentState[a.name] = agentState[a.name].ApplyEffect(agents[map[agentName]].domain.mapActionNameToAction[act], a.Predicates);
                            }
                        }
                    }
                }
                else
                {
                    return(false);
                }
            }
            Dictionary <string, CompoundFormula> agentGoal = new Dictionary <string, CompoundFormula>();

            foreach (Agent a in agents)
            {
                agentGoal.Add(a.name, new CompoundFormula("and"));
                foreach (GroundedPredicate goalPredicate in a.goal)
                {
                    bool sat = false;

                    /* foreach (var state in agentState.Values)
                     * {
                     *   if (state.m_lPredicates.Contains(goalPredicate))
                     *   {
                     *       sat = true;
                     *       break;
                     *   }
                     * }*/
                    if (true)//!sat)
                    {
                        agentGoal[a.name].AddOperand(goalPredicate);
                    }
                }

                /*  HashSet<GroundedPredicate> mutex = new HashSet<GroundedPredicate>();
                 * foreach (GroundedPredicate gp in agentGoal[a.name].GetAllPredicates())
                 * {
                 *    if (a.MutuallyExclusive.ContainsKey(gp))
                 *    {
                 *        foreach (GroundedPredicate mgp in a.MutuallyExclusive[gp])
                 *            mutex.Add(mgp);
                 *    }
                 * }
                 * foreach (GroundedPredicate publicPos in a.PublicPredicates)
                 * {
                 *    if (agentState[a.name].m_lPredicates.Contains(publicPos) && !mutex.Contains(publicPos))
                 *        agentGoal[a.name].AddOperand(publicPos);
                 * }*/
            }
            foreach (var agent in agents)
            {
                if (agentGoal[agent.name].Operands.Count > 0)
                {
                    //List<string> todo = Plan(d, p, agentState[agent.name], agentGoal[agent.name]);
                    // hsp = new HSPHeuristic(agent.m_actions, agentGoal[agent.name].GetAllPredicates().ToList(), admisabiliy);
                    //  forwardSearch = new ForwardSearchPlanner(agent.m_actions, hsp);
                    // List<Action> todo = forwardSearch.Plan(agentState[agent.name], agentGoal[agent.name].GetAllPredicates().ToList());
                    //List<string> todo = Plan(d, p, agentState[agent.name], agentGoal[agent.name], agent.m_actions);
                    bool             bUnsolvable      = false;
                    ExternalPlanners externalPlanners = new ExternalPlanners();
                    ffLplan = externalPlanners.FFPlan(agents[map[agent.name]].domain, agents[map[agent.name]].problem, agentState[agent.name], agentGoal[agent.name], agent.m_actions, 5 * 60 * 1000, out bUnsolvable);
                    if (ffLplan != null)
                    {
                        List <string> todo = ffLplan;

                        foreach (string act in todo)
                        {
                            agentState[agent.name] = agentState[agent.name].ApplyII(agents[map[agent.name]].domain.mapActionNameToAction[act]);
                            finalPlan.Add(act);
                            foreach (Agent a in agents)
                            {
                                if (!a.name.Equals(agent.name))
                                {
                                    agentState[a.name] = agentState[a.name].ApplyEffect(agents[map[agent.name]].domain.mapActionNameToAction[act], a.Predicates);
                                }
                            }
                        }
                    }
                    else
                    {
                        Program.KillPlanners();
                        //return planToGoalIIII(out finalPlan, out fault);
                    }
                }
            }
            foreach (var agSt in agentState)
            {
                if (agSt.Value == null)
                {
                    return(false);
                }
            }

            return(true);
        }