|
| 1 | +# ============================================================================ |
| 2 | +# Descriptive Mode Tests (Layer 2) |
| 3 | +# ============================================================================ |
| 4 | +# This file tests the `solve_descriptive` function. It verifies that when the user |
| 5 | +# provides a symbolic description (e.g., `:collocation, :adnlp, :ipopt`), the |
| 6 | +# components are correctly instantiated via the strategy registry before delegating |
| 7 | +# to the canonical Layer 3 solve. |
| 8 | + |
| 9 | +module TestDescriptive |
| 10 | + |
| 11 | +import Test |
| 12 | +import OptimalControl |
| 13 | +import CTModels |
| 14 | +import CTDirect |
| 15 | +import CTSolvers |
| 16 | +import CTBase |
| 17 | +import CommonSolve |
| 18 | + |
| 19 | +# Load solver extensions (import only to trigger extensions, avoid name conflicts) |
| 20 | +import NLPModelsIpopt |
| 21 | +import MadNLP |
| 22 | +import MadNCL |
| 23 | +import CUDA |
| 24 | + |
| 25 | +# Include shared test problems via TestProblems module |
| 26 | +include(joinpath(@__DIR__, "..", "..", "problems", "TestProblems.jl")) |
| 27 | +using .TestProblems |
| 28 | + |
| 29 | +const VERBOSE = isdefined(Main, :TestOptions) ? Main.TestOptions.VERBOSE : true |
| 30 | +const SHOWTIMING = isdefined(Main, :TestOptions) ? Main.TestOptions.SHOWTIMING : true |
| 31 | + |
| 32 | +function test_descriptive() |
| 33 | + Test.@testset "solve_descriptive (contract and integration tests)" verbose=VERBOSE showtiming=SHOWTIMING begin |
| 34 | + registry = OptimalControl.get_strategy_registry() |
| 35 | + |
| 36 | + # ==================================================================== |
| 37 | + # CONTRACT TESTS - Basic functionality |
| 38 | + # ==================================================================== |
| 39 | + |
| 40 | + Test.@testset "Complete symbolic description" begin |
| 41 | + ocp = TestProblems.Beam().ocp |
| 42 | + init = OptimalControl.build_initial_guess(ocp, nothing) |
| 43 | + |
| 44 | + # Test complete description |
| 45 | + result = OptimalControl.solve_descriptive( |
| 46 | + ocp, :collocation, :adnlp, :ipopt; |
| 47 | + initial_guess=init, |
| 48 | + display=false, |
| 49 | + registry=registry |
| 50 | + ) |
| 51 | + Test.@test result isa CTModels.AbstractSolution |
| 52 | + Test.@test OptimalControl.successful(result) |
| 53 | + end |
| 54 | + |
| 55 | + Test.@testset "Partial symbolic description" begin |
| 56 | + ocp = TestProblems.Goddard().ocp |
| 57 | + init = OptimalControl.build_initial_guess(ocp, nothing) |
| 58 | + |
| 59 | + # Test partial description (should complete via registry defaults) |
| 60 | + result = OptimalControl.solve_descriptive( |
| 61 | + ocp, :collocation; |
| 62 | + initial_guess=init, |
| 63 | + display=false, |
| 64 | + registry=registry |
| 65 | + ) |
| 66 | + Test.@test result isa CTModels.AbstractSolution |
| 67 | + Test.@test OptimalControl.successful(result) |
| 68 | + end |
| 69 | + |
| 70 | + # ==================================================================== |
| 71 | + # INTEGRATION TESTS - Real problems and strategies |
| 72 | + # ==================================================================== |
| 73 | + |
| 74 | + Test.@testset "Integration with real strategies" begin |
| 75 | + ocp = TestProblems.Beam().ocp |
| 76 | + init = OptimalControl.build_initial_guess(ocp, nothing) |
| 77 | + |
| 78 | + Test.@testset "Complete description - Beam" begin |
| 79 | + result = OptimalControl.solve_descriptive( |
| 80 | + ocp, :collocation, :adnlp, :ipopt; |
| 81 | + initial_guess=init, |
| 82 | + display=false, |
| 83 | + registry=registry |
| 84 | + ) |
| 85 | + Test.@test result isa CTModels.AbstractSolution |
| 86 | + Test.@test OptimalControl.successful(result) |
| 87 | + Test.@test OptimalControl.objective(result) ≈ TestProblems.Beam().obj rtol=1e-2 |
| 88 | + end |
| 89 | + |
| 90 | + Test.@testset "Partial description - Beam" begin |
| 91 | + result = OptimalControl.solve_descriptive( |
| 92 | + ocp, :collocation; |
| 93 | + initial_guess=init, |
| 94 | + display=false, |
| 95 | + registry=registry |
| 96 | + ) |
| 97 | + Test.@test result isa CTModels.AbstractSolution |
| 98 | + Test.@test OptimalControl.successful(result) |
| 99 | + end |
| 100 | + |
| 101 | + ocp = TestProblems.Goddard().ocp |
| 102 | + init = OptimalControl.build_initial_guess(ocp, nothing) |
| 103 | + |
| 104 | + Test.@testset "Complete description - Goddard" begin |
| 105 | + result = OptimalControl.solve_descriptive( |
| 106 | + ocp, :collocation, :adnlp, :ipopt; |
| 107 | + initial_guess=init, |
| 108 | + display=false, |
| 109 | + registry=registry |
| 110 | + ) |
| 111 | + Test.@test result isa CTModels.AbstractSolution |
| 112 | + Test.@test OptimalControl.successful(result) |
| 113 | + Test.@test OptimalControl.objective(result) ≈ TestProblems.Goddard().obj rtol=1e-2 |
| 114 | + end |
| 115 | + |
| 116 | + Test.@testset "Partial description - Goddard" begin |
| 117 | + result = OptimalControl.solve_descriptive( |
| 118 | + ocp, :collocation; |
| 119 | + initial_guess=init, |
| 120 | + display=false, |
| 121 | + registry=registry |
| 122 | + ) |
| 123 | + Test.@test result isa CTModels.AbstractSolution |
| 124 | + Test.@test OptimalControl.successful(result) |
| 125 | + end |
| 126 | + end |
| 127 | + |
| 128 | + # ==================================================================== |
| 129 | + # ALIAS TESTS - Initial guess aliases in descriptive mode |
| 130 | + # ==================================================================== |
| 131 | + |
| 132 | + Test.@testset "Initial guess aliases" begin |
| 133 | + ocp = TestProblems.Beam().ocp |
| 134 | + |
| 135 | + Test.@testset "alias 'init'" begin |
| 136 | + result = OptimalControl.solve_descriptive( |
| 137 | + ocp, :collocation, :adnlp, :ipopt; |
| 138 | + init=nothing, |
| 139 | + display=false, |
| 140 | + registry=registry |
| 141 | + ) |
| 142 | + Test.@test result isa CTModels.AbstractSolution |
| 143 | + Test.@test OptimalControl.successful(result) |
| 144 | + end |
| 145 | + end |
| 146 | + |
| 147 | + # ==================================================================== |
| 148 | + # ERROR TESTS - Invalid descriptions and error handling |
| 149 | + # ==================================================================== |
| 150 | + |
| 151 | + Test.@testset "Error handling" begin |
| 152 | + ocp = TestProblems.Beam().ocp |
| 153 | + init = OptimalControl.build_initial_guess(ocp, nothing) |
| 154 | + |
| 155 | + Test.@testset "Unknown strategy" begin |
| 156 | + Test.@test_throws Exception begin |
| 157 | + OptimalControl.solve_descriptive( |
| 158 | + ocp, :unknown_strategy, :adnlp, :ipopt; |
| 159 | + initial_guess=init, |
| 160 | + display=false, |
| 161 | + registry=registry |
| 162 | + ) |
| 163 | + end |
| 164 | + end |
| 165 | + end |
| 166 | + end |
| 167 | +end |
| 168 | + |
| 169 | +end # module |
| 170 | + |
| 171 | +# CRITICAL: Redefine in outer scope for TestRunner |
| 172 | +test_descriptive() = TestDescriptive.test_descriptive() |
0 commit comments