Changeset 737
- Timestamp:
- 07/31/14 11:08:02 (22 months ago)
- Location:
- mystic
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
mystic/examples/test_wavy.py
r729 r737 14 14 from mystic.strategy import Best1Exp, Best1Bin, Rand1Exp 15 15 from mystic.monitors import VerboseMonitor 16 from mystic.tools import getch , reduced16 from mystic.tools import getch 17 17 from numpy import arange 18 18 from mystic.solvers import fmin … … 24 24 from mystic.models import wavy1, wavy2 25 25 wavy = wavy1 26 27 # reduce wavy's multi-valued return28 #@reduced(sum, arraylike=True)29 def cost(x):30 return wavy(x)31 26 32 27 def show(): … … 67 62 solver.SetGenerationMonitor(stepmon) 68 63 #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), \ 70 65 strategy=strategy, CrossProbability=1.0, ScalingFactor=0.9, \ 71 66 sigint_callback = plot_solution) -
mystic/mystic/abstract_solver.py
r727 r737 78 78 import numpy 79 79 from numpy import inf, shape, asarray, absolute, asfarray 80 from mystic.tools import wrap_function, wrap_nested 80 from mystic.tools import wrap_function, wrap_nested, wrap_reducer 81 81 from mystic.tools import wrap_bounds, wrap_penalty, reduced 82 82 … … 146 146 self._constraints = lambda x: x 147 147 self._penalty = lambda x: 0.0 148 self._reducer = (None, False)148 self._reducer = None 149 149 self._cost = (None, None) 150 150 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 ? … … 220 220 (e.g. lambda x,y: x+y), taking two scalars and producing a scalar.""" 221 221 if not reducer: 222 self._reducer = (None, True)222 self._reducer = None 223 223 elif not callable(reducer): 224 224 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 227 229 return 228 230 … … 578 580 cost = wrap_penalty(cost, self._penalty) 579 581 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) 582 585 # hold on to the 'wrapped' cost function 583 586 self._cost = (cost, ExtraArgs) -
mystic/mystic/differential_evolution.py
r729 r737 209 209 cost = wrap_bounds(cost, self._strictMin, self._strictMax) 210 210 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) 213 214 # hold on to the 'wrapped' cost function 214 215 self._cost = (cost, ExtraArgs) … … 360 361 cost = wrap_bounds(cost, self._strictMin, self._strictMax) 361 362 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) 364 366 # hold on to the 'wrapped' cost function 365 367 self._cost = (cost, ExtraArgs) -
mystic/mystic/tools.py
r727 r737 25 25 to a function object 26 26 - wrap_bounds: impose bounds on a function object 27 - wrap_reducer: convert a reducer function to an arraylike interface 27 28 - reduced: apply a reducer function to reduce output to a single value 28 29 - unpair: convert a 1D array of N pairs to two 1D arrays of N values … … 183 184 return target_function(x) 184 185 return function_wrapper 186 187 def wrap_reducer(reducer_function): 188 """convert a reducer function to an arraylike interface 189 190 This is useful for converting a function that used python's 'y = reduce(f, x)' 191 interface 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 185 203 186 204 def reduced(reducer=None, arraylike=False):
Note: See TracChangeset
for help on using the changeset viewer.