-
Notifications
You must be signed in to change notification settings - Fork 0
/
predator_prey_beta_nu_z_w_policy.py
165 lines (128 loc) · 5.59 KB
/
predator_prey_beta_nu_z_w_policy.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# from multiprocessing import allow_connection_pickling
import random
import numpy as np
from policy_predator import *
# import pygame
# import sys
# # from policy_predator import *
t_max = 90000
'''
0 - empty
1 - predator
2 - prey
'''
types = [0, 1, 2]
model = Policy(load=True, pos=(0, 0))
cc = float(input("Enter with Cc: "))
rhos = []
def number_of_predators(particules):
return len(np.where(particules == 1)[0])
def rho(particules):
return len(np.where(particules == 1)[0]) / (len(particules) * len(particules))
# L, Li, Lf, Lstep, p = float(sys.argv[1]), float(sys.argv[2]), float(sys.argv[3]), float(sys.argv[4]), float(sys.argv[5])
L = 100
a = 0.4
b = 1 - a - cc
p = 0
all_pos = [(i, j) for i in range(L) for j in range(L)]
times = np.arange(0, t_max, 100)
# Density of predators
rhos = []
nb_copies = 50
for nb in range(nb_copies):
# Fill all array with prey
particules = np.array([[2 for i in range(L)] for j in range(L)])
predators_init = 0
while predators_init < (L**2) // 2:
i, j = random.randint(0, len(particules) - 1), random.randint(0, len(particules) - 1)
if particules[i][j] == 2:
particules[i][j] = 1
predators_init += 1
curr_rho = []
for t in range(0, t_max, 100):
# random.shuffle(all_pos)
for iter in range(L * L):
i, j = random.randint(0, L - 1), random.randint(0, L - 1)
particule = particules[i][j]
adress = [(i, (j + 1) % len(particules)), (i, (j - 1) % len(particules)), ((i - 1) % len(particules), j), ((i + 1) % len(particules), j)]
right, left, up, down = particules[adress[0][0]][adress[0][1]], particules[adress[1][0]][adress[1][1]], particules[adress[2][0]][adress[2][1]], particules[adress[3][0]][adress[3][1]]
neighboor = [right, left, up, down]
if particule == 1:
z = random.random()
# Probability of die
if z < float(cc):
particules[i][j] = 0
else:
obs = np.array(get_obs(particules, (i, j))).reshape(1, -1)
# print(get_obs(particules, (i, j)), 'before')
# print(particules[i][j], 'my predator before')
# print(neighboor, 'neighboor before')
# print(obs)
action = model.policy.predict(obs.reshape(1, -1))[0]
# print(action, 'action')
# print(action, 'action')
particules = apply_action(particules, (i, j), action, model=model)
# print(particules[i][j], 'my predator after')
# right, left, up, down = particules[adress[0][0]][adress[0][1]], particules[adress[1][0]][adress[1][1]], particules[adress[2][0]][adress[2][1]], particules[adress[3][0]][adress[3][1]]
# neighboor = [right, left, up, down]
# print(neighboor, 'neighboor after')
# print(particules[i][j], )
# print(get_obs(particules, (i, j)), 'after')
# Probability of move (in canonical basis is null)
# else:
# z = random.random()
# if z < float(p):
# '''
# 0 - direita
# 1 - esquerda
# 2 - cima
# 3 - baixo
# '''
# # obs = np.array(get_obs(particules, (i, j))).reshape(1, -1)
# # action = random.randint(0, 3)
# # action = model.policy.predict(obs.reshape(1, -1))[0]
# # apply_action(particules, (i, j), action, model=model)
# Probability of predator born
# If is prey, try to insert predator
elif particule == 2:
vpd = 0
for k in range(4):
ng_index = adress[k]
ng = particules[ng_index[0]][ng_index[1]]
if ng == 1:
vpd += 1
z = random.random()
if z < (float(b) / 4) * vpd:
particules[i][j] = 1
# Probability of prey born
# If is empty, try to insert prey
elif particule == 0:
vp = 0
for k in range(4):
ng_index = adress[k]
ng = particules[ng_index[0]][ng_index[1]]
if ng == 2:
vp += 1
z = random.random()
if z < (float(a) / 4) * vp:
particules[i][j] = 2
if number_of_predators(particules) == 0:
# introduce a new predator
particules[(L // 2) + 1][(L // 2) + 1] = 1
curr_rho.append(rho(particules))
print(t, curr_rho[-1])
content = 'time,rho\n'
for i in range(min(len(times), len(curr_rho))):
content += str(times[i]) + ',' + str(curr_rho[i]) + '\n'
with open(f'data_w_policy_cc{int(cc*1000)}_{L}/results_{nb}_w_policy_c{int(cc*1000)}_{L}.csv', 'w') as f:
f.write(content)
print(f'{nb} - {rho(particules)}')
rhos.append(curr_rho)
rhos = np.array(rhos)
rhos = np.mean(rhos, axis=0)
# save in csv file the results
content = ''
for i in range(min(len(times), len(rhos))):
content += str(times[i]) + ',' + str(rhos[i]) + '\n'
with open(f'data_w_policy_cc{int(cc*1000)}_{L}/results_w_policy_{int(cc*1000)}_{L}_total_nb{nb_copies}.csv', 'w') as f:
f.write(content)