private void sToolStripMenuItem_Click(object sender, EventArgs e) { visibilityGraphToolStripMenuItem_Click(null, null); Wall wall = (Wall)_graphObjects.Where(s => s is Wall).First(); RectangleF bounds = wall.getBounds(); Bitmap bitmap = pictureBoxMain.Image as Bitmap; Graphics g = setupGraphic(bitmap, bounds); VisibilityGraph vg = new VisibilityGraph(); vg.addObjects(_graphObjects); vg.Goal = _goal; vg.Start = _start; NodeGraph ng = new NodeGraph(); foreach (LineSegment line in vg.getVisibilityGraph()) { ng.addLine(line); } Node start = new Node(); start.Location = _start; Node goal = new Node(); goal.Location = _goal; Node solution = Djikstra.ShortestPath(ng, start, goal); ScaledDrawer.drawShortestPath(g, _shortestPathPen, solution, _scale); pictureBoxMain.Image = bitmap; Driver driver = new Driver(solution); System.Threading.Thread tr = new System.Threading.Thread(driver.followPathToGoal); tr.Start(); }
public void GrokkinAlgortihms_Ex_7_1_A() { // Arrange var startNode = new Node("Start"); var aNode = new Node("A"); var bNode = new Node("B"); var cNode = new Node("C"); var dNode = new Node("D"); var endNode = new Node("End"); var graph = new Graph(startNode, endNode); var edges = graph.Add(startNode); edges.Add(new Edge { Node = aNode, Weight = 5 }); edges.Add(new Edge { Node = bNode, Weight = 2 }); edges = graph.Add(aNode); edges.Add(new Edge { Node = cNode, Weight = 4 }); edges.Add(new Edge { Node = dNode, Weight = 2 }); edges = graph.Add(bNode); edges.Add(new Edge { Node = aNode, Weight = 8 }); edges.Add(new Edge { Node = dNode, Weight = 7 }); edges = graph.Add(cNode); edges.Add(new Edge { Node = endNode, Weight = 3 }); edges.Add(new Edge { Node = dNode, Weight = 6 }); edges = graph.Add(dNode); edges.Add(new Edge { Node = endNode, Weight = 1 }); graph.Add(endNode); // Act var shortestPath = Djikstra.Run(graph); // Assert Assert.Equal(new[] { startNode, aNode, dNode, endNode }, shortestPath); }
public void GrokkinAlgortihms_Page_125() { // Arrange var startNode = new Node("Book - Start"); var aNode = new Node("LP"); var bNode = new Node("Poster"); var cNode = new Node("Guitar"); var dNode = new Node("Drum"); var endNode = new Node("Piano - End"); var graph = new Graph(startNode, endNode); var edges = graph.Add(startNode); edges.Add(new Edge { Node = aNode, Weight = 5 }); edges.Add(new Edge { Node = bNode, Weight = 0 }); edges = graph.Add(aNode); edges.Add(new Edge { Node = cNode, Weight = 15 }); edges.Add(new Edge { Node = dNode, Weight = 20 }); edges = graph.Add(bNode); edges.Add(new Edge { Node = cNode, Weight = 30 }); edges.Add(new Edge { Node = dNode, Weight = 35 }); edges = graph.Add(cNode); edges.Add(new Edge { Node = endNode, Weight = 20 }); edges = graph.Add(dNode); edges.Add(new Edge { Node = endNode, Weight = 10 }); graph.Add(endNode); // Act var shortestPath = Djikstra.Run(graph); // Assert Assert.Equal(new[] { startNode, aNode, dNode, endNode }, shortestPath); }
private static int[,] RunDjikstra(Vertex[] vertices) { int[,] paths = new int[vertices.Length, vertices.Length]; for (int i = 0; i < vertices.Length; i++) { ShortestPathTestClass.ResetAllVertices(vertices); Djikstra.Run(vertices[i]); for (int j = 0; j < vertices.Length; j++) { paths[i, j] = vertices[j].Depth; } } return(paths); }
public void GrokkinAlgortihms_Page_120() { // Arrange var startNode = new Node("Twin Peaks"); var aNode = new Node("A"); var bNode = new Node("B"); var cNode = new Node("C"); var dNode = new Node("D"); var endNode = new Node("Golden Gate"); var graph = new Graph(startNode, endNode); var edges = graph.Add(startNode); edges.Add(new Edge { Node = aNode, Weight = 4 }); edges.Add(new Edge { Node = bNode, Weight = 10 }); edges = graph.Add(aNode); edges.Add(new Edge { Node = dNode, Weight = 21 }); edges = graph.Add(bNode); edges.Add(new Edge { Node = cNode, Weight = 5 }); edges = graph.Add(cNode); edges.Add(new Edge { Node = dNode, Weight = 5 }); edges = graph.Add(dNode); edges.Add(new Edge { Node = endNode, Weight = 4 }); graph.Add(endNode); // Act var shortestPath = Djikstra.Run(graph); // Assert Assert.Equal(new[] { startNode, bNode, cNode, dNode, endNode }, shortestPath); }
public void GrokkinAlgortihms_Page_117() { // Arrange var startNode = new Node("Start"); var aNode = new Node("A"); var bNode = new Node("B"); var endNode = new Node("Fin"); var graph = new Graph(startNode, endNode); var edges = graph.Add(startNode); edges.Add(new Edge { Node = aNode, Weight = 6 }); edges.Add(new Edge { Node = bNode, Weight = 2 }); edges = graph.Add(aNode); edges.Add(new Edge { Node = endNode, Weight = 1 }); edges = graph.Add(bNode); edges.Add(new Edge { Node = aNode, Weight = 3 }); edges.Add(new Edge { Node = endNode, Weight = 5 }); graph.Add(endNode); // Act var shortestPath = Djikstra.Run(graph); // Assert Assert.Equal(new[] { startNode, bNode, aNode, endNode }, shortestPath); }
}//end getAStarPath //=====================================================================================================================// //Given a start and an end supergrid, we create compound flowfield between both grids and any adjacent tiles (if they are diagonal) public void getNextFlowfield(int curr_i, int curr_j, int nex_i, int nex_j) { //GetComponent<unit_behavior>().ff_table.deleteFF(gameObject.GetInstanceID()); //unlink the previous flowfield from this object's id int current_i = curr_i; int current_j = curr_j; int next_i = nex_i; int next_j = nex_j; int row_length = 0; int col_length = 0; int xOff = 0; //offset for calculating global int zOff = 0; //grid indices int destRow = 0; //calculate local grid int destCol = 0; //indices int type = 0; //tells us which direction we are generating our flowfield in //this way we can figure out what cells are blocked when plotting a route generate_obstacle_grid obs = new generate_obstacle_grid(); //row_length, col_length, xOff,zOff, and destrow and col depend //on which direction the next cell is in //[SE] if (current_i == next_i && current_j == next_j) { row_length = grid_ratio; col_length = grid_ratio; xOff = grid_ratio * (current_i); zOff = grid_ratio * (current_j); destCol = subGrid.worldToCell(destination).Item1 % grid_ratio; destRow = subGrid.worldToCell(destination).Item2 % grid_ratio; if (destCol < 0) //deal with negatives { destCol = grid_ratio + destCol; } if (destRow < 0) { destRow = grid_ratio + destRow; } type = 0; } //[S][E] else if (current_i < next_i && current_j == next_j) { row_length = 2 * grid_ratio; //for dynamic sizing, use 10 * Math.abs(current_i - next_i) col_length = grid_ratio; xOff = grid_ratio * (current_i); zOff = grid_ratio * (current_j); destCol = grid_ratio / 2; //these are fixed destinations, we need to dynamically destRow = (2 * grid_ratio) - 1; //pick destinations based on which cells are blocked or not type = 0; } //[ ][E] //[S][ ] 2. else if (current_i < next_i && current_j < next_j) { row_length = 2 * grid_ratio; col_length = 2 * grid_ratio; xOff = grid_ratio * (current_i); zOff = grid_ratio * (current_j); destCol = (2 * grid_ratio) - 1; destRow = (2 * grid_ratio) - 1; type = 0; } //[E] //[S] 3. else if (current_i == next_i && current_j < next_j) { row_length = grid_ratio; col_length = 2 * grid_ratio; xOff = grid_ratio * (current_i); zOff = grid_ratio * (current_j); destCol = (2 * grid_ratio) - 1; destRow = grid_ratio / 2; type = 0; } //[E][ ] //[ ][S] 4. else if (current_i > next_i && current_j < next_j) { row_length = 2 * grid_ratio; col_length = 2 * grid_ratio; xOff = grid_ratio * (current_i - 1); zOff = grid_ratio * (current_j); destCol = (2 * grid_ratio) - 1; destRow = 0; type = 1; } //[E][S] 5. else if (current_i > next_i && current_j == next_j) { row_length = 2 * grid_ratio; col_length = grid_ratio; xOff = grid_ratio * (current_i - 1); zOff = grid_ratio * (current_j); destCol = grid_ratio / 2; destRow = 0; type = 1; } //[ ][S] //[E][ ] 6. else if (current_i > next_i && current_j > next_j) { row_length = 2 * grid_ratio; col_length = 2 * grid_ratio; xOff = grid_ratio * (current_i - 1); zOff = grid_ratio * (current_j - 1); destCol = 0; destRow = 0; type = 2; } //[S] //[E] 7. else if (current_i == next_i && current_j > next_j) { row_length = grid_ratio; col_length = 2 * grid_ratio; xOff = grid_ratio * (current_i); zOff = grid_ratio * (current_j - 1); destCol = 0; destRow = grid_ratio / 2; type = 3; } //[S][ ] //[ ][E] else if (current_i < next_i && current_j > next_j) { row_length = 2 * grid_ratio; col_length = 2 * grid_ratio; xOff = grid_ratio * (current_i); zOff = grid_ratio * (current_j - 1); destCol = 0; destRow = (2 * grid_ratio) - 1; type = 3; } //find the obstacles int[,] obstacle_grid = obs.Generate_grid(subGrid.cellSize, row_length, col_length, xOff, zOff); if (Path.Count == 0) //if we are at the final supergrid location { destRow = subGrid.worldToCell(destination).Item1 % grid_ratio; //subgrid cells per supergrid destCol = subGrid.worldToCell(destination).Item2 % grid_ratio; //needs to be done for negative indices if (destRow < 0) { destRow = grid_ratio + destRow; } if (destCol < 0) { destCol = grid_ratio + destCol; } //allows us to flowfield in all 4 quadrants switch (type) { case 0: break; case 1: destRow = destRow + grid_ratio; break; case 2: destRow = destRow + grid_ratio; destCol = destCol + grid_ratio; break; case 3: destCol = destCol + grid_ratio; break; } } //get destination (find nearest unblocked cell) Pair <int, int> p = obs.find_nearest_unblocked(obstacle_grid, destRow, destCol, row_length, col_length); destRow = p.first; destCol = p.second; //make sure that the destination isn't entirely blocked //if it isn't, then we say that the cell is blocked switch (type) { case 0: if (destRow < grid_ratio && destCol < grid_ratio) { if (next_i != current_i || next_j != current_j) //make sure we are not in the destination cell { aStarBlocked.Add(new Pair <int, int>(next_i, next_j)); // buffer++; //expand our search grid getAstarPath(ref Path); //recalculate path return; } } break; case 1: //Debug.Log("Destrow is: " + destRow); //Debug.Log("Destcol is: " + destCol); if (destCol < grid_ratio && destRow >= grid_ratio) { aStarBlocked.Add(new Pair <int, int>(next_i, next_j)); // buffer++; //expand our search grid getAstarPath(ref Path); //recalculate path return; } break; /* * case 2: * if (destRow >= grid_ratio && destCol >= grid_ratio) * { * aStarBlocked.Add(new Pair<int, int>(next_i, next_j)); // * buffer++; //expand our search grid * getAstarPath(ref Path); //recalculate path * return; * } * break; */ case 3: if (destRow < grid_ratio && destCol >= grid_ratio) { aStarBlocked.Add(new Pair <int, int>(next_i, next_j)); // buffer++; //expand our search grid getAstarPath(ref Path); //recalculate path return; } break; default: break; } //GENERATE FLOWFIELD WITH GIVEN PARAMETERS Djikstra dijk = new Djikstra(); //Debug.Log("Type = " + type); //Debug.Log("row_length: " + row_length + " col_length: " + col_length); //Debug.Log("destCol: " + destCol + " destRow: " + destRow); int[,] dijkstra = dijk.generate_djikstra_grid(row_length, col_length, obstacle_grid, new Pair <int, int>(destCol, destRow)); //these values contextualize our Dijkstra grid for future use djgrid.grid = dijkstra; djgrid.row_length = row_length; djgrid.col_length = col_length; djgrid.type = type; djgrid.xOff = xOff; djgrid.zOff = zOff; //if there is no path, mark this astar cell as blocked if (djgrid.getValuefromObject(gameObject) == -1) { //Debug.Log("next_i: " + next_i + " next_j: " + next_j); aStarBlocked.Add(new Pair <int, int>(next_i, next_j)); // buffer++; //expand our search grid getAstarPath(ref Path); //recalculate path return; } else if (djgrid.getValuefromObject(gameObject) == 0) //if we have reached an objective, but this function is still active for some reason, reset our pathfinding { setDestination(destination); return; } Generate_Flowfield field_obj = new Generate_Flowfield(); Vector3[,] field = field_obj.generate_flowfield(row_length, col_length, dijkstra, xOff, zOff, subGrid.cellSize); //these values also contextualize our flowfield for future use Flowfield ff = new Flowfield(); ff.flowfield = field; ff.row_length = row_length; ff.col_length = col_length; ff.type = type; ff.xOff = xOff; ff.zOff = zOff; GetComponent <unit_behavior>().ff_table.addFF(gameObject.GetInstanceID(), ff); //add flowfield to dictionary and link by ID }//end function
private static bool[] RunDjikstra(Vertex[] vertices, int source) { Djikstra.Run(vertices[source]); return(vertices.Select(v => v.Depth != int.MaxValue).ToArray()); }
private static int[] RunDjikstra(Vertex[] vertices, int source) { Djikstra.Run(vertices[source]); return(vertices.Select(v => v.Depth).ToArray()); }
public static void Main() { var result = Djikstra.GetShortestPath(); }
public void Computerphile_Djikstra_NotOptimal() { // Arrange var startNode = new Node("Start"); var aNode = new Node("A"); var bNode = new Node("B"); var cNode = new Node("C"); var dNode = new Node("D"); var eNode = new Node("E"); var fNode = new Node("F"); var endNode = new Node("End"); var graph = new Graph(startNode, endNode); var edges = graph.Add(startNode); edges.Add(new Edge { Node = aNode, Weight = 1 }); edges = graph.Add(aNode); edges.Add(new Edge { Node = startNode, Weight = 1 }); edges.Add(new Edge { Node = bNode, Weight = 1 }); edges.Add(new Edge { Node = eNode, Weight = 2 }); edges = graph.Add(bNode); edges.Add(new Edge { Node = aNode, Weight = 1 }); edges.Add(new Edge { Node = cNode, Weight = 1 }); edges = graph.Add(cNode); edges.Add(new Edge { Node = dNode, Weight = 1 }); edges.Add(new Edge { Node = bNode, Weight = 1 }); edges = graph.Add(dNode); edges.Add(new Edge { Node = cNode, Weight = 1 }); edges = graph.Add(eNode); edges.Add(new Edge { Node = aNode, Weight = 2 }); edges.Add(new Edge { Node = fNode, Weight = 2 }); edges = graph.Add(fNode); edges.Add(new Edge { Node = endNode, Weight = 2 }); edges.Add(new Edge { Node = eNode, Weight = 2 }); graph.Add(endNode); // Act var shortestPath = Djikstra.Run(graph); // Assert Assert.Equal(new[] { startNode, aNode, eNode, fNode, endNode }, shortestPath); }
public void Computerphile_Djikstra() { // Arrange var startNode = new Node("Start"); var aNode = new Node("A"); var bNode = new Node("B"); var cNode = new Node("C"); var dNode = new Node("D"); var fNode = new Node("F"); var gNode = new Node("G"); var hNode = new Node("H"); var iNode = new Node("I"); var jNode = new Node("J"); var kNode = new Node("K"); var lNode = new Node("L"); var endNode = new Node("End"); var graph = new Graph(startNode, endNode); var edges = graph.Add(startNode); edges.Add(new Edge { Node = aNode, Weight = 7 }); edges.Add(new Edge { Node = bNode, Weight = 2 }); edges.Add(new Edge { Node = cNode, Weight = 3 }); edges = graph.Add(aNode); edges.Add(new Edge { Node = startNode, Weight = 7 }); edges.Add(new Edge { Node = bNode, Weight = 3 }); edges.Add(new Edge { Node = dNode, Weight = 4 }); edges = graph.Add(bNode); edges.Add(new Edge { Node = startNode, Weight = 2 }); edges.Add(new Edge { Node = aNode, Weight = 3 }); edges.Add(new Edge { Node = dNode, Weight = 4 }); edges.Add(new Edge { Node = hNode, Weight = 1 }); edges = graph.Add(cNode); edges.Add(new Edge { Node = startNode, Weight = 3 }); edges.Add(new Edge { Node = lNode, Weight = 2 }); edges = graph.Add(dNode); edges.Add(new Edge { Node = aNode, Weight = 4 }); edges.Add(new Edge { Node = bNode, Weight = 4 }); edges.Add(new Edge { Node = fNode, Weight = 5 }); edges = graph.Add(fNode); edges.Add(new Edge { Node = dNode, Weight = 5 }); edges.Add(new Edge { Node = hNode, Weight = 3 }); edges = graph.Add(hNode); edges.Add(new Edge { Node = bNode, Weight = 1 }); edges.Add(new Edge { Node = fNode, Weight = 3 }); edges.Add(new Edge { Node = gNode, Weight = 2 }); edges = graph.Add(lNode); edges.Add(new Edge { Node = cNode, Weight = 2 }); edges.Add(new Edge { Node = iNode, Weight = 4 }); edges.Add(new Edge { Node = jNode, Weight = 4 }); edges = graph.Add(iNode); edges.Add(new Edge { Node = lNode, Weight = 4 }); edges.Add(new Edge { Node = kNode, Weight = 4 }); edges = graph.Add(jNode); edges.Add(new Edge { Node = lNode, Weight = 4 }); edges.Add(new Edge { Node = kNode, Weight = 4 }); edges = graph.Add(kNode); edges.Add(new Edge { Node = iNode, Weight = 4 }); edges.Add(new Edge { Node = jNode, Weight = 4 }); edges.Add(new Edge { Node = endNode, Weight = 5 }); edges = graph.Add(gNode); edges.Add(new Edge { Node = hNode, Weight = 2 }); edges.Add(new Edge { Node = endNode, Weight = 2 }); graph.Add(endNode); // Act var shortestPath = Djikstra.Run(graph); // Assert Assert.Equal(new[] { startNode, bNode, hNode, gNode, endNode }, shortestPath); }