public Agent(AgentID id, int mapSize, int cellSize, Color backColor, Color foreColor) { this.id = id; this.map = new Map(); this.map.create(Map.MapType.Empty, mapSize, cellSize, 100); this.start = null; this.finish = null; this.current = null; this.target = null; this.backColor = backColor; this.foreColor = foreColor; this.counters = new Counters(); this.active = false; this.meetingList = new List<AgentID>(); this.aLock = new object(); }
public Node(int x, int y, int width, int height) { this.position = new Point(x, y); this.parent = null; this.isWalkable = true; this.isVisible = false; this.isPath = false; this.isSpecial = false; this.isShared = false; this.heuristicCost = 0; this.movementCost = 0; this.totalCost = 0; this.TextAlign = ContentAlignment.MiddleCenter; this.Font = new Font("Arial", 4); this.Location = new Point(position.X * width, position.Y * height); this.Size = new Size(width, height); }
public AStarAlgorithm(Agent agent, Model model, Node.Method heuristicMethod, Node.Method fovMethod) { this.agent = agent; this.model = model; this.heuristicMethod = heuristicMethod; this.fovMethod = fovMethod; open = new List<Node>(); closed = new List<Node>(); neighbors = new List<Node>(); }
protected bool visualizationPaintOK(Node node, Agent agent, Model model) { bool isActive = agent.isActive(); View.Visualizations visualizations = model.getSimulation().getVisualizations(); bool notSpecial = !model.isSpecialNode(node); bool notPath = !model.isPathNode(node); if (isActive && visualizations == View.Visualizations.Enabled && notSpecial && notPath) return true; else return false; }
protected void takeStep(Agent agent, Node algorithmCurrent, Model model) { /* check to see if we didn't move at all */ if (algorithmCurrent.isEqual(agent.getNode(Agent.NodeType.Current))) return; /* check to see if we need to backtrack */ while (!algorithmCurrent.getParent().isEqual(agent.getNode(Agent.NodeType.Current))) algorithmCurrent = algorithmCurrent.getParent(); /* take a step! */ agent.setNode(Agent.NodeType.Current, algorithmCurrent); /* increment step counter */ agent.getCounters().incSteps(); }
protected bool checkTarget(Model model, Agent agent, Node algorithmCurrent) { bool targetReached; /* target is target node if target is not null */ targetReached = algorithmCurrent.isEqual(agent.getNode(Agent.NodeType.Target)); /* take step if target is reached */ if (targetReached) takeStep(agent, algorithmCurrent, model); return targetReached; }
public void setHeuristicMethod(Node.Method method) { this.heuristicMethod = method; }
public override void Init(Node current, bool resetCounters) { open.Clear(); closed.Clear(); neighbors.Clear(); if (resetCounters) agent.getCounters().reset(); open = agent.getMap().getWalkableNodes(); foreach (Node node in open) { if (node.isEqual(current)) node.setCost(Node.Cost.Total, 0); else node.setCost(Node.Cost.Total, double.MaxValue); node.setParent(null); } }
public override bool Step(object sender) { BackgroundWorker worker = sender as BackgroundWorker; int visualizationDelay = model.getSimulation().get(View.ViewNumericUpDown.VisualizationDelay); /* run algorithm on agent's viewable map to determine the next step */ while (open.Count > 0) { /* check if the run has been cancelled */ if (worker.CancellationPending) return false; /* get the next node */ current = getNext(open); if (current == null) return false; /* check if target node has been reached */ Node target = agent.getNode(Agent.NodeType.Target); if (target != null && current.isEqual(target)) break; /* move node from open list to closed list */ open.Remove(current); closed.Add(current); /* update current node visualizations when put on closed list */ if (visualizationPaintOK(current, agent, model)) { Node sharedMapNode = model.getMap().getNodeAtLocation(current.getPosition()); int count = agent.getCounters().getNodesEvaluated(); sharedMapNode.repaintNode(Color.Pink, Color.Black, sharedMapNode.Text); Thread.Sleep(visualizationDelay); } /* get the neighbors */ neighbors.Clear(); neighbors = agent.getMap().getNeighbors(agent, current, null, fovMethod); /* add neighbor's to open list if not already there */ foreach (Node node in neighbors) { /* skip nodes that are already on the closed list */ if (closed.Contains(node)) continue; /* calculate the new movement cost */ double newMovementCost = current.getCost(Node.Cost.Movement) + current.getDistance(node, fovMethod); if (!open.Contains(node) || newMovementCost < node.getCost(Node.Cost.Movement)) { /* save the parent node */ node.setParent(current); /* update the movement cost */ node.setCost(Node.Cost.Movement, newMovementCost); /* calculate the heuristic cost */ node.setCost(Node.Cost.Heuristic, node.getDistance(agent.getNode(Agent.NodeType.Target), heuristicMethod)); /* update the f-score */ node.setCost(Node.Cost.Total, node.getCost(Node.Cost.Movement) + node.getCost(Node.Cost.Heuristic)); /* add to open list if not already there */ if (!open.Contains(node)) { open.Add(node); /* increment nodes evaluated only when nodes are added to open list */ agent.getCounters().incNodesEvaluated(); /* visualizations */ if (visualizationPaintOK(node, agent, model)) { Node sharedMapNode = model.getMap().getNodeAtLocation(node.getPosition()); int count = agent.getCounters().getNodesEvaluated(); sharedMapNode.repaintNode(Color.LightGreen, Color.Black, count.ToString()); Thread.Sleep(visualizationDelay); } } } } } return checkTarget(model, agent, current); ; }
public void setNode(NodeType type, Node node) { switch (type) { case NodeType.Start: this.start = node; break; case NodeType.Finish: this.finish = node; break; case NodeType.Current: this.current = node; break; case NodeType.Target: this.target = node; break; default: throw new Exception("invalid node type"); } }
public double getDistance(Node a, Method method) { double dx = Math.Abs(a.getPosition().X - this.position.X); double dy = Math.Abs(a.getPosition().Y - this.position.Y); switch (method) { case Method.Manhattan: return dx + dy; case Method.Euclidean: return Math.Sqrt(Math.Pow(dx, 2) + Math.Pow(dy, 2)); case Method.EuclideanSquared: return Math.Pow(dx, 2) + Math.Pow(dy, 2); case Method.Chebyshev: return Math.Max(dx, dy); default: throw new Exception("method not defined"); } }
public void setParent(Node parent) { this.parent = parent; }
public bool isEqual(Node a) { return position.Equals(a.getPosition()); }
public bool isAdjacent(Node node, int max) { /* 0 1 2 * _ _ _ * 0 |_|_|_| * 1 |_|_|_| * 2 |_|_|_| * * */ try { if (node.getPosition().X - 1 == this.position.X && node.getPosition().Y + 1 == this.position.Y) return true; if (node.getPosition().X - 0 == this.position.X && node.getPosition().Y + 1 == this.position.Y) return true; if (node.getPosition().X + 1 == this.position.X && node.getPosition().Y + 1 == this.position.Y) return true; if (node.getPosition().X - 1 == this.position.X && node.getPosition().Y + 0 == this.position.Y) return true; if (node.getPosition().X - 0 == this.position.X && node.getPosition().Y + 0 == this.position.Y) return true; if (node.getPosition().X + 1 == this.position.X && node.getPosition().Y + 0 == this.position.Y) return true; if (node.getPosition().X - 1 == this.position.X && node.getPosition().Y - 1 == this.position.Y) return true; if (node.getPosition().X - 0 == this.position.X && node.getPosition().Y - 1 == this.position.Y) return true; if (node.getPosition().X + 1 == this.position.X && node.getPosition().Y - 1 == this.position.Y) return true; } catch { return false; } return false; }
public abstract void Init(Node node, bool resetCounters);
public Agent getSpecialNodeAgent(Node node) { foreach (Agent a in agents) { if (node.isEqual(a.getNode(Agent.NodeType.Start)) | node.isEqual(a.getNode(Agent.NodeType.Finish))) return a; } return null; }
public override void Init(Node node, bool resetCounters) { closed.Clear(); open.Clear(); neighbors.Clear(); if (resetCounters) agent.getCounters().reset(); this.current = node; node.setParent(node.getParent()); node.setCost(Node.Cost.Movement, 0); node.setCost(Node.Cost.Heuristic, node.getCost(Node.Cost.Movement) + node.getDistance(agent.getNode(Agent.NodeType.Target), heuristicMethod)); open.Add(node); }
public bool isSpecialNode(Node node) { foreach (Agent a in agents) { if (node.isEqual(a.getNode(Agent.NodeType.Start)) | node.isEqual(a.getNode(Agent.NodeType.Finish))) return true; } return false; }
public DijkstraAlgorithm(Agent agent, Model model, Node.Method fovMethod) { this.agent = agent; this.model = model; this.fovMethod = fovMethod; open = new List<Node>(); closed = new List<Node>(); neighbors = new List<Node>(); }
public bool isVisibleNode(Node node) { foreach (Agent agent in agents) { if (agent.getMap().isNodeFlag(node, Node.Flag.IsVisible)) return true; } return false; }
public override bool Step(object sender) { BackgroundWorker worker = sender as BackgroundWorker; int visualizationDelay = model.getSimulation().get(View.ViewNumericUpDown.VisualizationDelay); while (open.Count > 0) { /* check if the run has been cancelled */ if (worker.CancellationPending) return false; /* pick the node in the open list with the shortest distance */ current = getNext(open); if (current == null) return false; if (current.isEqual(agent.getNode(Agent.NodeType.Target)) | (current.getCost(Node.Cost.Total) == double.MaxValue)) break; open.Remove(current); closed.Add(current); agent.getCounters().incNodesEvaluated(); /* update current node visualizations when put on closed list */ if (visualizationPaintOK(current, agent, model)) { Node sharedMapNode = model.getMap().getNodeAtLocation(current.getPosition()); int count = agent.getCounters().getNodesEvaluated(); sharedMapNode.repaintNode(Color.Pink, Color.Black, agent.getCounters().getNodesEvaluated().ToString()); Thread.Sleep(visualizationDelay); } /* get neighbors that are on the open list only */ neighbors.Clear(); neighbors = agent.getMap().getNeighbors(agent, current, open, fovMethod); foreach (Node node in neighbors) { /* calculate the new distance */ double distance = current.getCost(Node.Cost.Total) + current.getDistance(node, fovMethod); /* if distance is lower then replace */ if (distance < node.getCost(Node.Cost.Total)) { node.setCost(Node.Cost.Total, distance); node.setParent(current); if (!closed.Contains(node)) { if (visualizationPaintOK(node, agent, model)) { Node sharedMapNode = model.getMap().getNodeAtLocation(node.getPosition()); int count = agent.getCounters().getNodesEvaluated(); sharedMapNode.repaintNode(Color.LightGreen, Color.Black, agent.getCounters().getNodesEvaluated().ToString()); Thread.Sleep(visualizationDelay); } } } } } return checkTarget(model, agent, current); }
public void setFieldOfViewMethod(Node.Method method) { this.fieldOfViewMethod = method; }