def Test_Directed(): G = Graph(directed=True) vertices = {} # Create vertices for i in range(6): v = G.insert_vertex(Node(i)) vertices[i] = v v.element().set_vertex(v) # Create edges # list of edges edges = [(0, 1), (0, 2), (1, 2), (1, 3), (2, 3), (3, 4), (3, 5), (4, 5)] for e in edges: edge = G.insert_edge(vertices[e[0]], vertices[e[1]], Arc()) edge.element().set_edge(edge) print("=== Testing hardcoded graph ===") print("Vertex count: ", G.vertex_count()) print("Edge count: ", G.edge_count()) u = vertices[0] v = vertices[5] print("Edges incident to u: ", len(list(G.incident_edges(u)))) print("Edges incident to v: ", len(list(G.incident_edges(v)))) print("Vertices of G: ", [v.element().__str__() for v in G.vertices()]) print("Edges of G: ", [e.element().__str__() for e in G.edges()]) # Test DFS print("DFS test:") dfs_discovered = {u: None} Graph.DFS(G, u, dfs_discovered) # Print vertices of path path = Graph.construct_vertex_path(u, v, dfs_discovered) result = [vertex.element() for vertex in path] print("Vertices in 0-5 path:") print(*result, sep=",") # Print edges of path print("Edges in 0-5 path:") path = Graph.construct_edge_path(u, v, dfs_discovered) result = [edge.element() for edge in path] print(*result, sep=",") # Test BFS print("BFS test:") bfs_discovered = {u: None} Graph.BFS(G, u, bfs_discovered) # Print vertices of path. path = Graph.construct_vertex_path(u, v, bfs_discovered) result = [edge.element() for edge in path] print(*result, sep=",") # Print edges of path. path = Graph.construct_edge_path(u, v, bfs_discovered) result = [edge.element() for edge in path] print(*result, sep=",") print("\n")
def main(): moveList = findPaths() start_state = State(max_m, max_c, True, max_m, max_c, boat_cap, moveList) INITIAL_STATE = State(max_m, max_c, True, max_m, max_c, boat_cap, moveList) # TERMINAL_STATE = State(-1, -1, Direction.NEW_TO_OLD, -1, -1, 0) g = Graph() p = g.BFS(INITIAL_STATE) p = g.DFS(INITIAL_STATE)
def Calculate_Avg_Path_BFS(Graph, iterations, node): BFS_Path_Avg = [] counter = 0 while counter < iterations: try: BFS_Output = Graph.BFS(random.choice(Graph.GraphDictionary.keys())) Step_Counter = 0 while not (BFS_Output[Step_Counter] == node): Step_Counter += 1 BFS_Path_Avg.append(Step_Counter) counter += 1 except: None # Calculamos el promedio de los pasos return sum(BFS_Path_Avg) / iterations
def next_action(self): if (self.info.status != 0 and self.state.stepCount < 100): print("WTF", self.info.status) countPlayerAtGoldMine = 0 x, y = self.info.posx, self.info.posy r_Action = self.ACTION_FREE #for safe if (self.isKeepFree): self.isKeepFree = False return r_Action # 1st rule. Heighest Priority. Craft & Survive if (valid(y, x)): goldOnGround = self.state.mapInfo.gold_amount(x, y) countPlayerAtGoldMine = 0 for player in self.state.players: px, py = player['posx'], player['posy'] if (px == x and py == y): countPlayerAtGoldMine += 1 if (goldOnGround > 0): if (goldOnGround // countPlayerAtGoldMine > 0 and self.info.energy > 5): r_Action = self.ACTION_CRAFT else: g = Graph(9, 21) g.convertToMap(state=self.state, estWood=self.estWood, botInfo=self.info, isBot=True) g.BFS() target = g.getBFSResult(self.pEnergyToStep, self.pStepToGold) if (target == -1): print("NO TARGET") return self.ACTION_FREE ny, nx = g.traceBack(target) ny, nx = int(ny), int(nx) typeOb = self.state.mapInfo.get_obstacle(nx, ny) nextTrap = g.boardMap[ny, nx] if (typeOb == 1): # WOOOD nextTrap = 20 if (nextTrap >= self.info.energy): r_Action = self.ACTION_FREE else: if (ny == y): if (nx > x): r_Action = self.ACTION_GO_RIGHT elif (nx < x): r_Action = self.ACTION_GO_LEFT else: #nx==x if (ny > y): r_Action = self.ACTION_GO_DOWN elif (ny < y): r_Action = self.ACTION_GO_UP else: print("INVALID WTF") if (r_Action < 4 and self.info.energy <= 13 and self.state.stepCount < 90): self.isKeepFree = True return r_Action
def next_action(self): #if (self.info.status!=0 and self.state.stepCount < 100): # print ("WTF",self.info.status) countPlayerAtGoldMine = 0 x, y = self.info.posx, self.info.posy r_Action = self.ACTION_FREE #for safe if (self.isKeepFree): self.isKeepFree = False return r_Action # 1st rule. Heighest Priority. Craft & Survive if (valid(y, x)): goldOnGround = self.state.mapInfo.gold_amount(x, y) countPlayerAtGoldMine = 1 for player in self.state.players: px, py = player['posx'], player['posy'] if (px == x and py == y): countPlayerAtGoldMine += 1 if (goldOnGround > 0 and countPlayerAtGoldMine > 0): if (goldOnGround / countPlayerAtGoldMine > 0 and self.info.energy > 5): r_Action = self.ACTION_CRAFT self.tx = -1 else: g = Graph(Constants.MAP_MAX_Y, Constants.MAP_MAX_X) g.convertToMap(state=self.state, estWood=self.estWood, botInfo=self.info, isBot=True) g.BFS() if (self.tx == -1 or self.state.mapInfo.gold_amount( self.ty, self.tx) == 0): #print ("Change Target") self.tx, self.ty = g.getRandomTarget( y, x, self.selectTargetOption) #print ("Target",self.tx,self.ty) ny, nx = g.traceBack(self.tx, self.ty) ny, nx = int(ny), int(nx) if (ny == -1): self.tx = -1 return self.ACTION_FREE typeOb = self.state.mapInfo.get_obstacle(nx, ny) nextTrap = g.boardMap[ny, nx] if (typeOb == 1): # WOOOD nextTrap = 20 if (nextTrap >= self.info.energy): r_Action = self.ACTION_FREE else: if (ny == y): if (nx > x): r_Action = self.ACTION_GO_RIGHT elif (nx < x): r_Action = self.ACTION_GO_LEFT else: #nx==x if (ny > y): r_Action = self.ACTION_GO_DOWN elif (ny < y): r_Action = self.ACTION_GO_UP else: print("BOT 5 INVALID WTF") if (r_Action < 4 and self.info.energy <= 13 and self.state.stepCount < 90): self.isKeepFree = True return r_Action
def Test_Undirected(): # Test creating graph G = Graph() u = G.insert_vertex(Node('u')) u.element().set_vertex(u) v = G.insert_vertex(Node('v')) v.element().set_vertex(v) w = G.insert_vertex(Node('w')) w.element().set_vertex(w) x = G.insert_vertex(Node('x')) x.element().set_vertex(x) e = G.insert_edge(u, v, Arc()) e.element().set_edge(e) e = G.insert_edge(v, w, Arc()) e.element().set_edge(e) e = G.insert_edge(w, x, Arc()) e.element().set_edge(e) e = G.insert_edge(u, x, Arc()) e.element().set_edge(e) print("=== Testing hardcoded graph ===") print("Vertex count: ", G.vertex_count()) print("Edge count: ", G.edge_count()) print("Edges incident to u: ", len(list(G.incident_edges(u)))) print("Edges incident to v: ", len(list(G.incident_edges(v)))) print("Vertices of G: ", [v.element().__str__() for v in G.vertices()]) print("Edges of G: ", [e.element().__str__() for e in G.edges()]) # Test DFS print("DFS test:") dfs_discovered = {u: None} Graph.DFS(G, u, dfs_discovered) # Print vertices of path path = Graph.construct_vertex_path(u, x, dfs_discovered) result = [vertex.element() for vertex in path] print("Vertices in u-x path:") print(*result, sep=",") # Print edges of path print("Edges in u-x path:") path = Graph.construct_edge_path(u, x, dfs_discovered) result = [edge.element() for edge in path] print(*result, sep=",") # Test BFS print("BFS test:") bfs_discovered = {u: None} Graph.BFS(G, u, bfs_discovered) # Print vertices of path. path = Graph.construct_vertex_path(u, x, bfs_discovered) result = [edge.element() for edge in path] print(*result, sep=",") # Print edges of path. path = Graph.construct_edge_path(u, x, bfs_discovered) result = [edge.element() for edge in path] print(*result, sep=",") print("\n")
import context from Graph import Graph g = Graph() g.add_edge('a', 'b', 0) g.add_edge('d', 'e', 0) g.add_edge('c', 'a', 0) g.add_edge('b', 'c', 0) g.add_edge('d', 'g', 0) g.add_edge('f', 'd', 0) g.add_edge('e', 'f', 0) g.add_edge('b', 'd', 0) g.add_edge('f', 'g', 0) dag, leaves = g.BFS('a') print(dag.calculate_betweeness('a')) print(dag.weights) h = Graph() h.add_edge('a', 'b', 0) h.add_edge('a', 'c', 0) h.add_edge('b', 'd', 0) h.add_edge('c', 'd', 0) dag, leaves = h.BFS('a') dag.calculate_betweeness('a') print(dag.weights)