def crossover(protein,parents): possible_rotations = [0,90,180,270] is_invalid = True while is_invalid: is_invalid = False p1 = choice(parents) p2 = choice(parents) parent1 = p1[0] parent2 = p2[0] offspring = [] cut_point = choice(range(len(protein))) rotation_to_apply = choice(possible_rotations) offspring += parent1[:cut_point+1] pivot = offspring[cut_point] offspring_tail = remap_parent2_coords(pivot,parent2[cut_point:],rotation_to_apply) if offspring_tail == None: is_invalid = True else: for c in offspring_tail: if c in offspring: is_invalid = True break else: offspring += offspring_tail new_ind = (offspring,fitness_function(protein,offspring)) return new_ind
def monte_carlo_mutation(protein,ind): possible_rotations = [90,180,270] mutation_tries = 0 #defines the number of tries to mutate the indivuidual conformation_s1 = ind[0] is_invalid = True while is_invalid and mutation_tries != MUTATION_TRIES: #defines the am that on wich the rotation will be applied am_pivot = choice(range(len(conformation_s1))) rotation_to_apply = choice(possible_rotations) #inits the conformation_s2 as an empty list conformation_s2 = [] for i in range(len(conformation_s1)): is_invalid = False if i > am_pivot: #bring to (0,0) to rotate using am_pivot as reference am_to_rotate = (lambda p1,p2: (p1[0] - p2[0], p1[1] - p2[1])) (conformation_s1[i], conformation_s1[am_pivot]) new_pos = apply_rotation(am_to_rotate,rotation_to_apply) #put the point back in the position it belongs new_pos = (lambda p1,p2: (p1[0] + p2[0], p1[1] + p2[1])) (new_pos, conformation_s1[am_pivot]) if new_pos in conformation_s2: #we have to try another rotation mutation_tries += 1 is_invalid = True break else: conformation_s2.append(new_pos) else: conformation_s2.append(conformation_s1[i]) if not is_invalid: new_ind = (conformation_s2,fitness_function(protein,conformation_s2)) return check_acceptance(new_ind,ind) else: return ind
population.append(temprow) ############## # MAIN LOOP: # ############## for idx in range(GENERATIONS): # Run each population member and score it. scores = [] for i in range(POPULATION_SIZE): gene = population[i] instance = CKBotSimEngine.CKBotSim(robotfile) set_periodic_gait_from_GA(instance, gene, instance.gain, free_modules) instance.run(SIMULATION_STEPS) fitness = fitness_function(instance, traits) scores.append(fitness) poses = instance.pose_info # Write all the information to text files for post_processing. if filename != "none": # Write gene/fitness information. str_out = "" for j in range(len(gene)): str_out = str_out + str(gene[j]) + " " f_gene.write(str_out + "\n") f_gene.write(str(fitness) + "\n") # Write pose information for j in range(len(poses)):
temprow.extend([5*random.randint(0,13), random.randint(0,5), random.randint(0,7)]) population.append(temprow) ############## # MAIN LOOP: # ############## for idx in range(GENERATIONS): # Run each population member and score it. scores = [] for i in range(POPULATION_SIZE): gene = population[i] instance = CKBotSimEngine.CKBotSim(robotfile) set_periodic_gait_from_GA(instance, gene, instance.gain, free_modules) instance.run(SIMULATION_STEPS) fitness = fitness_function(instance, traits) scores.append(fitness) poses = instance.pose_info # Write all the information to text files for post_processing. if filename != "none": # Write gene/fitness information. str_out = "" for j in range(len(gene)): str_out = str_out + str(gene[j]) + " " f_gene.write(str_out+"\n") f_gene.write(str(fitness)+"\n") # Write pose information for j in range(len(poses)):