Beispiel #1
0
 public Action(State stateA, State stateB, string name)
 {
     this.StateA = stateA;
     this.StateB = stateB;
     this.Cost = 1;
     this.Name = name;
 }
Beispiel #2
0
        public Node(Node parent, Action action, State state, State target)
        {
            this.State = state;
              this.Parent = parent;
              this.Action = action;
              if (this.Parent != null && this.Action != null)
            this.PathCost = this.Parent.PathCost + action.Cost;

              this.EstimatedTotalPathCost = this.PathCost + Math.Sqrt(Math.Pow(this.State.X - target.X,2) + Math.Pow(this.State.Y - target.Y,2));
        }
Beispiel #3
0
        public BFNode(BFNode parent, Action action, State state)
        {
            this.State = state;
            this.Parent = parent;
            this.Action = action;
            if (this.Parent != null && this.Action != null)
            this.PathCost = this.Parent.PathCost + action.Cost;

            this.EstimatedTotalPathCost = this.PathCost;
        }
Beispiel #4
0
        protected Action GetOrCreateAction(State a, State b, string name)
        {
            // no action to no state
            if (a == null || b == null || a.Type == Tile.Wall || b.Type == Tile.Wall) return null;

            // check if action already exists
            var action = this.Actions.SingleOrDefault(act => (act.StateA.Equals(a) && act.StateB.Equals(b)) || (act.StateA.Equals(b) && act.StateB.Equals(a)));
            if (action == null)
            {
            action = new Action(a, b, name);
            this.Actions.Add(action);
            }

            return action;
        }
Beispiel #5
0
        public static int Search(List<State> states, List<Action> actions, State start, Tile target)
        {
            var found = 0;

            PriorityQueue<BFNode> frontier = new PriorityQueue<BFNode>();
            List<State> explored = new List<State>();

            frontier.Add(new BFNode(start));

            while (frontier.Count > 0)
            {
            // Chooses the lowest-cost node in the frontier
            BFNode currentBFNode = frontier.Pop();

            // Win condition
            if (currentBFNode.State.Type.Equals(target))
            found++;

            explored.Add(currentBFNode.State);

            // Filter actions to the ones connected to the current node
            foreach (Action action in actions.Where(a => a.StateA.Equals(currentBFNode.State)))
            {
            // One of A or B will be the currentBFNode's action
            // but it won't be added to the frontier since it
            // is already in explored
            var childA = new BFNode(currentBFNode, action, action.StateA);
            var childB = new BFNode(currentBFNode, action, action.StateB);

            if (!explored.Contains(childA.State) && !frontier.Any(n => n.State == childA.State))
            frontier.Add(childA);

            if (!explored.Contains(childB.State) && !frontier.Any(n => n.State == childB.State))
            frontier.Add(childB);
            }
            }
            return found;
        }
Beispiel #6
0
        public static INode Search(List<State> states, List<Action> actions, State start, State end)
        {
            PriorityQueue<Node> frontier = new PriorityQueue<Node>();
            List<State> explored = new List<State>();

            frontier.Add(new Node(start, end));

            while (frontier.Count > 0)
            {
            // Chooses the lowest-cost node in the frontier
            Node currentNode = frontier.Pop();

            // Win condition
            if (currentNode.State.Equals(end))
            return currentNode;

            // Add currentNode to list of explored
            explored.Add(currentNode.State);

            // Filter actions to the ones connected to the current node
            foreach (Action action in actions.Where(a => a.StateA.Equals(currentNode.State) || a.StateB.Equals(currentNode.State)))
            {
            // One of A or B will be the currentNode's action
            // but it won't be added to the frontier since it
            // is already in explored
            var childA = new Node(currentNode, action, action.StateA, end);
            if (!explored.Contains(childA.State))
            frontier.Add(childA);

            var childB = new Node(currentNode, action, action.StateB, end);
            if (!explored.Contains(childB.State))
            frontier.Add(childB);
            }
            }

            return null;
        }
Beispiel #7
0
        protected State GetOrCreateState(int px, int py, Tile type, Map map)
        {
            // check bounds
            if (type == Tile.Invalid) return null;
            if (px < 0  || py < 0 || px > map.Tiles[0].Count - 1 || py > map.Tiles.Count - 1) return null;

            // check if state already exists
            var state = this.States.SingleOrDefault(s => s.X == px && s.Y == py);
            if (state == null)
            {
            state = new State(px, py, type);
            this.States.Add(state);
            }

            return state;
        }
Beispiel #8
0
 public Node(State state, State target)
     : this(null, null, state, target)
 {
 }
Beispiel #9
0
 public Node(Node parent, Action action, State target)
     : this(parent, action, action.StateA, target)
 {
 }
Beispiel #10
0
 public BFNode(State state)
     : this(null, null, state)
 {
 }