<s> """<STR_LIT>""" <EOL> __author__ = '<STR_LIT>' <EOL> from pybrain . utilities import Named <EOL> from random import randint <EOL> from scipy import zeros , argmax , array , power , exp , sqrt , var , zeros_like , arange , mean , log <EOL> def rankedFitness ( R ) : <EOL> """<STR_LIT>""" <EOL> res = zeros_like ( R ) <EOL> l = list ( zip ( R , list ( range ( len ( R ) ) ) ) ) <EOL> l . sort ( ) <EOL> for i , ( _ , j ) in enumerate ( l ) : <EOL> res [ j ] = i <EOL> return res <EOL> def normalizedFitness ( R ) : <EOL> return array ( ( R - mean ( R ) ) / sqrt ( var ( R ) ) ) . flatten ( ) <EOL> class RankingFunction ( Named ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , ** args ) : <EOL> self . setArgs ( ** args ) <EOL> n = self . __class__ . __name__ <EOL> for k , val in list ( args . items ( ) ) : <EOL> n += '<STR_LIT:->' + str ( k ) + '<STR_LIT:=>' + str ( val ) <EOL> self . name = n <EOL> def __call__ ( self , R ) : <EOL> """<STR_LIT>""" <EOL> res = rankedFitness ( R ) <EOL> return res / float ( max ( res ) ) <EOL> class TournamentSelection ( RankingFunction ) : <EOL> """<STR_LIT>""" <EOL> tournamentSize = <NUM_LIT:2> <EOL> def __call__ ( self , R ) : <EOL> res = zeros ( len ( R ) ) <EOL> for i in range ( len ( R ) ) : <EOL> l = [ i ] <EOL> for dummy in range ( self . tournamentSize - <NUM_LIT:1> ) : <EOL> randindex = i <EOL> while randindex == i : <EOL> randindex = randint ( <NUM_LIT:0> , len ( R ) - <NUM_LIT:1> ) <EOL> l . append ( randindex ) <EOL> fits = [ R [ x ] for x in l ] <EOL> res [ argmax ( fits ) ] += <NUM_LIT:1> <EOL> return res <EOL> class SmoothGiniRanking ( RankingFunction ) : <EOL> """<STR_LIT>""" <EOL> gini = <NUM_LIT:0.1> <EOL> linearComponent = <NUM_LIT:0.> <EOL> def __call__ ( self , R ) : <EOL> def smoothup ( x ) : <EOL> """<STR_LIT>""" <EOL> return power ( x , <NUM_LIT:2> / self . gini - <NUM_LIT:1> ) <EOL> ranks = rankedFitness ( R ) <EOL> res = zeros ( len ( R ) ) <EOL> for i in range ( len ( ranks ) ) : <EOL> res [ i ] = ranks [ i ] * self . linearComponent + smoothup ( ranks [ i ] / float ( len ( R ) - <NUM_LIT:1> ) ) * ( <NUM_LIT:1> - self . linearComponent ) <EOL> res /= max ( res ) <EOL> return res <EOL> class ExponentialRanking ( RankingFunction ) : <EOL> """<STR_LIT>""" <EOL> temperature = <NUM_LIT> <EOL> def __call__ ( self , R ) : <EOL> ranks = rankedFitness ( R ) <EOL> ranks = ranks / ( len ( R ) - <NUM_LIT:1.0> ) <EOL> return exp ( ranks * self . temperature ) <EOL> class HansenRanking ( RankingFunction ) : <EOL> """<STR_LIT>""" <EOL> def __call__ ( self , R ) : <EOL> ranks = rankedFitness ( R ) <EOL> return array ( [ max ( <NUM_LIT:0.> , x ) for x in log ( len ( R ) / <NUM_LIT> + <NUM_LIT:1.0> ) - log ( len ( R ) - array ( ranks ) ) ] ) <EOL> class TopSelection ( RankingFunction ) : <EOL> """<STR_LIT>""" <EOL> topFraction = <NUM_LIT:0.1> <EOL> def __call__ ( self , R ) : <EOL> res = zeros ( len ( R ) ) <EOL> ranks = rankedFitness ( R ) <EOL> cutoff = len ( R ) * ( <NUM_LIT:1.> - self . topFraction ) <EOL> for i in range ( len ( R ) ) : <EOL> if ranks [ i ] >= cutoff : <EOL> res [ i ] = <NUM_LIT:1.0> <EOL> else : <EOL> res [ i ] = <NUM_LIT:0.0> <EOL> return res <EOL> class TopLinearRanking ( TopSelection ) : <EOL> """<STR_LIT>""" <EOL> topFraction = <NUM_LIT> <EOL> def __call__ ( self , R ) : <EOL> res = zeros ( len ( R ) ) <EOL> ranks = rankedFitness ( R ) <EOL> cutoff = len ( R ) * ( <NUM_LIT:1.> - self . topFraction ) <EOL> for i in range ( len ( R ) ) : <EOL> if ranks [ i ] >= cutoff : <EOL> res [ i ] = ranks [ i ] - cutoff <EOL> else : <EOL> res [ i ] = <NUM_LIT:0.0> <EOL> res /= max ( res ) <EOL> return res <EOL> def getPossibleParameters ( self , numberOfSamples ) : <EOL> x = <NUM_LIT:1.> / float ( numberOfSamples ) <EOL> return arange ( x * <NUM_LIT:2> , <NUM_LIT:1> + x , x ) <EOL> def setParameter ( self , p ) : <EOL> self . topFraction = p <EOL> class BilinearRanking ( RankingFunction ) : <EOL> """<STR_LIT>""" <EOL> bilinearFactor = <NUM_LIT:20> <EOL> def __call__ ( self , R ) : <EOL> ranks = rankedFitness ( R ) <EOL> res = zeros ( len ( R ) ) <EOL> transitionpoint = <NUM_LIT:4> * len ( ranks ) / <NUM_LIT:5> <EOL> for i in range ( len ( ranks ) ) : <EOL> if ranks [ i ] < transitionpoint : <EOL> res [ i ] = ranks [ i ] <EOL> else : <EOL> res [ i ] = ranks [ i ] + ( ranks [ i ] - transitionpoint ) * self . bilinearFactor <EOL> res /= max ( res ) <EOL> return res </s>
