-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclass.py
285 lines (219 loc) · 8.69 KB
/
class.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
# -*- coding: utf-8 -*-
"""
Created on Sat Nov 28 21:47:22 2020
@author: boucher
"""
import numpy as np
import random
import matplotlib.pyplot as plt
class agent:
def __init__(self, x,y,angle):
self.x = x
self.y = y
self.angle = angle
def get_pos(self):
return([self.x,self.y])
def try_go_forward(self,SS): # get is forward position
x1 = self.x + SS*np.cos(self.angle)
y1 = self.y + SS*np.sin(self.angle)
return(x1,y1)
def go_forward(self,x,y):
self.x = x
self.y = y
class data_map:
def __init__(self, liste_agent,trail_map,SA,RA,SO,SW,SS,depT,decayT):
self.list_agent = []
self.trail_map = trail_map
self.free_place = np.array([])
self.depT= depT
self.SA = SA
self.RA = RA
self.SO = SO
self.SW = SW
self.SS = SS
self.depT = depT
self.decayT = decayT
def add_agent(self,agent):
self.list_agent.append(agent)
def initialise(self,pop,L,l):
n_agent = int(pop * L**2)
self.free_place = np.ones([L+2*l,L+2*l])
if self.trail_map == 0:
self.trail_map = np.ones([L+2*l,L+2*l])
for i in range(n_agent):
x,y,angle = np.array([l,l,0]) + np.array([L*random.random() , L*random.random(), 2*np.pi*random.random()])
if self.free_place[int(x),int(y)]:
self.add_agent(agent(x,y,angle))
self.free_place[int(x),int(y)]=0
def plot_free_place(self):
plt.figure()
plt.matshow(self.free_place,cmap=plt.cm.inferno)
plt.show()
def plot_trail_map(self):
plt.figure()
plt.matshow(self.trail_map,cmap=plt.cm.gray)
plt.show()
def motor_step(self):
#random.shuffle(self.list_agent)
for ag in self.list_agent :
x_0,y_0 = ag.get_pos()
#print('step : ' , x_0,y_0)
x,y = ag.try_go_forward(self.SS)
x,y = np.array([x,y]) % len(self.free_place) # circle board !
if self.free_place[int(x),int(y)]==1:
self.free_place[int(x_0),int(y_0)] = 1
self.free_place[int(x),int(y)] = 0
#print('step2 : ' , [int(x_0),int(y_0)])
self.trail_map[int(x_0),int(y_0)] = self.trail_map[int(x_0),int(y_0)] + self.depT
ag.go_forward(x,y)
else:
ag.angle = 2*np.pi*random.random()
def get_FL(self,trail_map,SW,SA,SO,angle,x_0,y_0):
L = len(trail_map)
x_FL_1 = (x_0 + SO*np.cos(angle+SA) ) % L
x_FL_2 = ( x_FL_1 + SW) % L
y_FL_1 = (y_0 + SO*np.sin(angle+SA) ) % L
y_FL_2 = (y_FL_1 + SW ) % L
x_lim = np.sort([x_FL_1, x_FL_2]).astype(int)
y_lim = np.sort([y_FL_1, y_FL_2]).astype(int)
FL = np.mean(trail_map[x_lim[0]:x_lim[1] , y_lim[0]:y_lim[1] ])
return(FL)
def get_FR(self,trail_map,SW,SA,SO,angle,x_0,y_0):
x_FR_1 = (x_0 + SO*np.cos(angle-SA) ) % L
x_FR_2 = ( x_FR_1 + SW) % L
y_FR_1 = (y_0 + SO*np.sin(angle-SA) ) % L
y_FR_2 = (y_FR_1 + SW ) % L
x_lim = np.sort([x_FR_1, x_FR_2]).astype(int)
y_lim = np.sort([y_FR_1, y_FR_2]).astype(int)
FR = np.mean(trail_map[x_lim[0]:x_lim[1] , y_lim[0]:y_lim[1] ])
return(FR)
def get_F(self,trail_map,SW,SA,SO,angle,x_0,y_0):
L = len(trail_map)
x_F_1 = (x_0 + SO*np.cos(angle) ) % L
x_F_2 = ( x_F_1 + SW) % L
y_F_1 = (y_0 + SO*np.sin(angle) ) % L
y_F_2 = (y_F_1 + SW ) % L
x_lim = np.sort([x_F_1, x_F_2]).astype(int)
y_lim = np.sort([y_F_1, y_F_2]) .astype(int)
F = np.mean(trail_map[x_lim[0]:x_lim[1] , y_lim[0]:y_lim[1] ])
return(F)
def sensory_step(self):
#random.shuffle(self.list_agent)
for ag in self.list_agent :
x_0,y_0= ag.get_pos()
FL = self.get_FL(self.trail_map,self.SW,self.SA,self.SO,ag.angle,x_0,y_0)
FR = self.get_FR(self.trail_map,self.SW,self.SA,self.SO,ag.angle,x_0,y_0)
F = self.get_F(self.trail_map,self.SW,self.SA,self.SO,ag.angle,x_0,y_0)
if F>FL and F > FR:
pass
elif F<FL and F < FR:
ag.angle = random.choice([ag.angle+self.RA, ag.angle-self.RA])
elif FL < FR :
#ag.angle = ag.angle-self.RA*(random.random()+1)
ag.angle = ag.angle-self.RA
elif FL > FR :
#ag.angle = ag.angle + self.RA*(random.random()+1)
ag.angle = ag.angle-self.RA
else:
pass
def diffusion_eq(self,trail_map,Dx,Dy):
L = len(trail_map)
new_trail_map = np.zeros([L,L])
for i in range(L):
for j in range(L):
new_trail_map[i,j]=Dx*(trail_map[(i+1)%L,j]+trail_map[(i-1)%L,j]) + Dy*(trail_map[i,(j+1)%L]+trail_map[i,(j-1)%L]) + (1-2*Dx-2*Dy)*trail_map[i,j]
return(np.array(new_trail_map))
def diffusion_mean(self,K,t):
L = len(self.trail_map)
new_trail_map = np.zeros([L,L])
for i in range(L):
for j in range(L):
new_trail_map[i,j]=(1-t)*new_trail_map[i,j] + t*np.mean(self.trail_map[(i-K)%L:(i+K)%L,(j-K)%L:(j+K)%L])
return(np.array(new_trail_map))
def decay(self,trail_map,decayT):
new_trail_map = np.array([ [(1-decayT)*j for j in i] for i in trail_map])
return(new_trail_map)
def border_test(self,x,y,x_0,y_0): # stay where yo uare if go over border
L = len(self.free_place)
if x >= L or y>= L:
r=t
return()
# (self, liste_agent,trail_map,SA,RA,SO,SW,depT,decayT)
L = 100
l = 50
# (self, liste_agent,trail_map,SA,RA,SO,SW,SS,depT,decayT):
board = data_map([],0,22.5*np.pi/180,np.pi/8,9,1,1,5,0.1)
board.initialise(0.3,L,l)
import time
a = time.time()
n = 100
data = np.empty(n, dtype=object)
for i in range(n):
#board.plot_free_place()
# print(i)
#board.plot_free_place()
#board.plot_trail_map()
board.motor_step()
board.sensory_step()
board.trail_map = board.diffusion_mean(1,0.2)
#board.trail_map = board.diffusion_eq(board.trail_map,0.2,0.2)
board.trail_map = board.decay(board.trail_map,0.1)
data[i] = board.trail_map
# m = n//10
# if float(i//m)==i/m :
# print(i)
# board.plot_trail_map()
# board.plot_free_place()
######################################### ANIMATION #########################################
import matplotlib.animation as animation
fig = plt.figure()
plot = plt.imshow(data[0])
def init():
plot.set_data(data[0])
return plot
def update(j):
plot.set_data(data[j])
return [plot]
anim = animation.FuncAnimation(fig, update, init_func = init, frames=n, interval = 30)
plt.show()
#Writer = animation.writers['ffmpeg']
#writer = Writer(fps=15, metadata=dict(artist='Me'), bitrate=1800)
#anim.save('lines.mp4', writer=writer)
b = time.time()
print(b-a,' sec')
"""
class Student:
def __init__(self, name, student_number):
self.name = name
self.student_number = student_number
self.classes = []
def enrol(self, course_running):
self.classes.append(course_running)
course_running.add_student(self)
class Department:
def __init__(self, name, department_code):
self.name = name
self.department_code = department_code
self.courses = {}
def add_course(self, description, course_code, credits):
self.courses[course_code] = Course(description, course_code, credits, self)
return self.courses[course_code]
class Course:
def __init__(self, description, course_code, credits, department):
self.description = description
self.course_code = course_code
self.credits = credits
self.department = department
self.department.add_course(self)
self.runnings = []
def add_running(self, year):
self.runnings.append(CourseRunning(self, year))
return self.runnings[-1]
class CourseRunning:
def __init__(self, course, year):
self.course = course
self.year = year
self.students = []
def add_student(self, student):
self.students.append(student)
"""