-
Notifications
You must be signed in to change notification settings - Fork 1
/
ia.py
88 lines (82 loc) · 3.18 KB
/
ia.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File --ia.py--
import random
from graph import *
## TEST ##
class thief():
class spawn():
def random():
tlocation = random.choice(nodes) # Random spawn
return(tlocation)
class move():
# Random move
def random(tlocation):
adj = list(g[tlocation].keys()) # Gets the nodes adjacents to the thief
tlocation = random.choice(adj)
return(tlocation)
# Go to the furthest node to the cops
def furthest(tlocation, clocation):
## TEST ##
#adj = list(g[tlocation].keys()) # Gets the nodes adjacents to the thief
#clocation = [6, 9]
#paths = []
#for x in clocation:
# for y in adj:
# paths.append(nx.shortest_path(g)[x][y])
#print(paths)
##------##
adj = list(g[tlocation].keys()) # Gets the nodes adjacents to the thief
#paths = [[(nx.shortest_path(g)[x][clocation]) for x in adj] for clocation in clocation]
#tlocation = paths[0]
# Finds the ajacent node who is the furthest to the policeman
paths = [(nx.shortest_path(g)[x][clocation]) for x in adj]
maxlen = max(map(len, paths))
result = [x for x in paths if len(x) == maxlen]
path = [x[0] for x in result]
path_adj = [list(g[x].keys()) for x in path]
choose = path_adj.index(max(path_adj, key=len))
tlocation = path[choose]
return(tlocation)
class cops():
class spawn():
def random(used):
try:
tlocation = int(used)
while True:
clocation = random.choice(nodes)
if clocation != tlocation:
break
except:
while True:
rand = random.choice(nodes)
if rand not in used:
clocation = rand
break
return(clocation)
class move():
def nearest(tlocation, clocation):
# Finds the shortest path to the thief
adj = list(g[clocation].keys()) # Gets the nodes adjacents to the cops
# Finds the ajacent node who is the furthest to the thief
paths = [nx.shortest_path(g)[x][tlocation] for x in adj]
minlen = min(map(len, paths))
result = [x for x in paths if len(x) == minlen]
path = [x[0] for x in result]
path_adj = [list(g[x].keys()) for x in path]
choose = path_adj.index(max(path_adj, key=len))
clocation = path[choose]
return(clocation)
#def Cstats(Clocation):
# Cadj_list = list(g[Clocation].keys())
# Cadj_loc_list = list(g[Clocation].keys())
# Cadj_loc_list.append(Clocation)
# return(Cadj_list, Cadj_loc_list)
#
#def Tstats(Tlocation):
# Tadj_list = list(g[Tlocation].keys())
# return(Tadj_list)
#def logs(Tlocation, Clocation):
# logs = pd.Dataframe({'Voleur', 'Policier',
# 'Graphe'})
# logs = logs.append({'Voleur':Tlocation,'Policier':Clocation,
# 'Graphe':config.graph['name']})
# logs.to_csv('logs.csv')