Skip to content

Commit 8c9cd52

Browse files
Update runtests.jl
Fixed empty constraints test.
1 parent a74a454 commit 8c9cd52

File tree

1 file changed

+32
-11
lines changed

1 file changed

+32
-11
lines changed

lib/OptimizationODE/test/runtests.jl

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ using Optimization
44
using LinearAlgebra, ForwardDiff
55
using OrdinaryDiffEq, DifferentialEquations, SteadyStateDiffEq, Sundials
66

7+
# Test helper functions
78
function rosenbrock(x, p,args...)
89
return (p[1] - x[1])^2 + p[2] * (x[2] - x[1]^2)^2
910
end
@@ -27,20 +28,19 @@ function constrained_objective(x, p,args...)
2728
return x[1]^2 + x[2]^2
2829
end
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]
3334
end
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
3839
end
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
4444
end
4545

4646
@testset "OptimizationODE.jl Tests" begin
@@ -102,6 +102,26 @@ end
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

@@ -195,7 +215,7 @@ end
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!)
@@ -268,3 +288,4 @@ end
268288
end
269289
end
270290
end
291+

0 commit comments

Comments
 (0)