Ejemplo n.º 1
0
        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);
            }
        }
Ejemplo n.º 2
0
        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();
        }
Ejemplo n.º 3
0
        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;
        }