コード例 #1
0
                song.crossover_chance = get_crossover_prob(song.score, total_score)
                song.mutation_chance = get_mutation_prob(song.crossover_chance)
    else:
        with open(save_file, 'w+') as save:
            for song in song_list:
                save.write(str(song) + '\n')
               
        raise SystemExit
    
    song_list = BiasedRandomSequence(*song_list, insert_key=lambda v: v.crossover_chance) 
    new_song_list = []

    
    for i in range(len(song_list)):
        
        song1, song2 = sample_pair(song_list)
        
    
        song3 = song1.crossover(song2, random_crossover_point(chromo_len, randomizer))

        
        if song2.mutation_chance <= get_prob(randomizer):
            delta_mask = random_delta_mask(max_step_size, randomizer, song3)
            song3.mutate(*delta_mask)

        
        new_song_list.append(song3)
        
   
    song_list = new_song_list
コード例 #2
0
    get_population_sample(config_file, *pheno_list)
    logging.info("avg fitness: {}, max fitness: {}, min fitness: {}".format(get_avg(*score_list), max(score_list), min(score_list)))
    gen_num = get_gen_num(pheno_list[1], config_file)
    value_list = [get_avg(*score_list), max(score_list), min(score_list), get_avg(*diversity_list)]
    append_to_graph_file(config_file.graph_file, gen_num, *value_list)

    #pre-crossover setup
    new_pheno_list = []
    top_pheno = find_min_pheno(pheno_list, config_file)
    new_pheno_list.append(top_pheno)
    pheno_list = BiasedRandomSequence(*pheno_list, insert_key=lambda v: v.crossover_chance) 
    
    #crossover/mutation loop
    for i in range(get_pheno_count(config_file) - 1):
        #phenotype selection and crossover
        pheno1, pheno2 = sample_pair(pheno_list)
        c_over_point = randomizer.get_crossover_point(pheno1, pheno2)
        pheno3 = pheno1.crossover(pheno2, c_over_point)
        log_cross_over(pheno1, pheno2, c_over_point)

        #mutation phase
        if randomizer.get_prob() <= config_file.mutation_chance:
            if randomizer.get_prob() <= config_file.chromo_delete_prob and len(pheno3) > 1:
                pheno3 = delete_mutation(pheno3, randomizer)
                log_delete_mutation(pheno3)
            elif randomizer.get_prob() <= config_file.chromo_add_prob:
                pheno3 = add_mutation(pheno3, config_file)
                log_add_mutation(pheno3)
            else:
                pheno3 = mutation(pheno3, config_file)
                log_mutation(pheno3)