<s> """<STR_LIT>""" <EOL> import string <EOL> import sys <EOL> import time <EOL> import warnings <EOL> import numpy as np <EOL> import numpy . random as nr <EOL> from cocopf . minstep import MinimizeStepping <EOL> from cocopf . methods import SteppingData <EOL> class Population : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , fi , K , methods ) : <EOL> self . fi = fi <EOL> self . K = K <EOL> self . methods = methods <EOL> self . points = <NUM_LIT> * np . random . rand ( self . K , self . fi . dim ) - <NUM_LIT> <EOL> self . values = np . zeros ( self . K ) + <NUM_LIT> <EOL> self . minimizers = [ self . _minimizer_make ( i ) for i in range ( self . K ) ] <EOL> self . iters = np . zeros ( self . K , dtype = np . int ) <EOL> self . total_steps = <NUM_LIT:0> <EOL> self . total_iters = <NUM_LIT:0> <EOL> self . data = SteppingData ( self . fi ) <EOL> def _minimizer_make ( self , i ) : <EOL> warnings . simplefilter ( "<STR_LIT:ignore>" ) <EOL> return MinimizeStepping ( self . fi . f . evalfun , self . points [ i ] , <EOL> self . methods [ i % len ( self . methods ) ] ) <EOL> def step_one ( self , i ) : <EOL> """<STR_LIT>""" <EOL> for retry in [ <NUM_LIT:0> , <NUM_LIT:1> ] : <EOL> try : <EOL> self . points [ i ] = self . minimizers [ i ] . next ( ) <EOL> x = self . points [ i ] <EOL> break <EOL> except StopIteration : <EOL> x = self . points [ i ] <EOL> self . restart_one ( i ) <EOL> continue <EOL> y = self . fi . evalfun ( x ) <EOL> self . values [ i ] = y <EOL> self . iters [ i ] += <NUM_LIT:1> <EOL> self . total_steps += <NUM_LIT:1> <EOL> self . data . record ( i , self . minimizers [ i ] . minmethod . name , self . iters [ i ] , self . values [ i ] - self . fi . f . fopt , self . points [ i ] ) <EOL> return ( x , y ) <EOL> def restart_one ( self , i ) : <EOL> """<STR_LIT>""" <EOL> self . points [ i ] = <NUM_LIT> * np . random . rand ( self . fi . dim ) - <NUM_LIT> <EOL> self . values [ i ] = <NUM_LIT> <EOL> self . minimizers [ i ] = self . _minimizer_make ( i ) <EOL> self . iters [ i ] = <NUM_LIT:0> <EOL> def add ( self ) : <EOL> """<STR_LIT>""" <EOL> self . points = np . append ( self . points , [ <NUM_LIT> * np . random . rand ( self . fi . dim ) - <NUM_LIT> ] , axis = <NUM_LIT:0> ) <EOL> self . values = np . append ( self . values , [ <NUM_LIT> ] , axis = <NUM_LIT:0> ) <EOL> i = len ( self . points ) - <NUM_LIT:1> <EOL> self . minimizers . append ( self . _minimizer_make ( i ) ) <EOL> self . iters = np . append ( self . iters , [ <NUM_LIT:0> ] , axis = <NUM_LIT:0> ) <EOL> return i <EOL> def end_iter ( self ) : <EOL> """<STR_LIT>""" <EOL> self . total_iters += <NUM_LIT:1> <EOL> self . data . end_iter ( ) <EOL> def stop ( self ) : <EOL> for m in self . minimizers : <EOL> m . stop ( ) </s>
