226 const int ci = detail::climbingStopImage(path);
227 if (n < 3 || ci <= 0 || ci >= n - 1)
return out;
229 if (!std::isfinite(e_ci))
return out;
231 (std::isfinite(force_tol) && force_tol > 0.0) ? force_tol : 0.0;
232 constexpr int kMaxRelaxSteps = 200;
233 constexpr int kMaxBacktracks = 24;
234 constexpr double kArmijo = 1e-4;
235 constexpr double kEnergyConfidence = 2.0;
236 const double variance_floor = std::max(0.0, session.
gp.
getSigma2());
237 out.evaluated =
true;
239 out.excess = -std::numeric_limits<double>::infinity();
240 double worst_upper_excess = -std::numeric_limits<double>::infinity();
241 const double nan = std::numeric_limits<double>::quiet_NaN();
242 out.segment_excess.assign(
static_cast<std::size_t
>(n - 1), nan);
243 out.segment_raw_excess.assign(
static_cast<std::size_t
>(n - 1), nan);
244 out.segment_variance.assign(
static_cast<std::size_t
>(n - 1), nan);
245 out.segment_position.assign(
static_cast<std::size_t
>(n - 1),
247 for (
int i = 0; i + 1 < n; ++i) {
250 if (a.size() != b.size() || a.size() == 0)
continue;
251 Eigen::VectorXd tau = b - a;
252 const double chord = tau.norm();
253 if (!(chord > 0.0) || !std::isfinite(chord))
continue;
255 const Eigen::VectorXd mid = 0.5 * (a + b);
256 Eigen::VectorXd
x = mid;
257 double e = surrogateEnergy(session,
x);
258 if (!std::isfinite(e))
continue;
259 const double e_raw = e;
260 const double v_mid = surrogateEnergyVariance(session, mid);
261 const double trust_cap = std::isfinite(v_mid)
262 ? std::max(variance_floor, v_mid)
264 double step = 0.5 * chord;
265 const auto descend_from_observed_rows = [&]() {
270 if (rows.cols() !=
x.size())
return false;
271 std::vector<std::pair<double, Eigen::Index>> candidates;
272 const auto projected = [&](Eigen::Index row) -> Eigen::VectorXd {
273 const Eigen::VectorXd offset = rows.row(row).transpose() - mid;
274 return mid + offset - tau.dot(offset) * tau;
276 for (Eigen::Index row = 0; row < rows.rows(); ++row) {
277 const Eigen::VectorXd trial = projected(row);
278 if (trial.allFinite() && (trial - mid).norm() <= chord &&
279 !(trial.array() ==
x.array()).all())
280 candidates.emplace_back((trial -
x).squaredNorm(), row);
282 std::sort(candidates.begin(), candidates.end());
283 for (
const auto& candidate: candidates) {
284 const Eigen::VectorXd trial = projected(candidate.second);
285 const double trial_energy = surrogateEnergy(session, trial);
286 if (!std::isfinite(trial_energy) || !(trial_energy < e))
288 const double variance = surrogateEnergyVariance(session, trial);
289 if (!std::isfinite(variance) || variance > trust_cap)
continue;
297 for (
int it = 0; it < kMaxRelaxSteps && tol > 0.0 && e > e_ci; ++it) {
298 const Eigen::VectorXd g = surrogateGradient(session,
x);
299 if (g.size() !=
x.size() || !g.allFinite())
break;
300 Eigen::VectorXd f = -(g - g.dot(tau) * tau);
301 const double fn = f.norm();
303 if (descend_from_observed_rows())
continue;
307 bool accepted =
false;
308 const double initial_step = step;
309 bool rejected_by_variance =
false;
310 for (
int direction = 0; direction < 2 && !accepted; ++direction) {
311 if (direction == 1) {
312 if (!rejected_by_variance)
break;
313 Eigen::VectorXd normal =
314 surrogateEnergyVarianceGradient(session,
x);
315 if (normal.size() !=
x.size() || !normal.allFinite())
break;
316 normal -= normal.dot(tau) * tau;
317 const double normal_norm = normal.norm();
318 if (std::getenv(
"GPR_MIDPOINT_DEBUG"))
319 std::cerr <<
"[midpoint] seg=" << i
320 <<
" energy_direction=" << f.transpose()
321 <<
" variance_gradient=" << normal.transpose()
322 <<
" cap=" << trust_cap << std::endl;
323 if (!(normal_norm > 0.0))
break;
324 normal /= normal_norm;
330 const double feasible_norm = f.norm();
331 if (!(feasible_norm >
332 std::sqrt(std::numeric_limits<double>::epsilon())))
337 const double descent = -g.dot(f);
338 if (!(descent > 0.0))
break;
339 for (
int ls = 0; ls < kMaxBacktracks; ++ls) {
340 const Eigen::VectorXd xt =
x + step * f;
341 if ((xt.array() ==
x.array()).all())
break;
342 if ((xt - mid).norm() > chord) {
346 const double et = surrogateEnergy(session, xt);
347 const double vt = surrogateEnergyVariance(session, xt);
348 const bool trusted = std::isfinite(vt) && vt <= trust_cap;
349 if (std::getenv(
"GPR_MIDPOINT_DEBUG") &&
350 (ls == 0 || ls + 1 == kMaxBacktracks))
351 std::cerr <<
"[midpoint] seg=" << i
352 <<
" direction=" << direction <<
" ls=" << ls
353 <<
" step=" << step <<
" e=" << e
354 <<
" trial=" << et <<
" variance=" << vt
355 <<
" cap=" << trust_cap
356 <<
" descent=" << descent << std::endl;
357 rejected_by_variance = rejected_by_variance || !trusted;
358 if (trusted && std::isfinite(et) && et < e &&
359 et <= e - kArmijo * step * descent) {
363 step = std::min(2.0 * step, chord);
370 if (descend_from_observed_rows())
continue;
373 if (std::getenv(
"GPR_MIDPOINT_DEBUG")) {
374 std::cerr <<
"[midpoint] seg=" << i <<
" it=" << it
375 <<
" e=" << e <<
" fn=" << fn <<
" step=" << step
376 <<
" disp=" << (
x - mid).norm() <<
" chord=" << chord
380 if (std::getenv(
"GPR_MIDPOINT_DEBUG")) {
382 <<
"[midpoint] seg=" << i <<
" final e=" << e
384 <<
" e_raw=" << surrogateEnergy(session, mid)
385 <<
" variance=" << surrogateEnergyVariance(session,
x)
392 const double excess = e - e_ci;
393 const double v_x = surrogateEnergyVariance(session,
x);
397 const double uncertainty =
399 ? kEnergyConfidence *
400 std::sqrt(std::max(0.0, v_x - variance_floor))
401 : std::numeric_limits<double>::infinity();
402 const double upper_excess = excess + uncertainty;
403 out.segment_excess[
static_cast<std::size_t
>(i)] = excess;
404 out.segment_raw_excess[
static_cast<std::size_t
>(i)] = e_raw - e_ci;
405 out.segment_variance[
static_cast<std::size_t
>(i)] = v_x;
406 out.segment_position[
static_cast<std::size_t
>(i)] =
x;
407 if (upper_excess > worst_upper_excess) {
408 worst_upper_excess = upper_excess;
410 out.raw_excess = e_raw - e_ci;
411 out.worst_segment = i;
412 out.worst_position =
x;
413 out.worst_variance = v_x;
415 if (upper_excess > 0.0)
out.holds =
false;
417 if (!std::isfinite(
out.excess))
out.excess = 0.0;
561 const Eigen::VectorXd& x_ci,
double e_ci,
const Eigen::VectorXd& g_ci,
562 double hessian_fd_step,
double max_move,
double force_tol,
566 const Eigen::Index n = x_ci.size();
567 if (n < 3 || g_ci.size() != n || !(hessian_fd_step > 0.0) ||
568 !std::isfinite(hessian_fd_step) || !(force_tol > 0.0)) {
569 out.reason =
"invalid input";
572 const Eigen::VectorXd
x = detail::broadcastRootVector(x_ci);
574 const Eigen::MatrixXd Q =
576 const Eigen::Index dimension = Q.cols();
578 out.reason =
"fewer than two non-rigid modes";
582 const double h = hessian_fd_step;
583 const Eigen::MatrixXd H = surrogateHessian(session,
x);
584 if (H.rows() != n || !H.allFinite()) {
586 "surrogate Hessian unavailable (rows=" + std::to_string(H.rows()) +
590 const Eigen::MatrixXd surrogate = Q.transpose() * H * Q;
591 Eigen::SelfAdjointEigenSolver<Eigen::MatrixXd> es(surrogate);
592 if (es.info() != Eigen::Success) {
593 out.reason =
"eigensolver failed";
596 out.evaluated =
true;
597 out.surrogate_lambda1 = es.eigenvalues()(0);
598 out.surrogate_lambda2 = es.eigenvalues()(1);
601 const int probes = 2 *
static_cast<int>(dimension);
602 Eigen::MatrixXd positions(n, probes);
603 for (Eigen::Index j = 0; j < dimension; ++j) {
604 positions.col(2 * j) =
x + h * Q.col(j);
605 positions.col(2 * j + 1) =
x - h * Q.col(j);
607 const auto observations = detail::evaluateOwnedOraclePositions(
608 positions, oracle, batch_oracle, batch_chunk);
609 out.oracle_calls = probes;
611 for (
int i = 0; i < probes; ++i) {
612 if (observations.valid[i]) {
614 observations.gradient.row(i).transpose());
620 out.reason =
"oracle probe failed";
624 out.rounds =
static_cast<int>(dimension);
635 const Eigen::MatrixXd Q_sq = Q.array().square().matrix();
636 const auto physical_variance =
637 [&](
const Eigen::VectorXd& cartesian_error) -> Eigen::VectorXd {
638 return Q_sq.transpose() * cartesian_error.array().square().matrix();
640 const double eps = std::numeric_limits<double>::epsilon();
643 double action_scale = g_ci.norm();
644 for (Eigen::Index j = 0; j < dimension; ++j)
646 std::max(action_scale, (observations.gradient.row(2 * j) -
647 observations.gradient.row(2 * j + 1))
650 const double roundoff_scale = eps * action_scale;
651 const auto roundoff_error = [&](
const Eigen::VectorXd& g_plus,
652 const Eigen::VectorXd& g_minus,
653 double step) -> Eigen::VectorXd {
654 if (!(step > 0.0) || !std::isfinite(step) ||
655 g_plus.size() != g_minus.size() || !g_plus.allFinite() ||
656 !g_minus.allFinite()) {
657 return Eigen::VectorXd::Zero(g_plus.size());
659 return (eps * (g_plus.array().abs() + g_minus.array().abs()) /
665 struct ColumnStencil {
666 Eigen::VectorXd coarse_action;
667 Eigen::VectorXd even_residual;
669 std::vector<gpr::optim::MeasuredHessianColumn> columns;
670 std::vector<ColumnStencil> stencils;
671 Eigen::MatrixXd second_differences = Eigen::MatrixXd::Zero(n, dimension);
672 columns.reserve(
static_cast<std::size_t
>(dimension));
673 for (Eigen::Index j = 0; j < dimension; ++j) {
674 const Eigen::VectorXd g_plus =
675 observations.gradient.row(2 * j).transpose();
676 const Eigen::VectorXd g_minus =
677 observations.gradient.row(2 * j + 1).transpose();
678 const Eigen::VectorXd coarse = (g_plus - g_minus) / (2.0 * h);
679 const Eigen::VectorXd even = 0.5 * (g_plus + g_minus) - g_ci;
680 const Eigen::VectorXd roundoff = roundoff_error(g_plus, g_minus, h);
681 Eigen::VectorXd error;
682 if (!even.allFinite() || !(h > 0.0) || !std::isfinite(h)) {
683 error = Eigen::VectorXd::Zero(even.size());
685 error = (even.array().abs() / h).max(roundoff.array()).matrix();
687 columns.push_back({Eigen::VectorXd::Unit(dimension, j),
688 Q.transpose() * coarse, physical_variance(error),
690 stencils.push_back({coarse, even});
691 second_differences.col(j) = (2.0 / (h * h)) * even;
697 const auto refine_column = [&](Eigen::Index j) {
698 auto& column = columns[
static_cast<std::size_t
>(j)];
699 const auto& stencil = stencils[
static_cast<std::size_t
>(j)];
700 Eigen::MatrixXd pair(n, 2);
701 pair.col(0) =
x + 0.5 * h * Q.col(j);
702 pair.col(1) =
x - 0.5 * h * Q.col(j);
703 const auto probed = detail::evaluateOwnedOraclePositions(
704 pair, oracle, batch_oracle, batch_chunk);
705 out.oracle_calls += 2;
706 bool probed_valid =
true;
707 for (
int i = 0; i < 2; ++i) {
710 probed.gradient.row(i).transpose());
712 probed_valid =
false;
714 if (!probed_valid)
return false;
715 const Eigen::VectorXd g_plus = probed.gradient.row(0).transpose();
716 const Eigen::VectorXd g_minus = probed.gradient.row(1).transpose();
717 const Eigen::VectorXd fine = (g_plus - g_minus) / h;
718 const Eigen::VectorXd refined =
719 (4.0 * fine - stencil.coarse_action) / 3.0;
720 const Eigen::VectorXd truncation = (stencil.coarse_action - fine) / 3.0;
721 column.action = Q.transpose() * refined;
722 column.variance = physical_variance(
725 .max(roundoff_error(g_plus, g_minus, 0.5 * h).array())
727 column.refined =
true;
728 std::cout <<
"[ocineb] index certificate refined column " << j <<
": "
729 << Q.col(j).dot(stencil.coarse_action) <<
" coarse, "
730 << Q.col(j).dot(refined) <<
" extrapolated, truncation "
731 << Q.col(j).dot(truncation) << std::endl;
734 const Eigen::VectorXd gradient_physical = Q.transpose() * g_ci;
736 const auto stationarity_floor = [&](Eigen::Index k) {
738 Q, second_differences, Q * smoothed.
modes.col(k));
739 const double f = gradient_physical.dot(smoothed.
modes.col(k));
740 return std::sqrt(2.0 * std::max(b * f, 0.0));
748 const auto resolution = [&](Eigen::Index k) {
return smoothed.
spreads(k); };
749 const auto irreducible = [&](Eigen::Index k) {
return smoothed.
floors(k); };
750 const auto decidable = [&](Eigen::Index k) {
752 return curvature * curvature > irreducible(k) * irreducible(k);
757 enum class IndexVerdict {
765 IndexVerdict verdict = IndexVerdict::Undecided;
766 double resolution1 = std::numeric_limits<double>::infinity();
767 double resolution2 = std::numeric_limits<double>::infinity();
768 while (verdict == IndexVerdict::Undecided) {
770 surrogate, columns, smoothed.
valid ? &smoothed :
nullptr,
773 if (!smoothed.
valid) {
774 out.reason =
"projected eigensolver failed";
778 resolution1 = resolution(0);
779 resolution2 = resolution(1);
786 out.mode1 = Q * smoothed.
modes.col(0);
787 out.mode2 = Q * smoothed.
modes.col(1);
788 out.residual1 = h * resolution1;
789 out.residual2 = h * resolution2;
790 std::cout <<
"[ocineb] index certificate at energy " << e_ci
791 <<
": lowest curvature " <<
out.oracle_curvature1
792 <<
" (resolution " << resolution1 <<
"), second "
793 <<
out.oracle_curvature2 <<
" (resolution " << resolution2
794 <<
", spread " << smoothed.
spreads(1)
795 <<
", stationarity floor " << stationarity_floor(1)
796 <<
", irreducible " << irreducible(1) <<
"), calls "
797 <<
out.oracle_calls << std::endl;
798 if (!(
out.oracle_curvature1 + resolution1 < 0.0)) {
799 if (
out.oracle_curvature1 - resolution1 > 0.0) {
800 out.reason =
"resolved minimum";
801 verdict = IndexVerdict::Minimum;
806 const Eigen::Index coarse =
810 if (coarse >= 0 && refine_column(coarse))
continue;
811 out.reason =
"lowest curvature is unresolved";
812 verdict = IndexVerdict::Unresolved;
815 if (
out.oracle_curvature2 - resolution2 > 0.0) {
816 out.reason =
"resolved positive second curvature";
817 out.positive_complement_lower_bound =
818 out.oracle_curvature2 - resolution2;
819 verdict = IndexVerdict::IndexOne;
822 if (
out.oracle_curvature2 + resolution2 < 0.0) {
827 Q, second_differences, Q * smoothed.
modes.col(1));
828 const double f = gradient_physical.dot(smoothed.
modes.col(1));
830 const double sigma_b = 2.0 * sigma_f / (h * h);
832 out.oracle_curvature2, resolution2, f, sigma_f, b, sigma_b);
833 std::cout <<
"[ocineb] index certificate: second curvature at the "
834 "stationary point in ["
835 << stationary.lower <<
", " << stationary.upper
836 <<
"], undetermined fraction " << stationary.undetermined
837 <<
", force target " << stationary.force_target
839 if (stationary.valid && !(stationary.upper < 0.0) &&
840 stationary.force_decidable &&
841 stationary.force_target < force_tol) {
842 out.polish_force_target = stationary.force_target;
844 "second curvature negative at the point, "
845 "undetermined at the stationary point";
846 verdict = IndexVerdict::Polish;
849 out.reason =
"resolved negative second curvature";
850 verdict = IndexVerdict::HigherIndex;
855 const Eigen::Index coarse =
859 if (coarse >= 0 && refine_column(coarse))
continue;
863 out.reason =
"second curvature unresolved within " +
864 std::to_string(resolution2) +
" of zero";
865 verdict = IndexVerdict::Unresolved;
867 out.certified = verdict == IndexVerdict::IndexOne;
875 const bool escape = verdict == IndexVerdict::Minimum ||
876 verdict == IndexVerdict::HigherIndex;
877 if (
out.certified || !(max_move > 0.0) || !escape)
return out;
883 const Eigen::VectorXd& mode =
884 verdict == IndexVerdict::HigherIndex ?
out.mode2 :
out.mode1;
885 const Eigen::VectorXd xa =
x + max_move * mode;
886 const Eigen::VectorXd xb =
x - max_move * mode;
887 const double ea = surrogateEnergy(session, xa);
888 const double eb = surrogateEnergy(session, xb);
891 const bool prefer_b = std::isfinite(eb) && (!std::isfinite(ea) || eb < ea);
892 out.displaced_positions = prefer_b ? xb : xa;