Commit de90fdc3 authored by Julian Rudolf's avatar Julian Rudolf
Browse files

Changes:

  - option to change speeds
  - option to change length
  - names of snakes
  - function to detect if pos(x,y) is on or after crossing
  - little ui changes
parent bc75c478
This diff is collapsed.
......@@ -89,12 +89,12 @@
# "vel": [[0,-1],[0,-1],[0,-1],[0,-1],[-1,0],[-1,0],[-1,0],[-1,0],[-1,0],[-1,0]],
# "angle": 'up'}]
start3 = [{"pos": [[5,6],[5,5],[5,4],[5,3],[5,2],[5,1],[4,1],[3,1],[2,1],[2,2],[2,3],[2,4],[2,5],[2,6],[2,7]],
"vel": [[1,0],[1,0],[1,0],[1,0],[1,0],[0,1],[0,1],[0,1],[0,1],[-1,0],[-1,0],[-1,0],[-1,0],[-1,0],[-1,0]],
start3 = [{"pos": [[5,6],[5,5],[5,4],[5,3],[5,2],[5,1],[4,1],[3,1],[2,1],[2,2],[2,3],[2,4],[2,5],[2,6],[2,7],[2,8],[2,9],[2,10],[2,11],[2,12]],
"vel": [[1,0],[1,0],[1,0],[1,0],[1,0],[0,1],[0,1],[0,1],[0,1],[-1,0],[-1,0],[-1,0],[-1,0],[-1,0],[-1,0],[-1,0],[-1,0],[-1,0],[-1,0],[-1,0]],
"angle": 'right'},
{"pos": [[24,1],[25,1],[26,1],[27,1],[27,2],[27,3],[27,4],[27,5],[27,6],[27,7],[27,8],[27,9],[27,10],[27,11],[27,12]],
"vel": [[0,-1],[0,-1],[0,-1],[0,-1],[-1,0],[-1,0],[-1,0],[-1,0],[-1,0],[-1,0],[-1,0],[-1,0],[-1,0],[-1,0],[-1,0]],
{"pos": [[24,1],[25,1],[26,1],[27,1],[27,2],[27,3],[27,4],[27,5],[27,6],[27,7],[27,8],[27,9],[27,10],[27,11],[27,12],[27,13],[27,14],[27,15],[27,16],[27,17]],
"vel": [[0,-1],[0,-1],[0,-1],[0,-1],[-1,0],[-1,0],[-1,0],[-1,0],[-1,0],[-1,0],[-1,0],[-1,0],[-1,0],[-1,0],[-1,0],[-1,0],[-1,0],[-1,0],[-1,0],[-1,0]],
"angle": 'up'}]
map3 = [[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,1,1,1,1,1,1,1,1,1,1,1,1],
......@@ -290,7 +290,7 @@ start8 = [{"pos": [[4,13],[3,13],[2,13],[1,13]],
"vel": [[0,1],[0,1],[0,1],[0,1]],
"angle": 'down'},
{"pos": [[25,13],[26,13],[27,13],[28,13]],
{"pos": [[26,13],[27,13],[28,13],[29,13]],
"vel": [[0,-1],[0,-1],[0,-1],[0,-1]],
"angle": 'up'}]
......@@ -306,7 +306,6 @@ map8 = [[1,1,1,1,1,1,1,1,1,1,1,0,1,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1],
[1,1,1,1,1,1,1,1,1,1,1,1,1,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1],
[1,1,1,1,1,1,1,1,1,1,1,1,1,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1],
[1,1,1,1,1,1,1,1,1,1,1,1,1,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1],
[1,1,1,1,1,1,1,1,1,1,1,1,1,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1],
[1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1],
[1,1,1,1,1,1,1,1,1,1,1,1,1,0,1,1,1,1,1,1,0,1,1,1,1,1,1,1,1,1],
[1,1,1,1,1,1,1,1,1,1,1,1,1,0,1,1,1,1,1,1,0,1,1,1,1,1,1,1,1,1],
......@@ -316,6 +315,7 @@ map8 = [[1,1,1,1,1,1,1,1,1,1,1,0,1,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1],
[1,1,1,1,1,1,1,1,1,1,1,1,1,0,1,1,1,1,1,1,0,1,1,1,1,1,1,1,1,1],
[1,1,1,1,1,1,1,1,1,1,1,1,1,0,1,1,1,1,1,1,0,1,1,1,1,1,1,1,1,1],
[1,1,1,1,1,1,1,1,1,1,1,1,1,0,1,1,1,1,1,1,0,1,1,1,1,1,1,1,1,1],
[1,1,1,1,1,1,1,1,1,1,1,1,1,0,1,1,1,1,1,1,0,1,1,1,1,1,1,1,1,1],
[1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1],
[1,1,1,1,1,1,1,1,1,1,1,1,1,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1],
[1,1,1,1,1,1,1,1,1,1,1,1,1,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1],
......
......@@ -19,13 +19,13 @@ import maps
# ------------------------------------------------------------------
# length of snakes
snake_length = 30
snake_length = 15
# save location
save_file = 'maps/map6.pickle'
save_file = '../Shield/maps/map3.pickle'
# map to convert
map = np.array(maps.map6)
map = np.array(maps.map3)
# results:
......@@ -274,8 +274,8 @@ def graph_from_map(map):
nodes_map = wrap_around_edges(nodes_map)
print('map')
print(nodes_map)
#print('map')
#print(nodes_map)
# add edges to the corresponding nodes
for i in range(len(nodes_map)):
......@@ -291,9 +291,9 @@ def graph_from_map(map):
if j < len(nodes_map[i]) - 1 and nodes_map[i][j + 1] < 0:
nodes[current_node - 2].add_edge(nodes_map[i][j + 1])
print('nodes')
for n in nodes:
print(str(n.node_number) + ": " + str(n.get_edges()))
#print('nodes')
#for n in nodes:
#print(str(n.node_number) + ": " + str(n.get_edges()))
return nodes_map, nodes
......@@ -315,43 +315,53 @@ def networkx_from_graph(graph):
edges[e] = ([n.node_number], np.sum(nodes_map == e))
else:
edges[e][0].append(n.node_number)
print('edges')
print(edges)
#print('edges')
#print(edges)
for k, e in edges.items():
nx_graph.add_edge(e[0][0], e[0][1], weight=e[1] + 1)
return nx_graph, edges
# get map with nodes and edges
graph = graph_from_map(map)
# get networkx graph and edges
nx_graph, edges = networkx_from_graph(graph)
# compute all shortest paths with length of snake
paths = dict(nx.all_pairs_dijkstra_path(nx_graph, cutoff=snake_length, weight='weight'))
# find longest path with length of snake
max = 0
for d1 in paths.values():
for d2 in d1.values():
max = len(d2) if len(d2) > max else max
# draw network
plt.plot()
# networkx can't really draw multigraphs
nx.draw_spectral(nx_graph, with_labels=True, font_weight='bold', connectionstyle='arc3, rad = 0.1')
labels = nx.get_edge_attributes(nx_graph, 'weight')
plt.show()
# get amount of nodes and edges
node_name_max = len(nx_graph.nodes) + 1
edge_name_max = 0
for edge_name in edges.keys():
if edge_name < edge_name_max:
edge_name_max = edge_name
# get maximum edge length
dist_max = int(sorted(nx_graph.edges(data=True), key=lambda x: x[2]['weight'], reverse=True)[0][2]['weight']) - 1
# save all infos in file
pickle.dump((graph[0], edges, max, node_name_max, edge_name_max, dist_max), open(save_file, 'wb'))
# nx.write_gpickle(nx_graph, 'graph1.pickle')
def generate_graph(length, new_map, path):
global snake_length
global map
global save_file
snake_length = length
map = np.array(new_map)
save_file = path
# get map with nodes and edges
graph = graph_from_map(map)
# get networkx graph and edges
nx_graph, edges = networkx_from_graph(graph)
# compute all shortest paths with length of snake
paths = dict(nx.all_pairs_dijkstra_path(nx_graph, cutoff=snake_length, weight='weight'))
# find longest path with length of snake
max = 0
for d1 in paths.values():
for d2 in d1.values():
max = len(d2) if len(d2) > max else max
# draw network
plt.plot()
# networkx can't really draw multigraphs
nx.draw_spectral(nx_graph, with_labels=True, font_weight='bold', connectionstyle='arc3, rad = 0.1')
labels = nx.get_edge_attributes(nx_graph, 'weight')
#plt.show()
# get amount of nodes and edges
node_name_max = len(nx_graph.nodes) + 1
edge_name_max = 0
for edge_name in edges.keys():
if edge_name < edge_name_max:
edge_name_max = edge_name
# get maximum edge length
dist_max = int(sorted(nx_graph.edges(data=True), key=lambda x: x[2]['weight'], reverse=True)[0][2]['weight']) - 1
# save all infos in file
pickle.dump((graph[0], edges, max, node_name_max, edge_name_max, dist_max), open(save_file, 'wb'))
# nx.write_gpickle(nx_graph, 'graph1.pickle')
print("Finished creating graph")
No preview for this file type
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment