<s> __author__ = '<STR_LIT>' <EOL> from scipy import randn , zeros <EOL> from scipy import random as rd , array <EOL> from random import choice , random , gauss , shuffle , sample <EOL> from numpy import ndarray <EOL> from pybrain . optimization . populationbased . evolution import Evolution <EOL> from pybrain . optimization . optimizer import ContinuousOptimizer <EOL> class GA ( ContinuousOptimizer , Evolution ) : <EOL> """<STR_LIT>""" <EOL> tournament = False <EOL> tournamentSize = <NUM_LIT:2> <EOL> topProportion = <NUM_LIT> <EOL> elitism = False <EOL> eliteProportion = <NUM_LIT:0.5> <EOL> _eliteSize = None <EOL> mutationProb = <NUM_LIT:0.1> <EOL> mutationStdDev = <NUM_LIT:0.5> <EOL> initRangeScaling = <NUM_LIT> <EOL> initialPopulation = None <EOL> mustMaximize = True <EOL> '''<STR_LIT>''' <EOL> def initBoundaries ( self ) : <EOL> assert len ( self . xBound ) == self . numParameters <EOL> self . mins = array ( [ min_ for min_ , max_ in self . xBound ] ) <EOL> self . maxs = array ( [ max_ for min_ , max_ in self . xBound ] ) <EOL> def initPopulation ( self ) : <EOL> '''<STR_LIT>''' <EOL> if self . xBound is not None : <EOL> self . initBoundaries ( ) <EOL> if self . initialPopulation is not None : <EOL> self . currentpop = self . initialPopulation <EOL> else : <EOL> self . currentpop = [ self . _initEvaluable ] <EOL> for _ in range ( self . populationSize - <NUM_LIT:1> ) : <EOL> '''<STR_LIT>''' <EOL> if self . xBound is None : <EOL> self . currentpop . append ( self . _initEvaluable + randn ( self . numParameters ) <EOL> * self . mutationStdDev * self . initRangeScaling ) <EOL> '''<STR_LIT>''' <EOL> else : <EOL> position = rd . random ( self . numParameters ) <EOL> position *= ( self . maxs - self . mins ) <EOL> position += self . mins <EOL> self . currentpop . append ( position ) <EOL> def crossOverOld ( self , parents , nbChildren ) : <EOL> """<STR_LIT>""" <EOL> xdim = self . numParameters <EOL> children = [ ] <EOL> for _ in range ( nbChildren ) : <EOL> p1 = choice ( parents ) <EOL> if xdim < <NUM_LIT:2> : <EOL> children . append ( p1 ) <EOL> else : <EOL> p2 = choice ( parents ) <EOL> point = choice ( list ( range ( xdim - <NUM_LIT:1> ) ) ) <EOL> point += <NUM_LIT:1> <EOL> res = zeros ( xdim ) <EOL> res [ : point ] = p1 [ : point ] <EOL> res [ point : ] = p2 [ point : ] <EOL> children . append ( res ) <EOL> return children <EOL> def mutatedOld ( self , indiv ) : <EOL> """<STR_LIT>""" <EOL> res = indiv . copy ( ) <EOL> for i in range ( self . numParameters ) : <EOL> if random ( ) < self . mutationProb : <EOL> res [ i ] = indiv [ i ] + gauss ( <NUM_LIT:0> , self . mutationStdDev ) <EOL> return res <EOL> '''<STR_LIT>''' <EOL> def crossOver ( self , parents , nbChildren ) : <EOL> """<STR_LIT>""" <EOL> """<STR_LIT>""" <EOL> xdim = self . numParameters <EOL> shuffle ( parents ) <EOL> children = [ ] <EOL> for i in range ( len ( parents ) // <NUM_LIT:2> ) : <EOL> p1 = parents [ i ] <EOL> p2 = parents [ i + ( len ( parents ) // <NUM_LIT:2> ) ] <EOL> if xdim < <NUM_LIT:2> : <EOL> children . append ( p1 ) <EOL> children . append ( p2 ) <EOL> else : <EOL> point = choice ( list ( range ( xdim - <NUM_LIT:1> ) ) ) <EOL> point += <NUM_LIT:1> <EOL> res = zeros ( xdim ) <EOL> res [ : point ] = p1 [ : point ] <EOL> res [ point : ] = p2 [ point : ] <EOL> children . append ( res ) <EOL> res = zeros ( xdim ) <EOL> res [ : point ] = p2 [ : point ] <EOL> res [ point : ] = p1 [ point : ] <EOL> children . append ( res ) <EOL> shuffle ( children ) <EOL> if len ( children ) > nbChildren : <EOL> children = children [ : nbChildren ] <EOL> else : <EOL> while ( len ( children ) < nbChildren ) : <EOL> children += sample ( children , <NUM_LIT:1> ) <EOL> return children <EOL> def childexist ( self , indiv , pop ) : <EOL> if isinstance ( pop , list ) : <EOL> for i in range ( len ( pop ) ) : <EOL> if all ( ( abs ( indiv [ k ] - pop [ i ] [ k ] ) / ( self . maxs [ k ] - self . mins [ k ] ) ) <EOL> < <NUM_LIT> for k in range ( self . numParameters ) ) : <EOL> return True <EOL> return False <EOL> def mutated ( self , indiv ) : <EOL> """<STR_LIT>""" <EOL> res = indiv . copy ( ) <EOL> for i in range ( self . numParameters ) : <EOL> if random ( ) < self . mutationProb : <EOL> if self . xBound is None : <EOL> res [ i ] = indiv [ i ] + gauss ( <NUM_LIT:0> , self . mutationStdDev ) <EOL> else : <EOL> res [ i ] = max ( min ( indiv [ i ] + gauss ( <NUM_LIT:0> , self . mutationStdDev ) , self . maxs [ i ] ) , <EOL> self . mins [ i ] ) <EOL> return res <EOL> def old_jpq_mutated ( self , indiv , pop ) : <EOL> """<STR_LIT>""" <EOL> res = indiv . copy ( ) <EOL> in_pop = self . childexist ( indiv , pop ) <EOL> for i in range ( self . numParameters ) : <EOL> if random ( ) < self . mutationProb : <EOL> res [ i ] = max ( min ( indiv [ i ] + gauss ( <NUM_LIT:0> , self . mutationStdDev ) , self . maxs [ i ] ) , <EOL> self . mins [ i ] ) <EOL> if random ( ) < self . mutationProb or in_pop : <EOL> if self . xBound is None : <EOL> res [ i ] = indiv [ i ] + gauss ( <NUM_LIT:0> , self . mutationStdDev ) <EOL> else : <EOL> if in_pop : <EOL> cmin = abs ( indiv [ i ] - self . mins [ i ] ) / ( self . maxs [ i ] - self . mins [ i ] ) <EOL> cmax = abs ( indiv [ i ] - self . maxs [ i ] ) / ( self . maxs [ i ] - self . mins [ i ] ) <EOL> if cmin < <NUM_LIT> or cmax < <NUM_LIT> : <EOL> res [ i ] = self . mins [ i ] + random ( ) * random ( ) * ( self . maxs [ i ] - self . mins [ i ] ) <EOL> else : <EOL> res [ i ] = max ( min ( indiv [ i ] + gauss ( <NUM_LIT:0> , self . mutationStdDev ) , self . maxs [ i ] ) , <EOL> self . mins [ i ] ) <EOL> else : <EOL> res [ i ] = max ( min ( indiv [ i ] + gauss ( <NUM_LIT:0> , self . mutationStdDev ) , self . maxs [ i ] ) , <EOL> self . mins [ i ] ) <EOL> return res <EOL> @ property <EOL> def selectionSize ( self ) : <EOL> """<STR_LIT>""" <EOL> return int ( self . populationSize * self . topProportion ) <EOL> @ property <EOL> def eliteSize ( self ) : <EOL> if self . elitism : <EOL> if self . _eliteSize != None : <EOL> return self . _eliteSize <EOL> else : <EOL> return int ( self . populationSize * self . eliteProportion ) <EOL> else : <EOL> return <NUM_LIT:0> <EOL> def select ( self ) : <EOL> """<STR_LIT>""" <EOL> if not self . tournament : <EOL> tmp = list ( zip ( self . fitnesses , self . currentpop ) ) <EOL> tmp . sort ( key = lambda x : x [ <NUM_LIT:0> ] ) <EOL> tmp2 = list ( reversed ( tmp ) ) [ : self . selectionSize ] <EOL> return [ x [ <NUM_LIT:1> ] for x in tmp2 ] <EOL> else : <EOL> raise NotImplementedError ( ) <EOL> def produceOffspring ( self ) : <EOL> """<STR_LIT>""" <EOL> parents = self . select ( ) <EOL> es = min ( self . eliteSize , self . selectionSize ) <EOL> self . currentpop = parents [ : es ] <EOL> '''<STR_LIT>''' <EOL> nbchildren = self . populationSize - es <EOL> if self . populationSize - es <= <NUM_LIT:0> : <EOL> nbchildren = len ( parents ) <EOL> for child in self . crossOver ( parents , nbchildren ) : <EOL> self . currentpop . append ( self . mutated ( child ) ) </s>
