Repository navigation
Expand file tree
/
Copy pathSlime12.py
More file actions
576 lines (521 loc) · 19.3 KB
/
Copy pathSlime12.py
File metadata and controls
576 lines (521 loc) · 19.3 KB
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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
# -*- coding: utf-8 -*-
"""
Created on Wed May 03 14:03:42 2017
@author: Fantasia
"""
import random
import pygame
import math
class Unit:
def __init__(self, c, state, c2, c3, c4, life):
self.c = c # 信息素浓度
self.c2 = c2
self.c3 = c3 # b反捕食者信息素
self.c4 = c4 # 尸体腐烂的味道
self.s = state # 状态:0-空位 1-a生长细胞 2-物体 3-捕食细胞 4-反捕食者 5-尸体 6-分解者
self.l = life # 生命值
class Ground:
def __init__(self, n):
self.ground = []
self.n = n
self.l = []
for i in range(n):
self.ground.append([])
for l in self.ground:
for i in range(n):
l.append(Unit(0, 0, 0, 0, 0, 0))
def show(self):
for i in range(self.n):
print([u.s for u in self.ground[i]])
def randinit(self):
for i in range(self.n):
for j in range(self.n):
self.ground[i][j] = Unit(random.random(), 0, 0, 0, 0, 0)
def addslime(self, x, y):
self.ground[x][y].s = 1
self.ground[x][y].l = 100 # 满100
def addslime3(self, x, y):
self.ground[x][y].s = 4
self.ground[x][y].l = 50
def addglass(self, x, y):
self.ground[x][y].s = 2
self.ground[x][y].l = 15 # 护甲会降解
def addshiti(self, x, y):
self.ground[x][y].s = 5
self.ground[x][y].l = 10
def addpredator(self, x, y):
self.ground[x][y].s = 3
self.ground[x][y].l = 100
def adddecomposer(self, x, y):
self.ground[x][y].s = 6
self.ground[x][y].l = 50
def inrange(self, x, y):
return (x in range(n)) and (y in range(n))
def addc(self, x, y, n):
sc = self.ground[x][y].c
# if sc<10000:
self.ground[x][y].c = sc+n
def addc2(self, x, y, n):
sc2 = self.ground[x][y].c2
self.ground[x][y].c2 = sc2+n
def addc3(self, x, y, n):
sc3 = self.ground[x][y].c3
self.ground[x][y].c3 = sc3+n
def addc4(self, x, y, n):
sc4 = self.ground[x][y].c4
self.ground[x][y].c4 = sc4+n
def release(self, x, y):
sc = self.ground[x][y].c
k = 0.1
try:
dc = k*sc/self.abalcount(x, y)
except:
dc = 0.0
for f in self.fl:
i = f(self, x, y)[0]-x
j = f(self, x, y)[1]-y
self.addc(x+i, y+j, dc)
def release3(self, x, y):
sc = self.ground[x][y].c2
k = 0.1
try:
dc = k*sc/self.abalcount(x, y)
except:
dc = 0.0
for f in self.fl:
i = f(self, x, y)[0]-x
j = f(self, x, y)[1]-y
self.addc3(x+i, y+j, dc)
def release4(self, x, y):
k = 10
dc = self.ground[x][y].l*k
for f in self.fl:
i = f(self, x, y)[0]-x
j = f(self, x, y)[1]-y
self.addc4(x+i, y+j, dc)
def nrelease(self, x, y):
sc = self.ground[x][y].c # 根据要捕食的细胞/自己的信息素浓度施放自己的信息素
k = 0.9
try:
dc = k*sc/self.abalcount(x, y)
except:
dc = 0.0
for f in self.fl:
i = f(self, x, y)[0]-x
j = f(self, x, y)[1]-y
if self.inrange(x+i, y+j):
self.addc2((x+i), (y+j), dc)
def move(self, x, y, nx, ny):
thes = self.ground[x][y].s
thel = self.ground[x][y].l
self.ground[x][y].s = 0
self.ground[x][y].l = 0
x = nx
y = ny
self.ground[x][y].s = thes
self.ground[x][y].l = thel
def die(self, x, y):
self.ground[x][y].s = 5
self.ground[x][y].l = 50
def upperslime(self, x, y):
nx = x
ny = y
myc = self.ground[x][y].c
myc2 = self.ground[x][y].c2
myc3 = self.ground[x][y].c3
k1 = 1.0
k2 = 7.0
k3 = 3.0
totalmyc = k1*myc+k3*myc3-k2*myc2
count = 0 # 对周围的非空位计数
for f in self.fl:
i = f(self, x, y)[0]-x
j = f(self, x, y)[1]-y
nc = self.ground[x+i][y+j].c
nc2 = self.ground[x+i][y+j].c2
nc3 = self.ground[x+i][y+j].c3
if self.ground[x+i][y+j].s != 0: # 如果不是空位且为1就计数
if self.ground[x+i][y+j].s == 1 or self.ground[x+i][y+j].s == 4:
count += 1
else:
pass
elif self.ground[x+i][y+j].s == 0: # 如果周围某一个是空位:
if self.ground[x][y].l >= 100: # 如果生命值满了有能力:1、分裂 2、产生护甲
if self.ground[x+i][y+j].c2 > 0.00001: # 如果信息素c2浓度超过就产生护甲
self.ground[x+i][y+j].s = 4
self.ground[x+i][y+j].l = 10
self.ground[x][y].l -= 10
elif True: # self.ground[x+i][y+j].c3>0.0001:
self.addslime(x+i, y+j)
self.ground[x][y].l = 50
elif k1*nc+k3*nc3-k2*nc2 <= totalmyc:
pass
elif k1*nc+k3*nc3-k2*nc2 > totalmyc:
myc = self.ground[x+i][y+j].c
myc2 = self.ground[x+i][y+j].c2
myc3 = self.ground[x+i][y+j].c3
totalmyc = k1*myc+k3*myc3-k2*myc2
nx = x+i
ny = y+j
else:
if k1*nc+k3*nc3-k2*nc2 <= totalmyc:
pass
elif k1*nc+k3*nc3-k2*nc2 > totalmyc:
myc = self.ground[x+i][y+j].c
myc2 = self.ground[x+i][y+j].c2
myc3 = self.ground[x+i][y+j].c3
totalmyc = k1*myc+k3*myc3-k2*myc2
nx = x+i
ny = y+j
# if count==8:
# if self.ground[x][y].l>=100:
# for f in self.fl:
# i=f(self,x,y)[0]-x
# j=f(self,x,y)[1]-y
# if not (i==j and i==0):#如果生命值满且于内部就给周围加资源
# if self.ground[x+i][y+j].s==1:
# self.ground[x+i][y+j].l+=5#通过控制增加的资源数可以调控聚合体稳定后的大小
# self.die(x,y)#然后死亡
self.move(x, y, nx, ny)
self.ground[nx][ny].l += 5
if self.ground[nx][ny].l >= 100:
self.ground[nx][ny].l = 100
self.release(nx, ny)
def upperslime3(self, x, y):
nx = x
ny = y
myc = self.ground[x][y].c
myc2 = self.ground[x][y].c2
myc3 = self.ground[x][y].c3
k2 = 7.0
k1 = 1.0
k3 = 1.0
totalmyc = k1*myc+k3*myc3+k2*myc2
count = 0 # 对周围的非空位计数
for f in self.fl:
i = f(self, x, y)[0]-x
j = f(self, x, y)[1]-y
nc = self.ground[x+i][y+j].c
nc2 = self.ground[x+i][y+j].c2
nc3 = self.ground[x+i][y+j].c3
# if self.ground[x+i][y+j].s!=0:#如果不是空位且为1就计数
# if self.ground[x+i][y+j].s==4 or self.ground[x+i][y+j].s==1:
# count+=1
if self.ground[x+i][y+j].s == 3:
self.ground[x+i][y+j].s = 4
self.ground[x+i][y+j].l == 50
elif self.ground[x+i][y+j].s == 0: # 如果周围某一个是空位:
if self.ground[x][y].l >= 100: # 如果生命值满了有能力:1、分裂 2、产生护甲
# if self.ground[x+i][y+j].c2>0.01:#如果信息素c2浓度超过就产生护甲
# self.addglass(x+i,y+j)
# elif True:#self.ground[x+i][y+j].c>0.0001:#并且对方信息素足够多才能分裂
# self.addslime3(x+i,y+j)
# self.ground[x][y].l=50
if k1*nc+k3*nc3+k2*nc2 <= totalmyc:
pass
elif k1*nc+k3*nc3+k2*nc2 > totalmyc:
myc = self.ground[x+i][y+j].c
myc2 = self.ground[x+i][y+j].c2
myc3 = self.ground[x+i][y+j].c3
totalmyc = k1*myc+k3*myc3-k2*myc2
nx = x+i
ny = y+j
else: # 生命值不满只有移动的选择
if k1*nc+k3*nc3+k2*nc2 <= totalmyc:
pass
elif k1*nc+k3*nc3+k2*nc2 > totalmyc:
myc = self.ground[x+i][y+j].c
myc2 = self.ground[x+i][y+j].c2
myc3 = self.ground[x+i][y+j].c3
totalmyc = k1*myc+k3*myc3-k2*myc2
nx = x+i
ny = y+j
if count == 8:
if self.ground[x][y].l >= 100:
for i in [-1, 0, 1]:
for j in [-1, 0, 1]:
if not (i == j and i == 0): # 如果生命值满且于内部就给周围加资源
if self.ground[x+i][y+j].s == 1:
# 通过控制增加的资源数可以调控聚合体稳定后的大小
self.ground[x+i][y+j].l += 5
# self.die(x,y)#然后死亡
self.move(x, y, nx, ny)
self.ground[nx][ny].l -= 15
# if self.ground[nx][ny].l>=100:
# self.ground[nx][ny].l=100
self.release3(nx, ny)
if self.ground[nx][ny].l <= 0:
self.die(nx, ny)
def upperpredator(self, x, y):
nx = x
ny = y
mc = self.ground[x][y].c # 按照自己/对方的信息素浓度梯度移动
mc3 = self.ground[x][y].c3
k1 = 1.0
k3 = -7.0
mytc = k1*mc+k3*mc3
count = 0
for f in self.fl:
i = f(self, x, y)[0]-x
j = f(self, x, y)[1]-y
if self.ground[x+i][y+j].s == 1: # or self.ground[x+i][y+j].s==6:
self.ground[x+i][y+j].s = 3
self.ground[x+i][y+j].l = 100
elif self.ground[x+i][y+j].s == 0:
if mc3 >= 0.000001:
self.ground[x+i][y+j].s = 2
self.ground[x+i][y+j].l = 10
# else:
nc = self.ground[x+i][y+j].c
nc3 = self.ground[x+i][y+j].c3
netc = k1*nc+k3*nc3
if mytc >= netc:
pass
else:
mc = nc
mc3 = nc3
mytc = netc
nx = x+i
ny = y+j
count += 1
# if mc3>0.000001 and count!=0:
# self.ground[x][y].s=2
# self.ground[x][y].l=10
self.move(x, y, nx, ny)
self.nrelease(nx, ny)
self.ground[nx][ny].l -= 0.5
if self.ground[nx][ny].l <= 0:
self.die(nx, ny)
def upperglass(self, x, y):
if self.ground[x][y].l <= 0:
self.ground[x][y].l = 0
self.ground[x][y].s = 0
else: # 护甲生命值会减少 到零死
self.ground[x][y].l -= 1.5
def uppershiti(self, x, y):
self.release4(x, y)
if self.ground[x][y].l <= 0:
self.ground[x][y].l = 0
self.ground[x][y].s = 0
else: # 护甲生命值会减少 到零死
self.ground[x][y].l -= 0.1
def upperdecomposer(self, x, y):
nx = x
ny = y
mc = self.ground[x][y].c
mc3 = self.ground[x][y].c3
mc4 = self.ground[x][y].c4
k = 0.0
k3 = 0.0
k4 = 100
mytc = k*mc+k3*mc3+k4*mc4
count = 0
for f in self.fl:
i = f(self, x, y)[0]-x
j = f(self, x, y)[1]-y
if self.ground[x+i][y+j].s == 5: # or self.ground[x+i][y+j].s==4:
self.ground[x+i][y+j].l -= 50
self.ground[x][y].l += 6
elif self.ground[x+i][y+j].s == 0:
if self.ground[x][y].l >= 100:
self.ground[x+i][y+j].s = 6
self.ground[x+i][y+j].l = 30
else:
nc = self.ground[x+i][y+j].c
nc3 = self.ground[x+i][y+j].c3
nc4 = self.ground[x+i][y+j].c4
netc = k*nc+k3*nc3+k4*nc4
if mytc >= netc:
pass
else:
mc = nc
mc3 = nc3
mc4 = nc4
mytc = netc
nx = x+i
ny = y+j
count += 1
# if mc3>0.000001 and count!=0:
# self.ground[x][y].s=2
# self.ground[x][y].l=10
self.move(x, y, nx, ny)
self.ground[nx][ny].l -= 2
if self.ground[nx][ny].l <= 0:
self.ground[nx][ny].s = 0
def z(self, x, y):
return x, y-1
def y(self, x, y):
if y != self.n-1:
return x, y+1
else:
return x, 0
def s(self, x, y):
return x-1, y
def x(self, x, y):
if x != self.n-1:
return x+1, y
else:
return 0, y
def zs(self, x, y):
return self.s(*self.z(x, y))
def zx(self, x, y):
return self.x(*self.z(x, y))
def ys(self, x, y):
return self.s(*self.y(x, y))
def yx(self, x, y):
return self.x(*self.y(x, y))
fl = [z, y, s, x, zs, zx, ys, yx]
def abalcount(self, x, y):
# count=0
# for i in [-1,0,1]:
# for j in [-1,0,1]:
# if self.inrange(x+i,y+j):
# if self.ground[x+i][y+j].s!=2:
# count+=1
# return count
return 8
def kuosan1(self, x, y):
rate = 0.9
deltaout = self.ground[x][y].c*rate
deltain = 0
for f in self.fl:
tx, ty = f(self, x, y)
try:
d = self.ground[tx][ty].c*rate/self.abalcount(tx, ty)
except:
d = 0.0
deltain += d
self.ground[x][y].c += deltain
self.ground[x][y].c -= deltaout
def kuosan2(self, x, y): # 扩散到边界会到相反的另一边
rate = 0.9
deltaout = self.ground[x][y].c2*rate
deltain = 0
for f in self.fl:
tx, ty = f(self, x, y)
try:
d = self.ground[tx][ty].c2*rate/self.abalcount(tx, ty)
except:
d = 0.0
deltain += d
self.ground[x][y].c2 += deltain
self.ground[x][y].c2 -= deltaout
def kuosan3(self, x, y):
rate = 0.9
deltaout = self.ground[x][y].c3*rate
deltain = 0
for f in self.fl:
tx, ty = f(self, x, y)
try:
d = self.ground[tx][ty].c3*rate/self.abalcount(tx, ty)
except:
d = 0.0
deltain += d
self.ground[x][y].c3 += deltain
self.ground[x][y].c3 -= deltaout
def kuosan4(self, x, y):
rate = 0.9
deltaout = self.ground[x][y].c4*rate
deltain = 0
for f in self.fl:
tx, ty = f(self, x, y)
try:
d = self.ground[tx][ty].c4*rate/self.abalcount(tx, ty)
except:
d = 0.0
deltain += d
self.ground[x][y].c4 += deltain
self.ground[x][y].c4 -= deltaout
def decoposeC(self, x, y): # 信息素c的降解
c = self.ground[x][y].c
k = 0.01
self.ground[x][y].c -= k*c
def decomposeC2(self, x, y): # c2的降解 对护甲的解除非常关键
c2 = self.ground[x][y].c2
k = 0.9
self.ground[x][y].c2 -= k*c2
def decomposeC3(self, x, y):
c3 = self.ground[x][y].c3
k = 0.9
self.ground[x][y].c3 -= k*c3
def decomposeC4(self, x, y):
c4 = self.ground[x][y].c4
k = 0.1
self.ground[x][y].c4 -= k*c4
def uppergroundunit(self, x, y):
self.kuosan1(x, y)
self.kuosan2(x, y)
self.kuosan3(x, y)
self.kuosan4(x, y)
if self.ground[x][y].s == 1:
self.upperslime(x, y)
elif self.ground[x][y].s == 3:
self.upperpredator(x, y)
elif self.ground[x][y].s == 2:
self.upperglass(x, y)
elif self.ground[x][y].s == 4:
self.upperslime3(x, y)
elif self.ground[x][y].s == 5:
self.uppershiti(x, y)
elif self.ground[x][y].s == 6:
self.upperdecomposer(x, y)
self.decomposeC2(x, y)
self.decoposeC(x, y)
self.decomposeC3(x, y)
self.decomposeC4(x, y)
def update(self):
l1 = list(range(self.n))
l2 = list(range(self.n))
random.shuffle(l1)
random.shuffle(l2)
for i in l1: # 因并不是严格的并行更新 所以打乱更新顺序防止瞬移的bug
for j in l2:
self.uppergroundunit(i, j)
n = 50
g = Ground(n)
g.randinit()
for i in range(150):
g.addslime(random.randint(0, g.n-1), random.randint(0, g.n-1))
for i in range(150):
g.addpredator(random.randint(0, g.n-1), random.randint(0, g.n-1))
for i in range(30):
g.adddecomposer(random.randint(0, g.n-1), random.randint(0, g.n-1))
# g.addslime(25,25)
BLUE = (0, 0, 255)
BLACK = (0, 0, 0)
YELLOW = (0, 255, 0)
RED = (255, 0, 0)
YR = (200, 100, 200)
GREY = (100, 100, 100)
R = (255, 255, 0)
ge = 6
g.addpredator(25, 25)
g.addpredator(20, 10)
g.addpredator(30, 25)
def drawone(i, j, c):
if c.ground[i][j].s == 0:
pygame.draw.rect(dissurf, BLACK, (i*ge, j*ge, ge-2, ge-2))
elif c.ground[i][j].s == 1:
pygame.draw.rect(dissurf, YELLOW, (i*ge, j*ge, ge-2, ge-2))
elif c.ground[i][j].s == 2:
pygame.draw.rect(dissurf, BLUE, (i*ge, j*ge, ge-2, ge-2))
elif c.ground[i][j].s == 3:
pygame.draw.rect(dissurf, RED, (i*ge, j*ge, ge-2, ge-2))
elif c.ground[i][j].s == 4:
pygame.draw.rect(dissurf, YR, (i*ge, j*ge, ge-2, ge-2))
elif c.ground[i][j].s == 5:
pygame.draw.rect(dissurf, GREY, (i*ge, j*ge, ge-2, ge-2))
elif c.ground[i][j].s == 6:
pygame.draw.rect(dissurf, R, (i*ge, j*ge, ge-2, ge-2))
pygame.init()
dissurf = pygame.display.set_mode((n*ge, n*ge))
dissurf.fill(BLACK)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
g.update()
for i in range(n):
for j in range(n):
drawone(i, j, g)
pygame.display.update()