diff options
author | Thomas Jourdan <b.vehikel@googlemail.com> | 2009-09-04 16:02:30 (GMT) |
---|---|---|
committer | Thomas Jourdan <b.vehikel@googlemail.com> | 2009-09-04 16:02:30 (GMT) |
commit | 05f2a6684a42209e30fa2fcd0d7cb014c4663a39 (patch) | |
tree | 6b3cfd307e94f6b640ff985d141daa82b8fef7c1 /ep_layer_markovchain.py | |
parent | 7ee9a0b36814d90954929a4fead2bf9ff4314c19 (diff) |
Additional samplers added. Using set_sensitive to disable GUI elements.
Diffstat (limited to 'ep_layer_markovchain.py')
-rw-r--r-- | ep_layer_markovchain.py | 97 |
1 files changed, 69 insertions, 28 deletions
diff --git a/ep_layer_markovchain.py b/ep_layer_markovchain.py index b7485c8..3eabda3 100644 --- a/ep_layer_markovchain.py +++ b/ep_layer_markovchain.py @@ -22,26 +22,33 @@ import model_constraintpool import ka_random import ka_factory -NUMBER_OF_TILES_CONSTRAINT = 'number_of_tiles_constraint' +#NUMBER_OF_TILES_CONSTRAINT = 'number_of_tiles_constraint' NUMBER_OF_STATES_CONSTRAINT = 'number_of_states_constraint' +SAMPLERTYPE_CONSTRAINT = 'samplertype_constraint' STAMPTYPE_CONSTRAINT = 'stamptype_constraint' class MarkovChainLayer(model_layer.Layer): """Markov chain layer inv: self.cell_colors is not None and len(self.cell_colors) == self.states inv: self.probability is not None and len(self.probability) == self.states - inv: 1 <= self.number_of_tiles + #inv: 1 <= self.number_of_tiles inv: 1 <= self.states + inv: self.sampler is not None + inv: self.stamp is not None """ - cdef = [{'bind' : NUMBER_OF_TILES_CONSTRAINT, - 'name' : 'Number of tiles per axis', - 'domain': model_constraintpool.INT_RANGE, - 'min' : 2, 'max': 10}, + cdef = [#{'bind' : NUMBER_OF_TILES_CONSTRAINT, +# 'name' : 'Number of tiles per axis', +# 'domain': model_constraintpool.INT_RANGE, +# 'min' : 2, 'max': 10}, {'bind' : NUMBER_OF_STATES_CONSTRAINT, 'name' : 'Number of states', 'domain': model_constraintpool.INT_RANGE, 'min' : 2, 'max': 8}, + {'bind' : SAMPLERTYPE_CONSTRAINT, + 'name' : 'Permitted sampler types', + 'domain': model_constraintpool.STRING_1_OF_N, + 'enum' : ka_factory.get_factory('sampler').keys()}, {'bind' : STAMPTYPE_CONSTRAINT, 'name' : 'Permitted stamp types', 'domain': model_constraintpool.STRING_1_OF_N, @@ -56,7 +63,12 @@ class MarkovChainLayer(model_layer.Layer): self.probability = [[1.0 / self.states] * self.states for i in range(self.states)] self._init_states(2) - self.number_of_tiles = 2 + +# self.number_of_tiles = 2 + sampler_factory = ka_factory.get_factory('sampler') + sampler_key = sampler_factory.keys()[0] + self.sampler = sampler_factory.create(sampler_key, self.path) + stamp_factory = ka_factory.get_factory('stamp') stamp_key = stamp_factory.keys()[0] self.stamp = stamp_factory.create(stamp_key, self.path) @@ -126,8 +138,9 @@ class MarkovChainLayer(model_layer.Layer): equal = isinstance(other, MarkovChainLayer) \ and super(MarkovChainLayer, self).__eq__(other) \ and self.states == other.states \ - and self.number_of_tiles == other.number_of_tiles \ + and self.sampler == other.sampler \ and self.stamp == other.stamp +# and self.number_of_tiles == other.number_of_tiles \ if equal: for cix, cell_color in enumerate(self.cell_colors): equal = equal and cell_color == other.cell_colors[cix] @@ -146,9 +159,15 @@ class MarkovChainLayer(model_layer.Layer): self._init_states(ka_random.randint_constrained( number_of_states_constraint)) - number_of_tiles_constraint = cpool.get(self, NUMBER_OF_TILES_CONSTRAINT) - self.number_of_tiles = ka_random.randint_constrained( - number_of_tiles_constraint) +# number_of_tiles_constraint = cpool.get(self, NUMBER_OF_TILES_CONSTRAINT) +# self.number_of_tiles = ka_random.randint_constrained( +# number_of_tiles_constraint) + + sampler_factory = ka_factory.get_factory('sampler') + samplertype_constraint = cpool.get(self, SAMPLERTYPE_CONSTRAINT) + self.sampler = sampler_factory.create_random(samplertype_constraint, + self.path) + self.sampler.randomize() stamp_factory = ka_factory.get_factory('stamp') stamptype_constraint = cpool.get(self, STAMPTYPE_CONSTRAINT) @@ -157,7 +176,7 @@ class MarkovChainLayer(model_layer.Layer): self.stamp.randomize() def mutate(self): - """Make small random changes to the layers components.""" + """Make random changes to the layers components.""" super(MarkovChainLayer, self).mutate() cpool = model_constraintpool.ConstraintPool.get_pool() number_of_states_constraint = cpool.get(self, NUMBER_OF_STATES_CONSTRAINT) @@ -174,10 +193,11 @@ class MarkovChainLayer(model_layer.Layer): ka_random.jitter(1.0 / self.states) self._normalize_row(row) - number_of_tiles_constraint = cpool.get(self, NUMBER_OF_TILES_CONSTRAINT) - self.number_of_tiles = ka_random.jitter_discret_constrained( - self.number_of_tiles, number_of_tiles_constraint) +# number_of_tiles_constraint = cpool.get(self, NUMBER_OF_TILES_CONSTRAINT) +# self.number_of_tiles = ka_random.jitter_discret_constrained( +# self.number_of_tiles, number_of_tiles_constraint) + self.sampler.mutate() self.stamp.mutate() def shuffle(self): @@ -191,6 +211,8 @@ class MarkovChainLayer(model_layer.Layer): for row_probabilities in self.probability: if ka_random.is_shuffling(): random.shuffle(row_probabilities) + if ka_random.is_shuffling(): + self.sampler.shuffle() def crossingover(self, other): """ @@ -203,9 +225,11 @@ class MarkovChainLayer(model_layer.Layer): new_one = MarkovChainLayer(self.get_trunk()) cross_sequence = self.crossingover_base(new_one, other, \ 2 + len(self.cell_colors)) - new_one.number_of_tiles = self.number_of_tiles \ - if cross_sequence[0] else other.number_of_tiles +# new_one.number_of_tiles = self.number_of_tiles \ +# if cross_sequence[0] else other.number_of_tiles + new_one.sampler = self.sampler if cross_sequence[0] else other.sampler new_one.stamp = self.stamp if cross_sequence[1] else other.stamp + if cross_sequence[1]: probability = self.probability cell_colors = self.cell_colors @@ -224,24 +248,29 @@ class MarkovChainLayer(model_layer.Layer): new_one.cell_colors.append(cell_colors[cix].copy()) return new_one - def draw(self, ctx, width, height): + def render(self, ctx, width, height): """ pre: ctx is not None pre: width > 0 pre: height > 0 pre: width == height """ - self.begin_draw(ctx, width, height) + self.begin_render(ctx, width, height) cell_rand = random.Random(self.random_seed) cell_state = 0 - delta = 1.0 / self.number_of_tiles - for y_index in range(self.number_of_tiles): - for x_index in range(self.number_of_tiles): - rgba = self.cell_colors[cell_state].rgba - ctx.set_source_rgba(rgba[0], rgba[1], rgba[2], rgba[3]) - self.stamp.draw(ctx, (delta*(x_index+0.5)-0.5, - delta*(y_index+0.5)-0.5) ) - cell_state = self._next_state(cell_state, cell_rand) +# delta = 1.0 / self.number_of_tiles + for point in self.sampler.get_sample_points(): + rgba = self.cell_colors[cell_state].rgba + ctx.set_source_rgba(rgba[0], rgba[1], rgba[2], rgba[3]) + self.stamp.render(ctx, (point[0]-0.5, point[1]-0.5) ) + cell_state = self._next_state(cell_state, cell_rand) +# for y_index in range(self.number_of_tiles): +# for x_index in range(self.number_of_tiles): +# rgba = self.cell_colors[cell_state].rgba +# ctx.set_source_rgba(rgba[0], rgba[1], rgba[2], rgba[3]) +# self.stamp.render(ctx, (delta*(x_index+0.5)-0.5, +# delta*(y_index+0.5)-0.5) ) +# cell_state = self._next_state(cell_state, cell_rand) def _next_state(self, cell_state, cell_rand): next_cell_state = self.states-1 @@ -257,6 +286,17 @@ class MarkovChainLayer(model_layer.Layer): def explain(self, formater): # super(MarkovChainLayer, self).explain(formater) formater.color_array(self.cell_colors, 'cell colors:') + formater.text_item('number of states: ' + str(self.states)) + text, surface, descr = self.sampler.explain() + if surface is not None: + formater.surface_item(surface, 'sampling points:' + text, descr) + else: + formater.text_item(text) + text, surface, descr = self.stamp.explain() + if surface is not None: + formater.surface_item(surface, 'stamp:' + text, descr) + else: + formater.text_item(text) def copy(self): """The Markov chain layer copy constructor @@ -266,7 +306,7 @@ class MarkovChainLayer(model_layer.Layer): new_one = MarkovChainLayer(self.get_trunk()) self.copy_base(new_one) new_one.states = self.states - new_one.number_of_tiles = self.number_of_tiles +# new_one.number_of_tiles = self.number_of_tiles new_one.cell_colors = [None] * self.states for cix in range(len(self.cell_colors)): new_one.cell_colors[cix] = self.cell_colors[cix].copy() @@ -274,5 +314,6 @@ class MarkovChainLayer(model_layer.Layer): for row, row_probabilities in enumerate(self.probability): for col, cell_probability in enumerate(row_probabilities): new_one.probability[row][col] = cell_probability + new_one.sampler = self.sampler.copy() new_one.stamp = self.stamp.copy() return new_one |