<s> """<STR_LIT>""" <EOL> __all__ = [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> import numpy as np <EOL> from . . import stop <EOL> from . . import solver <EOL> from . . util import choose_without_replacement <EOL> CROSSOVER = '<STR_LIT>' , '<STR_LIT>' <EOL> MUTATE = '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' <EOL> def c_exp ( ndim , CR ) : <EOL> """<STR_LIT>""" <EOL> L = min ( abs ( np . random . geometric ( <NUM_LIT:1> - CR ) ) , ndim ) <EOL> idx = np . zeros ( ndim , '<STR_LIT:bool>' ) <EOL> n = np . random . randint ( ndim ) <EOL> idx [ np . arange ( n , n + L ) % ndim ] = True <EOL> return idx <EOL> def c_bin ( ndim , CR ) : <EOL> """<STR_LIT>""" <EOL> n = np . random . randint ( ndim ) <EOL> idx = np . random . rand ( ndim ) < CR <EOL> idx [ n ] = True <EOL> return idx <EOL> def best1 ( F , best , pop , idx , dims ) : <EOL> """<STR_LIT>""" <EOL> r1 , r2 = _candidates ( pop , <NUM_LIT:2> , exclude = idx ) <EOL> return best [ dims ] + F * ( r1 [ dims ] - r2 [ dims ] ) <EOL> def best1u ( F , best , pop , idx , dims ) : <EOL> """<STR_LIT>""" <EOL> r1 , r2 = _candidates ( pop , <NUM_LIT:2> , exclude = idx ) <EOL> return best [ dims ] + F * np . random . rand ( ) * ( r1 [ dims ] - r2 [ dims ] ) <EOL> def best2 ( F , best , pop , idx , dims ) : <EOL> """<STR_LIT>""" <EOL> r1 , r2 , r3 , r4 = _candidates ( pop , <NUM_LIT:4> , exclude = idx ) <EOL> return best [ dims ] + F * ( r1 [ dims ] + r2 [ dims ] - r3 [ dims ] - r4 [ dims ] ) <EOL> def randtobest1 ( F , best , pop , idx , dims ) : <EOL> """<STR_LIT>""" <EOL> r1 , r2 = _candidates ( pop , <NUM_LIT:2> , exclude = idx ) <EOL> return F * ( best [ dims ] - pop [ idx ] [ dims ] + r1 [ dims ] - r2 [ dims ] ) <EOL> def rand1 ( F , best , pop , idx , dims ) : <EOL> """<STR_LIT>""" <EOL> r0 , r1 , r2 = _candidates ( pop , <NUM_LIT:3> , exclude = idx ) <EOL> return r0 [ dims ] + F * ( r1 [ dims ] - r2 [ dims ] ) <EOL> def rand1u ( F , best , pop , idx , dims ) : <EOL> """<STR_LIT>""" <EOL> r0 , r1 , r2 = _candidates ( pop , <NUM_LIT:3> , exclude = idx ) <EOL> return r0 [ dims ] + F * np . random . rand ( ) * ( r1 [ dims ] - r2 [ dims ] ) <EOL> def rand2 ( F , best , pop , idx , dims ) : <EOL> """<STR_LIT>""" <EOL> r0 , r1 , r2 , r3 , r4 = _candidates ( pop , <NUM_LIT:5> , exclude = idx ) <EOL> return r0 [ dims ] + F * ( r1 [ dims ] + r2 [ dims ] - r3 [ dims ] - r4 [ dims ] ) <EOL> def _candidates ( pop , n , exclude = None ) : <EOL> """<STR_LIT>""" <EOL> selection = choose_without_replacement ( len ( pop ) - <NUM_LIT:1> , n ) <EOL> selection [ selection >= exclude ] += <NUM_LIT:1> <EOL> return pop [ selection ] <EOL> class DifferentialEvolution ( solver . Strategy ) : <EOL> """<STR_LIT>""" <EOL> requires = [ ( '<STR_LIT>' , '<STR_LIT>' ) ] <EOL> def __init__ ( self , CR = <NUM_LIT:0.5> , F = <NUM_LIT> , npop = <NUM_LIT:3> , <EOL> crossover = c_exp , mutate = best1u ) : <EOL> self . crossover = crossover <EOL> self . mutate = mutate <EOL> self . CR , self . F = CR , F <EOL> self . npop = npop <EOL> def default_termination_conditions ( self , problem ) : <EOL> success = stop . Cf ( tol = <NUM_LIT> , scaled = False ) <EOL> maxiter = len ( problem . getp ( ) ) * <NUM_LIT:200> <EOL> failure = stop . Steps ( maxiter ) <EOL> return success , failure <EOL> def config_history ( self , history ) : <EOL> """<STR_LIT>""" <EOL> history . requires ( value = <NUM_LIT:1> , population_points = <NUM_LIT:2> , population_values = <NUM_LIT:2> ) <EOL> def start ( self , problem ) : <EOL> """<STR_LIT>""" <EOL> current = problem . getp ( ) <EOL> ndim = len ( current ) <EOL> population = problem . randomize ( self . npop * ndim ) <EOL> population [ <NUM_LIT:0> ] = current <EOL> return population <EOL> def step ( self , history ) : <EOL> """<STR_LIT>""" <EOL> best = history . point [ <NUM_LIT:0> ] <EOL> pop = history . population_points [ <NUM_LIT:0> ] <EOL> pop_size , ndim = pop . shape <EOL> trial = pop . copy ( ) <EOL> for idx , vec in enumerate ( trial ) : <EOL> dims = self . crossover ( ndim , self . CR ) <EOL> vec [ dims ] = self . mutate ( self . F , best , pop , idx , dims ) <EOL> return trial <EOL> def update ( self , history ) : <EOL> """<STR_LIT>""" <EOL> if len ( history . population_points ) > <NUM_LIT:1> : <EOL> oldpop = history . population_points [ <NUM_LIT:1> ] <EOL> oldval = history . population_values [ <NUM_LIT:1> ] <EOL> newpop = history . population_points [ <NUM_LIT:0> ] <EOL> newval = history . population_values [ <NUM_LIT:0> ] <EOL> worse = newval > oldval <EOL> newpop [ worse ] = oldpop [ worse ] <EOL> newval [ worse ] = oldval [ worse ] </s>
