<s> from __future__ import print_function <EOL> __author__ = '<STR_LIT>' <EOL> from numpy import floor , log , eye , zeros , array , sqrt , sum , dot , tile , outer , real <EOL> from numpy import exp , diag , power , ravel <EOL> from numpy . linalg import eig , norm <EOL> from numpy . random import randn <EOL> from pybrain . optimization . optimizer import ContinuousOptimizer <EOL> class CMAES ( ContinuousOptimizer ) : <EOL> """<STR_LIT>""" <EOL> mustMinimize = True <EOL> stopPrecision = <NUM_LIT> <EOL> storeAllCenters = False <EOL> initStepSize = <NUM_LIT:0.5> <EOL> def _additionalInit ( self ) : <EOL> self . center = self . _initEvaluable <EOL> self . stepSize = self . initStepSize <EOL> if self . storeAllCenters : <EOL> self . _allCenters = [ ] <EOL> self . mu = int ( floor ( self . batchSize / <NUM_LIT:2> ) ) <EOL> self . weights = log ( self . mu + <NUM_LIT:1> ) - log ( array ( range ( <NUM_LIT:1> , self . mu + <NUM_LIT:1> ) ) ) <EOL> self . weights /= sum ( self . weights ) <EOL> self . muEff = sum ( self . weights ) ** <NUM_LIT:2> / sum ( power ( self . weights , <NUM_LIT:2> ) ) <EOL> self . cumCov = <NUM_LIT:4> / float ( self . numParameters + <NUM_LIT:4> ) <EOL> self . cumStep = ( self . muEff + <NUM_LIT:2> ) / ( self . numParameters + self . muEff + <NUM_LIT:3> ) <EOL> self . muCov = self . muEff <EOL> self . covLearningRate = ( ( <NUM_LIT:1> / self . muCov ) * <NUM_LIT:2> / ( self . numParameters + <NUM_LIT> ) ** <NUM_LIT:2> + ( <NUM_LIT:1> - <NUM_LIT:1> / self . muCov ) * <EOL> ( ( <NUM_LIT:2> * self . muEff - <NUM_LIT:1> ) / ( ( self . numParameters + <NUM_LIT:2> ) ** <NUM_LIT:2> + <NUM_LIT:2> * self . muEff ) ) ) <EOL> self . dampings = <NUM_LIT:1> + <NUM_LIT:2> * max ( <NUM_LIT:0> , sqrt ( ( self . muEff - <NUM_LIT:1> ) / ( self . numParameters + <NUM_LIT:1> ) ) - <NUM_LIT:1> ) + self . cumStep <EOL> self . covPath = zeros ( self . numParameters ) <EOL> self . stepPath = zeros ( self . numParameters ) <EOL> self . B = eye ( self . numParameters , self . numParameters ) <EOL> self . D = eye ( self . numParameters , self . numParameters ) <EOL> self . C = dot ( dot ( self . B , self . D ) , dot ( self . B , self . D ) . T ) <EOL> self . chiN = self . numParameters ** <NUM_LIT:0.5> * ( <NUM_LIT:1> - <NUM_LIT:1.> / ( <NUM_LIT> * self . numParameters ) + <NUM_LIT:1> / ( <NUM_LIT> * self . numParameters ** <NUM_LIT:2> ) ) <EOL> def _learnStep ( self ) : <EOL> arz = randn ( self . numParameters , self . batchSize ) <EOL> arx = tile ( self . center . reshape ( self . numParameters , <NUM_LIT:1> ) , ( <NUM_LIT:1> , self . batchSize ) ) + self . stepSize * dot ( dot ( self . B , self . D ) , arz ) <EOL> arfitness = zeros ( self . batchSize ) <EOL> for k in range ( self . batchSize ) : <EOL> arfitness [ k ] = self . _oneEvaluation ( arx [ : , k ] ) <EOL> arfitness , arindex = sorti ( arfitness ) <EOL> arz = arz [ : , arindex ] <EOL> arx = arx [ : , arindex ] <EOL> arzsel = arz [ : , range ( self . mu ) ] <EOL> arxsel = arx [ : , range ( self . mu ) ] <EOL> arxmut = arxsel - tile ( self . center . reshape ( self . numParameters , <NUM_LIT:1> ) , ( <NUM_LIT:1> , self . mu ) ) <EOL> zmean = dot ( arzsel , self . weights ) <EOL> self . center = dot ( arxsel , self . weights ) <EOL> if self . storeAllCenters : <EOL> self . _allCenters . append ( self . center ) <EOL> self . stepPath = ( <NUM_LIT:1> - self . cumStep ) * self . stepPath + sqrt ( self . cumStep * ( <NUM_LIT:2> - self . cumStep ) * self . muEff ) * dot ( self . B , zmean ) <EOL> hsig = norm ( self . stepPath ) / sqrt ( <NUM_LIT:1> - ( <NUM_LIT:1> - self . cumStep ) ** ( <NUM_LIT:2> * self . numEvaluations / float ( self . batchSize ) ) ) / self . chiN < <NUM_LIT> + <NUM_LIT> / ( self . numParameters + <NUM_LIT:1> ) <EOL> self . covPath = ( <NUM_LIT:1> - self . cumCov ) * self . covPath + hsig * sqrt ( self . cumCov * ( <NUM_LIT:2> - self . cumCov ) * self . muEff ) * dot ( dot ( self . B , self . D ) , zmean ) <EOL> self . C = ( ( <NUM_LIT:1> - self . covLearningRate ) * self . C <EOL> + self . covLearningRate * ( <NUM_LIT:1> / self . muCov ) * ( outer ( self . covPath , self . covPath ) <EOL> + ( <NUM_LIT:1> - hsig ) * self . cumCov * ( <NUM_LIT:2> - self . cumCov ) * self . C ) <EOL> + self . covLearningRate * ( <NUM_LIT:1> - <NUM_LIT:1> / self . muCov ) <EOL> * dot ( dot ( arxmut , diag ( self . weights ) ) , arxmut . T ) <EOL> ) <EOL> self . stepSize *= exp ( ( self . cumStep / self . dampings ) * ( norm ( self . stepPath ) / self . chiN - <NUM_LIT:1> ) ) <EOL> self . C = ( self . C + self . C . T ) / <NUM_LIT:2> <EOL> Ev , self . B = eig ( self . C ) <EOL> Ev = real ( Ev ) <EOL> self . D = diag ( sqrt ( Ev ) ) <EOL> self . B = real ( self . B ) <EOL> if arfitness [ <NUM_LIT:0> ] == arfitness [ - <NUM_LIT:1> ] or ( abs ( arfitness [ <NUM_LIT:0> ] - arfitness [ - <NUM_LIT:1> ] ) / <EOL> ( abs ( arfitness [ <NUM_LIT:0> ] ) + abs ( arfitness [ - <NUM_LIT:1> ] ) ) ) <= self . stopPrecision : <EOL> if self . verbose : <EOL> print ( "<STR_LIT>" ) <EOL> self . maxLearningSteps = self . numLearningSteps <EOL> if min ( Ev ) > <NUM_LIT> : <EOL> if self . verbose : <EOL> print ( "<STR_LIT>" ) <EOL> self . maxLearningSteps = self . numLearningSteps <EOL> @ property <EOL> def batchSize ( self ) : <EOL> return int ( <NUM_LIT:4> + floor ( <NUM_LIT:3> * log ( self . numParameters ) ) ) <EOL> def sorti ( vect ) : <EOL> """<STR_LIT>""" <EOL> tmp = sorted ( [ ( x_y [ <NUM_LIT:1> ] , x_y [ <NUM_LIT:0> ] ) for x_y in enumerate ( ravel ( vect ) ) ] ) <EOL> res1 = array ( [ x [ <NUM_LIT:0> ] for x in tmp ] ) <EOL> res2 = array ( [ int ( x [ <NUM_LIT:1> ] ) for x in tmp ] ) <EOL> return res1 , res2 </s>
