@@ -4,6 +4,7 @@ using Optimization
4
4
using LinearAlgebra, ForwardDiff
5
5
using OrdinaryDiffEq, DifferentialEquations, SteadyStateDiffEq, Sundials
6
6
7
+ # Test helper functions
7
8
function rosenbrock (x, p,args... )
8
9
return (p[1 ] - x[1 ])^ 2 + p[2 ] * (x[2 ] - x[1 ]^ 2 )^ 2
9
10
end
@@ -27,20 +28,19 @@ function constrained_objective(x, p,args...)
27
28
return x[1 ]^ 2 + x[2 ]^ 2
28
29
end
29
30
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 ]
33
34
end
34
35
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
38
39
end
39
40
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
44
44
end
45
45
46
46
@testset " OptimizationODE.jl Tests" begin
102
102
# Minimize f(x) = x₁² + x₂²
103
103
# Subject to x₁ - x₂ = 1
104
104
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
+
105
125
x0 = [1.0 , 0.0 ] # reasonable initial guess
106
126
p = [1.0 ] # enforce x₁ - x₂ = 1
107
127
195
215
callback_values = Vector {Vector{Float64}} ()
196
216
197
217
function test_callback (x, p, t)
198
- return false
218
+ return false
199
219
end
200
220
201
221
optf = OptimizationFunction (quadratic; grad= quadratic_grad!)
268
288
end
269
289
end
270
290
end
291
+
0 commit comments