コード例 #1
0
        private State ApplyAxiom(State s, List <Action> lActions, Domain d)
        {
            State     sCurrent = s;
            Predicate pNew     = new GroundedPredicate("new");
            Predicate pDone    = new GroundedPredicate("done");

            while (!sCurrent.Contains(pNew.Negate()) || !sCurrent.Contains(pDone))
            {
                Action a1 = d.GroundActionByName(new string[] { "pre-axiom", "" }, sCurrent.Predicates, false);
                Action a2 = d.GroundActionByName(new string[] { "axiom", "" }, sCurrent.Predicates, false);
                if (a1 != null && a2 != null)
                {
                    sCurrent = sCurrent.Apply(a1);
                    sCurrent = sCurrent.Apply(a2);
                    lActions.Add(a1);
                    lActions.Add(a2);
                }
            }

            Action a = d.GroundActionByName(new string[] { "observe-new-F", "" }, sCurrent.Predicates, false);

            sCurrent = sCurrent.Apply(a);
            lActions.Add(a);

            a        = d.GroundActionByName(new string[] { "fixpoint", "" }, sCurrent.Predicates, false);
            sCurrent = sCurrent.Apply(a);
            lActions.Add(a);

            return(sCurrent);
        }
コード例 #2
0
        private State ApplyCompute(State s, string sName, List <Action> lActions, Domain d)
        {
            State     sCurrent = s;
            Predicate pNew     = new GroundedPredicate("new-" + sName);
            Predicate pDone    = new GroundedPredicate("done-" + sName);
            int       i        = 0;

            while (!sCurrent.Contains(pNew.Negate()) || !sCurrent.Contains(pDone) || i < 10)
            {
                Action a1 = d.GroundActionByName(new string[] { "pre-" + sName, "" }, sCurrent.Predicates, false);
                Action a2 = d.GroundActionByName(new string[] { "compute-" + sName, "" }, sCurrent.Predicates, false);
                if (a1 != null && a2 != null)
                {
                    sCurrent = sCurrent.Apply(a1);
                    sCurrent = sCurrent.Apply(a2);
                    lActions.Add(a1);
                    lActions.Add(a2);
                }
                i++;
            }

            Action a = d.GroundActionByName(new string[] { "observe-new-" + sName + "-F", "" }, sCurrent.Predicates, false);

            sCurrent = sCurrent.Apply(a);
            lActions.Add(a);

            a        = d.GroundActionByName(new string[] { "post-" + sName, "" }, sCurrent.Predicates, false);
            sCurrent = sCurrent.Apply(a);
            lActions.Add(a);

            return(sCurrent);
        }
コード例 #3
0
        public List <Action> SolveII(Problem p, Domain d)
        {
            State        sStart    = p.GetInitialBelief().ChooseState(true);
            List <State> lOpenList = new List <State>();

            lOpenList.Add(sStart);
            State sCurrent = null, sNext = null;
            Dictionary <State, Action> dMapStateToGeneratingAction = new Dictionary <State, Action>();

            dMapStateToGeneratingAction[sStart] = null;
            Dictionary <State, State> dParents = new Dictionary <State, State>();
            Dictionary <State, int>   dDepth = new Dictionary <State, int>();

            dDepth[sStart]   = 0;
            dParents[sStart] = null;
            int           cProcessed           = 0;
            List <string> lActionNames         = new List <string>();

            while (lOpenList.Count > 0)
            {
                sCurrent = lOpenList[0];
                lOpenList.RemoveAt(0);
                List <Action> lActions = d.GroundAllActions(sCurrent.Predicates, false);
                foreach (Action a in lActions)
                {
                    sNext = sCurrent.Apply(a);
                    bool bGiven = false;
                    foreach (Predicate pGiven in sNext.Predicates)
                    {
                        if (pGiven.Name.ToLower().Contains("given"))
                        {
                            bGiven = true;
                        }
                    }
                    if (!lActionNames.Contains(a.Name))
                    {
                        lActionNames.Add(a.Name);
                    }
                    if (sNext != null && p.IsGoalState(sNext))
                    {
                        return(GeneratePlan(sCurrent, a, dParents, dMapStateToGeneratingAction));
                    }
                    if (!dParents.Keys.Contains(sNext))
                    {
                        dDepth[sNext]   = dDepth[sCurrent] + 1;
                        dParents[sNext] = sCurrent;
                        dMapStateToGeneratingAction[sNext] = a;
                        lOpenList.Add(sNext);
                    }
                }
                cProcessed++;
                if (cProcessed % 10 == 0)
                {
                    Debug.WriteLine(cProcessed + ") " + dDepth[sCurrent] + "," + lOpenList.Count);
                }
            }
            return(null);
        }
コード例 #4
0
        public List <Action> PlanII(State sStartState)
        {
            CompoundFormula fGoal = (CompoundFormula)m_pProblem.Goal;
            //List<Predicate> lGoal = new List<Predicate>();
            //foreach (PredicateFormula vf in fGoal.Operands)
            //    lGoal.Add(vf.Predicate);
            State        sCurrent = null, sNext = null;
            List <State> lOpenList = new List <State>();
            Dictionary <State, State>  dParents          = new Dictionary <State, State>();
            Dictionary <State, Action> dGeneratingAction = new Dictionary <State, Action>();
            Dictionary <State, double> dHeuristic        = new Dictionary <State, double>();
            Dictionary <State, double> dCost             = new Dictionary <State, double>();
            int cRepeated = 0;

            dCost[sStartState] = 0;
            sStartState.GroundAllActions();
            dHeuristic[sStartState] = m_fHeuristic.h(sStartState);
            lOpenList.Add(sStartState);
            dParents[sStartState]          = null;
            dGeneratingAction[sStartState] = null;
            m_cObservedStates = 1;
            int cHandled = 0;

            while (lOpenList.Count > 0)
            {
                sCurrent = GetMinimalStateBFS(lOpenList, dHeuristic, dCost);
                cHandled++;
                Debug.Write("\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b" + cHandled + ", " + dHeuristic[sCurrent] + "," + lOpenList.Count);
                lOpenList.Remove(sCurrent);
                sCurrent.GroundAllActions();
                foreach (Action a in sCurrent.AvailableActions)
                {
                    sNext = sCurrent.Apply(a);
                    if (sNext != null && !dParents.ContainsKey(sNext))
                    {
                        m_cObservedStates++;
                        dParents[sNext]          = sCurrent;
                        dGeneratingAction[sNext] = a;
                        if (sNext.Contains(fGoal))
                        {
                            return(CreatePlan(sNext, dParents, dGeneratingAction));
                        }
                        else
                        {
                            dCost[sNext]      = dCost[sCurrent] + 1;
                            dHeuristic[sNext] = m_fHeuristic.h(sNext);
                            lOpenList.Add(sNext);
                        }
                    }
                    else if (sNext != null)
                    {
                        cRepeated++;
                    }
                }
            }
            return(null);
        }
コード例 #5
0
 /*
 public override Dictionary<State, Action> GetNextStates(State s)
 {
     List<Action> lPlan = new List<Action>();
     double cActions = Delta(s, lPlan);
     Dictionary<State, Action> lStates = new Dictionary<State, Action>();
     foreach (Action a in lPlan)
     {
         State sTag = s.Apply(a);
         if (sTag != null)
             lStates[sTag] = a;
     }
     return lStates;
 }
 */
 public override Dictionary<State, Action> GetNextStates(State s)
 {
     Dictionary<State, Action> lStates = new Dictionary<State, Action>();
     List<Action> lActions = m_dDomain.GroundAllActions(s.Predicates, false);
     foreach (Action a in lActions)
     {
         State sNext = s.Apply(a);
         if(sNext != null)
             lStates[sNext] = a;
     }
     return lStates;
 }
コード例 #6
0
        private bool ValidatePlan(State sStartState, List <Action> lPlan)
        {
            State sCurrent = sStartState;

            foreach (Action a in lPlan)
            {
                sCurrent = sCurrent.Apply(a);
            }
            bool bValid = sCurrent.Contains(m_pProblem.Goal);

            return(bValid);
        }
コード例 #7
0
        /*
         * public override Dictionary<State, Action> GetNextStates(State s)
         * {
         *  List<Action> lPlan = new List<Action>();
         *  double cActions = Delta(s, lPlan);
         *  Dictionary<State, Action> lStates = new Dictionary<State, Action>();
         *  foreach (Action a in lPlan)
         *  {
         *      State sTag = s.Apply(a);
         *      if (sTag != null)
         *          lStates[sTag] = a;
         *  }
         *  return lStates;
         * }
         */
        public override Dictionary <State, Action> GetNextStates(State s)
        {
            Dictionary <State, Action> lStates = new Dictionary <State, Action>();
            List <Action> lActions             = m_dDomain.GroundAllActions(s.Predicates, false);

            foreach (Action a in lActions)
            {
                State sNext = s.Apply(a);
                if (sNext != null)
                {
                    lStates[sNext] = a;
                }
            }
            return(lStates);
        }
コード例 #8
0
        private State ObserveAll(State s, List <Action> lActions, Domain d)
        {
            State         sCurrent = s;
            List <Action> l        = d.GroundAllActions(s.Predicates, false);

            foreach (Action a in l)
            {
                if (a.Name.Contains("observe"))
                {
                    sCurrent = sCurrent.Apply(a);
                    lActions.Add(a);
                }
            }
            return(sCurrent);
        }
コード例 #9
0
        public List <Action> Solve(Problem p, Domain d)
        {
            State         sStart   = p.GetInitialBelief().ChooseState(true);
            List <Action> lActions = new List <Action>();
            Action        aClear   = d.GroundActionByName(new string[] { "clear-all", "" }, sStart.Predicates, false);

            sStart = sStart.Apply(aClear);
            lActions.Add(aClear);
            State sComputeUpstream = ApplyCompute(sStart, "upstream", lActions, d);
            State sComputeAffected = ApplyCompute(sComputeUpstream, "affected", lActions, d);
            State sComputePath     = ApplyCompute(sComputeAffected, "path", lActions, d);
            State sComputeLine     = ApplyCompute(sComputePath, "line", lActions, d);

            //State sObserveAll = ObserveAll(sComputeLine, lActions, d);
            return(lActions);
        }
コード例 #10
0
        public List <Action> ManualSolve(Problem p, Domain d)
        {
            State sStart = p.GetInitialBelief().ChooseState(true);
            State sCurrent = null, sNext = null;
            Dictionary <State, Action> dMapStateToGeneratingAction = new Dictionary <State, Action>();

            dMapStateToGeneratingAction[sStart] = null;
            Dictionary <State, State> dParents = new Dictionary <State, State>();

            dParents[sStart] = null;
            int           cProcessed           = 0;
            List <string> lActionNames         = new List <string>();

            sCurrent = sStart;
            while (!p.IsGoalState(sCurrent))
            {
                List <Action> lActions = d.GroundAllActions(sCurrent.Predicates, false);
                Debug.WriteLine("Available actions:");
                for (int i = 0; i < lActions.Count; i++)
                {
                    Debug.WriteLine(i + ") " + lActions[i].Name);
                }
                Debug.Write("Choose action number: ");
                int    iAction = int.Parse(Console.ReadLine());
                Action a       = lActions[iAction];
                sNext = sCurrent.Apply(a);

                foreach (Predicate pNew in sNext.Predicates)
                {
                    if (!sCurrent.Predicates.Contains(pNew))
                    {
                        Debug.WriteLine(pNew);
                    }
                }

                if (!dParents.Keys.Contains(sNext))
                {
                    dParents[sNext] = sCurrent;
                    dMapStateToGeneratingAction[sNext] = a;
                }

                sCurrent = sNext;

                cProcessed++;
            }
            return(GeneratePlan(sCurrent, null, dParents, dMapStateToGeneratingAction));
        }
コード例 #11
0
        private List<State> ApplyActions(List<List<Predicate>> lChosen, List<Action> lActions)
        {
            List<State> lCurrent = new List<State>();
            foreach (List<Predicate> lState in lChosen)
            {
                State s = new State(Problem);
                foreach (Predicate p in lState)
                    s.AddPredicate(p);
                foreach (Predicate p in Observed)
                    s.AddPredicate(p);
                lCurrent.Add(s);
            }

            List<State> lNext = null;
            foreach (Action a in lActions)
            {
                lNext = new List<State>();
                foreach (State s in lCurrent)
                {
                    State sTag = s.Apply(a);
                    if (sTag == null)
                        sTag = s.Apply(a);
                    lNext.Add(sTag);
                }
                lCurrent = lNext;
            }

            return lCurrent;
        }