<s> import random <EOL> from pyeasyga import pyeasyga <EOL> seed_data = [ <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> , <NUM_LIT:5> , <NUM_LIT:6> , <NUM_LIT:7> ] <EOL> ga = pyeasyga . GeneticAlgorithm ( seed_data , <EOL> population_size = <NUM_LIT:200> , <EOL> generations = <NUM_LIT:100> , <EOL> crossover_probability = <NUM_LIT> , <EOL> mutation_probability = <NUM_LIT> , <EOL> elitism = True , <EOL> maximise_fitness = False ) <EOL> def create_individual ( data ) : <EOL> individual = data [ : ] <EOL> random . shuffle ( individual ) <EOL> return individual <EOL> ga . create_individual = create_individual <EOL> def crossover ( parent_1 , parent_2 ) : <EOL> crossover_index = random . randrange ( <NUM_LIT:1> , len ( parent_1 ) ) <EOL> child_1a = parent_1 [ : crossover_index ] <EOL> child_1b = [ i for i in parent_2 if i not in child_1a ] <EOL> child_1 = child_1a + child_1b <EOL> child_2a = parent_2 [ crossover_index : ] <EOL> child_2b = [ i for i in parent_1 if i not in child_2a ] <EOL> child_2 = child_2a + child_2b <EOL> return child_1 , child_2 <EOL> ga . crossover_function = crossover <EOL> def mutate ( individual ) : <EOL> mutate_index1 = random . randrange ( len ( individual ) ) <EOL> mutate_index2 = random . randrange ( len ( individual ) ) <EOL> index_val1 = individual [ mutate_index1 ] <EOL> index_val2 = individual [ mutate_index2 ] <EOL> index_val1 , index_val2 = index_val2 , index_val1 <EOL> ga . mutate_function = mutate <EOL> def selection ( population ) : <EOL> return random . choice ( population ) <EOL> ga . selection_function = selection <EOL> def fitness ( individual , data ) : <EOL> collisions = <NUM_LIT:0> <EOL> for item in individual : <EOL> item_index = individual . index ( item ) <EOL> for elem in individual : <EOL> elem_index = individual . index ( elem ) <EOL> if item_index != elem_index : <EOL> if item - ( elem_index - item_index ) == elem or ( elem_index - item_index ) + item == elem : <EOL> collisions += <NUM_LIT:1> <EOL> return collisions <EOL> ga . fitness_function = fitness <EOL> ga . run ( ) <EOL> def print_board ( board_representation ) : <EOL> def print_x_in_row ( row_length , x_position ) : <EOL> print '<STR_LIT>' , <EOL> for _ in xrange ( row_length ) : <EOL> print '<STR_LIT>' , <EOL> print '<STR_LIT>' , <EOL> for i in xrange ( row_length ) : <EOL> if i == x_position : <EOL> print '<STR_LIT>' . format ( '<STR_LIT:X>' ) , <EOL> else : <EOL> print '<STR_LIT>' , <EOL> print '<STR_LIT>' <EOL> def print_board_bottom ( row_length ) : <EOL> print '<STR_LIT>' , <EOL> for _ in xrange ( row_length ) : <EOL> print '<STR_LIT>' , <EOL> num_of_rows = len ( board_representation ) <EOL> row_length = num_of_rows <EOL> for row in xrange ( num_of_rows ) : <EOL> print_x_in_row ( row_length , board_representation [ row ] ) <EOL> print_board_bottom ( row_length ) <EOL> print '<STR_LIT:\n>' <EOL> if ga . best_individual ( ) [ <NUM_LIT:0> ] == <NUM_LIT:0> : <EOL> print ga . best_individual ( ) <EOL> print_board ( ga . best_individual ( ) [ <NUM_LIT:1> ] ) <EOL> else : <EOL> print None </s>
