16 m_MaximumIteration = 1000;
18 m_NumberSamplingPoints = 0;
27 m_StopConditionDescription << this->GetNameOfClass() <<
": ";
41 if( m_CostFunction.IsNull() )
46 m_StopConditionDescription.str(
"");
47 m_StopConditionDescription << this->GetNameOfClass() <<
": ";
49 this->InvokeEvent( itk::StartEvent() );
52 m_SpaceDimension = m_CostFunction->GetNumberOfParameters();
54 if (!this->m_ScalesInitialized)
57 for (
unsigned i = 0; i < m_SpaceDimension; ++i) sc[i] = 1.;
64 ip = this->GetInitialPosition();
66 double *p =
new double[m_SpaceDimension];
67 double *xl =
new double[m_SpaceDimension];
68 double *xu =
new double[m_SpaceDimension];
69 for (
unsigned i = 0; i < m_SpaceDimension; ++i)
71 p[i] = ip[i] * this->GetScales()[i];
72 xl[i] = m_LowerBounds[i] * this->GetScales()[i];
73 xu[i] = m_UpperBounds[i] * this->GetScales()[i];
76 double av = 2 * m_SpaceDimension + 1;
78 if (this->m_NumberSamplingPoints > m_SpaceDimension+2)
79 av = m_NumberSamplingPoints;
81 long int npt = (int) av;
82 double *w =
new double [(npt+13)*(npt+m_SpaceDimension)+3*m_SpaceDimension*(m_SpaceDimension+3)/2 + 10];
84 long int maxfun = this->m_MaximumIteration;
86 this->InvokeEvent( itk::StartEvent() );
88 this->optimize(npt,p,xl,xu,m_RhoBegin,m_RhoEnd,maxfun,w);
91 for (
unsigned i = 0; i < m_SpaceDimension; ++i) m_CurrentPosition[i] = p[i] / this->GetScales()[i];
107 return m_StopConditionDescription.str();
117 Superclass::PrintSelf(os,indent);
119 os << indent <<
"Metric Worst Possible Value " << m_MetricWorstPossibleValue << std::endl;
120 os << indent <<
"Catch GetValue Exception " << m_CatchGetValueException << std::endl;
121 os << indent <<
"Space Dimension " << m_SpaceDimension << std::endl;
122 os << indent <<
"Maximum Iteration " << m_MaximumIteration << std::endl;
123 os << indent <<
"Current Iteration " << m_CurrentIteration << std::endl;
124 os << indent <<
"Maximize On/Off " << m_Maximize << std::endl;
125 os << indent <<
"Initial Space Rad. " << m_RhoBegin << std::endl;
126 os << indent <<
"Final Space Radius " << m_RhoEnd << std::endl;
127 os << indent <<
"Current Cost " << m_CurrentCost << std::endl;
128 os << indent <<
"Stop " << m_Stop << std::endl;
132 ::optimize(
long int &npt,
double *x,
double *xl,
double *xu,
double &rhobeg,
double &rhoend,
133 long int &maxfun,
double *w)
140 long int j, id, np, iw, igo, ihq, ixb, ixa, ifv, isl, jsl, ipq, ivl, ixn, ixo, ixp, isu, jsu, ndim;
142 long int ibmat, izmat;
194 np = m_SpaceDimension + 1;
195 if (npt < m_SpaceDimension + 2 || npt > (m_SpaceDimension + 2) * np / 2) {
196 std::cerr <<
"Number of Points for the Quadratic Approximation not set right" << std::endl
197 <<
"We'll use the minimal value here: " << this->m_SpaceDimension
200 npt = this->m_SpaceDimension
209 ndim = npt + m_SpaceDimension;
211 ixp = ixb + m_SpaceDimension;
212 ifv = ixp + m_SpaceDimension * npt;
214 igo = ixo + m_SpaceDimension;
215 ihq = igo + m_SpaceDimension;
216 ipq = ihq + m_SpaceDimension * np / 2;
218 izmat = ibmat + ndim * m_SpaceDimension;
219 isl = izmat + npt * (npt - np);
220 isu = isl + m_SpaceDimension;
221 ixn = isu + m_SpaceDimension;
222 ixa = ixn + m_SpaceDimension;
223 id = ixa + m_SpaceDimension;
224 ivl =
id + m_SpaceDimension;
235 i__1 = m_SpaceDimension;
236 for (j = 1; j <= i__1; ++j) {
237 temp = xu[j] - xl[j];
238 if (temp < rhobeg + rhobeg) {
244 jsu = jsl + m_SpaceDimension;
245 w[jsl] = xl[j] - x[j];
246 w[jsu] = xu[j] - x[j];
247 if (w[jsl] >= -(rhobeg)) {
248 if (w[jsl] >= zero) {
253 x[j] = xl[j] + rhobeg;
257 w[jsu] = std::max(d__1,rhobeg);
259 }
else if (w[jsu] <= rhobeg) {
260 if (w[jsu] <= zero) {
265 x[j] = xu[j] - rhobeg;
267 d__1 = xl[j] - x[j], d__2 = -(rhobeg);
268 w[jsl] = std::min(d__1,d__2);
277 return bobyqb_(npt, &x[1], &xl[1], &xu[1], rhobeg, rhoend, maxfun, &w[ixb], &w[ixp], &w[ifv],
278 &w[ixo], &w[igo], &w[ihq], &w[ipq], &w[ibmat], &w[izmat], ndim, &w[isl], &w[isu], &w[ixn],
279 &w[ixa], &w[
id], &w[ivl], &w[iw]);
286 ::bobyqb_(
long int &npt,
double *x,
double *xl,
double *xu,
double &rhobeg,
double &rhoend,
long int &maxfun,
double *xbase,
double *xpt,
double *fval,
double *xopt,
double *gopt,
double *hq,
double *pq,
double *bmat,
double *zmat,
long int &ndim,
double *sl,
double *su,
double *xnew,
double *xalt,
double *d__,
double *vlag,
double *w)
289 long int xpt_dim1, xpt_offset, bmat_dim1, bmat_offset, zmat_dim1,
290 zmat_offset, i__1, i__2, i__3;
291 double d__1, d__2, d__3, d__4;
295 long int i__, j, k, ih, nf, jj, nh, ip, jp;
298 double den, one, ten, dsq, rho, sum, two, diff, half, beta, gisq;
300 double temp, suma, sumb, bsum, fopt;
304 double gqsq, dist, sumw, sumz, diffa, diffb, diffc = 0, hdiag;
306 double alpha, delta, adelt, denom, fsave, bdtol, delsq;
307 long int nresc, nfsav;
308 double ratio = 0, dnorm, vquad, pqold, tenth;
310 double sumpq, scaden;
312 double errbig, cauchy, fracsq, biglsq, densav;
314 double crvmin, frhosq;
355 zmat_offset = 1 + zmat_dim1;
358 xpt_offset = 1 + xpt_dim1;
370 bmat_offset = 1 + bmat_dim1;
387 np = m_SpaceDimension + 1;
389 nh = m_SpaceDimension * np / 2;
398 prelim_(npt, &x[1], &xl[1], &xu[1], rhobeg, maxfun, &xbase[1],
399 &xpt[xpt_offset], &fval[1], &gopt[1], &hq[1], &pq[1], &bmat[bmat_offset],
400 &zmat[zmat_offset], ndim, &sl[1], &su[1], nf, kopt);
403 i__1 = m_SpaceDimension;
404 for (i__ = 1; i__ <= i__1; ++i__) {
405 xopt[i__] = xpt[kopt + i__ * xpt_dim1];
409 xoptsq += d__1 * d__1;
438 i__1 = m_SpaceDimension;
439 for (j = 1; j <= i__1; ++j) {
441 for (i__ = 1; i__ <= i__2; ++i__) {
444 gopt[j] += hq[ih] * xopt[i__];
447 gopt[i__] += hq[ih] * xopt[j];
452 for (k = 1; k <= i__2; ++k) {
454 i__1 = m_SpaceDimension;
455 for (j = 1; j <= i__1; ++j) {
457 temp += xpt[k + j * xpt_dim1] * xopt[j];
460 i__1 = m_SpaceDimension;
461 for (i__ = 1; i__ <= i__1; ++i__) {
463 gopt[i__] += temp * xpt[k + i__ * xpt_dim1];
477 trsbox_(npt, &xpt[xpt_offset], &xopt[1], &gopt[1], &hq[1], &pq[1], &sl[1],
478 &su[1], &delta, &xnew[1], &d__[1], &w[1], &w[np], &w[np + m_SpaceDimension],
479 &w[np + (m_SpaceDimension << 1)], &w[np + m_SpaceDimension * 3], &dsq, &crvmin);
481 d__1 = delta, d__2 = std::sqrt(dsq);
482 dnorm = std::min(d__1,d__2);
483 if (dnorm < half * rho) {
487 distsq = d__1 * d__1;
488 if (nf <= nfsav + 2) {
499 d__1 = std::max(diffa,diffb);
500 errbig = std::max(d__1,diffc);
501 frhosq = rho * .125 * rho;
502 if (crvmin > zero && errbig > frhosq * crvmin) {
505 bdtol = errbig / rho;
506 i__1 = m_SpaceDimension;
507 for (j = 1; j <= i__1; ++j) {
509 if (xnew[j] == sl[j]) {
512 if (xnew[j] == su[j]) {
515 if (bdtest < bdtol) {
516 curv = hq[(j + j * j) / 2];
518 for (k = 1; k <= i__2; ++k) {
521 d__1 = xpt[k + j * xpt_dim1];
522 curv += pq[k] * (d__1 * d__1);
524 bdtest += half * curv * rho;
525 if (bdtest < bdtol) {
542 if (dsq <= xoptsq * .001) {
543 fracsq = xoptsq * .25;
546 for (k = 1; k <= i__1; ++k) {
548 sum = -half * xoptsq;
549 i__2 = m_SpaceDimension;
550 for (i__ = 1; i__ <= i__2; ++i__) {
552 sum += xpt[k + i__ * xpt_dim1] * xopt[i__];
555 temp = fracsq - half * sum;
556 i__2 = m_SpaceDimension;
557 for (i__ = 1; i__ <= i__2; ++i__) {
558 w[i__] = bmat[k + i__ * bmat_dim1];
559 vlag[i__] = sum * xpt[k + i__ * xpt_dim1] + temp * xopt[i__];
562 for (j = 1; j <= i__3; ++j) {
564 bmat[ip + j * bmat_dim1] = bmat[ip + j * bmat_dim1] + w[
565 i__] * vlag[j] + vlag[i__] * w[j];
573 for (jj = 1; jj <= i__3; ++jj) {
577 for (k = 1; k <= i__2; ++k) {
578 sumz += zmat[k + jj * zmat_dim1];
579 vlag[k] = w[npt + k] * zmat[k + jj * zmat_dim1];
583 i__2 = m_SpaceDimension;
584 for (j = 1; j <= i__2; ++j) {
585 sum = (fracsq * sumz - half * sumw) * xopt[j];
587 for (k = 1; k <= i__1; ++k) {
589 sum += vlag[k] * xpt[k + j * xpt_dim1];
593 for (k = 1; k <= i__1; ++k) {
595 bmat[k + j * bmat_dim1] += sum * zmat[k + jj * zmat_dim1];
598 i__1 = m_SpaceDimension;
599 for (i__ = 1; i__ <= i__1; ++i__) {
603 for (j = 1; j <= i__2; ++j) {
605 bmat[ip + j * bmat_dim1] += temp * w[j];
614 i__2 = m_SpaceDimension;
615 for (j = 1; j <= i__2; ++j) {
616 w[j] = -half * sumpq * xopt[j];
618 for (k = 1; k <= i__1; ++k) {
619 w[j] += pq[k] * xpt[k + j * xpt_dim1];
621 xpt[k + j * xpt_dim1] -= xopt[j];
624 for (i__ = 1; i__ <= i__1; ++i__) {
626 hq[ih] = hq[ih] + w[i__] * xopt[j] + xopt[i__] * w[j];
628 bmat[npt + i__ + j * bmat_dim1] = bmat[npt + j + i__ *
632 i__1 = m_SpaceDimension;
633 for (i__ = 1; i__ <= i__1; ++i__) {
634 xbase[i__] += xopt[i__];
635 xnew[i__] -= xopt[i__];
636 sl[i__] -= xopt[i__];
637 su[i__] -= xopt[i__];
660 rescue_(npt, &xl[1], &xu[1], maxfun, &xbase[1], &xpt[xpt_offset],
661 &fval[1], &xopt[1], &gopt[1], &hq[1], &pq[1], &bmat[bmat_offset],
662 &zmat[zmat_offset], ndim, &sl[1], &su[1], nf, &delta, kopt, &vlag[1],
663 &w[1], &w[m_SpaceDimension + np], &w[ndim + np]);
671 i__1 = m_SpaceDimension;
672 for (i__ = 1; i__ <= i__1; ++i__) {
673 xopt[i__] = xpt[kopt + i__ * xpt_dim1];
677 xoptsq += d__1 * d__1;
709 altmov_(npt, &xpt[xpt_offset], &xopt[1], &bmat[bmat_offset], &zmat[zmat_offset],
710 ndim, &sl[1], &su[1], kopt, knew, &adelt, &xnew[1], &xalt[1], &alpha, &cauchy,
711 &w[1], &w[np], &w[ndim + 1]);
712 i__1 = m_SpaceDimension;
713 for (i__ = 1; i__ <= i__1; ++i__) {
715 d__[i__] = xnew[i__] - xopt[i__];
724 for (k = 1; k <= i__1; ++k) {
728 i__2 = m_SpaceDimension;
729 for (j = 1; j <= i__2; ++j) {
730 suma += xpt[k + j * xpt_dim1] * d__[j];
731 sumb += xpt[k + j * xpt_dim1] * xopt[j];
733 sum += bmat[k + j * bmat_dim1] * d__[j];
735 w[k] = suma * (half * suma + sumb);
742 for (jj = 1; jj <= i__1; ++jj) {
745 for (k = 1; k <= i__2; ++k) {
747 sum += zmat[k + jj * zmat_dim1] * w[k];
751 for (k = 1; k <= i__2; ++k) {
753 vlag[k] += sum * zmat[k + jj * zmat_dim1];
759 i__2 = m_SpaceDimension;
760 for (j = 1; j <= i__2; ++j) {
766 for (k = 1; k <= i__1; ++k) {
768 sum += w[k] * bmat[k + j * bmat_dim1];
770 bsum += sum * d__[j];
772 i__1 = m_SpaceDimension;
773 for (i__ = 1; i__ <= i__1; ++i__) {
775 sum += bmat[jp + i__ * bmat_dim1] * d__[i__];
778 bsum += sum * d__[j];
780 dx += d__[j] * xopt[j];
782 beta = dx * dx + dsq * (xoptsq + dx + dx + half * dsq) + beta - bsum;
792 denom = d__1 * d__1 + alpha * beta;
793 if (denom < cauchy && cauchy > zero) {
794 i__2 = m_SpaceDimension;
795 for (i__ = 1; i__ <= i__2; ++i__) {
796 xnew[i__] = xalt[i__];
798 d__[i__] = xnew[i__] - xopt[i__];
805 if (denom <= half * (d__1 * d__1)) {
823 delsq = delta * delta;
828 for (k = 1; k <= i__2; ++k) {
834 for (jj = 1; jj <= i__1; ++jj) {
837 d__1 = zmat[k + jj * zmat_dim1];
838 hdiag += d__1 * d__1;
842 den = beta * hdiag + d__1 * d__1;
844 i__1 = m_SpaceDimension;
845 for (j = 1; j <= i__1; ++j) {
848 d__1 = xpt[k + j * xpt_dim1] - xopt[j];
849 distsq += d__1 * d__1;
853 d__3 = distsq / delsq;
854 d__1 = one, d__2 = d__3 * d__3;
855 temp = std::max(d__1,d__2);
856 if (temp * den > scaden) {
864 d__1 = biglsq, d__2 = temp * (d__3 * d__3);
865 biglsq = std::max(d__1,d__2);
869 if (scaden <= half * biglsq) {
889 i__2 = m_SpaceDimension;
890 for (i__ = 1; i__ <= i__2; ++i__) {
893 d__3 = xl[i__], d__4 = xbase[i__] + xnew[i__];
894 d__1 = std::max(d__3,d__4), d__2 = xu[i__];
895 x[i__] = std::min(d__1,d__2);
896 if (xnew[i__] == sl[i__]) {
899 if (xnew[i__] == su[i__]) {
905 this->m_StopConditionDescription <<
"Max cost function call reached " << this->m_MaximumIteration;
906 this->InvokeEvent(itk::EndEvent());
918 this->m_StopConditionDescription <<
"User ended optimisation ";
919 this->InvokeEvent(itk::EndEvent());
925 for (
unsigned int i = 0; i < m_SpaceDimension; ++i) px[i] = x[i+1] / this->GetScales()[i];
929 f = this->m_CostFunction->GetValue(px);
933 if (m_CatchGetValueException)
934 f = m_MetricWorstPossibleValue;
937 if (this->m_Maximize)
940 this->m_CurrentCost = f;
964 i__2 = m_SpaceDimension;
965 for (j = 1; j <= i__2; ++j) {
966 vquad += d__[j] * gopt[j];
968 for (i__ = 1; i__ <= i__1; ++i__) {
970 temp = d__[i__] * d__[j];
975 vquad += hq[ih] * temp;
979 for (k = 1; k <= i__1; ++k) {
983 vquad += half * pq[k] * (d__1 * d__1);
985 diff = f - fopt - vquad;
988 diffa = std::abs(diff);
1003 ratio = (f - fopt) / vquad;
1004 if (ratio <= tenth) {
1006 d__1 = half * delta;
1007 delta = std::min(d__1,dnorm);
1008 }
else if (ratio <= .7) {
1010 d__1 = half * delta;
1011 delta = std::max(d__1,dnorm);
1014 d__1 = half * delta, d__2 = dnorm + dnorm;
1015 delta = std::max(d__1,d__2);
1017 if (delta <= rho * 1.5) {
1026 delsq = delta * delta;
1031 for (k = 1; k <= i__1; ++k) {
1034 for (jj = 1; jj <= i__2; ++jj) {
1037 d__1 = zmat[k + jj * zmat_dim1];
1038 hdiag += d__1 * d__1;
1042 den = beta * hdiag + d__1 * d__1;
1044 i__2 = m_SpaceDimension;
1045 for (j = 1; j <= i__2; ++j) {
1048 d__1 = xpt[k + j * xpt_dim1] - xnew[j];
1049 distsq += d__1 * d__1;
1053 d__3 = distsq / delsq;
1054 d__1 = one, d__2 = d__3 * d__3;
1055 temp = std::max(d__1,d__2);
1056 if (temp * den > scaden) {
1057 scaden = temp * den;
1065 d__1 = biglsq, d__2 = temp * (d__3 * d__3);
1066 biglsq = std::max(d__1,d__2);
1068 if (scaden <= half * biglsq) {
1078 update_(npt, &bmat[bmat_offset], &zmat[zmat_offset], ndim, &vlag[1],
1079 &beta, &denom, knew, &w[1]);
1083 i__1 = m_SpaceDimension;
1084 for (i__ = 1; i__ <= i__1; ++i__) {
1085 temp = pqold * xpt[knew + i__ * xpt_dim1];
1087 for (j = 1; j <= i__2; ++j) {
1090 hq[ih] += temp * xpt[knew + j * xpt_dim1];
1094 for (jj = 1; jj <= i__2; ++jj) {
1095 temp = diff * zmat[knew + jj * zmat_dim1];
1097 for (k = 1; k <= i__1; ++k) {
1099 pq[k] += temp * zmat[k + jj * zmat_dim1];
1107 i__1 = m_SpaceDimension;
1108 for (i__ = 1; i__ <= i__1; ++i__) {
1109 xpt[knew + i__ * xpt_dim1] = xnew[i__];
1111 w[i__] = bmat[knew + i__ * bmat_dim1];
1114 for (k = 1; k <= i__1; ++k) {
1117 for (jj = 1; jj <= i__2; ++jj) {
1119 suma += zmat[knew + jj * zmat_dim1] * zmat[k + jj * zmat_dim1];
1122 i__2 = m_SpaceDimension;
1123 for (j = 1; j <= i__2; ++j) {
1125 sumb += xpt[k + j * xpt_dim1] * xopt[j];
1128 i__2 = m_SpaceDimension;
1129 for (i__ = 1; i__ <= i__2; ++i__) {
1131 w[i__] += temp * xpt[k + i__ * xpt_dim1];
1134 i__2 = m_SpaceDimension;
1135 for (i__ = 1; i__ <= i__2; ++i__) {
1137 gopt[i__] += diff * w[i__];
1146 i__2 = m_SpaceDimension;
1147 for (j = 1; j <= i__2; ++j) {
1151 xoptsq += d__1 * d__1;
1153 for (i__ = 1; i__ <= i__1; ++i__) {
1156 gopt[j] += hq[ih] * d__[i__];
1159 gopt[i__] += hq[ih] * d__[j];
1163 for (k = 1; k <= i__1; ++k) {
1165 i__2 = m_SpaceDimension;
1166 for (j = 1; j <= i__2; ++j) {
1168 temp += xpt[k + j * xpt_dim1] * d__[j];
1170 temp = pq[k] * temp;
1171 i__2 = m_SpaceDimension;
1172 for (i__ = 1; i__ <= i__2; ++i__) {
1174 gopt[i__] += temp * xpt[k + i__ * xpt_dim1];
1185 for (k = 1; k <= i__2; ++k) {
1186 vlag[k] = fval[k] - fval[kopt];
1191 for (j = 1; j <= i__2; ++j) {
1194 for (k = 1; k <= i__1; ++k) {
1196 sum += zmat[k + j * zmat_dim1] * vlag[k];
1199 for (k = 1; k <= i__1; ++k) {
1201 w[k] += sum * zmat[k + j * zmat_dim1];
1205 for (k = 1; k <= i__1; ++k) {
1207 i__2 = m_SpaceDimension;
1208 for (j = 1; j <= i__2; ++j) {
1210 sum += xpt[k + j * xpt_dim1] * xopt[j];
1218 i__1 = m_SpaceDimension;
1219 for (i__ = 1; i__ <= i__1; ++i__) {
1222 for (k = 1; k <= i__2; ++k) {
1224 sum = sum + bmat[k + i__ * bmat_dim1] * vlag[k] + xpt[k + i__ * xpt_dim1] * w[k];
1226 if (xopt[i__] == sl[i__]) {
1228 d__2 = zero, d__3 = gopt[i__];
1230 d__1 = std::min(d__2,d__3);
1231 gqsq += d__1 * d__1;
1233 d__1 = std::min(zero,sum);
1234 gisq += d__1 * d__1;
1235 }
else if (xopt[i__] == su[i__]) {
1237 d__2 = zero, d__3 = gopt[i__];
1239 d__1 = std::max(d__2,d__3);
1240 gqsq += d__1 * d__1;
1242 d__1 = std::max(zero,sum);
1243 gisq += d__1 * d__1;
1247 gqsq += d__1 * d__1;
1251 vlag[npt + i__] = sum;
1258 if (gqsq < ten * gisq) {
1262 i__1 = std::max(npt,nh);
1263 for (i__ = 1; i__ <= i__1; ++i__) {
1264 if (i__ <= m_SpaceDimension) {
1265 gopt[i__] = vlag[npt + i__];
1268 pq[i__] = w[npt + i__];
1286 if (f <= fopt + tenth * vquad) {
1298 d__1 = d__3 * d__3, d__2 = d__4 * d__4;
1299 distsq = std::max(d__1,d__2);
1303 for (k = 1; k <= i__1; ++k) {
1305 i__2 = m_SpaceDimension;
1306 for (j = 1; j <= i__2; ++j) {
1309 d__1 = xpt[k + j * xpt_dim1] - xopt[j];
1326 dist = std::sqrt(distsq);
1329 d__1 = tenth * delta, d__2 = half * dist;
1330 delta = std::min(d__1,d__2);
1331 if (delta <= rho * 1.5) {
1338 d__2 = tenth * dist;
1339 d__1 = std::min(d__2,delta);
1340 adelt = std::max(d__1,rho);
1341 dsq = adelt * adelt;
1350 if (std::max(delta,dnorm) > rho) {
1360 ratio = rho / rhoend;
1363 }
else if (ratio <= 250.) {
1364 rho = std::sqrt(ratio) * rhoend;
1368 delta = std::max(delta,rho);
1399 if (fval[kopt] <= fsave) {
1400 i__1 = m_SpaceDimension;
1401 for (i__ = 1; i__ <= i__1; ++i__) {
1404 d__3 = xl[i__], d__4 = xbase[i__] + xopt[i__];
1405 d__1 = std::max(d__3,d__4), d__2 = xu[i__];
1406 x[i__] = std::min(d__1,d__2);
1407 if (xopt[i__] == sl[i__]) {
1410 if (xopt[i__] == su[i__]) {
1433 double *bmat,
double *zmat,
long int &ndim,
1434 double *sl,
double *su,
long int &kopt,
1435 long int &knew,
double *adelt,
double *xnew,
1436 double *xalt,
double *alpha,
double *cauchy,
1437 double *glag,
double *hcol,
double *w)
1440 long int xpt_dim1, xpt_offset, bmat_dim1, bmat_offset, zmat_dim1,
1441 zmat_offset, i__1, i__2;
1442 double d__1, d__2, d__3, d__4;
1446 double ha, gw, one, diff, half;
1447 long int ilbd, isbd;
1450 double vlag, subd, temp;
1452 double step = 0, zero, curv;
1454 double scale, csave = 0, tempa, tempb, tempd, const__, sumin, ggfree;
1455 long int ibdsav = 0;
1456 double dderiv, bigstp, predsq, presav, distsq, stpsav = 0, wfixsq, wsqsav;
1491 zmat_offset = 1 + zmat_dim1;
1492 zmat -= zmat_offset;
1494 xpt_offset = 1 + xpt_dim1;
1498 bmat_offset = 1 + bmat_dim1;
1499 bmat -= bmat_offset;
1512 const__ = one + std::sqrt(2.);
1514 for (k = 1; k <= i__1; ++k) {
1519 for (j = 1; j <= i__1; ++j) {
1520 temp = zmat[knew + j * zmat_dim1];
1522 for (k = 1; k <= i__2; ++k) {
1524 hcol[k] += temp * zmat[k + j * zmat_dim1];
1527 *alpha = hcol[knew];
1533 for (i__ = 1; i__ <= i__2; ++i__) {
1535 glag[i__] = bmat[knew + i__ * bmat_dim1];
1538 for (k = 1; k <= i__2; ++k) {
1541 for (j = 1; j <= i__1; ++j) {
1543 temp += xpt[k + j * xpt_dim1] * xopt[j];
1545 temp = hcol[k] * temp;
1547 for (i__ = 1; i__ <= i__1; ++i__) {
1549 glag[i__] += temp * xpt[k + i__ * xpt_dim1];
1561 for (k = 1; k <= i__1; ++k) {
1568 for (i__ = 1; i__ <= i__2; ++i__) {
1569 temp = xpt[k + i__ * xpt_dim1] - xopt[i__];
1570 dderiv += glag[i__] * temp;
1572 distsq += temp * temp;
1574 subd = *adelt / std::sqrt(distsq);
1578 sumin = std::min(one,subd);
1583 for (i__ = 1; i__ <= i__2; ++i__) {
1584 temp = xpt[k + i__ * xpt_dim1] - xopt[i__];
1586 if (slbd * temp < sl[i__] - xopt[i__]) {
1587 slbd = (sl[i__] - xopt[i__]) / temp;
1590 if (subd * temp > su[i__] - xopt[i__]) {
1592 d__1 = sumin, d__2 = (su[i__] - xopt[i__]) / temp;
1593 subd = std::max(d__1,d__2);
1596 }
else if (temp < zero) {
1597 if (slbd * temp > su[i__] - xopt[i__]) {
1598 slbd = (su[i__] - xopt[i__]) / temp;
1601 if (subd * temp < sl[i__] - xopt[i__]) {
1603 d__1 = sumin, d__2 = (sl[i__] - xopt[i__]) / temp;
1604 subd = std::max(d__1,d__2);
1615 diff = dderiv - one;
1617 vlag = slbd * (dderiv - slbd * diff);
1619 temp = subd * (dderiv - subd * diff);
1620 if (std::abs(temp) > std::abs(vlag)) {
1625 tempd = half * dderiv;
1626 tempa = tempd - diff * slbd;
1627 tempb = tempd - diff * subd;
1628 if (tempa * tempb < zero) {
1629 temp = tempd * tempd / diff;
1630 if (std::abs(temp) > std::abs(vlag)) {
1631 step = tempd / diff;
1641 vlag = slbd * (one - slbd);
1643 temp = subd * (one - subd);
1644 if (std::abs(temp) > std::abs(vlag)) {
1650 if (std::abs(vlag) < .25) {
1661 temp = step * (one - step) * distsq;
1662 predsq = vlag * vlag * (vlag * vlag + ha * temp * temp);
1663 if (predsq > presav) {
1676 for (i__ = 1; i__ <= i__1; ++i__) {
1677 temp = xopt[i__] + stpsav * (xpt[ksav + i__ * xpt_dim1] - xopt[i__]);
1682 d__1 = sl[i__], d__2 = std::min(d__3,temp);
1683 xnew[i__] = std::max(d__1,d__2);
1686 xnew[-ibdsav] = sl[-ibdsav];
1689 xnew[ibdsav] = su[ibdsav];
1696 bigstp = *adelt + *adelt;
1702 for (i__ = 1; i__ <= i__1; ++i__) {
1705 d__1 = xopt[i__] - sl[i__], d__2 = glag[i__];
1706 tempa = std::min(d__1,d__2);
1708 d__1 = xopt[i__] - su[i__], d__2 = glag[i__];
1709 tempb = std::max(d__1,d__2);
1710 if (tempa > zero || tempb < zero) {
1714 ggfree += d__1 * d__1;
1718 if (ggfree == zero) {
1726 temp = *adelt * *adelt - wfixsq;
1729 step = std::sqrt(temp / ggfree);
1732 for (i__ = 1; i__ <= i__1; ++i__) {
1733 if (w[i__] == bigstp) {
1734 temp = xopt[i__] - step * glag[i__];
1735 if (temp <= sl[i__]) {
1736 w[i__] = sl[i__] - xopt[i__];
1739 wfixsq += d__1 * d__1;
1740 }
else if (temp >= su[i__]) {
1741 w[i__] = su[i__] - xopt[i__];
1744 wfixsq += d__1 * d__1;
1748 ggfree += d__1 * d__1;
1753 if (wfixsq > wsqsav && ggfree > zero) {
1763 for (i__ = 1; i__ <= i__1; ++i__) {
1764 if (w[i__] == bigstp) {
1765 w[i__] = -step * glag[i__];
1768 d__3 = su[i__], d__4 = xopt[i__] + w[i__];
1769 d__1 = sl[i__], d__2 = std::min(d__3,d__4);
1770 xalt[i__] = std::max(d__1,d__2);
1771 }
else if (w[i__] == zero) {
1772 xalt[i__] = xopt[i__];
1773 }
else if (glag[i__] > zero) {
1774 xalt[i__] = sl[i__];
1776 xalt[i__] = su[i__];
1779 gw += glag[i__] * w[i__];
1789 for (k = 1; k <= i__1; ++k) {
1792 for (j = 1; j <= i__2; ++j) {
1794 temp += xpt[k + j * xpt_dim1] * w[j];
1797 curv += hcol[k] * temp * temp;
1802 if (curv > -gw && curv < -const__ * gw) {
1805 for (i__ = 1; i__ <= i__1; ++i__) {
1806 temp = xopt[i__] + scale * w[i__];
1811 d__1 = sl[i__], d__2 = std::min(d__3,temp);
1812 xalt[i__] = std::max(d__1,d__2);
1815 d__1 = half * gw * scale;
1816 *cauchy = d__1 * d__1;
1819 d__1 = gw + half * curv;
1820 *cauchy = d__1 * d__1;
1829 for (i__ = 1; i__ <= i__1; ++i__) {
1830 glag[i__] = -glag[i__];
1832 w[m_SpaceDimension + i__] = xalt[i__];
1838 if (csave > *cauchy) {
1840 for (i__ = 1; i__ <= i__1; ++i__) {
1842 xalt[i__] = w[m_SpaceDimension + i__];
1851 double *xl,
double *xu,
double &rhobeg,
1852 long int &maxfun,
double *xbase,
double *xpt,
double *fval,
1853 double *gopt,
double *hq,
double *pq,
double *bmat,
1854 double *zmat,
long int &ndim,
double *sl,
double *su,
1855 long int &nf,
long int &kopt)
1858 long int xpt_dim1, xpt_offset, bmat_dim1, bmat_offset, zmat_dim1,
1859 zmat_offset, i__1, i__2;
1860 double d__1, d__2, d__3, d__4;
1864 long int i__, j, k, ih, np, nfm;
1866 long int nfx, ipt = 0, jpt = 0;
1867 double two, fbeg = 0, diff, half, temp, zero, recip, stepa = 0, stepb = 0;
1891 zmat_offset = 1 + zmat_dim1;
1892 zmat -= zmat_offset;
1894 xpt_offset = 1 + xpt_dim1;
1905 bmat_offset = 1 + bmat_dim1;
1906 bmat -= bmat_offset;
1915 rhosq = rhobeg * rhobeg;
1916 recip = one / rhosq;
1923 for (j = 1; j <= i__1; ++j) {
1926 for (k = 1; k <= i__2; ++k) {
1928 xpt[k + j * xpt_dim1] = zero;
1931 for (i__ = 1; i__ <= i__2; ++i__) {
1933 bmat[i__ + j * bmat_dim1] = zero;
1937 for (ih = 1; ih <= i__2; ++ih) {
1942 for (k = 1; k <= i__2; ++k) {
1945 for (j = 1; j <= i__1; ++j) {
1947 zmat[k + j * zmat_dim1] = zero;
1962 this->InvokeEvent( itk::IterationEvent() );
1964 if (nfm <= m_SpaceDimension << 1) {
1965 if (nfm >= 1 && nfm <= m_SpaceDimension) {
1967 if (su[nfm] == zero) {
1970 xpt[nf + nfm * xpt_dim1] = stepa;
1971 }
else if (nfm > m_SpaceDimension) {
1972 stepa = xpt[nf - m_SpaceDimension + nfx * xpt_dim1];
1974 if (sl[nfx] == zero) {
1976 d__1 = two * rhobeg, d__2 = su[nfx];
1977 stepb = std::min(d__1,d__2);
1979 if (su[nfx] == zero) {
1981 d__1 = -two * rhobeg, d__2 = sl[nfx];
1982 stepb = std::max(d__1,d__2);
1984 xpt[nf + nfx * xpt_dim1] = stepb;
1987 itemp = (nfm - np) / m_SpaceDimension;
1990 if (ipt > m_SpaceDimension) {
1995 xpt[nf + ipt * xpt_dim1] = xpt[ipt + 1 + ipt * xpt_dim1];
1996 xpt[nf + jpt * xpt_dim1] = xpt[jpt + 1 + jpt * xpt_dim1];
2003 for (j = 1; j <= i__1; ++j) {
2006 d__3 = xl[j], d__4 = xbase[j] + xpt[nf + j * xpt_dim1];
2007 d__1 = std::max(d__3,d__4), d__2 = xu[j];
2008 x[j] = std::min(d__1,d__2);
2009 if (xpt[nf + j * xpt_dim1] == sl[j]) {
2012 if (xpt[nf + j * xpt_dim1] == su[j]) {
2019 for (
unsigned i = 0; i <
m_SpaceDimension; ++i)
px[i] = x[i+1] / this->GetScales()[i];
2023 f = this->m_CostFunction->GetValue(
px);
2051 }
else if (f < fval[kopt]) {
2061 if (nf <= (m_SpaceDimension << 1) + 1) {
2062 if (nf >= 2 && nf <= m_SpaceDimension + 1) {
2063 gopt[nfm] = (f - fbeg) / stepa;
2064 if (npt < nf + m_SpaceDimension) {
2065 bmat[nfm * bmat_dim1 + 1] = -one / stepa;
2066 bmat[nf + nfm * bmat_dim1] = one / stepa;
2067 bmat[npt + nfm + nfm * bmat_dim1] = -half * rhosq;
2069 }
else if (nf >= m_SpaceDimension + 2) {
2070 ih = nfx * (nfx + 1) / 2;
2071 temp = (f - fbeg) / stepb;
2072 diff = stepb - stepa;
2073 hq[ih] = two * (temp - gopt[nfx]) / diff;
2074 gopt[nfx] = (gopt[nfx] * stepb - temp * stepa) / diff;
2075 if (stepa * stepb < zero) {
2076 if (f < fval[nf - m_SpaceDimension]) {
2082 xpt[nf - m_SpaceDimension + nfx * xpt_dim1] = stepb;
2083 xpt[nf + nfx * xpt_dim1] = stepa;
2086 bmat[nfx * bmat_dim1 + 1] = -(stepa + stepb) / (stepa * stepb);
2087 bmat[nf + nfx * bmat_dim1] = -half / xpt[nf - m_SpaceDimension + nfx *
2089 bmat[nf - m_SpaceDimension + nfx * bmat_dim1] = -bmat[nfx * bmat_dim1 + 1] -
2090 bmat[nf + nfx * bmat_dim1];
2091 zmat[nfx * zmat_dim1 + 1] = std::sqrt(two) / (stepa * stepb);
2092 zmat[nf + nfx * zmat_dim1] = std::sqrt(half) / rhosq;
2093 zmat[nf - m_SpaceDimension + nfx * zmat_dim1] = -zmat[nfx * zmat_dim1 + 1] -
2094 zmat[nf + nfx * zmat_dim1];
2101 ih = ipt * (ipt - 1) / 2 + jpt;
2102 zmat[nfx * zmat_dim1 + 1] = recip;
2103 zmat[nf + nfx * zmat_dim1] = recip;
2104 zmat[ipt + 1 + nfx * zmat_dim1] = -recip;
2105 zmat[jpt + 1 + nfx * zmat_dim1] = -recip;
2106 temp = xpt[nf + ipt * xpt_dim1] * xpt[nf + jpt * xpt_dim1];
2107 hq[ih] = (fbeg - fval[ipt + 1] - fval[jpt + 1] + f) / temp;
2109 if (nf < npt && nf < maxfun) {
2116 double *xu,
long int &maxfun,
double *xbase,
2117 double *xpt,
double *fval,
double *xopt,
double *gopt,
2118 double *hq,
double *pq,
double *bmat,
double *zmat,
2119 long int &ndim,
double *sl,
double *su,
long int &nf,
2120 double *delta,
long int &kopt,
double *vlag,
double *
2121 ptsaux,
double *ptsid,
double *w)
2124 long int xpt_dim1, xpt_offset, bmat_dim1, bmat_offset, zmat_dim1,
2125 zmat_offset, i__1, i__2, i__3;
2126 double d__1, d__2, d__3, d__4;
2130 long int i__, j, k, ih, jp, ip, iq, np, iw;
2131 double xp = 0, xq = 0, den;
2134 long int ihq, jpn, kpt;
2135 double sum, diff, half, beta;
2138 long int nrem, knew;
2141 double zero, hdiag, fbase, sfrac, denom, vquad, sumpq;
2143 double dsqmin, distsq, vlmxsq;
2184 zmat_offset = 1 + zmat_dim1;
2185 zmat -= zmat_offset;
2187 xpt_offset = 1 + xpt_dim1;
2198 bmat_offset = 1 + bmat_dim1;
2199 bmat -= bmat_offset;
2212 sfrac = half / (double) np;
2225 for (k = 1; k <= i__1; ++k) {
2228 for (j = 1; j <= i__2; ++j) {
2229 xpt[k + j * xpt_dim1] -= xopt[j];
2232 d__1 = xpt[k + j * xpt_dim1];
2233 distsq += d__1 * d__1;
2236 w[ndim + k] = distsq;
2237 winc = std::max(winc,distsq);
2239 for (j = 1; j <= i__2; ++j) {
2241 zmat[k + j * zmat_dim1] = zero;
2250 for (j = 1; j <= i__2; ++j) {
2251 w[j] = half * sumpq * xopt[j];
2253 for (k = 1; k <= i__1; ++k) {
2255 w[j] += pq[k] * xpt[k + j * xpt_dim1];
2258 for (i__ = 1; i__ <= i__1; ++i__) {
2261 hq[ih] = hq[ih] + w[i__] * xopt[j] + w[j] * xopt[i__];
2269 for (j = 1; j <= i__1; ++j) {
2270 xbase[j] += xopt[j];
2275 d__1 = *delta, d__2 = su[j];
2276 ptsaux[(j << 1) + 1] = std::min(d__1,d__2);
2278 d__1 = -(*delta), d__2 = sl[j];
2279 ptsaux[(j << 1) + 2] = std::max(d__1,d__2);
2280 if (ptsaux[(j << 1) + 1] + ptsaux[(j << 1) + 2] < zero) {
2281 temp = ptsaux[(j << 1) + 1];
2282 ptsaux[(j << 1) + 1] = ptsaux[(j << 1) + 2];
2283 ptsaux[(j << 1) + 2] = temp;
2285 if ((d__2 = ptsaux[(j << 1) + 2], std::abs(d__2)) < half * (d__1 = ptsaux[(
2286 j << 1) + 1], std::abs(d__1))) {
2287 ptsaux[(j << 1) + 2] = half * ptsaux[(j << 1) + 1];
2290 for (i__ = 1; i__ <= i__2; ++i__) {
2292 bmat[i__ + j * bmat_dim1] = zero;
2303 for (j = 1; j <= i__2; ++j) {
2306 ptsid[jp] = (double) j + sfrac;
2308 ptsid[jpn] = (double) j / (
double) np + sfrac;
2309 temp = one / (ptsaux[(j << 1) + 1] - ptsaux[(j << 1) + 2]);
2310 bmat[jp + j * bmat_dim1] = -temp + one / ptsaux[(j << 1) + 1];
2311 bmat[jpn + j * bmat_dim1] = temp + one / ptsaux[(j << 1) + 2];
2312 bmat[j * bmat_dim1 + 1] = -bmat[jp + j * bmat_dim1] - bmat[jpn +
2314 zmat[j * zmat_dim1 + 1] = std::sqrt(2.) / (d__1 = ptsaux[(j << 1) + 1]
2315 * ptsaux[(j << 1) + 2], std::abs(d__1));
2316 zmat[jp + j * zmat_dim1] = zmat[j * zmat_dim1 + 1] * ptsaux[(j <<
2318 zmat[jpn + j * zmat_dim1] = -zmat[j * zmat_dim1 + 1] * ptsaux[(j
2321 bmat[j * bmat_dim1 + 1] = -one / ptsaux[(j << 1) + 1];
2322 bmat[jp + j * bmat_dim1] = one / ptsaux[(j << 1) + 1];
2324 d__1 = ptsaux[(j << 1) + 1];
2325 bmat[j + npt + j * bmat_dim1] = -half * (d__1 * d__1);
2334 for (k = np << 1; k <= i__2; ++k) {
2339 if (iq > m_SpaceDimension) {
2342 ptsid[k] = (double) ip + (
double) iq / (double) np +
2344 temp = one / (ptsaux[(ip << 1) + 1] * ptsaux[(iq << 1) + 1]);
2345 zmat[(k - np) * zmat_dim1 + 1] = temp;
2346 zmat[ip + 1 + (k - np) * zmat_dim1] = -temp;
2347 zmat[iq + 1 + (k - np) * zmat_dim1] = -temp;
2349 zmat[k + (k - np) * zmat_dim1] = temp;
2361 for (j = 1; j <= i__2; ++j) {
2362 temp = bmat[kold + j * bmat_dim1];
2363 bmat[kold + j * bmat_dim1] = bmat[knew + j * bmat_dim1];
2365 bmat[knew + j * bmat_dim1] = temp;
2368 for (j = 1; j <= i__2; ++j) {
2369 temp = zmat[kold + j * zmat_dim1];
2370 zmat[kold + j * zmat_dim1] = zmat[knew + j * zmat_dim1];
2372 zmat[knew + j * zmat_dim1] = temp;
2374 ptsid[kold] = ptsid[knew];
2376 w[ndim + knew] = zero;
2380 vlag[kold] = vlag[knew];
2388 update_(npt, &bmat[bmat_offset], &zmat[zmat_offset], ndim, &vlag[1], &beta, &denom, knew, &w[1]);
2393 for (k = 1; k <= i__2; ++k) {
2395 w[ndim + k] = (d__1 = w[ndim + k], std::abs(d__1));
2406 for (k = 1; k <= i__2; ++k) {
2407 if (w[ndim + k] > zero) {
2408 if (dsqmin == zero || w[ndim + k] < dsqmin) {
2410 dsqmin = w[ndim + k];
2415 if (dsqmin == zero) {
2422 for (j = 1; j <= i__2; ++j) {
2424 w[npt + j] = xpt[knew + j * xpt_dim1];
2427 for (k = 1; k <= i__2; ++k) {
2430 }
else if (ptsid[k] == zero) {
2432 for (j = 1; j <= i__1; ++j) {
2434 sum += w[npt + j] * xpt[k + j * xpt_dim1];
2437 ip = (
long int) ptsid[k];
2439 sum = w[npt + ip] * ptsaux[(ip << 1) + 1];
2441 iq = (
long int) ((
double) np * ptsid[k] - (double) (ip *
2448 sum += w[npt + iq] * ptsaux[iw + (iq << 1)];
2452 w[k] = half * sum * sum;
2459 for (k = 1; k <= i__2; ++k) {
2462 for (j = 1; j <= i__1; ++j) {
2464 sum += bmat[k + j * bmat_dim1] * w[npt + j];
2471 for (j = 1; j <= i__2; ++j) {
2474 for (k = 1; k <= i__1; ++k) {
2476 sum += zmat[k + j * zmat_dim1] * w[k];
2480 for (k = 1; k <= i__1; ++k) {
2482 vlag[k] += sum * zmat[k + j * zmat_dim1];
2488 for (j = 1; j <= i__1; ++j) {
2491 for (k = 1; k <= i__2; ++k) {
2493 sum += bmat[k + j * bmat_dim1] * w[k];
2496 bsum += sum * w[jp];
2498 for (ip = npt + 1; ip <= i__2; ++ip) {
2500 sum += bmat[ip + j * bmat_dim1] * w[ip];
2502 bsum += sum * w[jp];
2506 d__1 = xpt[knew + j * xpt_dim1];
2507 distsq += d__1 * d__1;
2509 beta = half * distsq * distsq + beta - bsum;
2520 for (k = 1; k <= i__1; ++k) {
2521 if (ptsid[k] != zero) {
2524 for (j = 1; j <= i__2; ++j) {
2527 d__1 = zmat[k + j * zmat_dim1];
2528 hdiag += d__1 * d__1;
2532 den = beta * hdiag + d__1 * d__1;
2542 d__1 = vlmxsq, d__2 = d__3 * d__3;
2543 vlmxsq = std::max(d__1,d__2);
2545 if (denom <= vlmxsq * .01) {
2546 w[ndim + knew] = -w[ndim + knew] - winc;
2561 for (kpt = 1; kpt <= i__1; ++kpt) {
2562 if (ptsid[kpt] == zero) {
2567 this->InvokeEvent(itk::EndEvent());
2574 for (j = 1; j <= i__2; ++j) {
2575 w[j] = xpt[kpt + j * xpt_dim1];
2576 xpt[kpt + j * xpt_dim1] = zero;
2577 temp = pq[kpt] * w[j];
2579 for (i__ = 1; i__ <= i__3; ++i__) {
2582 hq[ih] += temp * w[i__];
2586 ip = (
long int) ptsid[kpt];
2587 iq = (
long int) ((
double) np * ptsid[kpt] - (double) (ip * np))
2590 xp = ptsaux[(ip << 1) + 1];
2591 xpt[kpt + ip * xpt_dim1] = xp;
2594 xq = ptsaux[(iq << 1) + 1];
2596 xq = ptsaux[(iq << 1) + 2];
2598 xpt[kpt + iq * xpt_dim1] = xq;
2605 ihp = (ip + ip * ip) / 2;
2606 vquad += xp * (gopt[ip] + half * xp * hq[ihp]);
2609 ihq = (iq + iq * iq) / 2;
2610 vquad += xq * (gopt[iq] + half * xq * hq[ihq]);
2612 iw = std::max(ihp,ihq) - (i__3 = ip - iq, std::abs(i__3));
2613 vquad += xp * xq * hq[iw];
2617 for (k = 1; k <= i__3; ++k) {
2620 temp += xp * xpt[k + ip * xpt_dim1];
2623 temp += xq * xpt[k + iq * xpt_dim1];
2626 vquad += half * pq[k] * temp * temp;
2634 for (i__ = 1; i__ <= i__3; ++i__) {
2637 d__3 = xl[i__], d__4 = xbase[i__] + xpt[kpt + i__ * xpt_dim1];
2638 d__1 = std::max(d__3,d__4), d__2 = xu[i__];
2639 w[i__] = std::min(d__1,d__2);
2640 if (xpt[kpt + i__ * xpt_dim1] == sl[i__]) {
2643 if (xpt[kpt + i__ * xpt_dim1] == su[i__]) {
2651 this->InvokeEvent( itk::IterationEvent() );
2654 for (
unsigned i = 0; i <
m_SpaceDimension; ++i)
px[i] = w[i+1] / this->GetScales()[i];
2658 f = this->m_CostFunction->GetValue(
px);
2683 if (f < fval[kopt]) {
2692 for (i__ = 1; i__ <= i__3; ++i__) {
2694 gopt[i__] += diff * bmat[kpt + i__ * bmat_dim1];
2697 for (k = 1; k <= i__3; ++k) {
2700 for (j = 1; j <= i__2; ++j) {
2702 sum += zmat[k + j * zmat_dim1] * zmat[kpt + j * zmat_dim1];
2705 if (ptsid[k] == zero) {
2708 ip = (
long int) ptsid[k];
2709 iq = (
long int) ((
double) np * ptsid[k] - (double) (ip
2711 ihq = (iq * iq + iq) / 2;
2714 d__1 = ptsaux[(iq << 1) + 2];
2715 hq[ihq] += temp * (d__1 * d__1);
2717 ihp = (ip * ip + ip) / 2;
2719 d__1 = ptsaux[(ip << 1) + 1];
2720 hq[ihp] += temp * (d__1 * d__1);
2723 d__1 = ptsaux[(iq << 1) + 1];
2724 hq[ihq] += temp * (d__1 * d__1);
2725 iw = std::max(ihp,ihq) - (i__2 = iq - ip, std::abs(i__2));
2726 hq[iw] += temp * ptsaux[(ip << 1) + 1] * ptsaux[(iq <<
2742 double *xopt,
double *gopt,
double *hq,
double *pq,
2743 double *sl,
double *su,
double *delta,
double *xnew,
2744 double *d__,
double *gnew,
double *xbdi,
double *s,
2745 double *hs,
double *hred,
double *dsq,
double *crvmin)
2748 long int xpt_dim1, xpt_offset, i__1, i__2;
2749 double d__1, d__2, d__3, d__4;
2752 long int i__, j, k, ih;
2755 double dhd, dhs, cth, one, shs, sth, ssq, half, beta, sdec, blen;
2756 long int iact = 0, nact;
2759 double temp, zero, xsav = 0, xsum, angbd = 0, dredg = 0, sredg = 0;
2761 double resid, delsq, ggsav = 0, tempa, tempb, redmax, dredsq = 0, redsav, onemin, gredsq = 0, rednew;
2762 long int itcsav = 0;
2763 double rdprev = 0, rdnext = 0, stplen, stepsq;
2764 long int itermax = 0;
2809 xpt_offset = 1 + xpt_dim1;
2841 for (i__ = 1; i__ <= i__1; ++i__) {
2843 if (xopt[i__] <= sl[i__]) {
2844 if (gopt[i__] >= zero) {
2847 }
else if (xopt[i__] >= su[i__]) {
2848 if (gopt[i__] <= zero) {
2852 if (xbdi[i__] != zero) {
2857 gnew[i__] = gopt[i__];
2859 delsq = *delta * *delta;
2874 for (i__ = 1; i__ <= i__1; ++i__) {
2875 if (xbdi[i__] != zero) {
2877 }
else if (beta == zero) {
2878 s[i__] = -gnew[i__];
2880 s[i__] = beta * s[i__] - gnew[i__];
2885 stepsq += d__1 * d__1;
2887 if (stepsq == zero) {
2894 if (gredsq * delsq <= qred * 1e-4 * qred) {
2909 for (i__ = 1; i__ <= i__1; ++i__) {
2910 if (xbdi[i__] == zero) {
2913 resid -= d__1 * d__1;
2914 ds += s[i__] * d__[i__];
2915 shs += s[i__] * hs[i__];
2919 if (resid <= zero) {
2922 temp = std::sqrt(stepsq * resid + ds * ds);
2924 blen = (temp - ds) / stepsq;
2926 blen = resid / (temp + ds);
2931 d__1 = blen, d__2 = gredsq / shs;
2932 stplen = std::min(d__1,d__2);
2940 for (i__ = 1; i__ <= i__1; ++i__) {
2941 if (s[i__] != zero) {
2942 xsum = xopt[i__] + d__[i__];
2943 if (s[i__] > zero) {
2944 temp = (su[i__] - xsum) / s[i__];
2946 temp = (sl[i__] - xsum) / s[i__];
2948 if (temp < stplen) {
2959 if (stplen > zero) {
2961 temp = shs / stepsq;
2962 if (iact == 0 && temp > zero) {
2963 *crvmin = std::min(*crvmin,temp);
2964 if (*crvmin == onemin) {
2971 for (i__ = 1; i__ <= i__1; ++i__) {
2972 gnew[i__] += stplen * hs[i__];
2973 if (xbdi[i__] == zero) {
2976 gredsq += d__1 * d__1;
2979 d__[i__] += stplen * s[i__];
2982 d__1 = stplen * (ggsav - half * stplen * shs);
2983 sdec = std::max(d__1,zero);
2992 if (s[iact] < zero) {
2993 xbdi[iact] = onemin;
2997 delsq -= d__1 * d__1;
2998 if (delsq <= zero) {
3007 if (stplen < blen) {
3008 if (iterc == itermax) {
3011 if (sdec <= qred * .01) {
3014 beta = gredsq / ggsav;
3032 for (i__ = 1; i__ <= i__1; ++i__) {
3033 if (xbdi[i__] == zero) {
3036 dredsq += d__1 * d__1;
3037 dredg += d__[i__] * gnew[i__];
3040 gredsq += d__1 * d__1;
3055 temp = gredsq * dredsq - dredg * dredg;
3056 if (temp <= qred * 1e-4 * qred) {
3059 temp = std::sqrt(temp);
3061 for (i__ = 1; i__ <= i__1; ++i__) {
3062 if (xbdi[i__] == zero) {
3063 s[i__] = (dredg * d__[i__] - dredsq * gnew[i__]) / temp;
3079 for (i__ = 1; i__ <= i__1; ++i__) {
3080 if (xbdi[i__] == zero) {
3081 tempa = xopt[i__] + d__[i__] - sl[i__];
3082 tempb = su[i__] - xopt[i__] - d__[i__];
3083 if (tempa <= zero) {
3087 }
else if (tempb <= zero) {
3096 ssq = d__1 * d__1 + d__2 * d__2;
3098 d__1 = xopt[i__] - sl[i__];
3099 temp = ssq - d__1 * d__1;
3101 temp = std::sqrt(temp) - s[i__];
3102 if (angbd * temp > tempa) {
3103 angbd = tempa / temp;
3109 d__1 = su[i__] - xopt[i__];
3110 temp = ssq - d__1 * d__1;
3112 temp = std::sqrt(temp) + s[i__];
3113 if (angbd * temp > tempb) {
3114 angbd = tempb / temp;
3131 for (i__ = 1; i__ <= i__1; ++i__) {
3132 if (xbdi[i__] == zero) {
3133 shs += s[i__] * hs[i__];
3134 dhs += d__[i__] * hs[i__];
3135 dhd += d__[i__] * hred[i__];
3147 iu = (
long int) (angbd * 17. + 3.1);
3149 for (i__ = 1; i__ <= i__1; ++i__) {
3150 angt = angbd * (double) i__ / (
double) iu;
3151 sth = (angt + angt) / (one + angt * angt);
3152 temp = shs + angt * (angt * dhd - dhs - dhs);
3153 rednew = sth * (angt * dredg - sredg - half * sth * temp);
3154 if (rednew > redmax) {
3158 }
else if (i__ == isav + 1) {
3172 temp = (rdnext - rdprev) / (redmax + redmax - rdprev - rdnext);
3173 angt = angbd * ((double) isav + half * temp) / (double) iu;
3175 cth = (one - angt * angt) / (one + angt * angt);
3176 sth = (angt + angt) / (one + angt * angt);
3177 temp = shs + angt * (angt * dhd - dhs - dhs);
3178 sdec = sth * (angt * dredg - sredg - half * sth * temp);
3190 for (i__ = 1; i__ <= i__1; ++i__) {
3191 gnew[i__] = gnew[i__] + (cth - one) * hred[i__] + sth * hs[i__];
3192 if (xbdi[i__] == zero) {
3193 d__[i__] = cth * d__[i__] + sth * s[i__];
3194 dredg += d__[i__] * gnew[i__];
3197 gredsq += d__1 * d__1;
3200 hred[i__] = cth * hred[i__] + sth * hs[i__];
3203 if (iact > 0 && isav == iu) {
3212 if (sdec > qred * .01) {
3218 for (i__ = 1; i__ <= i__1; ++i__) {
3221 d__3 = xopt[i__] + d__[i__], d__4 = su[i__];
3222 d__1 = std::min(d__3,d__4), d__2 = sl[i__];
3223 xnew[i__] = std::max(d__1,d__2);
3224 if (xbdi[i__] == onemin) {
3225 xnew[i__] = sl[i__];
3227 if (xbdi[i__] == one) {
3228 xnew[i__] = su[i__];
3230 d__[i__] = xnew[i__] - xopt[i__];
3234 *dsq += d__1 * d__1;
3245 for (j = 1; j <= i__1; ++j) {
3248 for (i__ = 1; i__ <= i__2; ++i__) {
3251 hs[j] += hq[ih] * s[i__];
3254 hs[i__] += hq[ih] * s[j];
3258 for (k = 1; k <= i__2; ++k) {
3259 if (pq[k] != zero) {
3262 for (j = 1; j <= i__1; ++j) {
3264 temp += xpt[k + j * xpt_dim1] * s[j];
3268 for (i__ = 1; i__ <= i__1; ++i__) {
3270 hs[i__] += temp * xpt[k + i__ * xpt_dim1];
3275 if (*crvmin != zero) {
3278 if (iterc > itcsav) {
3282 for (i__ = 1; i__ <= i__2; ++i__) {
3284 hred[i__] = hs[i__];
3290 double *zmat,
long int &ndim,
double *vlag,
double *beta,
3291 double *denom,
long int &knew,
double *w)
3294 long int bmat_dim1, bmat_offset, zmat_dim1, zmat_offset, i__1, i__2;
3295 double d__1, d__2, d__3;
3298 long int i__, j, k, jp;
3299 double one, tau, temp;
3301 double zero, alpha, tempa, tempb, ztest;
3317 zmat_offset = 1 + zmat_dim1;
3318 zmat -= zmat_offset;
3320 bmat_offset = 1 + bmat_dim1;
3321 bmat -= bmat_offset;
3331 for (k = 1; k <= i__1; ++k) {
3333 for (j = 1; j <= i__2; ++j) {
3336 d__2 = ztest, d__3 = (d__1 = zmat[k + j * zmat_dim1], std::abs(d__1));
3337 ztest = std::max(d__2,d__3);
3344 for (j = 2; j <= i__2; ++j) {
3345 if ((d__1 = zmat[knew + j * zmat_dim1], std::abs(d__1)) > ztest) {
3347 d__1 = zmat[knew + zmat_dim1];
3349 d__2 = zmat[knew + j * zmat_dim1];
3350 temp = std::sqrt(d__1 * d__1 + d__2 * d__2);
3351 tempa = zmat[knew + zmat_dim1] / temp;
3352 tempb = zmat[knew + j * zmat_dim1] / temp;
3354 for (i__ = 1; i__ <= i__1; ++i__) {
3355 temp = tempa * zmat[i__ + zmat_dim1] + tempb * zmat[i__ + j *
3357 zmat[i__ + j * zmat_dim1] = tempa * zmat[i__ + j * zmat_dim1]
3358 - tempb * zmat[i__ + zmat_dim1];
3360 zmat[i__ + zmat_dim1] = temp;
3363 zmat[knew + j * zmat_dim1] = zero;
3371 for (i__ = 1; i__ <= i__2; ++i__) {
3372 w[i__] = zmat[knew + zmat_dim1] * zmat[i__ + zmat_dim1];
3381 temp = std::sqrt(*denom);
3382 tempb = zmat[knew + zmat_dim1] / temp;
3385 for (i__ = 1; i__ <= i__2; ++i__) {
3387 zmat[i__ + zmat_dim1] = tempa * zmat[i__ + zmat_dim1] - tempb * vlag[
3394 for (j = 1; j <= i__2; ++j) {
3396 w[jp] = bmat[knew + j * bmat_dim1];
3397 tempa = (alpha * vlag[jp] - tau * w[jp]) / *denom;
3398 tempb = (-(*beta) * w[jp] - tau * vlag[jp]) / *denom;
3400 for (i__ = 1; i__ <= i__1; ++i__) {
3401 bmat[i__ + j * bmat_dim1] = bmat[i__ + j * bmat_dim1] + tempa *
3402 vlag[i__] + tempb * w[i__];
3404 bmat[jp + (i__ - npt) * bmat_dim1] = bmat[i__ + j *
int update_(long int &npt, double *bmat, double *zmat, long int &ndim, double *vlag, double *beta, double *denom, long int &knew, double *w)
int bobyqb_(long int &npt, double *x, double *xl, double *xu, double &rhobeg, double &rhoend, long int &maxfun, double *xbase, double *xpt, double *fval, double *xopt, double *gopt, double *hq, double *pq, double *bmat, double *zmat, long int &ndim, double *sl, double *su, double *xnew, double *xalt, double *d__, double *vlag, double *w)
unsigned int m_CurrentIteration
unsigned int m_SpaceDimension
int trsbox_(long int &npt, double *xpt, double *xopt, double *gopt, double *hq, double *pq, double *sl, double *su, double *delta, double *xnew, double *d__, double *gnew, double *xbdi, double *s, double *hs, double *hred, double *dsq, double *crvmin)
MeasureType m_CurrentCost
int altmov_(long int &npt, double *xpt, double *xopt, double *bmat, double *zmat, long int &ndim, double *sl, double *su, long int &kopt, long int &knew, double *adelt, double *xnew, double *xalt, double *alpha, double *cauchy, double *glag, double *hcol, double *w)
const std::string GetStopConditionDescription() const ITK_OVERRIDE
itk::SingleValuedNonLinearOptimizer::ParametersType ParametersType
void StartOptimization() ITK_OVERRIDE
unsigned int m_MaximumIteration
int prelim_(long int &npt, double *x, double *xl, double *xu, double &rhobeg, long int &maxfun, double *xbase, double *xpt, double *fval, double *gopt, double *hq, double *pq, double *bmat, double *zmat, long int &ndim, double *sl, double *su, long int &nf, long int &kopt)
BobyqaOptimizer::ParametersType px
bool m_CatchGetValueException
virtual ~BobyqaOptimizer()
int rescue_(long int &npt, double *xl, double *xu, long int &maxfun, double *xbase, double *xpt, double *fval, double *xopt, double *gopt, double *hq, double *pq, double *bmat, double *zmat, long int &ndim, double *sl, double *su, long int &nf, double *delta, long int &kopt, double *vlag, double *ptsaux, double *ptsid, double *w)
void PrintSelf(std::ostream &os, itk::Indent indent) const ITK_OVERRIDE
std::ostringstream m_StopConditionDescription
double m_MetricWorstPossibleValue
int optimize(long int &npt, double *x, double *xl, double *xu, double &rhobeg, double &rhoend, long int &maxfun, double *w)