(self, x0, sigma0, inopts = {})
see class `CMAEvolutionStrategy`
see class `CMAEvolutionStrategy`
see class CMAEvolutionStrategy
def __init__(self, x0, sigma0, inopts = {}):
        """see class `CMAEvolutionStrategy`

        """
        self.inputargs = dict(locals()) # for the record
        del self.inputargs['self'] # otherwise the instance self has a cyclic reference
        self.inopts = inopts
        opts = Options(inopts).complement()  # Options() == fmin([],[]) == defaultOptions()

        if opts['noise_handling'] and eval(opts['noise_handling']):
            raise ValueError('noise_handling not available with class CMAEvolutionStrategy, use function fmin')
        if opts['restarts'] and eval(opts['restarts']):
            raise ValueError('restarts not available with class CMAEvolutionStrategy, use function fmin')

        if x0 == str(x0):
            x0 = eval(x0)
        self.mean = array(x0)  # should not have column or row, is just 1-D
        if self.mean.ndim == 2:
            print('WARNING: input x0 should be a list or 1-D array, trying to flatten ' +
                    str(self.mean.shape) + '-array')
            if self.mean.shape[0] == 1:
                self.mean = self.mean[0]
            elif self.mean.shape[1] == 1:
                self.mean = array([x[0] for x in self.mean])
        if self.mean.ndim != 1:
            raise _Error('x0 must be 1-D array')
        if len(self.mean) <= 1:
            raise _Error('optimization in 1-D is not supported (code was never tested)')

        self.N = self.mean.shape[0]
        N = self.N
        self.mean.resize(N) # 1-D array, not really necessary?!
        self.x0 = self.mean
        self.mean = self.x0.copy()  # goes to initialize

        self.sigma0 = sigma0
        if isinstance(sigma0, str):  # TODO: no real need here (do rather in fmin)
            self.sigma0 = eval(sigma0)  # like '1./N' or 'np.random.rand(1)[0]+1e-2'
        if np.size(self.sigma0) != 1 or np.shape(self.sigma0):
            raise _Error('input argument sigma0 must be (or evaluate to) a scalar')
        self.sigma = self.sigma0  # goes to inialize

        # extract/expand options
        opts.evalall(locals())  # using only N
        self.opts = opts

        self.randn = opts['randn']
        self.gp = GenoPheno(N, opts['scaling_of_variables'], opts['typical_x'],
            opts['bounds'], opts['fixed_variables'], opts['transformation'])
        self.boundPenalty = BoundPenalty(self.gp.bounds)
        s = self.gp.geno(self.mean)
        self.mean = self.gp.geno(self.mean, bounds=self.gp.bounds)
        self.N = len(self.mean)
        N = self.N
        if (self.mean != s).any():
            print('WARNING: initial solution is out of the domain boundaries:')
            print('  x0   = ' + str(self.inputargs['x0']))
            print('  ldom = ' + str(self.gp.bounds[0]))
            print('  udom = ' + str(self.gp.bounds[1]))
        self.fmean = np.NaN             # TODO name should change? prints nan (OK with matlab&octave)
        self.fmean_noise_free = 0.  # for output only

        self.sp = CMAParameters(N, opts)
        self.sp0 = self.sp  # looks useless, as it is not a copy

        # initialization of state variables
        self.countiter = 0
        self.countevals = max((0, opts['verb_append'])) if type(opts['verb_append']) is not bool else 0
        self.ps = np.zeros(N)
        self.pc = np.zeros(N)

        stds = np.ones(N)
        self.sigma_vec = np.ones(N) if np.isfinite(self.sp.dampsvec) else 1
        if np.all(self.opts['CMA_teststds']):  # also 0 would not make sense
            stds = self.opts['CMA_teststds']
            if np.size(stds) != N:
                raise _Error('CMA_teststds option must have dimension = ' + str(N))
        if self.opts['CMA_diagonal']:  # is True or > 0
            # linear time and space complexity
            self.B = array(1) # works fine with np.dot(self.B, anything) and self.B.T
            self.C = stds**2  # TODO: remove this!?
            self.dC = self.C
        else:
            self.B = np.eye(N) # identity(N), do not from matlib import *, as eye is a matrix there
            # prevent equal eigenvals, a hack for np.linalg:
            self.C = np.diag(stds**2 * exp(1e-6*(np.random.rand(N)-0.5)))
            self.dC = np.diag(self.C)
            self.Zneg = np.zeros((N, N))
        self.D = stds

        self.flgtelldone = True
        self.itereigenupdated = self.countiter
        self.noiseS = 0  # noise "signal"
        self.hsiglist = []

        if not opts['seed']:
            np.random.seed()
            six_decimals = (time.time() - 1e6 * (time.time() // 1e6))
            opts['seed'] = 1e5 * np.random.rand() + six_decimals + 1e5 * (time.time() % 1)
        opts['seed'] = int(opts['seed'])
        np.random.seed(opts['seed'])

        self.sent_solutions = SolutionDict()
        self.best = BestSolution()

        out = {}  # TODO: obsolete, replaced by method results()?
        out['best'] = self.best
        # out['hsigcount'] = 0
        out['termination'] = {}
        self.out = out

        self.const = BlancClass()
        self.const.chiN = N**0.5*(1-1./(4.*N)+1./(21.*N**2)) # expectation of norm(randn(N,1))

        # attribute for stopping criteria in function stop
        self.stopdict = CMAStopDict()
        self.callbackstop = 0

        self.fit = BlancClass()
        self.fit.fit = []   # not really necessary
        self.fit.hist = []  # short history of best
        self.fit.histbest = []   # long history of best
        self.fit.histmedian = [] # long history of median

        self.more_to_write = []  #[1, 1, 1, 1]  #  N*[1]  # needed when writing takes place before setting

        # say hello
        if opts['verb_disp'] > 0:
            sweighted = '_w' if self.sp.mu > 1 else ''
            smirr = 'mirr%d' % (self.sp.lam_mirr) if self.sp.lam_mirr else ''
            print('(%d' % (self.sp.mu) + sweighted + ',%d' % (self.sp.popsize) + smirr + ')-CMA-ES' +
                  ' (mu_w=%2.1f,w_1=%d%%)' % (self.sp.mueff, int(100*self.sp.weights[0])) +
                  ' in dimension %d (seed=%d, %s)' % (N, opts['seed'], time.asctime())) # + func.__name__
            if opts['CMA_diagonal'] and self.sp.CMA_on:
                s = ''
                if opts['CMA_diagonal'] is not True:
                    s = ' for '
                    if opts['CMA_diagonal'] < np.inf:
                        s += str(int(opts['CMA_diagonal']))
                    else:
                        s += str(np.floor(opts['CMA_diagonal']))
                    s += ' iterations'
                    s += ' (1/ccov=' + str(round(1./(self.sp.c1+self.sp.cmu))) + ')'
                print('   Covariance matrix is diagonal' + s)
def __init__ ( self , x0 , sigma0 , inopts = { } ) : self . inputargs = dict ( locals ( ) ) # for the record del self . inputargs [ 'self' ] # otherwise the instance self has a cyclic reference self . inopts = inopts opts = Options ( inopts ) . complement ( ) # Options() == fmin([],[]) == defaultOptions() if opts [ 'noise_handling' ] and eval ( opts [ 'noise_handling' ] ) : raise ValueError ( 'noise_handling not available with class CMAEvolutionStrategy, use function fmin' ) if opts [ 'restarts' ] and eval ( opts [ 'restarts' ] ) : raise ValueError ( 'restarts not available with class CMAEvolutionStrategy, use function fmin' ) if x0 == str ( x0 ) : x0 = eval ( x0 ) self . mean = array ( x0 ) # should not have column or row, is just 1-D if self . mean . ndim == 2 : print ( 'WARNING: input x0 should be a list or 1-D array, trying to flatten ' + str ( self . mean . shape ) + '-array' ) if self . mean . shape [ 0 ] == 1 : self . mean = self . mean [ 0 ] elif self . mean . shape [ 1 ] == 1 : self . mean = array ( [ x [ 0 ] for x in self . mean ] ) if self . mean . ndim != 1 : raise _Error ( 'x0 must be 1-D array' ) if len ( self . mean ) <= 1 : raise _Error ( 'optimization in 1-D is not supported (code was never tested)' ) self . N = self . mean . shape [ 0 ] N = self . N self . mean . resize ( N ) # 1-D array, not really necessary?! self . x0 = self . mean self . mean = self . x0 . copy ( ) # goes to initialize self . sigma0 = sigma0 if isinstance ( sigma0 , str ) : # TODO: no real need here (do rather in fmin) self . sigma0 = eval ( sigma0 ) # like '1./N' or 'np.random.rand(1)[0]+1e-2' if np . size ( self . sigma0 ) != 1 or np . shape ( self . sigma0 ) : raise _Error ( 'input argument sigma0 must be (or evaluate to) a scalar' ) self . sigma = self . sigma0 # goes to inialize # extract/expand options opts . evalall ( locals ( ) ) # using only N self . opts = opts self . randn = opts [ 'randn' ] self . gp = GenoPheno ( N , opts [ 'scaling_of_variables' ] , opts [ 'typical_x' ] , opts [ 'bounds' ] , opts [ 'fixed_variables' ] , opts [ 'transformation' ] ) self . boundPenalty = BoundPenalty ( self . gp . bounds ) s = self . gp . geno ( self . mean ) self . mean = self . gp . geno ( self . mean , bounds = self . gp . bounds ) self . N = len ( self . mean ) N = self . N if ( self . mean != s ) . any ( ) : print ( 'WARNING: initial solution is out of the domain boundaries:' ) print ( '  x0   = ' + str ( self . inputargs [ 'x0' ] ) ) print ( '  ldom = ' + str ( self . gp . bounds [ 0 ] ) ) print ( '  udom = ' + str ( self . gp . bounds [ 1 ] ) ) self . fmean = np . NaN # TODO name should change? prints nan (OK with matlab&octave) self . fmean_noise_free = 0. # for output only self . sp = CMAParameters ( N , opts ) self . sp0 = self . sp # looks useless, as it is not a copy # initialization of state variables self . countiter = 0 self . countevals = max ( ( 0 , opts [ 'verb_append' ] ) ) if type ( opts [ 'verb_append' ] ) is not bool else 0 self . ps = np . zeros ( N ) self . pc = np . zeros ( N ) stds = np . ones ( N ) self . sigma_vec = np . ones ( N ) if np . isfinite ( self . sp . dampsvec ) else 1 if np . all ( self . opts [ 'CMA_teststds' ] ) : # also 0 would not make sense stds = self . opts [ 'CMA_teststds' ] if np . size ( stds ) != N : raise _Error ( 'CMA_teststds option must have dimension = ' + str ( N ) ) if self . opts [ 'CMA_diagonal' ] : # is True or > 0 # linear time and space complexity self . B = array ( 1 ) # works fine with np.dot(self.B, anything) and self.B.T self . C = stds ** 2 # TODO: remove this!? self . dC = self . C else : self . B = np . eye ( N ) # identity(N), do not from matlib import *, as eye is a matrix there # prevent equal eigenvals, a hack for np.linalg: self . C = np . diag ( stds ** 2 * exp ( 1e-6 * ( np . random . rand ( N ) - 0.5 ) ) ) self . dC = np . diag ( self . C ) self . Zneg = np . zeros ( ( N , N ) ) self . D = stds self . flgtelldone = True self . itereigenupdated = self . countiter self . noiseS = 0 # noise "signal" self . hsiglist = [ ] if not opts [ 'seed' ] : np . random . seed ( ) six_decimals = ( time . time ( ) - 1e6 * ( time . time ( ) // 1e6 ) ) opts [ 'seed' ] = 1e5 * np . random . rand ( ) + six_decimals + 1e5 * ( time . time ( ) % 1 ) opts [ 'seed' ] = int ( opts [ 'seed' ] ) np . random . seed ( opts [ 'seed' ] ) self . sent_solutions = SolutionDict ( ) self . best = BestSolution ( ) out = { } # TODO: obsolete, replaced by method results()? out [ 'best' ] = self . best # out['hsigcount'] = 0 out [ 'termination' ] = { } self . out = out self . const = BlancClass ( ) self . const . chiN = N ** 0.5 * ( 1 - 1. / ( 4. * N ) + 1. / ( 21. * N ** 2 ) ) # expectation of norm(randn(N,1)) # attribute for stopping criteria in function stop self . stopdict = CMAStopDict ( ) self . callbackstop = 0 self . fit = BlancClass ( ) self . fit . fit = [ ] # not really necessary self . fit . hist = [ ] # short history of best self . fit . histbest = [ ] # long history of best self . fit . histmedian = [ ] # long history of median self . more_to_write = [ ] #[1, 1, 1, 1]  #  N*[1]  # needed when writing takes place before setting # say hello if opts [ 'verb_disp' ] > 0 : sweighted = '_w' if self . sp . mu > 1 else '' smirr = 'mirr%d' % ( self . sp . lam_mirr ) if self . sp . lam_mirr else '' print ( '(%d' % ( self . sp . mu ) + sweighted + ',%d' % ( self . sp . popsize ) + smirr + ')-CMA-ES' + ' (mu_w=%2.1f,w_1=%d%%)' % ( self . sp . mueff , int ( 100 * self . sp . weights [ 0 ] ) ) + ' in dimension %d (seed=%d, %s)' % ( N , opts [ 'seed' ] , time . asctime ( ) ) ) # + func.__name__ if opts [ 'CMA_diagonal' ] and self . sp . CMA_on : s = '' if opts [ 'CMA_diagonal' ] is not True : s = ' for ' if opts [ 'CMA_diagonal' ] < np . inf : s += str ( int ( opts [ 'CMA_diagonal' ] ) ) else : s += str ( np . floor ( opts [ 'CMA_diagonal' ] ) ) s += ' iterations' s += ' (1/ccov=' + str ( round ( 1. / ( self . sp . c1 + self . sp . cmu ) ) ) + ')' print ( '   Covariance matrix is diagonal' + s )