|
@@ -1,5 +1,7 @@
|
1
|
1
|
#!/usr/bin/env python3
|
2
|
2
|
import copy
|
|
3
|
+import random
|
|
4
|
+import multiprocessing
|
3
|
5
|
|
4
|
6
|
try:
|
5
|
7
|
import pyrpn
|
|
@@ -19,16 +21,24 @@ from .ifs3 import IFS3
|
19
|
21
|
from .ifs4 import IFS4
|
20
|
22
|
from .ifs5 import IFS5
|
21
|
23
|
|
|
24
|
+from .shape_score import ShapeScore
|
|
25
|
+
|
22
|
26
|
|
23
|
27
|
#steps = 1024**2
|
24
|
|
-#steps=512**2
|
25
|
|
-steps=1024**2
|
|
28
|
+steps=512**2
|
|
29
|
+#steps=1024**2
|
26
|
30
|
#steps*=4
|
27
|
31
|
#steps*=10
|
28
|
|
-pool_sz = 30
|
29
|
|
-best_sz = 6
|
|
32
|
+
|
|
33
|
+#pool_sz = 30
|
|
34
|
+#best_sz = 6
|
|
35
|
+
|
|
36
|
+pool_sz = 32
|
|
37
|
+best_sz = 8
|
|
38
|
+
|
30
|
39
|
n_mutation = 10
|
31
|
40
|
outfile='/tmp/rpnifs2_%02d.png'
|
|
41
|
+outfile_grp='/tmp/rpnifs2_%02d_grp.png'
|
32
|
42
|
#world = IFS1.get_world()
|
33
|
43
|
#pool = [IFS1(init_sz=3, world=world) for _ in range(pool_sz)]
|
34
|
44
|
#world = IFS2.get_world()
|
|
@@ -42,7 +52,18 @@ world = IFS5.get_world()
|
42
|
52
|
pool = [IFS5(init_sz=6, world=world) for _ in range(pool_sz)]
|
43
|
53
|
print('POOL ready')
|
44
|
54
|
|
|
55
|
+def stepper(ifs):
|
|
56
|
+ for _ in range(steps):
|
|
57
|
+ ifs.step()
|
|
58
|
+ return ifs
|
|
59
|
+
|
|
60
|
+def get_score(world):
|
|
61
|
+ shapescore = ShapeScore(world, jobs=1)
|
|
62
|
+ score = shapescore.score()
|
|
63
|
+ return score, shapescore
|
|
64
|
+
|
45
|
65
|
generation=0
|
|
66
|
+mp_pool = multiprocessing.Pool(2)
|
46
|
67
|
while True:
|
47
|
68
|
generation+=1
|
48
|
69
|
print('='*78)
|
|
@@ -52,18 +73,27 @@ while True:
|
52
|
73
|
scores = []
|
53
|
74
|
i=0
|
54
|
75
|
#for ifs in pool:
|
55
|
|
- for ifs in tqdm(pool, unit='ifs'):
|
56
|
|
- i+=1
|
57
|
|
- ifs.raz_world()
|
58
|
|
- #for _ in tqdm(range(steps), total=steps, unit_scale=True):
|
59
|
|
- for _ in range(steps):
|
60
|
|
- ifs.step()
|
61
|
|
-
|
62
|
|
- score = ifs.score()
|
|
76
|
+ #for ifs in tqdm(pool, unit='ifs'):
|
|
77
|
+ # ifs.raz_world()
|
|
78
|
+ # #for _ in tqdm(range(steps), total=steps, unit_scale=True):
|
|
79
|
+ # for _ in range(steps):
|
|
80
|
+ # ifs.step()
|
|
81
|
+ pool = [ifs for ifs in tqdm(mp_pool.imap(stepper, pool),
|
|
82
|
+ total=len(pool),
|
|
83
|
+ unit='ifs')]
|
|
84
|
+
|
|
85
|
+ for i, (score,shapescore) in tqdm(enumerate(mp_pool.imap(get_score,
|
|
86
|
+ [ifs._world for ifs in pool])),
|
|
87
|
+ unit='ifs', total=len(pool)):
|
|
88
|
+ ifs = pool[i]
|
|
89
|
+
|
|
90
|
+ #score = ifs.score()
|
63
|
91
|
scores.append((ifs, score))
|
64
|
|
- print('%02d) %5.3f %s' % (i, score, ifs))
|
|
92
|
+ #print('%02d) %5.3f %s' % (i, score, ifs))
|
|
93
|
+ print('%02d) %5.3f' % (i, score))
|
65
|
94
|
im = Image.fromarray(ifs.get_image())
|
66
|
95
|
im.save(outfile % i)
|
|
96
|
+ shapescore.result_image(outfile_grp % i)
|
67
|
97
|
|
68
|
98
|
im_png = PngImageFile(outfile % i)
|
69
|
99
|
metadata = PngInfo()
|
|
@@ -81,20 +111,42 @@ while True:
|
81
|
111
|
for i, (ifs, score) in enumerate(best):
|
82
|
112
|
print('%02d) %5.3f %s' % (i, score, ifs))
|
83
|
113
|
|
|
114
|
+ # Saving best
|
|
115
|
+ im = Image.fromarray(best[0][0].get_image())
|
|
116
|
+ outfile_gen = '/tmp/gen_%03d.png'
|
|
117
|
+ im.save(outfile_gen % generation)
|
|
118
|
+
|
|
119
|
+ im_png = PngImageFile(outfile_gen % generation)
|
|
120
|
+ metadata = PngInfo()
|
|
121
|
+ metadata.add_text('Score', '%5.3f' % score)
|
|
122
|
+ metadata.add_text('Generation', '%04d' % generation)
|
|
123
|
+ metadata.add_text('Pool_position', '%02d' % 1)
|
|
124
|
+ for key, text in ifs.expr_dict().items():
|
|
125
|
+ metadata.add_text(key, text)
|
|
126
|
+ im_png.save(outfile_gen % generation, pnginfo=metadata)
|
|
127
|
+
|
84
|
128
|
# Mutating best
|
85
|
129
|
pool = [b for b, _ in best]
|
86
|
130
|
for ifs, _ in best:
|
87
|
|
- for _ in range((pool_sz//best_sz)-1):
|
|
131
|
+ nmut_max = (pool_sz//best_sz)-1
|
|
132
|
+ for nmut in range(nmut_max):
|
88
|
133
|
# needs >= IFS5
|
|
134
|
+ again = 0
|
89
|
135
|
while True:
|
90
|
|
- new = copy.copy(ifs)
|
91
|
|
- new.mutation(n_mutation)
|
|
136
|
+ # half random mutation and half mutation by adding two IFS
|
|
137
|
+ if again > 5 or nmut < nmut_max / 2:
|
|
138
|
+ new = copy.copy(ifs)
|
|
139
|
+ new.mutation(n_mutation)
|
|
140
|
+ else:
|
|
141
|
+ new = ifs + random.choice(best)[0]
|
|
142
|
+
|
92
|
143
|
for cur in pool:
|
93
|
144
|
if cur - new == 0:
|
94
|
145
|
break
|
95
|
146
|
else:
|
96
|
147
|
pool.append(new)
|
97
|
148
|
break
|
|
149
|
+ again += 1
|
98
|
150
|
###
|
99
|
151
|
#new = copy.copy(ifs)
|
100
|
152
|
#new.mutation(n_mutation)
|