Ejemplo n.º 1
0
 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();
 }
Ejemplo n.º 2
0
        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);
        }
Ejemplo n.º 3
0
 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>();
 }
Ejemplo n.º 4
0
 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;
 }
Ejemplo n.º 5
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.º 6
0
        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;
        }
Ejemplo n.º 7
0
 public void setHeuristicMethod(Node.Method method)
 {
     this.heuristicMethod = method;
 }
Ejemplo n.º 8
0
        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);
            }
        }
Ejemplo n.º 9
0
        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); ;
        }
Ejemplo n.º 10
0
 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");
     }
 }
Ejemplo n.º 11
0
        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");
            }
        }
Ejemplo n.º 12
0
 public void setParent(Node parent)
 {
     this.parent = parent;
 }
Ejemplo n.º 13
0
 public bool isEqual(Node a)
 {
     return position.Equals(a.getPosition());
 }
Ejemplo n.º 14
0
 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;
 }
Ejemplo n.º 15
0
 public abstract void Init(Node node, bool resetCounters);
Ejemplo n.º 16
0
 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;
 }
Ejemplo n.º 17
0
 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);
 }
Ejemplo n.º 18
0
 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;
 }
Ejemplo n.º 19
0
 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>();
 }
Ejemplo n.º 20
0
 public bool isVisibleNode(Node node)
 {
     foreach (Agent agent in agents)
     {
         if (agent.getMap().isNodeFlag(node, Node.Flag.IsVisible))
             return true;
     }
     return false;
 }
Ejemplo n.º 21
0
        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);
        }
Ejemplo n.º 22
0
 public void setFieldOfViewMethod(Node.Method method)
 {
     this.fieldOfViewMethod = method;
 }