def _create_offspring( breeders ): stock = breeders[:] kids = [] random.shuffle( stock ) print "Making babies" i = 0 while i < len(stock) - 1: parents = stock[i], stock[i+1] print " Parents %s and %s" % ( parents[0].identifier, parents[1].identifier ) for k in range(4): mom = parents[ k % 2 ] dad = parents[ (k + 1) % 2 ] kid = copy.deepcopy(mom) kid.age = 0 kid.game_activations = 0 kid.total_activations = 0 kid.identifier = ''.join(random.choice(string.ascii_uppercase + string.digits) for _ in range(10)) kid.strength = (mom.strength + dad.strength ) / 2 kid.specifity = 0 mutation_chance = random.random() for j,cond in enumerate(kid.conditions): if ( j < len ( dad.conditions ) ): division1 = random.randrange( 1, len(Message.game_msg_def) ) division2 = random.randrange( 1, len(Message.game_msg_def) ) l = 0 while ( l < len( Message.game_msg_def ) ): if ( l > division1 and l < division1 + division2 ): cond[l] = dad.conditions[j][l] if ( cond[l] != None ): kid.specifity += 6 - len( cond[l] ) l += 1 # Chance of mutation if ( mutation_chance < 0.01 / len(kid.conditions) + j * 0.02 / len(kid.conditions) and mutation_chance > 0.02 / len(kid.conditions) * j ): index = random.randrange( len( Message.game_msg_def ) ) if ( cond[index] == None ): cond[index] = random.sample(xrange(6),5) cond[index].sort() kid.specifity += 1 elif ( len(cond[index]) == 1 ): choices = [ x for x in range(6) if not x in cond[index] ] cond[index].append( random.choice( choices ) ) cond[index].sort() kid.specifity -= 1 else: if ( random.random() > 0.5 ): del cond[index][random.randrange(len(cond[index]))] kid.specifity += 1 else: choices = [ x for x in range(6) if not x in cond[index] ] if ( choices ): cond[index].append( random.choice( choices ) ) cond[index].sort() else: cond[index] = None kid.specifity -= 1 elif ( mutation_chance < 0.02 / len(kid.conditions) + j * 0.02 / len(kid.conditions) and mutation_chance > 0.02 / len(kid.conditions) * j ): index = random.randrange( len( Message.game_msg_def ) ) swap_index = index while ( index == swap_index ): swap_index = random.randrange( len( Message.game_msg_def ) ) tmp = cond[index] cond[index] = cond[swap_index] cond[swap_index] = tmp # Chance of mutation of output if ( mutation_chance < 0.04 and mutation_chance > 0.02 ): msg, act = kid.output if ( mutation_chance < 0.03 and mutation_chance > 0.02 ): if ( random.random() < 0.1 ): msg = Message() msg.classifier_message() else: msg = None elif ( mutation_chance < 0.04 and mutation_chance > 0.02 ): act = random.choice(['Heal','Mine','Attack','Wait']) kid.output = msg, act print " %s and %s Made %s." % (mom.identifier, dad.identifier, kid.identifier ) #print kid kids.append( kid ) i += 2 return kids
def _create_classifier(self, message = None): """Create a new classifier that optionally matches a message""" c = Classifier() min_specifity = 10 total_str = 0 if self.classifiers: for classify in self.classifiers: total_str += classify.strength average_str = total_str / len(self.classifiers) c.strength = average_str else: c.strength = 100 # Set condition(s) elements = range(len(Message.game_msg_index)) random.shuffle(elements) elements_specifity = [ random.randint(1,5) for x in xrange(len(Message.game_msg_index))] c.specifity = 0 # Create the condition for i,value in enumerate(elements): c.specifity += 6 - elements_specifity[i] c.conditions[0][value] = \ random.sample(xrange(0,5),elements_specifity[i] ) # Ensure message's value is in the condition if ( message != None ): if ( message.status[value] not in c.conditions[0][value] ): c.conditions[0][value][0] = message.status[value] c.conditions[0][value].sort() # Stop adding to the condition sometime after you have > min_specifity if ( c.specifity > min_specifity ): if ( random.random() < float(i) / float(len(Message.game_msg_index) ) ): break if ( random.random() < 0.01 and c.specifity < min_specifity * 0.3 ): c.conditions.append( [ None ] * len( Message.game_msg_index) ) break if ( len(c.conditions) == 2 ): elements = range(len(Message.game_msg_index)) random.shuffle(elements) elements_specifity = [ random.randint(1,5) for x in xrange(len(Message.game_msg_index))] # Create the condition for i,value in enumerate(elements): c.specifity += 6 - elements_specifity[i] c.conditions[1][value] = \ random.sample(xrange(0,5),elements_specifity[i] ) # Ensure message's value is in the condition if ( message != None ): if ( message.status[value] not in c.conditions[1][value] ): c.conditions[1][value][0] = message.status[value] c.conditions[1][value].sort() # Stop adding to the condition sometime after you have > min_specifity if ( c.specifity > min_specifity ): if ( random.random() < float(i) / float(len(Message.game_msg_index) ) ): break if ( random.random() < 0.01 and c.specifity < min_specifity * 0.3 ): c.conditions.append( [ None ] * len( Message.game_msg_index) ) break # Set output output_message = Message() output_message.classifier_message() output_message.emitter = c c.output = output_message, random.choice(['Heal','Mine','Attack','Wait']) return c