(self, es)
return self
Test termination criteria and update dictionary.
Test termination criteria and update dictionary.
Test termination criteria and update dictionary .
def _update(self, es):
        """Test termination criteria and update dictionary.

        """
        if es.countiter == self.lastiter:
            if es.countiter == 0:
                self.__init__()
                return self
            try:
                if es == self.es:
                    return self
            except: # self.es not yet assigned
                pass

        self.lastiter = es.countiter
        self.es = es

        self.stoplist = []

        N = es.N
        opts = es.opts
        self.opts = opts  # a hack to get _addstop going

        # fitness: generic criterion, user defined w/o default
        self._addstop('ftarget',
                     es.best.f < opts['ftarget'])
        # maxiter, maxfevals: generic criteria
        self._addstop('maxfevals',
                     es.countevals - 1 >= opts['maxfevals'])
        self._addstop('maxiter',
                     es.countiter >= opts['maxiter'])
        # tolx, tolfacupx: generic criteria
        # tolfun, tolfunhist (CEC:tolfun includes hist)
        self._addstop('tolx',
                     all([es.sigma*xi < opts['tolx'] for xi in es.pc]) and \
                     all([es.sigma*xi < opts['tolx'] for xi in sqrt(es.dC)]))
        self._addstop('tolfacupx',
                     any([es.sigma * sig > es.sigma0 * opts['tolfacupx']
                          for sig in sqrt(es.dC)]))
        self._addstop('tolfun',
                     es.fit.fit[-1] - es.fit.fit[0] < opts['tolfun'] and \
                     max(es.fit.hist) - min(es.fit.hist) < opts['tolfun'])
        self._addstop('tolfunhist',
                     len(es.fit.hist) > 9 and \
                     max(es.fit.hist) - min(es.fit.hist) <  opts['tolfunhist'])

        # worst seen false positive: table N=80,lam=80, getting worse for fevals=35e3 \approx 50 * N**1.5
        # but the median is not so much getting worse
        # / 5 reflects the sparsity of histbest/median
        # / 2 reflects the left and right part to be compared
        l = int(max(opts['tolstagnation'] / 5. / 2, len(es.fit.histbest) / 10));
        # TODO: why max(..., len(histbest)/10) ???
        # TODO: the problem in the beginning is only with best ==> ???
        if 11 < 3:  #
            print(es.countiter, (opts['tolstagnation'], es.countiter > N * (5 + 100 / es.popsize),
                        len(es.fit.histbest) > 100,
                        np.median(es.fit.histmedian[:l]) >= np.median(es.fit.histmedian[l:2*l]),
                        np.median(es.fit.histbest[:l]) >= np.median(es.fit.histbest[l:2*l])))
        # equality should handle flat fitness
        self._addstop('tolstagnation', # leads sometimes early stop on ftablet, fcigtab, N>=50?
                    1 < 3 and opts['tolstagnation'] and es.countiter > N * (5 + 100 / es.popsize) and
                    len(es.fit.histbest) > 100 and 2*l < len(es.fit.histbest) and
                    np.median(es.fit.histmedian[:l]) >= np.median(es.fit.histmedian[l:2*l]) and
                    np.median(es.fit.histbest[:l]) >= np.median(es.fit.histbest[l:2*l]))
        # iiinteger: stagnation termination can prevent to find the optimum

        self._addstop('tolupsigma', opts['tolupsigma'] and
                      es.sigma / es.sigma0 / np.max(es.D) > opts['tolupsigma'])

        if 11 < 3 and 2*l < len(es.fit.histbest):  # TODO: this might go wrong, because the nb of written columns changes
            tmp = np.array((-np.median(es.fit.histmedian[:l]) + np.median(es.fit.histmedian[l:2*l]),
                        -np.median(es.fit.histbest[:l]) + np.median(es.fit.histbest[l:2*l])))
            es.more_to_write += [(10**t if t < 0 else t + 1) for t in tmp] # the latter to get monotonicy

        if 1 < 3:
            # non-user defined, method specific
            # noeffectaxis (CEC: 0.1sigma), noeffectcoord (CEC:0.2sigma), conditioncov
            self._addstop('noeffectcoord',
                         any([es.mean[i] == es.mean[i] + 0.2*es.sigma*sqrt(es.dC[i])
                              for i in xrange(N)]))
            if opts['CMA_diagonal'] is not True and es.countiter > opts['CMA_diagonal']:
                i = es.countiter % N
                self._addstop('noeffectaxis',
                             sum(es.mean == es.mean + 0.1 * es.sigma * es.D[i] * es.B[:, i]) == N)
            self._addstop('conditioncov',
                         es.D[-1] > 1e7 * es.D[0], 1e14)  # TODO

            self._addstop('callback', es.callbackstop)  # termination_callback
        if len(self):
            self._addstop('flat fitness: please (re)consider how to compute the fitness more elaborate',
                         len(es.fit.hist) > 9 and \
                         max(es.fit.hist) == min(es.fit.hist))
        if 11 < 3 and opts['vv'] == 321:
            self._addstop('||xmean||^2<ftarget', sum(es.mean**2) <= opts['ftarget'])

        return self
def _update ( self , es ) : if es . countiter == self . lastiter : if es . countiter == 0 : self . __init__ ( ) return self try : if es == self . es : return self except : # self.es not yet assigned pass self . lastiter = es . countiter self . es = es self . stoplist = [ ] N = es . N opts = es . opts self . opts = opts # a hack to get _addstop going # fitness: generic criterion, user defined w/o default self . _addstop ( 'ftarget' , es . best . f < opts [ 'ftarget' ] ) # maxiter, maxfevals: generic criteria self . _addstop ( 'maxfevals' , es . countevals - 1 >= opts [ 'maxfevals' ] ) self . _addstop ( 'maxiter' , es . countiter >= opts [ 'maxiter' ] ) # tolx, tolfacupx: generic criteria # tolfun, tolfunhist (CEC:tolfun includes hist) self . _addstop ( 'tolx' , all ( [ es . sigma * xi < opts [ 'tolx' ] for xi in es . pc ] ) and all ( [ es . sigma * xi < opts [ 'tolx' ] for xi in sqrt ( es . dC ) ] ) ) self . _addstop ( 'tolfacupx' , any ( [ es . sigma * sig > es . sigma0 * opts [ 'tolfacupx' ] for sig in sqrt ( es . dC ) ] ) ) self . _addstop ( 'tolfun' , es . fit . fit [ - 1 ] - es . fit . fit [ 0 ] < opts [ 'tolfun' ] and max ( es . fit . hist ) - min ( es . fit . hist ) < opts [ 'tolfun' ] ) self . _addstop ( 'tolfunhist' , len ( es . fit . hist ) > 9 and max ( es . fit . hist ) - min ( es . fit . hist ) < opts [ 'tolfunhist' ] ) # worst seen false positive: table N=80,lam=80, getting worse for fevals=35e3 \approx 50 * N**1.5 # but the median is not so much getting worse # / 5 reflects the sparsity of histbest/median # / 2 reflects the left and right part to be compared l = int ( max ( opts [ 'tolstagnation' ] / 5. / 2 , len ( es . fit . histbest ) / 10 ) ) # TODO: why max(..., len(histbest)/10) ??? # TODO: the problem in the beginning is only with best ==> ??? if 11 < 3 : # print ( es . countiter , ( opts [ 'tolstagnation' ] , es . countiter > N * ( 5 + 100 / es . popsize ) , len ( es . fit . histbest ) > 100 , np . median ( es . fit . histmedian [ : l ] ) >= np . median ( es . fit . histmedian [ l : 2 * l ] ) , np . median ( es . fit . histbest [ : l ] ) >= np . median ( es . fit . histbest [ l : 2 * l ] ) ) ) # equality should handle flat fitness self . _addstop ( 'tolstagnation' , # leads sometimes early stop on ftablet, fcigtab, N>=50? 1 < 3 and opts [ 'tolstagnation' ] and es . countiter > N * ( 5 + 100 / es . popsize ) and len ( es . fit . histbest ) > 100 and 2 * l < len ( es . fit . histbest ) and np . median ( es . fit . histmedian [ : l ] ) >= np . median ( es . fit . histmedian [ l : 2 * l ] ) and np . median ( es . fit . histbest [ : l ] ) >= np . median ( es . fit . histbest [ l : 2 * l ] ) ) # iiinteger: stagnation termination can prevent to find the optimum self . _addstop ( 'tolupsigma' , opts [ 'tolupsigma' ] and es . sigma / es . sigma0 / np . max ( es . D ) > opts [ 'tolupsigma' ] ) if 11 < 3 and 2 * l < len ( es . fit . histbest ) : # TODO: this might go wrong, because the nb of written columns changes tmp = np . array ( ( - np . median ( es . fit . histmedian [ : l ] ) + np . median ( es . fit . histmedian [ l : 2 * l ] ) , - np . median ( es . fit . histbest [ : l ] ) + np . median ( es . fit . histbest [ l : 2 * l ] ) ) ) es . more_to_write += [ ( 10 ** t if t < 0 else t + 1 ) for t in tmp ] # the latter to get monotonicy if 1 < 3 : # non-user defined, method specific # noeffectaxis (CEC: 0.1sigma), noeffectcoord (CEC:0.2sigma), conditioncov self . _addstop ( 'noeffectcoord' , any ( [ es . mean [ i ] == es . mean [ i ] + 0.2 * es . sigma * sqrt ( es . dC [ i ] ) for i in xrange ( N ) ] ) ) if opts [ 'CMA_diagonal' ] is not True and es . countiter > opts [ 'CMA_diagonal' ] : i = es . countiter % N self . _addstop ( 'noeffectaxis' , sum ( es . mean == es . mean + 0.1 * es . sigma * es . D [ i ] * es . B [ : , i ] ) == N ) self . _addstop ( 'conditioncov' , es . D [ - 1 ] > 1e7 * es . D [ 0 ] , 1e14 ) # TODO self . _addstop ( 'callback' , es . callbackstop ) # termination_callback if len ( self ) : self . _addstop ( 'flat fitness: please (re)consider how to compute the fitness more elaborate' , len ( es . fit . hist ) > 9 and max ( es . fit . hist ) == min ( es . fit . hist ) ) if 11 < 3 and opts [ 'vv' ] == 321 : self . _addstop ( '||xmean||^2<ftarget' , sum ( es . mean ** 2 ) <= opts [ 'ftarget' ] ) return self