public Action(State stateA, State stateB, string name) { this.StateA = stateA; this.StateB = stateB; this.Cost = 1; this.Name = name; }
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)); }
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; }
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; }
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; }
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; }
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; }
public Node(State state, State target) : this(null, null, state, target) { }
public Node(Node parent, Action action, State target) : this(parent, action, action.StateA, target) { }
public BFNode(State state) : this(null, null, state) { }