Changeset 794
- Timestamp:
- 06/12/15 17:08:44 (11 months ago)
- Location:
- mystic/mystic
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
mystic/mystic/abstract_ensemble_solver.py
r792 r794 255 255 return bool(msg) 256 256 257 def _update_objective(self): 258 """decorate the cost function with bounds, penalties, monitors, etc""" 259 # rewrap the cost if the solver has been run 260 self.Finalize() 261 return 262 257 263 258 264 if __name__=='__main__': -
mystic/mystic/abstract_solver.py
r792 r794 76 76 77 77 78 import random 78 79 import numpy 79 80 from numpy import inf, shape, asarray, absolute, asfarray, seterr … … 155 156 import mystic.termination 156 157 self._EARLYEXIT = mystic.termination.EARLYEXIT 158 self._live = False 157 159 return 158 160 … … 228 230 else: #XXX: check if is arraylike? 229 231 self._reducer = reducer 230 return 232 return self._update_objective() 231 233 232 234 def SetPenalty(self, penalty): … … 247 249 else: #XXX: check for format: y' = penalty(x) ? 248 250 self._penalty = penalty 249 return 251 return self._update_objective() 250 252 251 253 def SetConstraints(self, constraints): … … 263 265 else: #XXX: check for format: x' = constraints(x) ? 264 266 self._constraints = constraints 265 return 267 return self._update_objective() 266 268 267 269 def SetGenerationMonitor(self, monitor, new=False): … … 312 314 if min is False or max is False: 313 315 self._useStrictRange = False 314 return 316 return self._update_objective() 315 317 #XXX: better to use 'defaultMin,defaultMax' or '-inf,inf' ??? 316 318 if min is None: min = self._defaultMin … … 329 331 self._strictMin = min 330 332 self._strictMax = max 331 return 332 333 def _clipGuessWithinRangeBoundary(self, x0 ): #FIXME: use self.trialSolution?333 return self._update_objective() 334 335 def _clipGuessWithinRangeBoundary(self, x0, at=True): 334 336 """ensure that initial guess is set within bounds 335 337 … … 339 341 # raise ValueError, "initial guess must be length %s" % self.nDim 340 342 x0 = asarray(x0) 341 lo = self._strictMin342 hi = self._strictMax343 # c rop x0 at bounds343 bounds = (self._strictMin,self._strictMax) 344 if not len(self._strictMin): return x0 345 # clip x0 at bounds 344 346 settings = numpy.seterr(all='ignore') 345 x0[x0<lo] = lo[x0<lo] 346 x0[x0>hi] = hi[x0>hi] 347 x_ = x0.clip(*bounds) 347 348 numpy.seterr(**settings) 349 if at: return x_ 350 # clip x0 within bounds 351 x_ = x_ != x0 352 x0[x_] = random.uniform(self._strictMin,self._strictMax)[x_] 348 353 return x0 349 354 … … 391 396 if min[i] is None: min[i] = self._defaultMin[0] 392 397 if max[i] is None: max[i] = self._defaultMax[0] 393 import random394 398 #generate random initial values 395 399 for i in range(len(self.population)): … … 591 595 def SetObjective(self, cost, ExtraArgs=None): # callback=None/False ? 592 596 """decorate the cost function with bounds, penalties, monitors, etc""" 593 self._bootstrap_objective(cost, ExtraArgs) # callback=null (see above)594 return595 596 def _decorate_objective(self, cost, ExtraArgs=None):597 """decorate the cost function with bounds, penalties, monitors, etc"""598 raw = cost599 if ExtraArgs is None: ExtraArgs = ()600 self._fcalls, cost = wrap_function(cost, ExtraArgs, self._evalmon)601 if self._useStrictRange:602 for i in range(self.nPop):603 self.population[i] = self._clipGuessWithinRangeBoundary(self.population[i])604 cost = wrap_bounds(cost, self._strictMin, self._strictMax)605 cost = wrap_penalty(cost, self._penalty)606 cost = wrap_nested(cost, self._constraints)607 if self._reducer:608 #cost = reduced(*self._reducer)(cost) # was self._reducer = (f,bool)609 cost = reduced(self._reducer, arraylike=True)(cost)610 # hold on to the 'wrapped' and 'raw' cost function611 self._cost = (cost, raw, ExtraArgs)612 return cost613 614 def _bootstrap_objective(self, cost=None, ExtraArgs=None):615 """HACK to enable not explicitly calling _decorate_objective"""616 597 _cost,_raw,_args = self._cost 617 598 # check if need to 'wrap' or can return the stored cost 618 599 if cost in [None, _raw, _cost] and ExtraArgs in [None, _args]: 619 return _cost600 return 620 601 # get cost and args if None was given 621 602 if cost is None: cost = _raw … … 631 612 #msg = '%s() invalid number of arguments (%d given)' % (name, val) 632 613 #raise TypeError(msg) 614 # hold on to the 'raw' cost function 615 self._cost = (None, cost, ExtraArgs) 616 self._live = False 617 return 618 619 def _update_objective(self): 620 """decorate the cost function with bounds, penalties, monitors, etc""" 621 # rewrap the cost if the solver has been run 622 if False: # trigger immediately 623 self._decorate_objective(*self._cost[1:]) 624 else: # delay update until _bootstrap 625 self.Finalize() 626 return 627 628 def _decorate_objective(self, cost, ExtraArgs=None): 629 """decorate the cost function with bounds, penalties, monitors, etc""" 630 #print ("@", cost, ExtraArgs, max) 631 raw = cost 632 if ExtraArgs is None: ExtraArgs = () 633 self._fcalls, cost = wrap_function(cost, ExtraArgs, self._evalmon) 634 if self._useStrictRange: 635 indx = list(self.popEnergy).index(self.bestEnergy) 636 ngen = self.generations #XXX: no random if generations=0 ? 637 for i in range(self.nPop): 638 self.population[i] = self._clipGuessWithinRangeBoundary(self.population[i], (not ngen) or (i is indx)) 639 cost = wrap_bounds(cost, self._strictMin, self._strictMax) 640 cost = wrap_penalty(cost, self._penalty) 641 cost = wrap_nested(cost, self._constraints) 642 if self._reducer: 643 #cost = reduced(*self._reducer)(cost) # was self._reducer = (f,bool) 644 cost = reduced(self._reducer, arraylike=True)(cost) 645 # hold on to the 'wrapped' and 'raw' cost function 646 self._cost = (cost, raw, ExtraArgs) 647 self._live = True 648 return cost 649 650 def _bootstrap_objective(self, cost=None, ExtraArgs=None): 651 """HACK to enable not explicitly calling _decorate_objective""" 652 _cost,_raw,_args = self._cost 653 # check if need to 'wrap' or can return the stored cost 654 if cost in [None, _raw, _cost] and ExtraArgs in [None, _args] \ 655 and self._live: 656 return _cost 633 657 # 'wrap' the 'new' cost function with _decorate 634 return self._decorate_objective(cost, args) 635 #XXX: **CAUTION** when _decorate called, solver._fcalls will be reset 658 self.SetObjective(cost, ExtraArgs) 659 return self._decorate_objective(*self._cost[1:]) 660 #XXX: when _decorate called, solver._fcalls will be reset ? 636 661 637 662 def _Step(self, cost=None, ExtraArgs=None, **kwds): … … 681 706 return 682 707 683 def _Finalize(self, **kwds):708 def Finalize(self, **kwds): 684 709 """cleanup upon exiting the main optimization loop""" 685 pass 710 self._live = False 711 return 686 712 687 713 def _process_inputs(self, kwds): … … 732 758 If the algorithm does not meet the given termination conditions after 733 759 the call to "Step", the solver may be left in an "out-of-sync" state. 734 When abandoning an non-terminated solver, one should call " _Finalize"760 When abandoning an non-terminated solver, one should call "Finalize" 735 761 to make sure the solver is fully returned to a "synchronized" state. 736 762 … … 753 779 self._Step(**kwds) #FIXME: not all kwds are given in __doc__ 754 780 if self.Terminated(): # then cleanup/finalize 755 self. _Finalize()781 self.Finalize() 756 782 757 783 # get termination message and log state -
mystic/mystic/differential_evolution.py
r793 r794 201 201 return 202 202 203 def SetConstraints(self, constraints): 204 """apply a constraints function to the optimization 205 206 input:: 207 - a constraints function of the form: xk' = constraints(xk), 208 where xk is the current parameter vector. Ideally, this function 209 is constructed so the parameter vector it passes to the cost function 210 will satisfy the desired (i.e. encoded) constraints.""" 211 if not constraints: 212 self._constraints = lambda x: x 213 elif not callable(constraints): 214 raise TypeError, "'%s' is not a callable function" % constraints 215 else: #XXX: check for format: x' = constraints(x) ? 216 self._constraints = constraints 217 return # doesn't use wrap_nested 218 203 219 def _decorate_objective(self, cost, ExtraArgs=None): 204 220 """decorate cost function with bounds, penalties, monitors, etc""" 221 #print ("@", cost, ExtraArgs, max) 205 222 raw = cost 206 223 if ExtraArgs is None: ExtraArgs = () 207 224 self._fcalls, cost = wrap_function(cost, ExtraArgs, self._evalmon) 208 225 if self._useStrictRange: 226 indx = list(self.popEnergy).index(self.bestEnergy) 227 ngen = self.generations #XXX: no random if generations=0 ? 209 228 for i in range(self.nPop): 210 self.population[i] = self._clipGuessWithinRangeBoundary(self.population[i] )229 self.population[i] = self._clipGuessWithinRangeBoundary(self.population[i], (not ngen) or (i is indx)) 211 230 cost = wrap_bounds(cost, self._strictMin, self._strictMax) 212 231 cost = wrap_penalty(cost, self._penalty) … … 216 235 # hold on to the 'wrapped' and 'raw' cost function 217 236 self._cost = (cost, raw, ExtraArgs) 237 self._live = True 218 238 return cost 219 239 … … 221 241 """perform a single optimization iteration 222 242 Note that ExtraArgs should be a *tuple* of extra arguments""" 223 # HACK to enable not explicitly calling _decorate_objective224 cost = self._bootstrap_objective(cost, ExtraArgs)225 243 # process and activate input settings 226 244 settings = self._process_inputs(kwds) 227 245 for key in settings: 228 246 exec "%s = settings['%s']" % (key,key) 247 248 # HACK to enable not explicitly calling _decorate_objective 249 cost = self._bootstrap_objective(cost, ExtraArgs) 229 250 230 251 init = False # flag to do 0th iteration 'post-initialization' … … 372 393 return 373 394 395 def SetConstraints(self, constraints): 396 """apply a constraints function to the optimization 397 398 input:: 399 - a constraints function of the form: xk' = constraints(xk), 400 where xk is the current parameter vector. Ideally, this function 401 is constructed so the parameter vector it passes to the cost function 402 will satisfy the desired (i.e. encoded) constraints.""" 403 if not constraints: 404 self._constraints = lambda x: x 405 elif not callable(constraints): 406 raise TypeError, "'%s' is not a callable function" % constraints 407 else: #XXX: check for format: x' = constraints(x) ? 408 self._constraints = constraints 409 return # doesn't use wrap_nested 410 374 411 def _decorate_objective(self, cost, ExtraArgs=None): 375 412 """decorate cost function with bounds, penalties, monitors, etc""" 413 #print ("@", cost, ExtraArgs, max) 376 414 raw = cost 377 415 if ExtraArgs is None: ExtraArgs = () … … 384 422 fcalls, cost = wrap_function(cost, ExtraArgs, evalmon) 385 423 if self._useStrictRange: 424 indx = list(self.popEnergy).index(self.bestEnergy) 425 ngen = self.generations #XXX: no random if generations=0 ? 386 426 for i in range(self.nPop): 387 self.population[i] = self._clipGuessWithinRangeBoundary(self.population[i] )427 self.population[i] = self._clipGuessWithinRangeBoundary(self.population[i], (not ngen) or (i is indx)) 388 428 cost = wrap_bounds(cost, self._strictMin, self._strictMax) 389 429 cost = wrap_penalty(cost, self._penalty) … … 393 433 # hold on to the 'wrapped' and 'raw' cost function 394 434 self._cost = (cost, raw, ExtraArgs) 435 self._live = True 395 436 return cost 396 437 … … 398 439 """perform a single optimization iteration 399 440 Note that ExtraArgs should be a *tuple* of extra arguments""" 400 # HACK to enable not explicitly calling _decorate_objective401 cost = self._bootstrap_objective(cost, ExtraArgs)402 441 # process and activate input settings 403 442 settings = self._process_inputs(kwds) 404 443 for key in settings: 405 444 exec "%s = settings['%s']" % (key,key) 445 446 # HACK to enable not explicitly calling _decorate_objective 447 cost = self._bootstrap_objective(cost, ExtraArgs) 406 448 407 449 init = False # flag to do 0th iteration 'post-initialization' -
mystic/mystic/scipy_optimize.py
r792 r794 63 63 64 64 65 from mystic.tools import unpair 65 from mystic.tools import wrap_function, unpair, wrap_nested 66 from mystic.tools import wrap_bounds, wrap_penalty, reduced 66 67 67 68 import numpy … … 142 143 return 143 144 145 def _decorate_objective(self, cost, ExtraArgs=None): 146 """decorate the cost function with bounds, penalties, monitors, etc""" 147 #print ("@", cost, ExtraArgs, max) 148 raw = cost 149 if ExtraArgs is None: ExtraArgs = () 150 self._fcalls, cost = wrap_function(cost, ExtraArgs, self._evalmon) 151 if self._useStrictRange: 152 if self.generations: 153 #NOTE: pop[0] was best, may not be after resetting simplex 154 for i,j in enumerate(self._setSimplexWithinRangeBoundary()): 155 self.population[i+1] = self.population[0].copy() 156 self.population[i+1][i] = j 157 else: 158 self.population[0] = self._clipGuessWithinRangeBoundary(self.population[0]) 159 cost = wrap_bounds(cost, self._strictMin, self._strictMax) 160 cost = wrap_penalty(cost, self._penalty) 161 cost = wrap_nested(cost, self._constraints) 162 if self._reducer: 163 #cost = reduced(*self._reducer)(cost) # was self._reducer = (f,bool) 164 cost = reduced(self._reducer, arraylike=True)(cost) 165 # hold on to the 'wrapped' and 'raw' cost function 166 self._cost = (cost, raw, ExtraArgs) 167 self._live = True 168 return cost 169 144 170 def _Step(self, cost=None, ExtraArgs=None, **kwds): 145 171 """perform a single optimization iteration 146 172 Note that ExtraArgs should be a *tuple* of extra arguments""" 147 # HACK to enable not explicitly calling _decorate_objective148 cost = self._bootstrap_objective(cost, ExtraArgs)149 173 # process and activate input settings 150 174 settings = self._process_inputs(kwds) 151 175 for key in settings: 152 176 exec "%s = settings['%s']" % (key,key) 177 178 # HACK to enable not explicitly calling _decorate_objective 179 cost = self._bootstrap_objective(cost, ExtraArgs) 153 180 154 181 rho = 1; chi = 2; psi = 0.5; sigma = 0.5; … … 466 493 """perform a single optimization iteration 467 494 Note that ExtraArgs should be a *tuple* of extra arguments""" 468 # HACK to enable not explicitly calling _decorate_objective469 cost = self._bootstrap_objective(cost, ExtraArgs)470 495 # process and activate input settings 471 496 settings = self._process_inputs(kwds) 472 497 for key in settings: 473 498 exec "%s = settings['%s']" % (key,key) 499 500 # HACK to enable not explicitly calling _decorate_objective 501 cost = self._bootstrap_objective(cost, ExtraArgs) 474 502 475 503 direc = self._direc #XXX: throws Error if direc=None after generation=0 … … 575 603 return #XXX: call Terminated ? 576 604 577 def _Finalize(self, **kwds):605 def Finalize(self, **kwds): 578 606 """cleanup upon exiting the main optimization loop""" 579 self.energy_history = None # resync with 'best' energy 580 self._stepmon(self.bestSolution, self.bestEnergy, self.id) 581 # if savefrequency matches, then save state 582 self._AbstractSolver__save_state() 607 if self.energy_history != None: 608 self.energy_history = None # resync with 'best' energy 609 self._stepmon(self.bestSolution, self.bestEnergy, self.id) 610 # if savefrequency matches, then save state 611 self._AbstractSolver__save_state() 612 self._live = False 583 613 return 584 614
Note: See TracChangeset
for help on using the changeset viewer.