16 for (i = 0; i < nvars; i++) z[i] = point[i];
17 for (i = 0; i < nvars; i++) {
18 if(
Exit)
return (minf);
19 z[i] = point[i] + delta[i];
20 ftmp = ObjectWrapper(nvars, z);
21 if (ftmp < minf) minf = ftmp;
23 delta[i] = 0.0L - delta[i];
24 z[i] = point[i] + delta[i];
25 ftmp = ObjectWrapper(nvars, z);
26 if (ftmp < minf) minf = ftmp;
30 for (i = 0; i < nvars; i++) point[i] = z[i];
34 int HookeJeeves::hooke(
int nvars,
dREAL* startpt,
dREAL* endpt,
dREAL rho,
dREAL epsilon,
int itermax) {
36 dREAL newf, fbefore, steplength, tmp;
37 dREAL xbefore[nvars], newx[nvars];
40 for (i = 0; i < nvars; i++) {
41 newx[i] = xbefore[i] = startpt[i];
42 delta[i] = std::fabs(startpt[i] * rho);
43 if (delta[i] == 0.0) delta[i] = rho;
48 fbefore = ObjectWrapper(nvars, newx);
50 while ((iters < itermax) && (steplength > epsilon)) {
51 if(
Exit)
return (iters);
55 for (i = 0; i < nvars; i++) {
58 newf = best_nearby(delta, newx, fbefore, nvars);
61 while ((newf < fbefore) && (keep == 1)) {
62 if(
Exit)
return (iters);
64 for (i = 0; i < nvars; i++) {
66 if (newx[i] <= xbefore[i]) delta[i] = 0.0 - std::fabs(delta[i]);
67 else delta[i] = std::fabs(delta[i]);
71 newx[i] = newx[i] + newx[i] - tmp;
74 newf = best_nearby(delta, newx, fbefore, nvars);
76 if (newf >= fbefore)
break;
82 for (i = 0; i < nvars; i++) {
84 if (std::fabs(newx[i] - xbefore[i]) > (0.5 * std::fabs(delta[i])))
break;
88 if ((steplength >= epsilon) && (newf >= fbefore)) {
89 steplength = steplength * rho;
90 for (i = 0; i < nvars; i++) {
95 for (i = 0; i < nvars; i++) endpt[i] = xbefore[i];
99 dREAL HookeJeeves::ObjectWrapper(
int nvars,
dREAL* x) {
100 for(
int i=0; i<nvars; i++) {
101 if(x[i]<LowerBound[i] || (UpperBound[i]>0 && x[i]>UpperBound[i]))
return 1.E101L;
103 return ObjectFunction(nvars, x, PL, LAS);
107 ObjectFunction = func;
111 if(UB != NULL)
for(
int i=0; i<nvars; i++) UpperBound[i] = UB[i];
112 else for(
int i=0; i<nvars; i++) UpperBound[i] = 1;
114 if(LB != NULL)
for(
int i=0; i<nvars; i++) LowerBound[i] = LB[i];
115 else for(
int i=0; i<nvars; i++) LowerBound[i] = 0;
117 dREAL RhoParameter = 0.95;
118 dREAL EpsParameter = 1E-4;
119 size_t MaxParameter = 100000;
121 if(SavePTS<=0) SavePTS = 1;
122 if(TryPTS<=0) TryPTS= 1;
123 double mPoints[SavePTS][nvars], mValue[SavePTS];
124 for(
int i=0; i<SavePTS; i++) mValue[i] = 1E5;
128 pts[TryPTS] = 1-1E-4;
129 for(
int i=1; i<TryPTS; i++) pts[i] = i*1.0/TryPTS;
130 for(
size_t ii=0; ii<std::pow(TryPTS+1, nvars); ii++) {
131 dREAL iPoints[nvars];
133 for(
int i=0; i<nvars; i++) {
134 int mi = li % (1+TryPTS);
135 iPoints[i] = LowerBound[i] + pts[mi]*(UpperBound[i]-LowerBound[i]);
138 auto tmp = ObjectWrapper(nvars, iPoints);
139 if(mValue[max_index] > tmp) {
140 mValue[max_index] = tmp;
141 for(
int j=0; j<nvars; j++) mPoints[max_index][j] = iPoints[j];
143 for(
int j=0; j<SavePTS && j<std::pow(TryPTS+1, nvars); j++) {
144 if(mValue[j] > mValue[max_index]) max_index = j;
150 for(
int ii=0; ii<SavePTS && ii<std::pow(TryPTS+1, nvars); ii++) {
151 dREAL iPoints[nvars], oPoints[nvars];
152 for(
int i=0; i<nvars; i++) iPoints[i] = mPoints[ii][i];
153 hooke(nvars, iPoints, oPoints, RhoParameter, EpsParameter, MaxParameter);
154 auto tmp_ret = ObjectWrapper(nvars, oPoints);
155 if(tmp_ret < ret) ret = tmp_ret;
166 ObjectFunction = func;
170 for(
int i=0; i<nvars; i++) UpperBound[i] = -1;
171 for(
int i=0; i<nvars; i++) LowerBound[i] = 0;
173 dREAL EpsParameter = 1E-3;
174 size_t MaxParameter = 100000;
176 dREAL iPoints[nvars], oPoints[nvars];
177 for(
int i=0; i<nvars; i++) {
179 if(ip[i] * 1E-3 > EpsParameter) EpsParameter = ip[i] * 1E-3;
181 hooke(nvars, iPoints, oPoints,
ErrMin::hjRHO, EpsParameter, MaxParameter);
virtual void Minimize(int nvars, FunctionType func, dREAL *ip) override
virtual dREAL FindMinimum(int nvars, FunctionType func, dREAL *PL=NULL, dREAL *las=NULL, dREAL *UB=NULL, dREAL *LB=NULL, dREAL *IP=NULL, bool compare0=false, int TryPTS=0, int SavePTS=0) override
virtual void ForceStop() override
namespace for Numerical integration with Sector Decomposition method