public void createAgents(int mapSize, int cellSize) { int x, y; seed = unchecked(DateTime.Now.Ticks.GetHashCode()); random = new Random(seed); /* create the agents */ agents.Clear(); for (int i = 0; i < simulation.get(View.ViewNumericUpDown.NumberOfAgents); i++) { /* create the agent */ Color backColor = agentColorPair[i, 0]; Color foreColor = agentColorPair[i, 1]; Agent agent = new Agent((Agent.AgentID)i, simulation.get(View.ViewNumericUpDown.MapSize), simulation.getCellSize(), backColor, foreColor); /* select random start node */ x = random.Next(simulation.get(View.ViewNumericUpDown.MapSize)); y = random.Next(simulation.get(View.ViewNumericUpDown.MapSize)); Node start = agent.getMap().getNodeAtLocation(new Point(x, y)); agent.setNode(Agent.NodeType.Start, start); start.setFlag(Node.Flag.IsWalkable, true); start.setFlag(Node.Flag.IsSpecial, true); map.getNodeAtLocation(start.getPosition()).setFlag(Node.Flag.IsWalkable, true); map.getNodeAtLocation(start.getPosition()).setFlag(Node.Flag.IsSpecial, true); /* select random finish node */ x = random.Next(simulation.get(View.ViewNumericUpDown.MapSize)); y = random.Next(simulation.get(View.ViewNumericUpDown.MapSize)); Node finish = agent.getMap().getNodeAtLocation(new Point(x, y)); agent.setNode(Agent.NodeType.Finish, finish); finish.setFlag(Node.Flag.IsWalkable, true); finish.setFlag(Node.Flag.IsSpecial, true); map.getNodeAtLocation(finish.getPosition()).setFlag(Node.Flag.IsWalkable, true); map.getNodeAtLocation(finish.getPosition()).setFlag(Node.Flag.IsSpecial, true); /* add agent to list */ agents.Add(agent); } }
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 void PostStep(Agent agent, Model model) { Agent.AgentID id = model.getSimulation().getViewableAgent(); /* only update the selected agent or all agents if selected */ Node currentNode = model.getMap().getNodeAtLocation(agent.getNode(Agent.NodeType.Current).getPosition()); Color backColor = agent.getColor(Agent.ColorType.BackColor); Color foreColor = agent.getColor(Agent.ColorType.ForeColor); if (currentNode.isEqual(agent.getNode(Agent.NodeType.Finish))) currentNode.repaintNode(backColor, foreColor, "F"); else { if (model.getSimulation().getVisualizations() == View.Visualizations.Enabled) currentNode.repaintNode(backColor, foreColor, currentNode.Text); else currentNode.repaintNode(backColor, foreColor, ""); } if (agent.getNode(Agent.NodeType.Current).getParent() != null) { Node parentNode = model.getMap().getNodeAtLocation(agent.getNode(Agent.NodeType.Current).getParent().getPosition()); if (model.getSimulation().getPersistenceEnabled() == View.PathPersistence.Disabled | !agent.isActive()) { if (model.isSpecialNode(parentNode)) { Agent specialAgent = model.getSpecialNodeAgent(parentNode); backColor = specialAgent.getColor(Agent.ColorType.BackColor); foreColor = specialAgent.getColor(Agent.ColorType.ForeColor); parentNode.repaintNode(backColor, foreColor, ""); } else { if (agent.isActive()) if (parentNode.getFlag(Node.Flag.IsWalkable)) parentNode.repaintNode(Color.White, Color.White, ""); else parentNode.repaintNode(Color.Gray, Color.Gray, ""); else { /* is node visible on active agent's map */ Agent viewAgent = model.getAgent(model.getSimulation().getViewableAgent()); bool isVisibleOnActiveAgentsMap = viewAgent.getMap().isNodeFlag(parentNode, Node.Flag.IsVisible); if (isVisibleOnActiveAgentsMap) { /* is node special */ bool isSpecial = viewAgent.getMap().isNodeFlag(parentNode, Node.Flag.IsSpecial); if (isSpecial) { if (parentNode.isEqual(viewAgent.getNode(Agent.NodeType.Start))) parentNode.repaintNode(viewAgent.getColor(Agent.ColorType.BackColor), viewAgent.getColor(Agent.ColorType.ForeColor), "S"); else parentNode.repaintNode(viewAgent.getColor(Agent.ColorType.BackColor), viewAgent.getColor(Agent.ColorType.ForeColor), "F"); } else { /* is node walkable on active agent's map */ bool isWalkable = viewAgent.getMap().isNodeFlag(parentNode, Node.Flag.IsWalkable); if (isWalkable) { bool isPathNode = viewAgent.getMap().isNodeFlag(parentNode,Node.Flag.IsPath); if (isPathNode) { if (model.getSimulation().getPersistenceEnabled() == View.PathPersistence.Enabled) { /* non active agent's parent node is also the active agent's path node */ Color b = viewAgent.getColor(Agent.ColorType.BackColor); Color f = viewAgent.getColor(Agent.ColorType.ForeColor); parentNode.repaintNode(b, f, ""); } else { /* non active agent's walkable path node with persistence disabled */ parentNode.repaintNode(Color.White, Color.White, ""); } } else { /* non active agent's parent node is just a visible walkable node */ parentNode.repaintNode(Color.White, Color.White, ""); } } else { /* non active agent's parent node is just a visible non-walkable node */ parentNode.repaintNode(Color.Gray, Color.Gray, ""); } } } else { /* node is not visible on active agent's map therefore we can just paint it black */ parentNode.repaintNode(Color.Black, Color.Black, ""); } } } } } }
public bool isSuccess(Agent agent) { return agent.getNode(Agent.NodeType.Current).isEqual(agent.getNode(Agent.NodeType.Finish)); }
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 Agent getAgent(Agent.AgentID agentID) { foreach (Agent agent in agents) { if (agent.getId() == agentID) return agent; } return null; }
public bool UpdateAgentMap(Agent agent) { int nonWalkableNonVisibleCount = 0; List<Node> sharedMapFovNodes; /* get viewable shared map nodes based on the agent's current location */ sharedMapFovNodes = map.getNodesWithinDistance( agent.getNode(Agent.NodeType.Current), simulation.getFieldOfViewMethod(), simulation.get(View.ViewNumericUpDown.FieldOfView)); foreach (Node sharedMapNode in sharedMapFovNodes) { /* update the agent's map */ bool isWalkable = sharedMapNode.getFlag(Node.Flag.IsWalkable); Node agentMapNode = agent.getMap().getNodeAtLocation(sharedMapNode.getPosition()); agentMapNode.setFlag(Node.Flag.IsVisible, true); agentMapNode.setFlag(Node.Flag.IsWalkable, isWalkable); /* count the nodes that are not already visible and non walkable in order to reset the algorithm for the next step */ if (!sharedMapNode.getFlag(Node.Flag.IsVisible) && !sharedMapNode.getFlag(Node.Flag.IsWalkable)) nonWalkableNonVisibleCount++; /* only paint the shared map if the agent is active */ if (agent.isActive()) { bool isPathNode = map.isNodeFlag(sharedMapNode, Node.Flag.IsPath); bool isSpecialNode = this.isSpecialNode(sharedMapNode); bool isVisualizations = (simulation.getVisualizations() == View.Visualizations.Enabled) ? true : false; /* dont repaint if its the start node, finish node, or a path node */ if (!isSpecialNode && !isPathNode) { if (isWalkable) { if (isVisualizations) sharedMapNode.repaintNode(Color.White, Color.Black, sharedMapNode.Text); else sharedMapNode.repaintNode(Color.White, Color.White, ""); } else sharedMapNode.repaintNode(Color.Gray, Color.Gray, ""); } } } return (nonWalkableNonVisibleCount == 0) ? false : true; }
public bool SetAgentTarget(Agent agent) { Agent agentInView = this.isAgentInView(agent); /* check if agent is in view */ if (agentInView != null && (simulation.getAgentCooperation() == View.AgentCooperation.Enabled)) { /* agent in view, check if meeting has already occurred */ if (agent.meetingListCheck(agentInView.getId())) { /* meeting has already occurred so set the target node to finish */ agent.setNode(Agent.NodeType.Target, agent.getNode(Agent.NodeType.Finish)); } else { /* agent in view, check if both agents occupy the same same node or adjacent node */ if (agentInView.getNode(Agent.NodeType.Current).isEqual(agent.getNode(Agent.NodeType.Current)) || agentInView.getNode(Agent.NodeType.Current).isAdjacent(agent.getNode(Agent.NodeType.Current), simulation.get(View.ViewNumericUpDown.MapSize))) { /* agent in view & agents are in sharing distance, therefore update the visible flags */ int sharedNodeCount = 0; foreach (Node agentInViewMapNode in agentInView.getMap().getNodes()) { /* get the agent map node */ Node agentMapNode = agent.getMap().getNodeAtLocation(agentInViewMapNode.getPosition()); /* skip nodes that are not visible */ if (!agentInViewMapNode.getFlag(Node.Flag.IsVisible)) continue; /* skip nodes that are already visible in the agent's map */ if (agentMapNode.getFlag(Node.Flag.IsVisible)) continue; /* don't share shared nodes (IsShared flag is cleared after both agents complete the sharing) */ if (agentInViewMapNode.getFlag(Node.Flag.IsShared)) continue; /* set flags flags */ agentMapNode.setFlag(Node.Flag.IsVisible, true); agentMapNode.setFlag(Node.Flag.IsWalkable, agentInViewMapNode.getFlag(Node.Flag.IsWalkable)); agentMapNode.setFlag(Node.Flag.IsShared, true); /* increment the shared node counter */ sharedNodeCount++; /* paint the shared map if the agent is active */ if (agent.isActive()) { /* get the shared map node */ Node sharedMapNode = map.getNodeAtLocation(agentMapNode.getPosition()); bool isWalkable = agentMapNode.getFlag(Node.Flag.IsWalkable); bool isPathNode = map.isNodeFlag(sharedMapNode, Node.Flag.IsPath); bool isSpecialNode = this.isSpecialNode(sharedMapNode); bool isVisualizations = (simulation.getVisualizations() == View.Visualizations.Enabled) ? true : false; /* dont repaint if its the start node, finish node, or a path node */ if (!isSpecialNode && !isPathNode) { if (isWalkable) { if (isVisualizations) sharedMapNode.repaintNode(Color.White, Color.Black, sharedMapNode.Text); else sharedMapNode.repaintNode(Color.White, Color.White, ""); } else sharedMapNode.repaintNode(Color.Gray, Color.Gray, ""); } } } agent.meetingListAdd(agentInView.getId()); agent.getCounters().incNodesShared(sharedNodeCount); /* set the target node to the finish node */ agent.setNode(Agent.NodeType.Target, agent.getNode(Agent.NodeType.Finish)); /* indicate that an algorithm reset is required because the target node was changed */ return true; } else { /* agent in view but both agents are not occupying the same node yet */ /* set the agent target node to the agent in view's current node */ Node target = agent.getMap().getNodeAtLocation(agentInView.getNode(Agent.NodeType.Current).getPosition()); agent.setNode(Agent.NodeType.Target, target); /* indicate that an algorithm reset is required because a new target node has been set */ return true; } } } else { /* agent not in view so just keep going to the agents finish node */ agent.setNode(Agent.NodeType.Target, agent.getNode(Agent.NodeType.Finish)); } /* indicate that an algorithm reset is not required because the target node was not changed */ return false; }
public Agent isAgentInView(Agent me) { foreach (Agent agent in agents) { if (agent.getId() != me.getId()) { double distance = agent.getNode(Agent.NodeType.Current).getDistance(me.getNode(Agent.NodeType.Current), simulation.getFieldOfViewMethod()); if (distance <= simulation.get(View.ViewNumericUpDown.FieldOfView)) { return agent; } } } return null; }
private void PaintAgentMap(Agent agent) { Debug.WriteLine("Painting " + agent.getId().ToString() + " Map"); for (int i = 0; i < agent.getMap().getNodes().Count; i++) { Node agentMapNode = agent.getMap().getNode(i); Node sharedMapNode = model.getMap().getNode(i); sharedMapNode.setFlag(Node.Flag.IsPath, agentMapNode.getFlag(Node.Flag.IsPath)); sharedMapNode.setFlag(Node.Flag.IsSpecial, agentMapNode.getFlag(Node.Flag.IsSpecial)); sharedMapNode.setFlag(Node.Flag.IsVisible, agentMapNode.getFlag(Node.Flag.IsVisible)); sharedMapNode.setFlag(Node.Flag.IsWalkable, agentMapNode.getFlag(Node.Flag.IsWalkable)); sharedMapNode.setCost(Node.Cost.Movement, agentMapNode.getCost(Node.Cost.Movement)); sharedMapNode.setCost(Node.Cost.Heuristic, agentMapNode.getCost(Node.Cost.Heuristic)); sharedMapNode.setCost(Node.Cost.Total, agentMapNode.getCost(Node.Cost.Total)); } model.Reset(true); }
private Agent.AgentID incNodeEditAgentId(Model model, Agent.AgentID init) { int index = (int)init; int maxIndex = (model.getSimulation().get(View.ViewNumericUpDown.NumberOfAgents) - 1); index = (index == maxIndex) ? (int)Agent.AgentID.Agent_0 : index + 1; this.nodeEditBackColor = model.getAgent((Agent.AgentID)index).getColor(Agent.ColorType.BackColor); this.nodeEditForeColor = model.getAgent((Agent.AgentID)index).getColor(Agent.ColorType.ForeColor); return (Agent.AgentID)index; }
public void setViewableAgent(Agent.AgentID viewableAgent) { this.viewableAgent = viewableAgent; }