@@ -364,16 +364,18 @@ end
364364 MadNLP. CompactLBFGS,
365365 MadNLP. ExactHessian, # For comparison
366366 ]
367+ initial_u = variant == MadNLP. ExactHessian ? x0[1 : 2 ] : x0
367368 # Only provide gradients, no Hessian needed for LBFGS
368369 ad = AutoForwardDiff () # First-order AD is sufficient
369370 optfunc = OptimizationFunction (extended_rosenbrock, ad)
370- prob = OptimizationProblem (optfunc, x0 , nothing )
371+ prob = OptimizationProblem (optfunc, initial_u , nothing )
371372
372373 if variant == MadNLP. ExactHessian
374+ # Exact Hessian is only a comparison here; the LBFGS path keeps the larger problem.
373375 # Use second-order AD for exact Hessian
374376 ad = SecondOrder (AutoForwardDiff (), AutoForwardDiff ())
375377 optfunc = OptimizationFunction (extended_rosenbrock, ad)
376- prob = OptimizationProblem (optfunc, x0 , nothing )
378+ prob = OptimizationProblem (optfunc, initial_u , nothing )
377379 end
378380
379381 opt = MadNLPOptimizer (
458460 return x0
459461 end
460462
461- @testset " N=5 electrons with $approx " for approx in [MadNLP. CompactLBFGS, MadNLP. ExactHessian]
462- np = 5
463+ @testset " electrons on sphere with $approx " for approx in [MadNLP. CompactLBFGS, MadNLP. ExactHessian]
464+ np = approx == MadNLP . ExactHessian ? 2 : 5
463465 x0 = init_electrons_on_sphere (np)
464466
465467 if approx == MadNLP. CompactLBFGS
499501 unit_sphere_constraints (cons_vals, sol. u, nothing )
500502 @test all (abs .(cons_vals) .< 1.0e-5 )
501503
502- # Known optimal energy for 5 electrons on unit sphere
504+ # Known optimal energy for electrons on unit sphere
503505 # Reference: https://en.wikipedia.org/wiki/Thomson_problem
504- # Configuration: Triangular dipyramid (trigonal bipyramid, D3h symmetry)
505- expected_energy = 6.474691495
506+ expected_energy = np == 2 ? 0.5 : 6.474691495
506507 @test isapprox (sol. objective, expected_energy, rtol = 1.0e-3 )
507508
508509 # Verify minimum distance between electrons
@@ -520,26 +521,26 @@ end
520521 @test min_dist > 0.5 # Electrons should be well-separated
521522 end
522523
523- @testset verbose = true " LBFGS vs Exact Hessian" begin
524- # Test with moderate size to show LBFGS efficiency
525- np = 10 # Gyroelongated square dipyramid configuration
526- x0 = init_electrons_on_sphere (np)
527-
524+ @testset verbose = true " LBFGS and Exact Hessian" begin
528525 results = []
529526
530- for (name, approx, ad) in [
527+ for (name, approx, ad, np, expected_energy ) in [
531528 (
532529 " CompactLBFGS" , MadNLP. CompactLBFGS,
533- AutoForwardDiff (),
530+ AutoForwardDiff (), 10 ,
531+ 32.71694946 ,
534532 )
535533 (
536534 " ExactHessian" ,
537535 MadNLP. ExactHessian,
538536 SecondOrder (
539537 AutoForwardDiff (), AutoZygote ()
540538 ),
539+ 2 ,
540+ 0.5 ,
541541 )
542542 ]
543+ x0 = init_electrons_on_sphere (np)
543544 optfunc = OptimizationFunction (
544545 coulomb_potential, ad,
545546 cons = unit_sphere_constraints
@@ -562,18 +563,18 @@ end
562563 objective = sol. objective,
563564 iterations = sol. stats. iterations,
564565 success = SciMLBase. successful_retcode (sol),
566+ expected = expected_energy,
565567 )
566568 )
567569 end
568570
569571 # All methods should converge
570572 @test all (r[2 ]. success for r in values (results))
571573
572- # All should find similar objective values (gyroelongated square dipyramid energy)
574+ # Check objective values against the corresponding Thomson problem energy.
573575 # Reference: https://en.wikipedia.org/wiki/Thomson_problem
574- objectives = [r[2 ]. objective for r in values (results)]
575- @testset " $(results[i][1 ]) " for (i, o) in enumerate (objectives)
576- @test o ≈ 32.71694946 rtol = 1.0e-2
576+ @testset " $(name) " for (name, result) in results
577+ @test result. objective ≈ result. expected rtol = 1.0e-2
577578 end
578579
579580 # LBFGS methods typically need more iterations but less cost per iteration
0 commit comments