diff options
-rw-r--r-- | .gitignore | 1 | ||||
-rw-r--r-- | activity/activity.info | 2 | ||||
-rw-r--r-- | ep_directionconstraint_none.py | 2 | ||||
-rw-r--r-- | ka_controller.py | 26 | ||||
-rw-r--r-- | ka_random.py | 23 | ||||
-rw-r--r-- | kandidtube.py | 6 | ||||
-rw-r--r-- | model_constraintpool.py | 8 | ||||
-rw-r--r-- | model_population.py | 2 | ||||
-rw-r--r-- | model_protozoon.py | 41 |
9 files changed, 56 insertions, 55 deletions
@@ -24,6 +24,7 @@ coverage.lst .project *.asc *.gpg +*.txt # allow logo !activity-kandid.svg diff --git a/activity/activity.info b/activity/activity.info index 47eb4fe..02adca3 100644 --- a/activity/activity.info +++ b/activity/activity.info @@ -3,5 +3,5 @@ name = Kandid service_name = net.sourceforge.kandid class = activity.KandidActivity icon = activity-kandid -activity_version = 1 +activity_version = 2 show_launcher = yes diff --git a/ep_directionconstraint_none.py b/ep_directionconstraint_none.py index 35fb6c1..2965c0b 100644 --- a/ep_directionconstraint_none.py +++ b/ep_directionconstraint_none.py @@ -30,7 +30,7 @@ class NoneDirectionConstraint(model_locus.Locus): cdef = [{'bind' : DISTANCE_CONSTRAINT, 'name' : 'Distance per hop', 'domain': model_constraintpool.FLOAT_RANGE, - 'min' : 0.0, 'max': 0.5, + 'min' : 0.2, 'max': 0.5, }, {'bind' : RADIAN_CONSTRAINT, 'name' : 'Rotation in radians', diff --git a/ka_controller.py b/ka_controller.py index 05ef161..1163e80 100644 --- a/ka_controller.py +++ b/ka_controller.py @@ -15,7 +15,6 @@ # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA -import hashlib import gtk import gtk.glade import cairo @@ -264,23 +263,20 @@ class KandidController(object): """Update population or protozoon preview when received from others.""" ka_debug.info('on_received: Received %u bytes, type: [%s] md5: [%s]' % \ (len(code_element), code_type, code_md5)) - if hashlib.md5(code_element).hexdigest() == code_md5: - inbox_widget = self._canvas.widgetTree.get_widget('incomingBox') - if code_type == kandidtube.SEND_POPULATION: - if self.is_overwrite_allowed: - self._update_model(model_population.from_buffer(code_element)) - self.model._state = model_population.STATE_EVOLVED - inbox_widget.queue_draw() - else: - ka_debug.info("I've already an evolved population, doing nothing") - elif code_type == kandidtube.SEND_PROTOZOON: - self.incoming.append_protozoon(model_population.from_buffer(code_element)) + inbox_widget = self._canvas.widgetTree.get_widget('incomingBox') + if code_type == kandidtube.SEND_POPULATION: + if self.is_overwrite_allowed: + self._update_model(model_population.from_buffer(code_element)) + self.model._state = model_population.STATE_EVOLVED inbox_widget.queue_draw() else: - ka_debug.info('Somebody called me using an illegal type [%s]' \ - % code_type) + ka_debug.info("I've already an evolved population, doing nothing") + elif code_type == kandidtube.SEND_PROTOZOON: + self.incoming.append_protozoon(model_population.from_buffer(code_element)) + inbox_widget.queue_draw() else: - ka_debug.info('Somebody called me with an corrupt data model') + ka_debug.err('Somebody called me using an illegal type [%s]' + % code_type) def on_new_tube(self, tube, is_initiator, get_buddy): """Creates communication object and sends population diff --git a/ka_random.py b/ka_random.py index fde952a..bdbd172 100644 --- a/ka_random.py +++ b/ka_random.py @@ -104,6 +104,29 @@ def crossingover(first, second): seq.append(second[index]) return seq +def mutate_list(this_list, number_of_constraint, new_element): + # maybe remove one element + len_this = len(this_list) + if len_this > number_of_constraint[0] and is_mutating(): + del this_list[random.randint(0, len_this-1)] + len_this -= 1 + # maybe duplicate one of the elements + if len_this < number_of_constraint[1] and is_mutating(): + random_element = this_list[random.randint(0, len_this-1)] + dupli_element = random_element.copy() + this_list.insert(random.randint(0, len_this-1), dupli_element) + len_this += 1 + # maybe insert a new element + if len_this < number_of_constraint[1] and is_mutating(): +# single_element = factory.create_random(type_constraint, path) + new_element.randomize() + this_list.insert(random.randint(0, len_this-1), new_element) + len_this += 1 + + # delegate mutation to the elements child components + for single_element in this_list: + single_element.mutate() + def limitate(value): """ post: 0.0 <= __return__ <= 1.0 diff --git a/kandidtube.py b/kandidtube.py index 67c0dba..4ae19ea 100644 --- a/kandidtube.py +++ b/kandidtube.py @@ -90,7 +90,6 @@ class KandidTube(ExportedGObject): self.send_population(self._controller.serialize_model(), sender) def publish_protozoon(self, code_element): - ka_debug.info('publish_protozoon: %s %s' % (type(code_element), code_element)) code_element_base64 = base64.b64encode(code_element) code_md5 = hashlib.md5(code_element).hexdigest() ka_debug.info('publish_protozoon: Sent %u bytes, type: [%s] md5: [%s]' % \ @@ -111,7 +110,10 @@ class KandidTube(ExportedGObject): ka_debug.info('on_publish: I got %u bytes, type: [%s] md5: [%s]' \ % (len(code_element), code_type, code_md5)) dec = base64.b64decode(code_element) - self._controller.on_received(code_type, dec, code_md5) + if hashlib.md5(code_element).hexdigest() == code_md5: + self._controller.on_received(code_type, dec, code_md5) + else: + ka_debug.info('Somebody called me with an corrupt data model') def send_population(self, code_element, sender=None): code_element_base64 = base64.b64encode(code_element) diff --git a/model_constraintpool.py b/model_constraintpool.py index 12eca65..7bad1be 100644 --- a/model_constraintpool.py +++ b/model_constraintpool.py @@ -130,9 +130,13 @@ class ConstraintPool(object): model_population.write_file(file_path, ConstraintPool._known_keys) def _my_defaults(self): - self.set('/Protozoon', 'layertypeconstraint', ['image', 'letterpress', 'markovchain', ]) - self.set('/Protozoon', 'mergertypeconstraint', ['layermask', ]) #TODO read from persistence, provide an constraint editor + self.set('/Protozoon', 'layertypeconstraint', ['image', 'letterpress', 'markovchain', 'randomwalkspline', ]) +# self.set('/Protozoon', 'layertypeconstraint', ['randomwalkspline', 'letterpress', 'markovchain', ]) +# self.set('/Protozoon', 'layertypeconstraint', ['randomwalkspline', ]) +# self.set('/Protozoon', 'layertypeconstraint', ['voronoidiagram', ]) +# self.set('/Protozoon', 'mergertypeconstraint', ['layermask', ]) + # self.set('/Protozoon', 'layertypeconstraint', ['referencepattern', 'image', 'letterpress', 'markovchain', ]) # self.set('/Protozoon', 'layertypeconstraint', ['referencepattern']) # self.set('/Protozoon', 'layertypeconstraint', ['image', ]) diff --git a/model_population.py b/model_population.py index 93ae43e..213147b 100644 --- a/model_population.py +++ b/model_population.py @@ -51,14 +51,12 @@ class KandidModel(object): def get_flurry_rate(self): return ka_random.get_flurry() -# return self._flurry_rate def set_flurry_rate(self, value): """ pre: 0 <= value <= 9 """ ka_random.set_flurry(value) -# self._flurry_rate = value def is_overwrite_allowed(self): """Preserve an already evolved population from over writing.""" diff --git a/model_protozoon.py b/model_protozoon.py index 2e389f0..9d28672 100644 --- a/model_protozoon.py +++ b/model_protozoon.py @@ -109,19 +109,20 @@ class Protozoon(model_allele.Allele): def mutate(self): - """Make small random changes to the protozoon. + """Make random changes to the protozoon. """ cpool = model_constraintpool.ConstraintPool.get_pool() - layertype_constraint = cpool.get(self, LAYERTYPE_CONSTRAINT) number_of_constraint = cpool.get(self, NUMBER_OF_LAYERS_CONSTRAINT) - self._mutate_list(self.layers, - layertype_constraint, number_of_constraint, - ka_factory.get_factory('layer'), self.path) + layertype_constraint = cpool.get(self, LAYERTYPE_CONSTRAINT) + new_element = ka_factory.get_factory('layer'). \ + create_random(layertype_constraint, self.path) + ka_random.mutate_list(self.layers, number_of_constraint, new_element) + mergertype_constraint = cpool.get(self, MERGER_CONSTRAINT) - self._mutate_list(self.mergers, - mergertype_constraint, number_of_constraint, - ka_factory.get_factory('merger'), self.path) + new_element = ka_factory.get_factory('merger'). \ + create_random(mergertype_constraint, self.path) + ka_random.mutate_list(self.mergers, number_of_constraint, new_element) # mutate background color self.background.mutate() @@ -184,30 +185,6 @@ class Protozoon(model_allele.Allele): new_list.append(other_list[index]) return new_list - def _mutate_list(self, this_list, type_constraint, number_of_constraint, - factory, path): - # maybe remove one element - len_this = len(this_list) - if len_this > number_of_constraint[0] and ka_random.is_mutating(): - del this_list[random.randint(0, len_this-1)] - len_this -= 1 - # maybe duplicate one of the elements - if len_this < number_of_constraint[1] and ka_random.is_mutating(): - random_element = this_list[random.randint(0, len_this-1)] - dupli_element = random_element.copy() - this_list.insert(random.randint(0, len_this-1), dupli_element) - len_this += 1 - # maybe insert a new element - if len_this < number_of_constraint[1] and ka_random.is_mutating(): - single_element = factory.create_random(type_constraint, path) - single_element.randomize() - this_list.insert(random.randint(0, len_this-1), single_element) - len_this += 1 - - # delegate mutation to the elements child components - for single_element in this_list: - single_element.mutate() - def draw(self, ctx, width, height): """ pre: ctx is not None |