Changeset 737


Ignore:
Timestamp:
07/31/14 11:08:02 (22 months ago)
Author:
mmckerns
Message:

simplified internal reducer state to always use arraylike; added wrap_reducer

Location:
mystic
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • mystic/examples/test_wavy.py

    r729 r737  
    1414from mystic.strategy import Best1Exp, Best1Bin, Rand1Exp 
    1515from mystic.monitors import VerboseMonitor 
    16 from mystic.tools import getch, reduced 
     16from mystic.tools import getch 
    1717from numpy import arange 
    1818from mystic.solvers import fmin 
    … …  
    2424from mystic.models import wavy1, wavy2 
    2525wavy = wavy1 
    26  
    27 # reduce wavy's multi-valued return 
    28 #@reduced(sum, arraylike=True) 
    29 def cost(x): 
    30     return wavy(x) 
    3126 
    3227def show(): 
    … …  
    6762    solver.SetGenerationMonitor(stepmon) 
    6863   #solver.SetReducer(sum, arraylike=True) # reduce wavy's multi-valued return 
    69     solver.Solve(cost, ChangeOverGeneration(generations=50), \ 
     64    solver.Solve(wavy, ChangeOverGeneration(generations=50), \ 
    7065                 strategy=strategy, CrossProbability=1.0, ScalingFactor=0.9, \ 
    7166                 sigint_callback = plot_solution) 
  • mystic/mystic/abstract_solver.py

    r727 r737  
    7878import numpy 
    7979from numpy import inf, shape, asarray, absolute, asfarray 
    80 from mystic.tools import wrap_function, wrap_nested 
     80from mystic.tools import wrap_function, wrap_nested, wrap_reducer 
    8181from mystic.tools import wrap_bounds, wrap_penalty, reduced 
    8282 
    … …  
    146146        self._constraints     = lambda x: x 
    147147        self._penalty         = lambda x: 0.0 
    148         self._reducer         = (None, False) 
     148        self._reducer         = None 
    149149        self._cost            = (None, None) 
    150150        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 ? 
    … …  
    220220      (e.g. lambda x,y: x+y), taking two scalars and producing a scalar.""" 
    221221        if not reducer: 
    222             self._reducer = (None, True) 
     222            self._reducer = None 
    223223        elif not callable(reducer): 
    224224            raise TypeError, "'%s' is not a callable function" % reducer 
    225         else: #XXX: check for format: x' = reducer(x) ? 
    226             self._reducer = (reducer, arraylike) #XXX: bool(arraylike) ? 
     225        elif not arraylike: 
     226            self._reducer = wrap_reducer(reducer)    
     227        else: #XXX: check if is arraylike? 
     228            self._reducer = reducer 
    227229        return 
    228230 
    … …  
    578580        cost = wrap_penalty(cost, self._penalty) 
    579581        cost = wrap_nested(cost, self._constraints) 
    580         if self._reducer[0]: 
    581             cost = reduced(*self._reducer)(cost) #XXX: decorated? as wrap_*? 
     582        if self._reducer: 
     583           #cost = reduced(*self._reducer)(cost) # was self._reducer = (f,bool) 
     584            cost = reduced(self._reducer, arraylike=True)(cost) 
    582585        # hold on to the 'wrapped' cost function 
    583586        self._cost = (cost, ExtraArgs) 
  • mystic/mystic/differential_evolution.py

    r729 r737  
    209209            cost = wrap_bounds(cost, self._strictMin, self._strictMax) 
    210210        cost = wrap_penalty(cost, self._penalty) 
    211         if self._reducer[0]: 
    212             cost = reduced(*self._reducer)(cost) #XXX: decorated? as wrap_*? 
     211        if self._reducer: 
     212           #cost = reduced(*self._reducer)(cost) # was self._reducer = (f,bool) 
     213            cost = reduced(self._reducer, arraylike=True)(cost) 
    213214        # hold on to the 'wrapped' cost function 
    214215        self._cost = (cost, ExtraArgs) 
    … …  
    360361            cost = wrap_bounds(cost, self._strictMin, self._strictMax) 
    361362        cost = wrap_penalty(cost, self._penalty) 
    362         if self._reducer[0]: 
    363             cost = reduced(*self._reducer)(cost) #XXX: decorated? as wrap_*? 
     363        if self._reducer: 
     364           #cost = reduced(*self._reducer)(cost) # was self._reducer = (f,bool) 
     365            cost = reduced(self._reducer, arraylike=True)(cost) 
    364366        # hold on to the 'wrapped' cost function 
    365367        self._cost = (cost, ExtraArgs) 
  • mystic/mystic/tools.py

    r727 r737  
    2525        to a function object 
    2626    - wrap_bounds: impose bounds on a function object 
     27    - wrap_reducer: convert a reducer function to an arraylike interface 
    2728    - reduced: apply a reducer function to reduce output to a single value 
    2829    - unpair: convert a 1D array of N pairs to two 1D arrays of N values 
    … …  
    183184            return target_function(x) 
    184185    return function_wrapper 
     186 
     187def wrap_reducer(reducer_function): 
     188    """convert a reducer function to an arraylike interface 
     189 
     190This is useful for converting a function that used python's 'y = reduce(f, x)' 
     191interface to an arraylike interface 'y = f(x)'.  Example usage... 
     192    >>> acum = wrap_reduce(numpy.add) 
     193    >>> acum([1,2,3,4]) 
     194    10 
     195    >>> prod = wrap_reduce(lambda x,y: x*y) 
     196    >>> prod([1,2,3,4]) 
     197    24 
     198    """ 
     199    def _reduce(x): # NOTE: not a decorator 
     200        return reduce(reducer_function, x) 
     201    return _reduce 
     202 
    185203 
    186204def reduced(reducer=None, arraylike=False): 
Note: See TracChangeset for help on using the changeset viewer.