Exemplo n.º 1
0
 public void AddNeighbor(GraphNode <T> n, double cost)
 {
     this.edges.AddLast(new GraphEdge <T>(this, n, cost));
 }
Exemplo n.º 2
0
 public GraphEdge(GraphNode <T> start, GraphNode <T> end, double cost)
 {
     this.start_node = start;
     this.end_node   = end;
     this.cost       = cost;
 }
Exemplo n.º 3
0
        public virtual LinkedList <GraphNode <T> > ShortestPath(GraphNode <T> start_node, GraphNode <T> end_node)
        {
            examined_nodes.Clear();
            curr_start_node = start_node;
            curr_end_node   = end_node;

            open_nodes.Clear();          //clear both open and closed lists since they will be repopulated in this pathfinding step
            closed_nodes.Clear();
            this.StartNode = start_node; //start node for pathfinding step

            resetCosts();                //reset all the costs

            start_node.CostSoFar = 0;
            DijkstraEvaluate();

            //after the evaluation is done, each node has its cost and connections set
            //starting at the end node and appending nodes by following the connections backwards allows us to build and return a list containing the nodes in the shortest path in order.
            GraphNode <T> curr = end_node;
            LinkedList <GraphNode <T> > node_order = new LinkedList <GraphNode <T> >();

            while (curr != start_node)
            {
                node_order.AddFirst(curr);
                curr = curr.Connection.Start;
            }

            node_order.AddFirst(curr);

            return(node_order);
        }
Exemplo n.º 4
0
        //shortest path using a*
        public override LinkedList <GraphNode <T> > ShortestPath(GraphNode <T> start_node, GraphNode <T> end_node)
        {
            examined_nodes.Clear();
            curr_start_node = start_node;
            curr_end_node   = end_node;

            open_nodes.Clear(); //clear open and closed and reset all costs (costs so far)
            closed_nodes.Clear();
            this.StartNode = start_node;
            resetCosts();
            start_node.CostSoFar = 0;

            //for each node compute its heuristic cost
            foreach (GraphNode <T> n in nodes)
            {
                n.Heuristic = n.Value.ComputeHeuristic(end_node.Value);
            }

            //same node path construction as parent class
            Evaluate(end_node);

            GraphNode <T> curr = end_node;
            LinkedList <GraphNode <T> > node_order = new LinkedList <GraphNode <T> >();

            while (curr != start_node)
            {
                node_order.AddFirst(curr);
                curr = curr.Connection.Start;
            }

            node_order.AddFirst(curr);

            return(node_order);
        }
Exemplo n.º 5
0
 public void Add(GraphNode <T> node)
 {
     nodes.AddLast(node);
 }