| 1 |
#!/usr/bin/env python |
| 2 |
#----------------------------------------------------------------------------- |
| 3 |
# Model Factory Interface: |
| 4 |
"""NOTES: |
| 5 |
- forward model "forward_poly" calculates a function of x (w/ fixed a,b,c) |
| 6 |
- ForwardPolyFactory is a "function generator", allowing a,b,c to be set |
| 7 |
""" |
| 8 |
def ForwardPolyFactory(params): |
| 9 |
a,b,c = params |
| 10 |
def forward_poly(x): |
| 11 |
""" x should be a 1D (1 by N) numpy array """ |
| 12 |
return array((a*x*x + b*x + c)) |
| 13 |
return forward_poly |
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
#----------------------------------------------------------------------------- |
| 26 |
# Forward Model Invocation: |
| 27 |
"""NOTES: |
| 28 |
- fwd is a instance of "forward_poly", built with chosen a,b,c |
| 29 |
- "data" converts a function of x into a function of a,b,c (w/ fixed x) [i.e. a functor] |
| 30 |
- same methodology is used in COST FUNCTION to produce "goodness of fit" |
| 31 |
""" |
| 32 |
def data(params): |
| 33 |
fwd = ForwardPolyFactory(params) |
| 34 |
x = (array([list(range(101))])-50.)[0] |
| 35 |
return fwd(x) |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
#----------------------------------------------------------------------------- |
| 48 |
# Build "Measured" Data: (optional... use real measured data) |
| 49 |
"""NOTES: |
| 50 |
- target is "target solution" for a,b,c |
| 51 |
- data is used to generate "measured data" (parameters a,b,c = target) |
| 52 |
""" |
| 53 |
target = [1., 2., 1.] |
| 54 |
datapts = data(target) |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
#----------------------------------------------------------------------------- |
| 71 |
# Cost Function Generation: (optional... write your cost function explicitly) |
| 72 |
"""NOTES: |
| 73 |
- F is an instance of Cost Function (goodness of fit) generator |
| 74 |
- myCost is an instance of a Cost Function |
| 75 |
- (default metric) calculates the LeastSquared difference for fwd(x) & datapts |
| 76 |
""" |
| 77 |
x = (array([list(range(101))])-50.)[0] |
| 78 |
F = CostFactory() |
| 79 |
F.addModel(ForwardPolyFactory,3,'poly') |
| 80 |
myCost = F.getCostFunction(evalpts=x, observations=datapts) |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
#----------------------------------------------------------------------------- |
| 94 |
# Call to Solver: |
| 95 |
"""NOTES: |
| 96 |
- solution is set of solved parameters a,b,c |
| 97 |
- stepmon holds a log of optimization steps |
| 98 |
""" |
| 99 |
solution, stepmon = de_solve(myCost) |
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
#----------------------------------------------------------------------------- |
| 107 |
ND = 3 |
| 108 |
NP = 80 |
| 109 |
MAX_GENERATIONS = ND*NP |
| 110 |
#----------------------------------------------------------------------------- |
| 111 |
# Standard "Solver" Configuration: |
| 112 |
"""NOTES: |
| 113 |
- ND is number of parameters (a,b,c) |
| 114 |
- NP is size of trial population |
| 115 |
- MAX_GENERATIONS is maximum optimization iterations |
| 116 |
#----------------------------------------------------------------------------- |
| 117 |
- VerboseMonitor logs/prints "goodness of fit" and "best solution" at each step |
| 118 |
- minrange/maxrange provide box constraints (for parameters a,b,c) |
| 119 |
- SetRandomInitialPoints chooses an initial solution within box constraints |
| 120 |
- SetStrictRanges only allows trial solutions within box constraints |
| 121 |
- 'termination' conditions are to end when "no change" after 300 generations |
| 122 |
- enable_signal_handler allows "interrupt" signal to be caught |
| 123 |
- sigint_callback registers a user-provided function to the signal_handler |
| 124 |
""" |
| 125 |
def de_solve(CF): |
| 126 |
solver = DifferentialEvolutionSolver(ND, NP) |
| 127 |
solver.enable_signal_handler() |
| 128 |
|
| 129 |
stepmon = VerboseMonitor(10,50) |
| 130 |
minrange = [-100., -100., -100.]; maxrange = [100., 100., 100.]; |
| 131 |
solver.SetRandomInitialPoints(min = minrange, max = maxrange) |
| 132 |
solver.SetStrictRanges(min = minrange, max = maxrange) |
| 133 |
solver.SetEvaluationLimits(maxiter=MAX_GENERATIONS) |
| 134 |
solver.SetGenerationMonitor(stepmon) |
| 135 |
|
| 136 |
solver.Solve(CF, ChangeOverGeneration(generations=300),\ |
| 137 |
CrossProbability=0.5, ScalingFactor=0.5,\ |
| 138 |
sigint_callback=plot_sol) |
| 139 |
|
| 140 |
solution = solver.Solution() |
| 141 |
return solution, stepmon |
| 142 |
#----------------------------------------------------------------------------- |
| 143 |
# BONUS... the Callback Function: |
| 144 |
"""NOTES: |
| 145 |
- called on "catch" of signal-interrupt |
| 146 |
- _MUST_ be a function of "params" |
| 147 |
- _only_one_ configuration parameter is (currently) allowed |
| 148 |
""" |
| 149 |
def plot_sol(params,linestyle='b-'): |
| 150 |
x = (array([list(range(101))])-50.)[0] |
| 151 |
d = data(params) |
| 152 |
pylab.plot(x,d,'%s'%linestyle,linewidth=2.0) |
| 153 |
pylab.axis(plotview) |
| 154 |
return |
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
# DONE |