Changeset 787
- Timestamp:
- 02/22/15 13:00:28 (15 months ago)
- Location:
- mystic/mystic
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
mystic/mystic/abstract_solver.py
r786 r787 80 80 from mystic.tools import wrap_function, wrap_nested, wrap_reducer 81 81 from mystic.tools import wrap_bounds, wrap_penalty, reduced 82 from klepto import isvalid, validate 82 83 83 84 abs = absolute 84 85 null = lambda x: None 85 86 86 87 class AbstractSolver(object): … … 147 148 self._penalty = lambda x: 0.0 148 149 self._reducer = None 149 self._cost = (None, None) 150 self._cost = (None, None, None) 151 # (cost, raw_cost, args) #,callback) 150 152 self._termination = lambda x, *ar, **kw: False if len(ar) < 1 or ar[0] is False or kw.get('info',True) == False else '' #XXX: better default ? 151 153 # (get termination details with self._termination.__doc__) … … 592 594 return 593 595 594 def SetObjective(self, cost, ExtraArgs=None, **kwds): # callback, fetch ? 595 """set the objective, decorated with bounds, penalties, monitors, etc""" 596 fetch = kwds.get('fetch', False) 597 cost = self._bootstrap_objective(cost, ExtraArgs) 598 return cost if fetch else None 596 def SetObjective(self, cost, ExtraArgs=None): # callback=None/False ? 597 """decorate the cost function with bounds, penalties, monitors, etc""" 598 self._bootstrap_objective(cost, ExtraArgs) # callback=null (see above) 599 return 599 600 600 601 def _decorate_objective(self, cost, ExtraArgs=None): 601 """decorate cost function with bounds, penalties, monitors, etc""" 602 """decorate the cost function with bounds, penalties, monitors, etc""" 603 raw = cost 602 604 if ExtraArgs is None: ExtraArgs = () 603 605 self._fcalls, cost = wrap_function(cost, ExtraArgs, self._evalmon) … … 611 613 #cost = reduced(*self._reducer)(cost) # was self._reducer = (f,bool) 612 614 cost = reduced(self._reducer, arraylike=True)(cost) 613 # hold on to the 'wrapped' cost function614 self._cost = (cost, ExtraArgs)615 # hold on to the 'wrapped' and 'raw' cost function 616 self._cost = (cost, raw, ExtraArgs) 615 617 return cost 616 618 617 619 def _bootstrap_objective(self, cost=None, ExtraArgs=None): 618 620 """HACK to enable not explicitly calling _decorate_objective""" 619 args = None 620 if cost is None: # 'use existing cost' 621 cost,args = self._cost # use args, unless override with ExtraArgs 622 if ExtraArgs is not None: args = ExtraArgs 623 if self._cost[0] is None: # '_decorate_objective not yet called' 624 if args is None: args = () 625 cost = self._decorate_objective(cost, args) 626 return cost 621 _cost,_raw,_args = self._cost 622 # check if need to 'wrap' or can return the stored cost 623 if cost in [None, _raw, _cost] and ExtraArgs in [None, _args]: 624 return _cost 625 # get cost and args if None was given 626 if cost is None: cost = _raw 627 args = _args if ExtraArgs is None else ExtraArgs 628 args = () if args is None else args 629 # quick validation check (so doesn't screw up internals) 630 if not isvalid(cost, [0]*self.nDim, *args): 631 try: name = cost.__name__ 632 except AttributeError: # raise new error for non-callables 633 cost(*args) 634 validate(cost, None, *args) 635 #val = len(args) + 1 #XXX: 'klepto.validate' for better error? 636 #msg = '%s() invalid number of arguments (%d given)' % (name, val) 637 #raise TypeError(msg) 638 # 'wrap' the 'new' cost function with _decorate 639 return self._decorate_objective(cost, args) 640 #XXX: **CAUTION** when _decorate called, solver._fcalls will be reset 627 641 628 642 def _Step(self, cost=None, ExtraArgs=None, **kwds): -
mystic/mystic/differential_evolution.py
r785 r787 203 203 def _decorate_objective(self, cost, ExtraArgs=None): 204 204 """decorate cost function with bounds, penalties, monitors, etc""" 205 raw = cost 205 206 if ExtraArgs is None: ExtraArgs = () 206 207 self._fcalls, cost = wrap_function(cost, ExtraArgs, self._evalmon) … … 213 214 #cost = reduced(*self._reducer)(cost) # was self._reducer = (f,bool) 214 215 cost = reduced(self._reducer, arraylike=True)(cost) 215 # hold on to the 'wrapped' cost function216 self._cost = (cost, ExtraArgs)216 # hold on to the 'wrapped' and 'raw' cost function 217 self._cost = (cost, raw, ExtraArgs) 217 218 return cost 218 219 … … 371 372 def _decorate_objective(self, cost, ExtraArgs=None): 372 373 """decorate cost function with bounds, penalties, monitors, etc""" 374 raw = cost 373 375 if ExtraArgs is None: ExtraArgs = () 374 376 #FIXME: EvaluationMonitor fails for MPI, throws error for 'pp' … … 386 388 #cost = reduced(*self._reducer)(cost) # was self._reducer = (f,bool) 387 389 cost = reduced(self._reducer, arraylike=True)(cost) 388 # hold on to the 'wrapped' cost function389 self._cost = (cost, ExtraArgs)390 # hold on to the 'wrapped' and 'raw' cost function 391 self._cost = (cost, raw, ExtraArgs) 390 392 return cost 391 393
Note: See TracChangeset
for help on using the changeset viewer.