Changeset 787


Ignore:
Timestamp:
02/22/15 13:00:28 (15 months ago)
Author:
mmckerns
Message:

remove return cost from SetObjective?; enable change cost and args in Step
as a byproduct, _cost now stores both wrapped and raw cost functions

Location:
mystic/mystic
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • mystic/mystic/abstract_solver.py

    r786 r787  
    8080from mystic.tools import wrap_function, wrap_nested, wrap_reducer 
    8181from mystic.tools import wrap_bounds, wrap_penalty, reduced 
     82from klepto import isvalid, validate 
    8283 
    8384abs = absolute 
    84  
     85null = lambda x: None 
    8586 
    8687class AbstractSolver(object): 
     
    147148        self._penalty         = lambda x: 0.0 
    148149        self._reducer         = None 
    149         self._cost            = (None, None) 
     150        self._cost            = (None, None, None) 
     151        #                       (cost, raw_cost, args) #,callback) 
    150152        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 ? 
    151153        # (get termination details with self._termination.__doc__) 
     
    592594        return 
    593595 
    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 
    599600 
    600601    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 
    602604        if ExtraArgs is None: ExtraArgs = () 
    603605        self._fcalls, cost = wrap_function(cost, ExtraArgs, self._evalmon) 
     
    611613           #cost = reduced(*self._reducer)(cost) # was self._reducer = (f,bool) 
    612614            cost = reduced(self._reducer, arraylike=True)(cost) 
    613         # hold on to the 'wrapped' cost function 
    614         self._cost = (cost, ExtraArgs) 
     615        # hold on to the 'wrapped' and 'raw' cost function 
     616        self._cost = (cost, raw, ExtraArgs) 
    615617        return cost 
    616618 
    617619    def _bootstrap_objective(self, cost=None, ExtraArgs=None): 
    618620        """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 
    627641 
    628642    def _Step(self, cost=None, ExtraArgs=None, **kwds): 
  • mystic/mystic/differential_evolution.py

    r785 r787  
    203203    def _decorate_objective(self, cost, ExtraArgs=None): 
    204204        """decorate cost function with bounds, penalties, monitors, etc""" 
     205        raw = cost 
    205206        if ExtraArgs is None: ExtraArgs = () 
    206207        self._fcalls, cost = wrap_function(cost, ExtraArgs, self._evalmon) 
     
    213214           #cost = reduced(*self._reducer)(cost) # was self._reducer = (f,bool) 
    214215            cost = reduced(self._reducer, arraylike=True)(cost) 
    215         # hold on to the 'wrapped' cost function 
    216         self._cost = (cost, ExtraArgs) 
     216        # hold on to the 'wrapped' and 'raw' cost function 
     217        self._cost = (cost, raw, ExtraArgs) 
    217218        return cost 
    218219 
     
    371372    def _decorate_objective(self, cost, ExtraArgs=None): 
    372373        """decorate cost function with bounds, penalties, monitors, etc""" 
     374        raw = cost 
    373375        if ExtraArgs is None: ExtraArgs = () 
    374376       #FIXME: EvaluationMonitor fails for MPI, throws error for 'pp' 
     
    386388           #cost = reduced(*self._reducer)(cost) # was self._reducer = (f,bool) 
    387389            cost = reduced(self._reducer, arraylike=True)(cost) 
    388         # hold on to the 'wrapped' cost function 
    389         self._cost = (cost, ExtraArgs) 
     390        # hold on to the 'wrapped' and 'raw' cost function 
     391        self._cost = (cost, raw, ExtraArgs) 
    390392        return cost 
    391393 
Note: See TracChangeset for help on using the changeset viewer.