209 refuseStaleMinimizationConfig(config);
211 throw std::invalid_argument(
212 "MinimizationConfig::max_inner_steps must be >= 1");
219 throw std::invalid_argument(
220 "minimization secant curvature seed requires the BFGS method");
221 const int d =
static_cast<int>(x_init.size());
250 bool warm_kernel_pending =
false;
251 if (seed !=
nullptr || gp_state !=
nullptr) {
253 if (gp_state !=
nullptr) warm = *gp_state;
254 if (seed !=
nullptr && seed->
R.rows() > 0)
258 session, warm,
true, x_init.size());
263 int hyperparameter_refits = 0;
264 bool has_fitted_gp =
false;
265 bool has_conditioned_gp =
false;
266 Eigen::Index seeded_row_count = 0;
268 std::size_t checked_secant_pairs = 0;
269 std::size_t prior_secant_pairs = 0;
271 std::size_t newton_factor_pairs = 0;
273 double seed_bound = std::numeric_limits<double>::infinity();
281 const auto budget_allows = [&]() {
285 const auto measure = [&](
const Eigen::VectorXd&
x) {
287 auto measured = oracle(
x);
290 if (config.
secant.
enabled() && measured.second.size() ==
x.size() &&
291 measured.second.allFinite()) {
292 const bool had_reference =
298 secant.
observe(
x, measured.second, measured.first);
305 const Eigen::VectorXd&
x,
double variance,
306 double distance,
bool within_trust,
307 bool surrogate_stop) {
308 if (!budget_allows())
return false;
309 if (
policy ==
nullptr)
return true;
325 state.
gp = has_conditioned_gp ? &session.
gp :
nullptr;
328 double best_observed_energy = std::numeric_limits<double>::infinity();
329 Eigen::VectorXd best_observed_x = x_init;
330 Eigen::VectorXd best_observed_g = Eigen::VectorXd::Zero(d);
331 bool has_best_observed =
false;
332 result.
e_final = std::numeric_limits<double>::quiet_NaN();
334 Eigen::VectorXd::Constant(d, std::numeric_limits<double>::quiet_NaN());
339 struct RawOracleObservation {
342 Eigen::VectorXd gradient;
344 std::vector<RawOracleObservation> raw_oracle_obs;
345 const auto find_exact_raw_observation =
346 [&](
const Eigen::VectorXd&
x) ->
const RawOracleObservation* {
347 for (
auto it = raw_oracle_obs.rbegin(); it != raw_oracle_obs.rend();
349 if (it->x.size() ==
x.size() &&
350 (it->x.array() ==
x.array()).all()) {
357 bool pending_retain =
false;
358 const auto admit = [&](
const Eigen::VectorXd&
x,
double energy,
359 const Eigen::VectorXd& gradient,
bool engine_would) {
360 if (!engine_would)
return false;
365 candidate.
energy = energy;
367 const std::vector<Eigen::VectorXd> current_rows =
369 const std::vector<int> indices =
379 const Eigen::VectorXd anchor =
380 has_best_observed ? best_observed_x :
x;
384 pending_retain =
true;
390 const auto retain_admitted = [&]() {
391 if (
policy !=
nullptr && pending_retain) {
394 pending_retain =
false;
397 const auto record_initial_observation =
398 [&](
const Eigen::VectorXd&
x,
double energy,
399 const Eigen::VectorXd& gradient) {
400 if (!std::isfinite(energy) || gradient.size() != d ||
401 !gradient.allFinite()) {
404 raw_oracle_obs.push_back({
x, energy, gradient});
406 if (!has_best_observed || energy < best_observed_energy) {
407 best_observed_energy = energy;
409 best_observed_g = gradient;
410 has_best_observed =
true;
415 return admit(
x, energy, gradient,
true);
418 for (Eigen::Index row = 0; row < session.
training_obs.
R.rows(); ++row) {
419 const Eigen::VectorXd seeded_x =
422 const Eigen::VectorXd seeded_gradient =
424 if (seeded_x.size() != d)
425 throw std::runtime_error(
426 "gp-state.npz coordinates do not match this search (" +
427 std::to_string(seeded_x.size()) +
" columns, this search has " +
428 std::to_string(d) +
")");
429 if (!seeded_x.allFinite() || !std::isfinite(seeded_energy) ||
430 seeded_gradient.size() != d || !seeded_gradient.allFinite())
431 throw std::runtime_error(
"gp-state.npz seed row " +
432 std::to_string(row) +
" is non-finite");
433 raw_oracle_obs.push_back({seeded_x, seeded_energy, seeded_gradient});
438 if (
const RawOracleObservation* seeded =
439 find_exact_raw_observation(x_init)) {
441 best_observed_energy = seeded->energy;
442 best_observed_x = seeded->x;
443 best_observed_g = seeded->gradient;
444 has_best_observed =
true;
446 result.
e_final = seeded->energy;
447 result.
g_final = seeded->gradient;
449 std::numeric_limits<double>::quiet_NaN(),
450 std::numeric_limits<double>::infinity(),
false,
452 auto [e_init, g_init] = measure(x_init);
453 record_initial_observation(x_init, e_init, g_init);
455 if (!has_best_observed && result.
oracle_calls == 0)
return result;
458 std::mt19937 rng(config.
seed);
459 std::uniform_real_distribution<double> dist(-0.5, 0.5);
461 if (!budget_allows())
break;
462 Eigen::VectorXd x_p = x_init;
463 for (
int i = 0; i < d; ++i) {
467 std::numeric_limits<double>::quiet_NaN(),
468 std::numeric_limits<double>::infinity(),
false,
472 auto [e_p, g_p] = measure(x_p);
473 record_initial_observation(x_p, e_p, g_p);
477 if (!has_best_observed) {
481 if (best_observed_g.size() == d && best_observed_g.allFinite() &&
482 best_observed_g.norm() < config.
conv_tol) {
484 result.
x_final = best_observed_x;
485 result.
e_final = best_observed_energy;
486 result.
g_final = best_observed_g;
494 Eigen::VectorXd x_curr = best_observed_x;
498 for (
int outer = 0; outer < config.
max_iter; ++outer) {
499 if (!budget_allows())
break;
502 if (warm_kernel_pending) {
507 has_fitted_gp =
true;
508 has_conditioned_gp =
true;
511 warm_kernel_pending =
false;
519 bool secant_prior_moved =
false;
521 prior_secant_pairs != secant.
pairs()) {
526 throw std::runtime_error(
527 "secant quadratic prior mean requires a finite oracle "
528 "energy at the reference geometry");
532 prior_secant_pairs = secant.
pairs();
533 secant_prior_moved =
true;
537 has_conditioned_gp =
true;
542 const Eigen::Index current_rows = session.
training_obs.
R.rows();
543 const bool reinit_seeds =
545 (seeded_row_count < 3 && current_rows > seeded_row_count);
546 if (session.
refitGP(reinit_seeds)) {
547 ++hyperparameter_refits;
548 has_fitted_gp =
true;
550 seeded_row_count = std::min<Eigen::Index>(
551 3, std::max(seeded_row_count, current_rows));
554 has_conditioned_gp =
true;
563 secant.
pairs() > checked_secant_pairs) {
564 const Eigen::VectorXd direction =
566 Coord at_reference(1, d);
568 Eigen::VectorXd posterior_action;
570 at_reference, direction, posterior_action)) {
571 throw std::runtime_error(
572 "secant consistency requested but GP Hessian action "
578 throw std::runtime_error(
579 "secant consistency requested but comparison is invalid");
581 checked_secant_pairs = secant.
pairs();
586 throw std::invalid_argument(
587 "secant finite-difference step must be positive");
588 const Eigen::VectorXd probe =
592 const double variance = energyVarianceAt(session.
gp, probe);
593 const double probe_distance =
595 const bool probe_within_trust =
600 probe_distance, probe_within_trust,
false)) {
601 auto [probe_energy, probe_gradient] = measure(probe);
602 if (!std::isfinite(probe_energy) ||
603 probe_gradient.size() != d ||
604 !probe_gradient.allFinite())
606 raw_oracle_obs.push_back(
607 {probe, probe_energy, probe_gradient});
608 admit(probe, probe_energy, probe_gradient,
true);
613 if (probe_energy <= best_observed_energy) {
614 best_observed_energy = probe_energy;
615 best_observed_x = probe;
616 best_observed_g = probe_gradient;
619 x_curr = best_observed_x;
621 if (probe_gradient.norm() < config.
conv_tol) {
625 result.
g_final = probe_gradient;
633 const double trust_boundary_tol =
636 Eigen::VectorXd x_inner = x_curr;
637 bool surrogate_converged =
false;
638 bool left_trust_region =
false;
639 bool inner_stopped =
false;
640 bool retried_overshoot =
false;
641 double exit_min_dist = 0.0;
643 for (
int inner = 0; inner < max_inner_steps; ++inner) {
645 query.
R.resize(1, d);
646 query.
R.row(0) = x_inner.transpose();
648 const double e_curr = query.
E(0, 0);
649 Eigen::VectorXd g_pred = query.
G.row(0).transpose();
650 double g_norm = g_pred.norm();
651 if (!std::isfinite(e_curr) || !g_pred.allFinite() ||
652 !std::isfinite(g_norm)) {
653 inner_stopped =
true;
668 if (
const RawOracleObservation* held =
669 find_exact_raw_observation(x_inner)) {
670 if (held->gradient.norm() < config.
conv_tol) {
674 result.
g_final = held->gradient;
675 inner_stopped =
true;
680 g_pred = held->gradient;
681 g_norm = g_pred.norm();
683 surrogate_converged =
true;
688 Eigen::VectorXd x_trial = x_inner;
689 bool secant_step =
false;
691 if (newton_factor_pairs != secant.
pairs()) {
693 newton_factor_pairs = secant.
pairs();
696 Eigen::VectorXd seed_step;
698 seed_step = newton_factor.
factor.solve(-g_pred);
700 if (!seed_step.allFinite() || !std::isfinite(reach))
702 else if (reach > seed_bound)
703 seed_step *= seed_bound / reach;
706 x_trial += seed_step;
713 std::cout <<
"[secant-seed] minimization keeps the "
727 double empty_scale = 1.0;
729 const double probe_cap =
733 if (probe_cap > 0.0 && std::isfinite(probe_cap) &&
734 g_atom_max > probe_cap) {
735 empty_scale = probe_cap / g_atom_max;
737 if (!secant_step && !gp_stepper.
step(x_trial, g_pred, 0.0,
738 empty_scale, step_objective)) {
739 inner_stopped =
true;
742 Eigen::VectorXd step = x_trial - x_inner;
743 if (!step.allFinite()) {
744 inner_stopped =
true;
747 clipAtomMaxMove(step, config.
max_move);
748 Eigen::VectorXd x_next = x_inner + step;
751 const bool trust_active =
753 bool crossed_trust_boundary =
false;
759 const double covered = detail::trustCoveredFraction(
762 const double raw_min_dist =
764 const bool leaves_union = covered < 1.0;
765 crossed_trust_boundary =
767 raw_min_dist >= config.
trust_radius - trust_boundary_tol;
769 x_next = x_inner + covered * (x_next - x_inner);
770 step = x_next - x_inner;
774 if (crossed_trust_boundary) {
775 if (!retried_overshoot && std::isfinite(e_curr) &&
776 g_pred.dot(step) < 0.0) {
778 probe.
R.resize(1, d);
779 probe.
R.row(0) = x_next.transpose();
781 const double e_next = probe.
E(0, 0);
782 const Eigen::VectorXd g_next = probe.
G.row(0).transpose();
783 if (std::isfinite(e_next) && g_next.allFinite() &&
784 e_next > e_curr && g_next.dot(step) > 0.0) {
786 const double dg0 = -g_pred.dot(step);
787 const double dg1 = g_next.dot(step);
789 std::clamp(dg0 / (dg0 + dg1), 0.1, 0.5);
790 x_inner += alpha * step;
791 retried_overshoot =
true;
796 left_trust_region =
true;
803 if (step.norm() <= 1e-14) {
812 if (surrogate_converged) {
813 if (find_exact_raw_observation(x_inner) !=
nullptr) {
816 const double min_dist =
818 const double variance =
819 policy ==
nullptr ? std::numeric_limits<double>::quiet_NaN()
820 : energyVarianceAt(session.
gp, x_inner);
822 variance, min_dist,
true,
true)) {
825 auto [e_true, g_true] = measure(x_inner);
826 if (!std::isfinite(e_true) || g_true.size() != d ||
827 !g_true.allFinite()) {
830 raw_oracle_obs.push_back({x_inner, e_true, g_true});
831 admit(x_inner, e_true, g_true,
true);
834 if (e_true <= best_observed_energy) {
835 best_observed_energy = e_true;
837 best_observed_x = x_inner;
838 best_observed_g = g_true;
841 x_curr = best_observed_x;
843 if (g_true.norm() < config.
conv_tol) {
853 if (!left_trust_region) {
854 if (neb::detail::positionsCoincide(x_inner, x_curr)) {
861 if (
const RawOracleObservation* held =
862 find_exact_raw_observation(x_inner)) {
866 if (neb::detail::findPositionRow(session.
training_obs, x_inner) >=
870 const RawOracleObservation record = *held;
871 admit(record.x, record.energy, record.gradient,
true);
873 if (neb::detail::findPositionRow(session.
training_obs, x_inner) <
877 if (record.energy <= best_observed_energy) {
881 x_curr = best_observed_x;
885 if (!budget_allows())
break;
886 const double variance =
policy ==
nullptr
887 ? std::numeric_limits<double>::quiet_NaN()
888 : energyVarianceAt(session.
gp, x_inner);
890 exit_min_dist,
false,
false)) {
894 auto [e_new, g_new] = measure(x_inner);
895 if (!std::isfinite(e_new) || g_new.size() != d || !g_new.allFinite()) {
898 raw_oracle_obs.push_back({x_inner, e_new, g_new});
899 admit(x_inner, e_new, g_new,
true);
903 if (e_new <= best_observed_energy) {
904 best_observed_energy = e_new;
906 best_observed_x = x_inner;
907 best_observed_g = g_new;
910 x_curr = best_observed_x;
913 if (g_new.norm() < config.
conv_tol) {
923 result.
x_final = best_observed_x;
924 result.
e_final = best_observed_energy;
925 result.
g_final = best_observed_g;