49 int verbose=0;
if(status!=NULL) {verbose=status->
verbose; fp=status->
fp;}
50 if(verbose>1) fprintf(fp,
"%s(NLOPT, %d, status)\n", __func__, maxIter);
61 for(
unsigned int i=0; i<nlo->
totalNr; i++)
63 !isfinite(nlo->
xfull[i]) || !isfinite(nlo->
xlower[i]) || !isfinite(nlo->
xupper[i]))
66 fprintf(fp,
"parameter %d failed: %e <= %e <= %e\n",
76 if(verbose>2) fprintf(fp,
"going for one-dimensional method.\n");
77 return(
nlopt1D(nlo, maxIter, NULL));
81 if(verbose>10) fprintf(fp,
"Simplex initialization\n");
82 unsigned int parNr=nlo->
totalNr;
83 unsigned int sn=parNr+1;
84 double simplexP[sn+2][parNr];
85 double simplexC[parNr];
86 double simplexR[sn+2];
87 for(
unsigned int i=0; i<(sn+2); i++) simplexR[i]=nan(
"");
88 if(verbose>16) fprintf(fp,
"simplexR[0..%d]\n", sn+2);
89 unsigned int newPnt=sn;
90 unsigned int newPnt2=newPnt+1;
92 if(maxIter<2) maxIter=500;
95 for(
unsigned int j=0; j<sn; j++)
96 for(
unsigned int i=0; i<parNr; i++)
97 simplexP[j][i]=nlo->
xfull[i];
99 for(
unsigned int j=1; j<sn; j++) {
100 for(
unsigned int i=0; i<parNr; i++) {
101 simplexP[j][i] = simplexP[j-1][i];
103 simplexP[j][i] += nlo->
xdelta[i];
104 if(simplexP[j][i]<nlo->
xlower[i] || simplexP[j][i]>nlo->
xupper[i]) {
106 simplexP[j][i] -= 2.0*nlo->
xdelta[i];
108 if(simplexP[j][i]<nlo->
xlower[i] || simplexP[j][i]>nlo->
xupper[i]) {
111 fprintf(fp,
"failed limits %e,%e or delta: %e\n", nlo->
xlower[i], nlo->
xupper[i], nlo->
xdelta[i]);
122 for(
unsigned int j=0; j<sn; j++) {
124 fprintf(fp,
"x[%d][]=", j);
125 for(
unsigned int i=0; i<parNr; i++) fprintf(fp,
"%g ", simplexP[j][i]);
128 simplexR[j] = (*nlo->
_fun)(parNr, simplexP[j], nlo->
fundata);
130 if(verbose>12) fprintf(fp,
" R[%d]=%g\n", j, simplexR[j]);
132 int ret=
nloptAddP(nlo, simplexP[j], simplexR[j]);
134 statusSet(status, __func__, __FILE__, __LINE__, ret);
139 double initialR=simplexR[0];
142 unsigned int best=0, nextBest=0, worst=0;
144 double max, min, min2;
145 max=min=simplexR[0]; best=worst=0;
146 for(
unsigned int j=1; j<sn; j++) {
147 if(!isfinite(max) || simplexR[j] > max) {max=simplexR[j]; worst=j;}
148 if(!isfinite(min) || simplexR[j] < min) {min=simplexR[j]; best=j; }
151 min2=max; nextBest=worst;
152 for(
unsigned int j=0; j<sn; j++)
153 if(j!=best && (simplexR[j] < min2)) {min2=simplexR[j]; nextBest=j;}
155 fprintf(fp,
" best := %g (%d)\n", min, best);
156 fprintf(fp,
" next best := %g (%d)\n", min2, nextBest);
157 fprintf(fp,
" worst := %g (%d)\n", max, worst);
162 if(verbose>10) fprintf(fp,
"Simplex iterations\n");
163 double prevBestR=simplexR[best];
164 unsigned int iterNr=0, stopTolerance=0, stopR=0;
167 if(verbose>13) {fprintf(fp,
"\niteration := %d\n", iterNr); fflush(fp);}
169 for(
unsigned int j=0; j<sn; j++) {
170 fprintf(fp,
"simplexP[%d][]=", j);
171 for(
unsigned int i=0; i<parNr; i++) fprintf(fp,
"%g ", simplexP[j][i]);
172 fprintf(fp,
" --> %g ", simplexR[j]);
173 if(j==best) fprintf(fp,
" (best)\n");
174 else if(j==nextBest) fprintf(fp,
" (next best)\n");
175 else if(j==worst) fprintf(fp,
" (worst)\n");
176 else fprintf(fp,
"\n");
181 for(
unsigned int i=0; i<parNr; i++) {
182 simplexC[i]=0.0;
unsigned int n=0;
183 for(
unsigned int j=0; j<sn; j++)
184 if(j!=worst && isfinite(simplexP[j][i])) {simplexC[i]+=simplexP[j][i]; n++;}
185 simplexC[i]/=(double)(n);
189 fprintf(fp,
"centroid x[]=");
190 for(
unsigned int i=0; i<parNr; i++) fprintf(fp,
"%g ", simplexC[i]);
191 fprintf(fp,
"\n"); fflush(fp);
198 for(k=0; k<parNr; k++) {
199 if(fabs(simplexC[k]-simplexP[worst][k])>0.5*nlo->
xtol[k])
break;
200 if(fabs(simplexC[k]-simplexP[best][k])>0.5*nlo->
xtol[k])
break;
201 if(fabs(simplexC[k]-simplexP[nextBest][k])>0.5*nlo->
xtol[k])
break;
203 if(k==parNr) stopTolerance++;
else stopTolerance=0;
206 if(stopTolerance>=parNr && stopR>=parNr) {
207 if(verbose>2) {fprintf(fp,
" stopping because simplex is not improving.\n"); fflush(fp);}
212 for(
unsigned int i=0; i<parNr; i++)
213 simplexP[newPnt][i] = simplexC[i] + f*(simplexC[i]-simplexP[worst][i]);
216 simplexR[newPnt] = (*nlo->
_fun)(parNr, simplexP[newPnt], nlo->
fundata);
219 fprintf(fp,
"new point x[%d][]=", newPnt);
220 for(
unsigned int i=0; i<parNr; i++) fprintf(fp,
"%g ", simplexP[newPnt][i]);
221 fprintf(fp,
"\n -> R[%d]=%g\n", newPnt, simplexR[newPnt]);
224 int ret=
nloptAddP(nlo, simplexP[newPnt], simplexR[newPnt]);
228 if(simplexR[newPnt] < simplexR[best]) {
229 if(verbose>14) fprintf(fp,
" new is better than best\n");
232 for(
unsigned int i=0; i<parNr; i++)
233 simplexP[newPnt2][i] = simplexC[i] + f*(simplexC[i]-simplexP[worst][i]);
235 simplexR[newPnt2] = (*nlo->
_fun)(parNr, simplexP[newPnt2], nlo->
fundata);
238 int ret=
nloptAddP(nlo, simplexP[newPnt2], simplexR[newPnt2]);
241 if(simplexR[newPnt2] < simplexR[newPnt]) {
242 if(verbose>14) fprintf(fp,
" expanded (%g) is better than new\n", simplexR[newPnt2]);
243 for(
unsigned int i=0; i<parNr; i++) simplexP[worst][i]=simplexP[newPnt2][i];
244 simplexR[worst]=simplexR[newPnt2];
246 if(verbose>14) fprintf(fp,
" expanded (%g) is no better than new\n", simplexR[newPnt2]);
247 for(
unsigned int i=0; i<parNr; i++) simplexP[worst][i]=simplexP[newPnt][i];
248 simplexR[worst]=simplexR[newPnt];
250 }
else if(!isfinite(simplexR[newPnt]) || simplexR[newPnt] > simplexR[worst]) {
251 if(verbose>14) fprintf(fp,
" new is worse than worst\n");
255 for(
unsigned int i=0; i<parNr; i++)
256 simplexP[newPnt2][i] = simplexC[i] + f*(simplexC[i]-simplexP[worst][i]);
258 simplexR[newPnt2] = (*nlo->
_fun)(parNr, simplexP[newPnt2], nlo->
fundata);
261 int ret=
nloptAddP(nlo, simplexP[newPnt2], simplexR[newPnt2]);
264 if(simplexR[newPnt2] < simplexR[newPnt] && isfinite(simplexR[newPnt2])) {
265 if(verbose>14) fprintf(fp,
" halfway (%g) is better than new\n", simplexR[newPnt2]);
266 for(
unsigned int i=0; i<parNr; i++) simplexP[worst][i]=simplexP[newPnt2][i];
267 simplexR[worst]=simplexR[newPnt2];
268 }
else if(isfinite(simplexR[newPnt])) {
269 if(verbose>14) fprintf(fp,
" halfway (%g) is no better than new\n", simplexR[newPnt2]);
270 for(
unsigned int i=0; i<parNr; i++) simplexP[worst][i]=simplexP[newPnt][i];
271 simplexR[worst]=simplexR[newPnt];
273 }
else if(simplexR[newPnt] > simplexR[nextBest]) {
274 if(verbose>14) fprintf(fp,
" new is worse than next best\n");
278 for(
unsigned int i=0; i<parNr; i++)
279 simplexP[newPnt2][i] = simplexC[i] + f*(simplexP[newPnt][i]-simplexC[i]);
281 simplexR[newPnt2] = (*nlo->
_fun)(parNr, simplexP[newPnt2], nlo->
fundata);
284 int ret=
nloptAddP(nlo, simplexP[newPnt2], simplexR[newPnt2]);
287 if(simplexR[newPnt2] < simplexR[newPnt]) {
288 if(verbose>14) fprintf(fp,
" halfway (%g) is better than new\n", simplexR[newPnt2]);
289 for(
unsigned int i=0; i<parNr; i++) simplexP[worst][i]=simplexP[newPnt2][i];
290 simplexR[worst]=simplexR[newPnt2];
291 }
else if(isfinite(simplexR[newPnt])) {
292 if(verbose>14) fprintf(fp,
" halfway (%g) is worse than new\n", simplexR[newPnt2]);
293 for(
unsigned int i=0; i<parNr; i++) simplexP[worst][i]=simplexP[newPnt][i];
294 simplexR[worst]=simplexR[newPnt];
296 }
else if(isfinite(simplexR[newPnt])) {
297 if(verbose>14) fprintf(fp,
" new is worse than the best but better than the next best\n");
299 for(
unsigned int i=0; i<parNr; i++) simplexP[worst][i]=simplexP[newPnt][i];
300 simplexR[worst]=simplexR[newPnt];
305 double max, min, min2;
306 max=min=simplexR[0]; best=worst=0;
307 for(
unsigned int j=1; j<sn; j++) {
308 if(!isfinite(max) || simplexR[j] > max) {max=simplexR[j]; worst=j;}
309 if(!isfinite(min) || simplexR[j] < min) {min=simplexR[j]; best=j; }
312 min2=max; nextBest=worst;
313 for(
unsigned int j=0; j<sn; j++)
314 if(j!=best && (simplexR[j] < min2)) {min2=simplexR[j]; nextBest=j;}
316 fprintf(fp,
" best := %g (%d)\n", min, best);
317 fprintf(fp,
" next best := %g (%d)\n", min2, nextBest);
318 fprintf(fp,
" worst := %g (%d)\n", max, worst);
323 if(simplexR[best]<prevBestR) stopR=0;
else stopR++;
324 prevBestR=simplexR[best];
326 }
while(iterNr<maxIter);
329 if(iterNr>=maxIter) fprintf(fp,
" stopped because max iterations reached.\n");
330 fprintf(fp,
" %d iterations\n", iterNr);
331 fprintf(fp,
" R[%d]=%g\n", best, simplexR[best]);
338 simplexR[best] = (*nlo->
_fun)(parNr, simplexP[best], nlo->
fundata);
342 if(!isfinite(simplexR[best])) {
348 if(simplexR[best]<=initialR) {
349 for(
unsigned int i=0; i<parNr; i++) nlo->
xfull[i]=simplexP[best][i];
350 nlo->
funval=simplexR[best];
353 if(1 || verbose>0) fprintf(fp,
"nloptSimplex() gives worse R than initially!\n");
384 unsigned int maxIter,
389 int verbose=0;
if(status!=NULL) {verbose=status->
verbose; fp=status->
fp;}
392 if(verbose>1) fprintf(fp,
"%s(NLOPT, %d, status)\n", __func__, maxIter);
400 if(verbose>2 && fixedParNr>0) fprintf(fp,
"fixedParNr := %d\n", fixedParNr);
401 unsigned int fittedParNr=nlo->
totalNr-fixedParNr;
402 if(verbose>2) fprintf(fp,
"fittedParNr := %d\n", fittedParNr);
409 for(
unsigned int i=0; i<nlo->
totalNr; i++) {
411 if(!(nlo->
xtol[i]>0.0)) {
412 if(verbose>0) {fprintf(stderr,
"Error: invalid xtol[].\n"); fflush(stderr);}
420 if(maxIter==0) maxIter=5+fittedParNr;
421 if(verbose>2) {fprintf(fp,
"maxIter := %u\n", maxIter); fflush(fp);}
426 for(
unsigned int i=0; i<dim; i++) {tol[i]=nlo->
xtol[i]; nlo->
xtol[i]*=0.1;}
431 for(
unsigned int i=0; i<dim; i++) {
433 if(d>0.0) nlo->
xdelta[i]=2.0*tol[i]+0.15*d;
else nlo->
xdelta[i]=0.0;
441 unsigned int sampleNr=30*fittedParNr;
442 if(verbose>2) {fprintf(fp,
"sampleNr := %u\n", sampleNr); fflush(fp);}
444 nlo->
plist=(
double*)malloc(sampleNr*(dim+1)*
sizeof(
double));
445 if(nlo->
plist==NULL) {
451 int initGuessAvailable=1;
452 for(
unsigned int i=0; i<nlo->
totalNr; i++) {
453 if(!isfinite(nlo->
xfull[i])) {initGuessAvailable=0;
break;}
454 if(nlo->
xfull[i]<nlo->
xlower[i]) {initGuessAvailable=0;
break;}
455 if(nlo->
xfull[i]>nlo->
xupper[i]) {initGuessAvailable=0;
break;}
458 if(isfinite(nlo->
funval)) {
460 fprintf(fp,
"Initial guess: ");
461 for(
unsigned int i=0; i<dim; i++) fprintf(fp,
"%g ", nlo->
xfull[i]);
462 fprintf(fp,
"=> %e\n", nlo->
funval); fflush(fp);
467 if(verbose>2) {fprintf(fp,
"invalid initial guess\n"); fflush(fp);}
468 initGuessAvailable=0;
470 if(initGuessAvailable) {
471 for(
unsigned int pi=1; pi<sampleNr; pi++) {
476 while(!isfinite(nlo->
plist[pi*(dim+1)+dim])) {
486 if(!initGuessAvailable) {
487 for(
unsigned int pi=0; pi<sampleNr; pi++) {
491 while(!isfinite(nlo->
plist[pi*(dim+1)+dim])) {
508 fprintf(fp,
"LO: initial point with deltas:\n");
509 for(
unsigned int i=0; i<dim; i++) fprintf(fp,
"\t%e\t%e\n", nlo->
xfull[i], nlo->
xdelta[i]);
512 if(verbose>1) {fprintf(fp,
" LO failed\n"); fflush(fp);}
516 fprintf(fp,
"Point after LO:");
517 for(
unsigned int i=0; i<dim; i++) fprintf(fp,
" %e ", nlo->
xfull[i]);
518 fprintf(fp,
" => %e\n", nlo->
funval); fflush(fp);
520 if(verbose>5) fprintf(fp,
"funCalls=%d\n", nlo->
funCalls);
526 unsigned int iterNr=0;
527 while(iterNr<maxIter && nlo->funCalls<nlo->maxFunCalls) {
529 if(verbose>4) {fprintf(fp,
"-----------------------------\nIteration %d\n", iterNr); fflush(fp);}
536 fprintf(fp,
"best point so far:");
537 for(
unsigned int i=0; i<dim; i++) fprintf(fp,
" %e", nlo->
plist[i]);
538 fprintf(fp,
" => %e\n", nlo->
plist[dim]); fflush(fp);
542 double mean[dim], sd[dim];
547 fprintf(fp,
"Mean and SD of best points so far:\n");
548 for(
unsigned int i=0; i<dim; i++) fprintf(fp,
"\t%e\t%e\n", mean[i], sd[i]);
553 unsigned int i;
for(i=0; i<dim; i++)
if(fabs(sd[i])>tol[i])
break;
555 if(verbose>1) fprintf(fp,
"\n Required tolerance reached.\n");
561 for(
unsigned int i=0; i<dim; i++) {
575 fprintf(fp,
"LO: initial point with deltas:\n");
576 for(
unsigned int i=0; i<dim; i++) fprintf(fp,
"\t%e\t%e\n", nlo->
xfull[i], nlo->
xdelta[i]);
579 if(verbose>1) {fprintf(fp,
" LO failed\n"); fflush(fp);}
583 fprintf(fp,
"Point after LO:");
584 for(
unsigned int i=0; i<dim; i++) fprintf(fp,
" %e ", nlo->
xfull[i]);
585 fprintf(fp,
" => %e\n", nlo->
funval); fflush(fp);
589 if(verbose>5) fprintf(fp,
"funCalls=%d\n", nlo->
funCalls);
594 if(iterNr>=maxIter) {
595 if(verbose>1) fprintf(fp,
"\n Exceeded the maximum number for loops.\n");
598 if(verbose>1) fprintf(fp,
"\n Exceeded the maximum number for function calls.\n");
606 for(
unsigned int i=0; i<dim; i++) nlo->
xfull[i]=nlo->
plist[i];
610 for(
unsigned int i=0; i<dim; i++) nlo->
xtol[i]=tol[i];
640 int verbose=0;
if(status!=NULL) {verbose=status->
verbose; fp=status->
fp;}
643 if(verbose>1) fprintf(fp,
"%s(NLOPT, %d, status)\n", __func__, spNr);
651 if(verbose>2 && fixedParNr>0) fprintf(fp,
"fixedParNr := %d\n", fixedParNr);
652 unsigned int fittedParNr=nlo->
totalNr-fixedParNr;
653 if(verbose>2) fprintf(fp,
"fittedParNr := %d\n", fittedParNr);
660 for(
unsigned int i=0; i<nlo->
totalNr; i++) {
662 if(!(nlo->
xtol[i]>0.0)) {
663 if(verbose>0) {fprintf(stderr,
"Error: invalid xtol[].\n"); fflush(stderr);}
670 if(spNr==0) spNr=100*fittedParNr;
671 if(verbose>2) {fprintf(fp,
"spNr := %u\n", spNr); fflush(fp);}
675 double ss[spNr], sp[spNr*xNr], he[fittedParNr];
676 for(
unsigned int si=0; si<spNr; si++) {
681 for(
unsigned int xi=0; xi<xNr; xi++) {
682 if(nlo->
xtol[xi]>0.0) {
684 }
else sp[si*xNr+xi] = nlo->
xfull[xi];
688 printf(
"Halton-based start points:\n");
689 for(
unsigned int si=0; si<spNr; si++) {
690 for(
unsigned int xi=0; xi<xNr; xi++) printf(
" %g", sp[si*xNr+xi]);
691 printf(
" -> %g\n", ss[si]);
698 for(
unsigned int xi=0; xi<xNr; xi++) {
700 if(d>0.0) nlo->
xdelta[xi]=2.0*nlo->
xtol[xi]+0.01*d;
else nlo->
xdelta[xi]=0.0;
703 for(
unsigned int si=0; si<spNr; si++) {
705 for(
unsigned int xi=0; xi<xNr; xi++)
706 nlo->
xfull[xi]=sp[si*xNr+xi];
708 if(verbose>1) {fprintf(fp,
" LO failed\n"); fflush(fp);}
711 for(
unsigned int xi=0; xi<xNr; xi++)
712 sp[si*xNr+xi]=nlo->
xfull[xi];
716 printf(
"Simplex results from Halton-based start points:\n");
717 for(
unsigned int si=0; si<spNr; si++) {
718 for(
unsigned int xi=0; xi<xNr; xi++) printf(
" %g", sp[si*xNr+xi]);
719 printf(
" -> %g\n", ss[si]);
726 for(
unsigned int si=0; si<spNr; si++) {
727 if(!isfinite(ss[si]))
continue;
728 if(!isfinite(bv) || ss[si]<bv) {bv=ss[si]; bi=si;}
730 for(
unsigned int xi=0; xi<xNr; xi++) nlo->
xfull[xi]=sp[bi*xNr+xi];