def Step(self, cost=None, ExtraArgs=None, strategy=None, **kwds): """perform a single optimization iteration Note that ExtraArgs should be a *tuple* of extra arguments""" # HACK to enable not explicitly calling _RegisterObjective cost = self._bootstrap_decorate(cost, ExtraArgs) if not len(self._stepmon): # do generation = 0 self.population[0] = asfarray(self.population[0]) # decouple bestSolution from population and bestEnergy from popEnergy self.bestSolution = self.population[0] self.bestEnergy = self.popEnergy[0] for candidate in range(self.nPop): if not len(self._stepmon): # generate trialSolution (within valid range) self.trialSolution[candidate][:] = self.population[candidate] if strategy: # generate trialSolution (within valid range) strategy(self, candidate) # apply constraints self.trialSolution[candidate][:] = self._constraints(self.trialSolution[candidate]) # apply penalty #trialEnergy = map(self._penalty, self.trialSolution)#,**self._mapconfig) # calculate cost trialEnergy = self._map(cost, self.trialSolution, **self._mapconfig) # each trialEnergy should be a scalar if isiterable(trialEnergy[0]) and len(trialEnergy[0]) == 1: trialEnergy = ravel(trialEnergy) # for len(trialEnergy) > 1, will throw ValueError below for candidate in range(self.nPop): if trialEnergy[candidate] < self.popEnergy[candidate]: # New low for this candidate self.popEnergy[candidate] = trialEnergy[candidate] self.population[candidate][:] = self.trialSolution[candidate] self.UpdateGenealogyRecords(candidate, self.trialSolution[candidate][:]) # Check if all-time low if trialEnergy[candidate] < self.bestEnergy: self.bestEnergy = trialEnergy[candidate] self.bestSolution[:] = self.trialSolution[candidate] # log bestSolution and bestEnergy (includes penalty) #FIXME: StepMonitor works for 'pp'? self._stepmon(self.bestSolution[:], self.bestEnergy, self.id) # if savefrequency matches, then save state self._AbstractSolver__save_state() return #XXX: call CheckTermination ?
def _Step(self, cost=None, ExtraArgs=None, **kwds): """perform a single optimization iteration Note that ExtraArgs should be a *tuple* of extra arguments""" # process and activate input settings settings = self._process_inputs(kwds) #(hardwired: due to python3.x exec'ing to locals()) callback = settings['callback'] if 'callback' in settings else None disp = settings['disp'] if 'disp' in settings else False strategy = settings[ 'strategy'] if 'strategy' in settings else self.strategy # HACK to enable not explicitly calling _decorate_objective cost = self._bootstrap_objective(cost, ExtraArgs) init = False # flag to do 0th iteration 'post-initialization' if not len(self._stepmon): # do generation = 0 init = True strategy = None self.population[0] = asfarray(self.population[0]) # decouple bestSolution from population and bestEnergy from popEnergy self.bestSolution = self.population[0] self.bestEnergy = self.popEnergy[0] for candidate in range(self.nPop): if not len(self._stepmon): # generate trialSolution (within valid range) self.trialSolution[candidate][:] = self.population[candidate] if strategy: # generate trialSolution (within valid range) strategy(self, candidate) # apply constraints self.trialSolution[candidate][:] = self._constraints( self.trialSolution[candidate]) # bind constraints to cost #XXX: apparently imposes constraints poorly #concost = wrap_nested(cost, self._constraints) # apply penalty #trialEnergy = map(self._penalty, self.trialSolution)#,**self._mapconfig) # calculate cost trialEnergy = self._map(cost, self.trialSolution, **self._mapconfig) self._fcalls[0] += len(self.trialSolution) #FIXME: manually increment # each trialEnergy should be a scalar if isiterable(trialEnergy[0]) and len(trialEnergy[0]) == 1: trialEnergy = ravel(trialEnergy) # for len(trialEnergy) > 1, will throw ValueError below for candidate in range(self.nPop): if trialEnergy[candidate] < self.popEnergy[candidate]: # New low for this candidate self.popEnergy[candidate] = trialEnergy[candidate] self.population[candidate][:] = self.trialSolution[candidate] self.UpdateGenealogyRecords(candidate, self.trialSolution[candidate][:]) # Check if all-time low if trialEnergy[candidate] < self.bestEnergy: self.bestEnergy = trialEnergy[candidate] self.bestSolution[:] = self.trialSolution[candidate] # log bestSolution and bestEnergy (includes penalty) #FIXME: StepMonitor works for 'pp'? self._stepmon(self.bestSolution[:], self.bestEnergy, self.id) # if savefrequency matches, then save state self._AbstractSolver__save_state() # do callback if callback is not None: callback(self.bestSolution) # initialize termination conditions, if needed if init: self._termination(self) #XXX: at generation 0 or always? return #XXX: call Terminated ?
def _Step(self, cost=None, ExtraArgs=None, **kwds): """perform a single optimization iteration Note that ExtraArgs should be a *tuple* of extra arguments""" # process and activate input settings settings = self._process_inputs(kwds) for key in settings: exec "%s = settings['%s']" % (key,key) # HACK to enable not explicitly calling _decorate_objective cost = self._bootstrap_objective(cost, ExtraArgs) init = False # flag to do 0th iteration 'post-initialization' if not len(self._stepmon): # do generation = 0 init = True strategy = None self.population[0] = asfarray(self.population[0]) # decouple bestSolution from population and bestEnergy from popEnergy self.bestSolution = self.population[0] self.bestEnergy = self.popEnergy[0] for candidate in range(self.nPop): if not len(self._stepmon): # generate trialSolution (within valid range) self.trialSolution[candidate][:] = self.population[candidate] if strategy: # generate trialSolution (within valid range) strategy(self, candidate) # apply constraints self.trialSolution[candidate][:] = self._constraints(self.trialSolution[candidate]) # bind constraints to cost #XXX: apparently imposes constraints poorly #concost = wrap_nested(cost, self._constraints) # apply penalty #trialEnergy = map(self._penalty, self.trialSolution)#,**self._mapconfig) # calculate cost trialEnergy = self._map(cost, self.trialSolution, **self._mapconfig) self._fcalls[0] += len(self.trialSolution) #FIXME: manually increment # each trialEnergy should be a scalar if isiterable(trialEnergy[0]) and len(trialEnergy[0]) == 1: trialEnergy = ravel(trialEnergy) # for len(trialEnergy) > 1, will throw ValueError below for candidate in range(self.nPop): if trialEnergy[candidate] < self.popEnergy[candidate]: # New low for this candidate self.popEnergy[candidate] = trialEnergy[candidate] self.population[candidate][:] = self.trialSolution[candidate] self.UpdateGenealogyRecords(candidate, self.trialSolution[candidate][:]) # Check if all-time low if trialEnergy[candidate] < self.bestEnergy: self.bestEnergy = trialEnergy[candidate] self.bestSolution[:] = self.trialSolution[candidate] # log bestSolution and bestEnergy (includes penalty) #FIXME: StepMonitor works for 'pp'? self._stepmon(self.bestSolution[:], self.bestEnergy, self.id) # if savefrequency matches, then save state self._AbstractSolver__save_state() # do callback if callback is not None: callback(self.bestSolution) # initialize termination conditions, if needed if init: self._termination(self) #XXX: at generation 0 or always? return #XXX: call Terminated ?
def _Step(self, cost=None, ExtraArgs=None, **kwds): """perform a single optimization iteration input:: - cost is the objective function, of the form y = cost(x, *ExtraArgs), where x is a candidate solution, and ExtraArgs is the tuple of positional arguments required to evaluate the objective. note:: ExtraArgs needs to be a *tuple* of extra arguments. This method accepts additional args that are specific for the current solver, as detailed in the `_process_inputs` method. """ # process and activate input settings settings = self._process_inputs(kwds) #(hardwired: due to python3.x exec'ing to locals()) callback = settings['callback'] if 'callback' in settings else None disp = settings['disp'] if 'disp' in settings else False strategy = settings[ 'strategy'] if 'strategy' in settings else self.strategy # HACK to enable not explicitly calling _decorate_objective cost = self._bootstrap_objective(cost, ExtraArgs) init = False # flag to do 0th iteration 'post-initialization' if not len(self._stepmon): # do generation = 0 init = True strategy = None self.population[0] = asfarray(self.population[0]) # decouple bestSolution from population and bestEnergy from popEnergy bs = self.population[0] self.bestSolution = bs.copy() if hasattr(bs, 'copy') else bs[:] self.bestEnergy = self.popEnergy[0] del bs if self._useStrictRange: from mystic.constraints import and_ constraints = and_(self._constraints, self._strictbounds, onfail=self._strictbounds) else: constraints = self._constraints for candidate in range(self.nPop): if not len(self._stepmon): # generate trialSolution (within valid range) self.trialSolution[candidate][:] = self.population[candidate] if strategy: # generate trialSolution (within valid range) strategy(self, candidate) # apply constraints self.trialSolution[candidate][:] = constraints( self.trialSolution[candidate]) # bind constraints to cost #XXX: apparently imposes constraints poorly #concost = wrap_nested(cost, constraints) # apply penalty #trialEnergy = map(self._penalty, self.trialSolution)#,**self._mapconfig) # calculate cost trialEnergy = self._map(cost, self.trialSolution, **self._mapconfig) self._fcalls[0] += len(self.trialSolution) #FIXME: manually increment # each trialEnergy should be a scalar if isiterable(trialEnergy[0]) and len(trialEnergy[0]) == 1: trialEnergy = ravel(trialEnergy) # for len(trialEnergy) > 1, will throw ValueError below for candidate in range(self.nPop): if trialEnergy[candidate] < self.popEnergy[candidate]: # New low for this candidate self.popEnergy[candidate] = trialEnergy[candidate] self.population[candidate][:] = self.trialSolution[candidate] self.UpdateGenealogyRecords(candidate, self.trialSolution[candidate][:]) # Check if all-time low if trialEnergy[candidate] < self.bestEnergy: self.bestEnergy = trialEnergy[candidate] self.bestSolution[:] = self.trialSolution[candidate] # log bestSolution and bestEnergy (includes penalty) #FIXME: StepMonitor works for 'pp'? self._stepmon(self.bestSolution[:], self.bestEnergy, self.id) # if savefrequency matches, then save state self._AbstractSolver__save_state() # do callback if callback is not None: callback(self.bestSolution) # initialize termination conditions, if needed if init: self._termination(self) #XXX: at generation 0 or always? return #XXX: call Terminated ?
def _Step(self, cost=None, ExtraArgs=None, **kwds): """perform a single optimization iteration Note that ExtraArgs should be a *tuple* of extra arguments""" # process and activate input settings settings = self._process_inputs(kwds) for key in settings: exec "%s = settings['%s']" % (key, key) # HACK to enable not explicitly calling _decorate_objective cost = self._bootstrap_objective(cost, ExtraArgs) init = False # flag to do 0th iteration 'post-initialization' if not len(self._stepmon): # do generation = 0 init = True strategy = None self.population[0] = asfarray(self.population[0]) # decouple bestSolution from population and bestEnergy from popEnergy self.bestSolution = self.population[0] self.bestEnergy = self.popEnergy[0] for candidate in range(self.nPop): if not len(self._stepmon): # generate trialSolution (within valid range) self.trialSolution[:] = self.population[candidate] if strategy: # generate trialSolution (within valid range) strategy(self, candidate) # apply constraints self.trialSolution[:] = self._constraints(self.trialSolution) # apply penalty #trialEnergy = self._penalty(self.trialSolution) # calculate cost trialEnergy = cost(self.trialSolution) # trialEnergy should be a scalar if isiterable(trialEnergy) and len(trialEnergy) == 1: trialEnergy = trialEnergy[0] # for len(trialEnergy) > 1, will throw ValueError below if trialEnergy < self.popEnergy[candidate]: # New low for this candidate self.popEnergy[candidate] = trialEnergy self.population[candidate][:] = self.trialSolution self.UpdateGenealogyRecords(candidate, self.trialSolution[:]) # Check if all-time low if trialEnergy < self.bestEnergy: self.bestEnergy = trialEnergy self.bestSolution[:] = self.trialSolution # log bestSolution and bestEnergy (includes penalty) self._stepmon(self.bestSolution[:], self.bestEnergy, self.id) # if savefrequency matches, then save state self._AbstractSolver__save_state() # do callback if callback is not None: callback(self.bestSolution) # initialize termination conditions, if needed if init: self._termination(self) #XXX: at generation 0 or always? return #XXX: call Terminated ?
def _Step(self, cost=None, ExtraArgs=None, **kwds): """perform a single optimization iteration Note that ExtraArgs should be a *tuple* of extra arguments""" # process and activate input settings settings = self._process_inputs(kwds) #(hardwired: due to python3.x exec'ing to locals()) callback = settings['callback'] if 'callback' in settings else None disp = settings['disp'] if 'disp' in settings else False strategy = settings['strategy'] if 'strategy' in settings else self.strategy # HACK to enable not explicitly calling _decorate_objective cost = self._bootstrap_objective(cost, ExtraArgs) init = False # flag to do 0th iteration 'post-initialization' if not len(self._stepmon): # do generation = 0 init = True strategy = None self.population[0] = asfarray(self.population[0]) # decouple bestSolution from population and bestEnergy from popEnergy self.bestSolution = self.population[0] self.bestEnergy = self.popEnergy[0] for candidate in range(self.nPop): if not len(self._stepmon): # generate trialSolution (within valid range) self.trialSolution[:] = self.population[candidate] if strategy: # generate trialSolution (within valid range) strategy(self, candidate) # apply constraints self.trialSolution[:] = self._constraints(self.trialSolution) # apply penalty #trialEnergy = self._penalty(self.trialSolution) # calculate cost trialEnergy = cost(self.trialSolution) # trialEnergy should be a scalar if isiterable(trialEnergy) and len(trialEnergy) == 1: trialEnergy = trialEnergy[0] # for len(trialEnergy) > 1, will throw ValueError below if trialEnergy < self.popEnergy[candidate]: # New low for this candidate self.popEnergy[candidate] = trialEnergy self.population[candidate][:] = self.trialSolution self.UpdateGenealogyRecords(candidate, self.trialSolution[:]) # Check if all-time low if trialEnergy < self.bestEnergy: self.bestEnergy = trialEnergy self.bestSolution[:] = self.trialSolution # log bestSolution and bestEnergy (includes penalty) self._stepmon(self.bestSolution[:], self.bestEnergy, self.id) # if savefrequency matches, then save state self._AbstractSolver__save_state() # do callback if callback is not None: callback(self.bestSolution) # initialize termination conditions, if needed if init: self._termination(self) #XXX: at generation 0 or always? return #XXX: call Terminated ?