@@ -4,6 +4,7 @@ using Optimization
44using LinearAlgebra, ForwardDiff
55using OrdinaryDiffEq, DifferentialEquations, SteadyStateDiffEq, Sundials
66
7+ # Test helper functions
78function rosenbrock (x, p,args... )
89 return (p[1 ] - x[1 ])^ 2 + p[2 ] * (x[2 ] - x[1 ]^ 2 )^ 2
910end
@@ -27,20 +28,19 @@ function constrained_objective(x, p,args...)
2728 return x[1 ]^ 2 + x[2 ]^ 2
2829end
2930
30- function constrained_objective_grad! (g , x, p, args... )
31- g . = 2 .* x .* p [1 ]
32- return nothing
31+ function constrained_objective_grad! (grad , x, p,args... )
32+ grad[ 1 ] = 2.0 * x [1 ]
33+ grad[ 2 ] = 2.0 * x[ 2 ]
3334end
3435
35- # Constraint: x₁ - x₂ - p[1] = 0 (p[1] = 1 → x₁ - x₂ = 1 )
36- function constraint_func (x, p, args ... )
37- return x[1 ] - x[2 ] - p[ 1 ]
36+ function constraint_func (res, x, p,args ... )
37+ res[ 1 ] = x[ 1 ] + x[ 2 ] - 1.0 # x[1] + x[2] = 1
38+ return x[1 ] + x[2 ] - 1.0
3839end
3940
40- function constraint_jac! (J, x,args... )
41- J[1 , 1 ] = 1.0
42- J[1 , 2 ] = - 1.0
43- return nothing
41+ function constraint_jac! (jac, x, p,args... )
42+ jac[1 , 1 ] = 1.0
43+ jac[1 , 2 ] = - 1.0
4444end
4545
4646@testset " OptimizationODE.jl Tests" begin
102102 # Minimize f(x) = x₁² + x₂²
103103 # Subject to x₁ - x₂ = 1
104104
105+ function constrained_objective (x, p,args... )
106+ return x[1 ]^ 2 + x[2 ]^ 2
107+ end
108+
109+ function constrained_objective_grad! (g, x, p, args... )
110+ g .= 2 .* x .* p[1 ]
111+ return nothing
112+ end
113+
114+ # Constraint: x₁ - x₂ - p[1] = 0 (p[1] = 1 → x₁ - x₂ = 1)
115+ function constraint_func (x, p, args... )
116+ return x[1 ] - x[2 ] - p[1 ]
117+ end
118+
119+ function constraint_jac! (J, x,args... )
120+ J[1 , 1 ] = 1.0
121+ J[1 , 2 ] = - 1.0
122+ return nothing
123+ end
124+
105125 x0 = [1.0 , 0.0 ] # reasonable initial guess
106126 p = [1.0 ] # enforce x₁ - x₂ = 1
107127
195215 callback_values = Vector {Vector{Float64}} ()
196216
197217 function test_callback (x, p, t)
198- return false
218+ return false
199219 end
200220
201221 optf = OptimizationFunction (quadratic; grad= quadratic_grad!)
268288 end
269289 end
270290end
291+
0 commit comments