1
2
3 """
4 Bounds contrained Nelder-Mead simplex.
5 """
6
7 import _boxmin
8 import numpy
9
10
12
14 self.ftol = 1.e-18
15 self.xtol = 1.e-18
16 self.maxfun=10000
17 self.maxiter=10000
18
19
20 - def reset(self,p0,scale=0.5):
21 """
22 Reset the simplex to contain Po=[x_1,...,x_n].
23 Scale sets the size of the simplex.
24 """
25 Pvec = numpy.asarray( p0, 'd')
26 _boxmin.amoeba_reset(self, Pvec, scale)
27 return
28
29
32
33
34 - def fit(self,
35 ftol=None,
36 xtol=None,
37 maxiter=None,
38 maxfun=None,
39 restart=0,
40 scale=0.1
41 ):
42 """
43 Find the minimum of the function.
44
45 Halt when the function is flat to within ftol, or itmax iterations
46 have been exceeded.
47 If restart>0, restart the fit that many times basing each new
48 fit off the best value from the last fit. The new simplex is
49 of size scale in [0,1]. Use restart=-1 to run the fit until it
50 stops improving.
51
52 Returns [P, fval, iteration, fcalls]:
53 P: the fit parameter vector
54 fval: the value of the function for the best fit value.
55 iterations: the number of iterations.
56 fcalls: the number of function calls.
57 """
58 if(ftol == None): ftol = self.ftol
59 if(xtol == None): xtol = self.ftol
60 if(maxfun == None): maxfun = self.maxfun
61 if(maxiter == None): maxiter = self.maxiter
62
63
64
65 best = numpy.empty( self.dims+3, 'd')
66 _boxmin.amoeba(self, best, ftol, xtol, maxiter, maxfun)
67
68 while restart!=0:
69 _boxmin.amoeba_reset( self, best, 0.1 )
70 nextbest = numpy.empty( self.dims+3, 'd' )
71 _boxmin.amoeba(self, nextbest, ftol, xtol, maxiter, maxfun)
72 if nextbest[self.dims] < best[self.dims]:
73 best=nextbest
74 restart-=1
75 else:
76 restart=0
77
78 return (best[:-3], best[-3], int(best[-2]), int(best[-1]) )
79
80
82 """
83 Perform a single simplex transformation.
84
85 Returns [P, fvalv, terminate], where
86 P: The parameter vector,
87 fval: The function value at the best vertex,
88 stopFlag: Whether flatness is less than tolerance.
89
90 E.g.::
91
92 for i in range(100):
93 [P, fval, stop] = fit.step()
94 if stop: break
95 """
96 best = numpy.empty( self.dims+1, 'd')
97 ftol = _boxmin.amoeba_step(self, best)
98 return (best[:-1], best[-1], (ftol<self.ftol) )
99
100
101 - def setup(self, func, p0, bounds=[]):
102 """
103 Create an amoeba fit object using function func.
104 Bounds should be a list of values like
105 [lo_1, lo_2, ... lo_n, hi_1, hi_2, ..., hi_n]
106 setting the minimum and maximum for each dimension.
107 """
108 dims = len(p0)
109 self.func = func
110 self.dims = dims
111 self.work = numpy.empty( dims**2+11*dims+4, 'd')
112 self.bounds = numpy.asarray(bounds,'d')
113 self.reset(p0)
114 return
115
116
117
118
119 -def boxmin_amoeba(objfunc,
120 x0,
121 bounds,
122 ftol=1e-10,
123 xtol=1e-10,
124 maxiter=10000,
125 maxfun=10000,
126 restart=0,
127 full_output=1,
128 ):
129 """
130 Minimize a function using the downhill simplex(dhs) algorithm with
131 the box bound constrains.
132
133 Description:
134 Uses a Nelder-Mead simplex(Amoeba) algorithm to find the minimum of
135 function of one or more variables with the [low, high] bound
136
137 Inputs:
138 func: the Python function or method to be minimized.
139 x0: the initial guess
140 bound: the box boundary, it is a list of (lowBounds, highBounds)
141 xtol: acceptable relative error in xopt for convergence.
142 ftol: acceptable relative error in func(xopt) for convergence.
143 maxiter: the maximum number of iterations to perform.
144 maxfun: the maximum number of function evaluations.
145 full_output: non-zero if fval and warnflag outputs are desired.
146
147 Outputs: (xopt, {fopt, iter, funcalls, warnflag})
148 xopt: minimizer of function
149 fopt: value of function at minimum: fopt = func(xopt)
150 iter: number of iterations
151 funcalls: number of function calls
152 """
153 _amoeba = amoeba()
154 _amoeba.setup(objfunc, x0, bounds)
155 [p0, fval, iters, fcalls] = _amoeba.fit(ftol,
156 xtol,
157 maxiter,
158 maxfun,
159 restart,
160 scale=0.1
161 )
162 if full_output==1:
163 return (p0, fval, iters, fcalls)
164 else:
165 return (p0, fval)
166