|
@@ -16,7 +16,7 @@ def eval_prog(args):
|
16
|
16
|
@param args : dirty argparser returned arguments
|
17
|
17
|
'''
|
18
|
18
|
generation, progid, prog, trynum, args = args
|
19
|
|
- w = World(args.world_height, args.world_width)
|
|
19
|
+ w = World(args.world_height, args.world_width, gray=True)
|
20
|
20
|
logger.debug('Running P%d run#%d %s' % (progid, trynum, prog))
|
21
|
21
|
w.raz()
|
22
|
22
|
turmits = [LivingTurmit(world=w, prog=prog)
|
|
@@ -44,7 +44,7 @@ def eval_prog(args):
|
44
|
44
|
class World(object):
|
45
|
45
|
''' @brief A Turmit world. An array of dim == 2 with tuple(R,G,B) '''
|
46
|
46
|
|
47
|
|
- def __init__(self, x, y):
|
|
47
|
+ def __init__(self, x, y, gray=False):
|
48
|
48
|
if x < 1:
|
49
|
49
|
raise ValueError('Invalid value %d for x size')
|
50
|
50
|
if y < 1:
|
|
@@ -52,6 +52,7 @@ class World(object):
|
52
|
52
|
self._x = x
|
53
|
53
|
self._y = y
|
54
|
54
|
self._val = None
|
|
55
|
+ self._gray = gray
|
55
|
56
|
self.raz()
|
56
|
57
|
|
57
|
58
|
@property
|
|
@@ -64,10 +65,13 @@ class World(object):
|
64
|
65
|
|
65
|
66
|
def raz(self, color=None):
|
66
|
67
|
''' @brief Set all value to color
|
67
|
|
- @param color tuple : tuple(R,G,B)
|
|
68
|
+ @param color tuple | float : tuple(R,G,B) or greylevel
|
68
|
69
|
'''
|
69
|
70
|
if color is None:
|
70
|
|
- self._val = np.zeros([self._y, self._x, 3])
|
|
71
|
+ if self._gray:
|
|
72
|
+ self._val = np.zeros([self._y, self._x])
|
|
73
|
+ else:
|
|
74
|
+ self._val = np.zeros([self._y, self._x, 3])
|
71
|
75
|
else:
|
72
|
76
|
self._val = [[color for _ in range(self._x)] for _ in range(self._y)]
|
73
|
77
|
self._val = np.array(self._val)
|
|
@@ -78,7 +82,10 @@ class World(object):
|
78
|
82
|
|
79
|
83
|
def fractdim(self, threshold=None):
|
80
|
84
|
''' @return Minkowski–Bouligand dimension (computed) '''
|
81
|
|
- Z = self.rgb2gray(self._val)
|
|
85
|
+ if not self._gray:
|
|
86
|
+ Z = self.rgb2gray(self._val)
|
|
87
|
+ else:
|
|
88
|
+ Z = self._val
|
82
|
89
|
|
83
|
90
|
# Only for 2d image
|
84
|
91
|
assert(len(Z.shape) == 2)
|
|
@@ -127,6 +134,12 @@ class World(object):
|
127
|
134
|
gray = 0.2989 * r + 0.5870 * g + 0.1140 * b
|
128
|
135
|
return gray
|
129
|
136
|
|
|
137
|
+ @staticmethod
|
|
138
|
+ def single_rgb2gray(rgb):
|
|
139
|
+ r,g,b = rgb
|
|
140
|
+ gray = 0.2989 * r + 0.5870 * g + 0.1140 * b
|
|
141
|
+ return gray
|
|
142
|
+
|
130
|
143
|
def __getitem__(self, a):
|
131
|
144
|
return self._val[a]
|
132
|
145
|
|
|
@@ -148,8 +161,17 @@ class LivingTurmit(Turmit):
|
148
|
161
|
raise TypeError(msg % color)
|
149
|
162
|
## @brief Stores Turmit color
|
150
|
163
|
rand_hue = randint(0,255) / 255
|
|
164
|
+ rand_lvl = randint(127,255) / 255
|
|
165
|
+ rand_val = randint(127,255) / 255
|
151
|
166
|
self._col = tuple(round(i * 255)
|
152
|
|
- for i in colorsys.hsv_to_rgb(rand_hue,1,1))
|
|
167
|
+ for i in colorsys.hsv_to_rgb(rand_hue,
|
|
168
|
+ rand_lvl,
|
|
169
|
+ rand_val))
|
|
170
|
+ if world._gray:
|
|
171
|
+ #self._col = 255.0
|
|
172
|
+ self._col = tuple(round(i * 255)
|
|
173
|
+ for i in colorsys.hsv_to_rgb(rand_hue,1,1))
|
|
174
|
+ self._col = World.single_rgb2gray(self._col)
|
153
|
175
|
#self._col = (randint(0,255), randint(0, 255), randint(0,255))
|
154
|
176
|
if color is not None:
|
155
|
177
|
self._col = color
|
|
@@ -170,7 +192,10 @@ class LivingTurmit(Turmit):
|
170
|
192
|
|
171
|
193
|
def __call__(self):
|
172
|
194
|
''' @brief Exec a turmit and move it '''
|
173
|
|
- r,g,b = self._world[self._y][self._x]
|
|
195
|
+ if self._world._gray:
|
|
196
|
+ r = g = b = self._world[self._y][self._x]
|
|
197
|
+ else:
|
|
198
|
+ r,g,b = self._world[self._y][self._x]
|
174
|
199
|
tdir = super().__call__(x=self._x, y=self._y, r=r, g=g, b=b)
|
175
|
200
|
self.move(tdir)
|
176
|
201
|
|