<s> import random <EOL> import time <EOL> import math <EOL> import itertools <EOL> import inspyred <EOL> def my_distance ( x , y ) : <EOL> return sum ( [ abs ( a - b ) for a , b in zip ( x , y ) ] ) <EOL> def generate ( random , args ) : <EOL> return [ random . uniform ( <NUM_LIT:0> , <NUM_LIT> ) ] <EOL> def evaluate ( candidates , args ) : <EOL> fitness = [ ] <EOL> for cand in candidates : <EOL> fit = sum ( [ math . sin ( c ) for c in cand ] ) <EOL> fitness . append ( fit ) <EOL> return fitness <EOL> def main ( prng = None , display = False ) : <EOL> if prng is None : <EOL> prng = random . Random ( ) <EOL> prng . seed ( time . time ( ) ) <EOL> ea = inspyred . ec . EvolutionaryComputation ( prng ) <EOL> ea . selector = inspyred . ec . selectors . tournament_selection <EOL> ea . replacer = inspyred . ec . replacers . crowding_replacement <EOL> ea . variator = inspyred . ec . variators . gaussian_mutation <EOL> ea . terminator = inspyred . ec . terminators . evaluation_termination <EOL> final_pop = ea . evolve ( generate , evaluate , pop_size = <NUM_LIT:30> , <EOL> bounder = inspyred . ec . Bounder ( <NUM_LIT:0> , <NUM_LIT> ) , <EOL> max_evaluations = <NUM_LIT> , <EOL> num_selected = <NUM_LIT:30> , <EOL> mutation_rate = <NUM_LIT:1.0> , <EOL> crowding_distance = <NUM_LIT:10> , <EOL> distance_function = my_distance ) <EOL> if display : <EOL> import matplotlib . pyplot as plt <EOL> x = [ ] <EOL> y = [ ] <EOL> for p in final_pop : <EOL> x . append ( p . candidate [ <NUM_LIT:0> ] ) <EOL> y . append ( math . sin ( p . candidate [ <NUM_LIT:0> ] ) ) <EOL> t = [ ( i / <NUM_LIT> ) * <NUM_LIT> for i in range ( <NUM_LIT:1000> ) ] <EOL> s = [ math . sin ( a ) for a in t ] <EOL> plt . plot ( t , s , color = '<STR_LIT:b>' ) <EOL> plt . scatter ( x , y , color = '<STR_LIT:r>' ) <EOL> plt . axis ( [ <NUM_LIT:0> , <NUM_LIT> , <NUM_LIT:0> , <NUM_LIT> ] ) <EOL> plt . savefig ( '<STR_LIT>' , format = '<STR_LIT>' ) <EOL> plt . show ( ) <EOL> return ea <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> main ( display = True ) </s>
