<s> from random import Random <EOL> from time import time <EOL> from math import cos <EOL> from math import pi <EOL> from inspyred import ec <EOL> from inspyred . ec import terminators <EOL> def generate_rastrigin ( random , args ) : <EOL> size = args . get ( '<STR_LIT>' , <NUM_LIT:10> ) <EOL> return [ random . uniform ( - <NUM_LIT> , <NUM_LIT> ) for i in range ( size ) ] <EOL> def evaluate_rastrigin ( candidates , args ) : <EOL> fitness = [ ] <EOL> for cs in candidates : <EOL> fit = <NUM_LIT:10> * len ( cs ) + sum ( [ ( ( x - <NUM_LIT:1> ) ** <NUM_LIT:2> - <NUM_LIT:10> * cos ( <NUM_LIT:2> * pi * ( x - <NUM_LIT:1> ) ) ) for x in cs ] ) <EOL> fitness . append ( fit ) <EOL> return fitness <EOL> rand = Random ( ) <EOL> rand . seed ( int ( time ( ) ) ) <EOL> es = ec . ES ( rand ) <EOL> es . terminator = terminators . evaluation_termination <EOL> final_pop = es . evolve ( generator = generate_rastrigin , <EOL> evaluator = evaluate_rastrigin , <EOL> pop_size = <NUM_LIT:100> , <EOL> maximize = False , <EOL> bounder = ec . Bounder ( - <NUM_LIT> , <NUM_LIT> ) , <EOL> max_evaluations = <NUM_LIT> , <EOL> mutation_rate = <NUM_LIT> , <EOL> num_inputs = <NUM_LIT:3> ) <EOL> final_pop . sort ( reverse = True ) <EOL> print ( final_pop [ <NUM_LIT:0> ] ) </s>
