コード例 #1
0
        /// <summary>
        /// for debugging purposes. Provides a human readable string of all the preconditions.
        /// </summary>
        /// <param name="planner">Planner.</param>
        public string Describe(ActionPlanner planner)
        {
            var sb = new StringBuilder();

            for (var i = 0; i < ActionPlanner.MAX_CONDITIONS; i++)
            {
                if ((DontCare & (1L << i)) == 0)
                {
                    var val = planner.ConditionNames[i];
                    if (val == null)
                    {
                        continue;
                    }

                    bool set = ((Values & (1L << i)) != 0L);

                    if (sb.Length > 0)
                    {
                        sb.Append(", ");
                    }
                    sb.Append(set ? val.ToUpper() : val);
                }
            }

            return(sb.ToString());
        }
コード例 #2
0
ファイル: ActionPlanner.cs プロジェクト: JonSnowbd/Ash
        internal WorldState ApplyPostConditions(ActionPlanner ap, int actionnr, WorldState fr)
        {
            var  pst        = ap._postConditions[actionnr];
            long unaffected = pst.DontCare;
            long affected   = (unaffected ^ -1L);

            fr.Values    = (fr.Values & unaffected) | (pst.Values & affected);
            fr.DontCare &= pst.DontCare;
            return(fr);
        }
コード例 #3
0
ファイル: AStar.cs プロジェクト: JonSnowbd/Ash
        /* from: http://theory.stanford.edu/~amitp/GameProgramming/ImplementationNotes.html
         * OPEN = priority queue containing START
         * CLOSED = empty set
         * while lowest rank in OPEN is not the GOAL:
         * current = remove lowest rank item from OPEN
         * add current to CLOSED
         * for neighbors of current:
         *  cost = g(current) + movementcost(current, neighbor)
         *  if neighbor in OPEN and cost less than g(neighbor):
         *    remove neighbor from OPEN, because new path is better
         *  if neighbor in CLOSED and cost less than g(neighbor): **
         *    remove neighbor from CLOSED
         *  if neighbor not in OPEN and neighbor not in CLOSED:
         *    set g(neighbor) to cost
         *    add neighbor to OPEN
         *    set priority queue rank to g(neighbor) + h(neighbor)
         *    set neighbor's parent to current
         */

        /// <summary>
        /// Make a plan of actions that will reach desired world state
        /// </summary>
        /// <param name="ap">Ap.</param>
        /// <param name="start">Start.</param>
        /// <param name="goal">Goal.</param>
        /// <param name="storage">Storage.</param>
        public static Stack <Action> Plan(ActionPlanner ap, WorldState start, WorldState goal,
                                          List <AStarNode> selectedNodes = null)
        {
            storage.Clear();

            var currentNode = Pool <AStarNode> .Obtain();

            currentNode.WorldState                = start;
            currentNode.ParentWorldState          = start;
            currentNode.CostSoFar                 = 0;                                                 // g
            currentNode.HeuristicCost             = CalculateHeuristic(start, goal);                   // h
            currentNode.CostSoFarAndHeuristicCost = currentNode.CostSoFar + currentNode.HeuristicCost; // f
            currentNode.Depth = 1;

            storage.AddToOpenList(currentNode);

            while (true)
            {
                // nothing left open so we failed to find a path
                if (!storage.HasOpened())
                {
                    storage.Clear();
                    return(null);
                }

                currentNode = storage.RemoveCheapestOpenNode();

                storage.AddToClosedList(currentNode);

                // all done. we reached our goal
                if (goal.Equals(currentNode.WorldState))
                {
                    var plan = ReconstructPlan(currentNode, selectedNodes);
                    storage.Clear();
                    return(plan);
                }

                var neighbors = ap.GetPossibleTransitions(currentNode.WorldState);
                for (var i = 0; i < neighbors.Count; i++)
                {
                    var cur    = neighbors[i];
                    var opened = storage.FindOpened(cur);
                    var closed = storage.FindClosed(cur);
                    var cost   = currentNode.CostSoFar + cur.CostSoFar;

                    // if neighbor in OPEN and cost less than g(neighbor):
                    if (opened != null && cost < opened.CostSoFar)
                    {
                        // remove neighbor from OPEN, because new path is better
                        storage.RemoveOpened(opened);
                        opened = null;
                    }

                    // if neighbor in CLOSED and cost less than g(neighbor):
                    if (closed != null && cost < closed.CostSoFar)
                    {
                        // remove neighbor from CLOSED
                        storage.RemoveClosed(closed);
                    }

                    // if neighbor not in OPEN and neighbor not in CLOSED:
                    if (opened == null && closed == null)
                    {
                        var nb = Pool <AStarNode> .Obtain();

                        nb.WorldState                = cur.WorldState;
                        nb.CostSoFar                 = cost;
                        nb.HeuristicCost             = CalculateHeuristic(cur.WorldState, goal);
                        nb.CostSoFarAndHeuristicCost = nb.CostSoFar + nb.HeuristicCost;
                        nb.Action           = cur.Action;
                        nb.ParentWorldState = currentNode.WorldState;
                        nb.Parent           = currentNode;
                        nb.Depth            = currentNode.Depth + 1;
                        storage.AddToOpenList(nb);
                    }
                }

                // done with neighbors so release it back to the pool
                ListPool <AStarNode> .Free(neighbors);
            }
        }
コード例 #4
0
 public Agent()
 {
     _planner = new ActionPlanner();
 }
コード例 #5
0
 public WorldState(ActionPlanner planner, long values, long dontcare)
 {
     this.planner = planner;
     Values       = values;
     DontCare     = dontcare;
 }
コード例 #6
0
 public static WorldState Create(ActionPlanner planner)
 {
     return(new WorldState(planner, 0, -1));
 }