Example #1
0
        /// <summary>
        /// Returns a plan queue for the agent to use.
        /// </summary>
        public Queue <GAction> Plan(List <GAction> actions, Dictionary <State, int> goal, WorldStates beliefStates)
        {
            // ---------- FILTER OUT UNACHIEVABLE ACTIONS ----------

            List <GAction> usableActions = new List <GAction>();

            foreach (GAction a in actions)
            {
                if (a.IsAchievable())
                {
                    usableActions.Add(a);
                }
            }

            // -------- CREATE THE FIRST NODE IN THE GRAPH --------

            Node startingNode = new Node(null, 0.0f, GWorld.Instance.GetWorld().GetStates(), beliefStates.GetStates(), null);

            // ------------- CALCULATE POSSIBLE PATHS -------------

            List <Node> leaves = new List <Node>();

            bool success = BuildGraph(startingNode, leaves, usableActions, goal);

            // ------- CHECK IF AT LEAST ONE PLAN WAS FOUND -------

            if (!success)
            {
                //Debug.Log("NO PLAN");
                return(null);
            }

            // -------------- FIND THE CHEAPEST PLAN --------------

            Node cheapest = null;

            foreach (Node leaf in leaves) // CONTAINS ACCUMULATED COST FROM ALL NODES IN EACH PLAN
            {
                if (cheapest == null || leaf.cost < cheapest.cost)
                {
                    cheapest = leaf;
                }
            }

            // --------- ASSEMBLE RESULTING PLAN INTO LIST ---------

            List <GAction> resultingPlanList = new List <GAction>();
            Node           n = cheapest;

            while (n != null)
            {
                if (n.action != null)
                {
                    resultingPlanList.Insert(0, n.action);
                }

                n = n.parent;
            }

            // --------- TRANSFER PLAN LIST INTO A QUEUE ---------

            Queue <GAction> resultingPlanQueue = new Queue <GAction>();

            foreach (GAction a in resultingPlanList)
            {
                resultingPlanQueue.Enqueue(a);
            }

            // --------------- PRINT OUT THE PLAN ----------------

            Debug.Log("The Plan is: ");
            foreach (GAction a in resultingPlanQueue)
            {
                Debug.Log("Q: " + a.actionName);
            }

            // ----------------- RETURN THE PLAN -----------------

            return(resultingPlanQueue);
        }