Skip to content

Commit 44462bd

Browse files
fixes tests
1 parent d991bea commit 44462bd

File tree

1 file changed

+163
-105
lines changed

1 file changed

+163
-105
lines changed

test/optimizationsystem.jl

Lines changed: 163 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -2,102 +2,130 @@ using ModelingToolkit, SparseArrays, Test, Optimization, OptimizationOptimJL,
22
OptimizationMOI, Ipopt, AmplNLWriter, Ipopt_jll
33
using ModelingToolkit: get_metadata
44

5-
@variables x y
6-
@parameters a b
7-
loss = (a - x)^2 + b * (y - x^2)^2
8-
sys1 = OptimizationSystem(loss, [x, y], [a, b], name = :sys1)
9-
10-
cons2 = [x^2 + y^2 ~ 0, y * sin(x) - x ~ 0]
11-
sys2 = OptimizationSystem(loss, [x, y], [a, b], name = :sys2, constraints = cons2)
12-
13-
@variables z
14-
@parameters β
15-
loss2 = sys1.x - sys2.y + z * β
16-
combinedsys = OptimizationSystem(loss2, [z], [β], systems = [sys1, sys2],
17-
name = :combinedsys)
18-
19-
equations(combinedsys)
20-
states(combinedsys)
21-
parameters(combinedsys)
22-
23-
calculate_gradient(combinedsys)
24-
calculate_hessian(combinedsys)
25-
generate_function(combinedsys)
26-
generate_gradient(combinedsys)
27-
generate_hessian(combinedsys)
28-
hess_sparsity = ModelingToolkit.hessian_sparsity(sys1)
29-
sparse_prob = OptimizationProblem(sys1, [x, y], [a, b], grad = true, sparse = true)
30-
@test sparse_prob.f.hess_prototype.rowval == hess_sparsity.rowval
31-
@test sparse_prob.f.hess_prototype.colptr == hess_sparsity.colptr
32-
33-
u0 = [sys1.x => 1.0
34-
sys1.y => 2.0
35-
sys2.x => 3.0
36-
sys2.y => 4.0
37-
z => 5.0]
38-
p = [sys1.a => 6.0
39-
sys1.b => 7.0
40-
sys2.a => 8.0
41-
sys2.b => 9.0
42-
β => 10.0]
43-
44-
prob = OptimizationProblem(combinedsys, u0, p, grad = true, hess = true)
45-
@test prob.f.sys === combinedsys
46-
sol = solve(prob, Ipopt.Optimizer())
47-
@test sol.minimum < -1e5
48-
49-
#inequality constraint, the bounds for constraints lcons !== ucons
50-
prob = OptimizationProblem(sys2, [x => 0.0, y => 0.0], [a => 1.0, b => 100.0],
51-
lcons = [-1.0, -1.0], ucons = [500.0, 500.0], grad = true,
52-
hess = true)
53-
@test prob.f.sys === sys2
54-
sol = solve(prob, IPNewton())
55-
@test sol.minimum < 1.0
56-
sol = solve(prob, Ipopt.Optimizer())
57-
@test sol.minimum < 1.0
58-
sol = solve(prob, AmplNLWriter.Optimizer(Ipopt_jll.amplexe))
59-
@test sol.minimum < 1.0
60-
61-
#equality constraint, lcons == ucons
62-
cons2 = [0.0 ~ x^2 + y^2]
63-
out = zeros(1)
64-
sys2 = OptimizationSystem(loss, [x, y], [a, b], name = :sys2, constraints = cons2)
65-
prob = OptimizationProblem(sys2, [x => 0.0, y => 0.0], [a => 1.0, b => 1.0], lcons = [1.0],
66-
ucons = [1.0], grad = true, hess = true)
67-
sol = solve(prob, IPNewton())
68-
@test sol.minimum < 1.0
69-
prob.f.cons(out, sol.minimizer, [1.0, 1.0])
70-
@test out [1.0]
71-
sol = solve(prob, Ipopt.Optimizer())
72-
@test sol.minimum < 1.0
73-
prob.f.cons(out, sol.minimizer, [1.0, 1.0])
74-
@test out [1.0]
75-
sol = solve(prob, AmplNLWriter.Optimizer(Ipopt_jll.amplexe))
76-
@test sol.minimum < 1.0
77-
prob.f.cons(out, sol.minimizer, [1.0, 1.0])
78-
@test out [1.0]
79-
80-
rosenbrock(x, p) = (p[1] - x[1])^2 + p[2] * (x[2] - x[1]^2)^2
81-
x0 = zeros(2)
82-
_p = [1.0, 100.0]
83-
84-
f = OptimizationFunction(rosenbrock, Optimization.AutoModelingToolkit())
85-
prob = OptimizationProblem(f, x0, _p)
86-
sol = solve(prob, Newton())
5+
@testset "basic" begin
6+
@variables x y
7+
@parameters a b
8+
loss = (a - x)^2 + b * (y - x^2)^2
9+
sys1 = OptimizationSystem(loss, [x, y], [a, b], name = :sys1)
10+
11+
cons2 = [x^2 + y^2 ~ 0, y * sin(x) - x ~ 0]
12+
sys2 = OptimizationSystem(loss, [x, y], [a, b], name = :sys2, constraints = cons2)
13+
14+
@variables z
15+
@parameters β
16+
loss2 = sys1.x - sys2.y + z * β
17+
combinedsys = OptimizationSystem(loss2, [z], [β], systems = [sys1, sys2],
18+
name = :combinedsys)
19+
20+
equations(combinedsys)
21+
states(combinedsys)
22+
parameters(combinedsys)
23+
24+
calculate_gradient(combinedsys)
25+
calculate_hessian(combinedsys)
26+
generate_function(combinedsys)
27+
generate_gradient(combinedsys)
28+
generate_hessian(combinedsys)
29+
hess_sparsity = ModelingToolkit.hessian_sparsity(sys1)
30+
sparse_prob = OptimizationProblem(sys1, [x, y], [a, b], grad = true, sparse = true)
31+
@test sparse_prob.f.hess_prototype.rowval == hess_sparsity.rowval
32+
@test sparse_prob.f.hess_prototype.colptr == hess_sparsity.colptr
33+
34+
u0 = [sys1.x => 1.0
35+
sys1.y => 2.0
36+
sys2.x => 3.0
37+
sys2.y => 4.0
38+
z => 5.0]
39+
p = [sys1.a => 6.0
40+
sys1.b => 7.0
41+
sys2.a => 8.0
42+
sys2.b => 9.0
43+
β => 10.0]
44+
45+
prob = OptimizationProblem(combinedsys, u0, p, grad = true, hess = true)
46+
@test prob.f.sys === combinedsys
47+
sol = solve(prob, Ipopt.Optimizer(); print_level = 0)
48+
@test sol.minimum < -1e5
49+
end
50+
51+
@testset "inequality constraint" begin
52+
@variables x y
53+
@parameters a b
54+
loss = (a - x)^2 + b * (y - x^2)^2
55+
cons = [
56+
x^2 + y^2 1.0,
57+
]
58+
@named sys = OptimizationSystem(loss, [x, y], [a, b], constraints = cons)
59+
60+
prob = OptimizationProblem(sys, [x => 0.0, y => 0.0], [a => 1.0, b => 1.0],
61+
grad = true, hess = true)
62+
@test prob.f.sys === sys
63+
sol = solve(prob, IPNewton())
64+
@test sol.minimum < 1.0
65+
sol = solve(prob, Ipopt.Optimizer(); print_level = 0)
66+
@test sol.minimum < 1.0
67+
sol = solve(prob, AmplNLWriter.Optimizer(Ipopt_jll.amplexe))
68+
@test sol.minimum < 1.0
69+
end
70+
71+
@testset "equality constraint" begin
72+
@variables x y
73+
@parameters a b
74+
loss = (a - x)^2 + b * (y - x^2)^2
75+
cons = [1.0 ~ x^2 + y^2]
76+
@named sys = OptimizationSystem(loss, [x, y], [a, b], constraints = cons)
77+
prob = OptimizationProblem(sys, [x => 0.0, y => 0.0], [a => 1.0, b => 1.0],
78+
grad = true, hess = true)
79+
sol = solve(prob, IPNewton())
80+
@test sol.minimum < 1.0
81+
@test sol.u[0.808, 0.589] atol=1e-3
82+
@test sol[x]^2 + sol[y]^2 1.0
83+
sol = solve(prob, Ipopt.Optimizer(); print_level = 0)
84+
@test sol.minimum < 1.0
85+
@test sol.u[0.808, 0.589] atol=1e-3
86+
@test sol[x]^2 + sol[y]^2 1.0
87+
@test_skip begin # MethodError: no method matching MathOptInterface.FileFormats.NL._NLExpr(::Int64)
88+
sol = solve(prob, AmplNLWriter.Optimizer(Ipopt_jll.amplexe))
89+
@test sol.minimum < 1.0
90+
@test sol.u[0.808, 0.589] atol=1e-3
91+
@test sol[x]^2 + sol[y]^2 1.0
92+
end
93+
end
94+
95+
@testset "rosenbrock" begin
96+
rosenbrock(x, p) = (p[1] - x[1])^2 + p[2] * (x[2] - x[1]^2)^2
97+
x0 = zeros(2)
98+
p = [1.0, 100.0]
99+
f = OptimizationFunction(rosenbrock, Optimization.AutoModelingToolkit())
100+
prob = OptimizationProblem(f, x0, p)
101+
sol = solve(prob, Newton())
102+
@test sol.u [1.0, 1.0]
103+
end
87104

88105
# issue #819
89106
@testset "Combined system name collisions" begin
107+
@variables x y
108+
@parameters a b
109+
loss = (a - x)^2 + b * (y - x^2)^2
110+
sys1 = OptimizationSystem(loss, [x, y], [a, b], name = :sys1)
90111
sys2 = OptimizationSystem(loss, [x, y], [a, b], name = :sys1)
112+
@variables z
113+
@parameters β
114+
loss2 = sys1.x - sys2.y + z * β
91115
@test_throws ArgumentError OptimizationSystem(loss2, [z], [β], systems = [sys1, sys2])
92116
end
93117

94-
# observed variable handling
95-
@variables OBS
96-
@named sys2 = OptimizationSystem(loss, [x, y], [a, b]; observed = [OBS ~ x + y])
97-
OBS2 = OBS
98-
@test isequal(OBS2, @nonamespace sys2.OBS)
99-
@unpack OBS = sys2
100-
@test isequal(OBS2, OBS)
118+
@testset "observed variable handling" begin
119+
@variables x y
120+
@parameters a b
121+
loss = (a - x)^2 + b * (y - x^2)^2
122+
@variables OBS
123+
@named sys2 = OptimizationSystem(loss, [x, y], [a, b]; observed = [OBS ~ x + y])
124+
OBS2 = OBS
125+
@test isequal(OBS2, @nonamespace sys2.OBS)
126+
@unpack OBS = sys2
127+
@test isequal(OBS2, OBS)
128+
end
101129

102130
# nested constraints
103131
@testset "nested systems" begin
@@ -128,8 +156,10 @@ end
128156
loss = (a - x)^2 + b * (y - x^2)^2
129157
sys1 = OptimizationSystem(loss, [x, y], [a, b], name = :sys1)
130158

131-
cons2 = [x^2 + y^2 ~ 0, y * sin(x) - x ~ 0]
132-
sys2 = OptimizationSystem(loss, [x, y], [a, b], name = :sys2, constraints = cons2)
159+
cons = [
160+
x^2 + y^2 1.0,
161+
]
162+
sys2 = OptimizationSystem(loss, [x, y], [a, b], name = :sys2, constraints = cons)
133163

134164
@variables z
135165
@parameters β
@@ -150,26 +180,54 @@ end
150180

151181
prob = OptimizationProblem(combinedsys, u0, p, grad = true, hess = true)
152182
@test prob.f.sys === combinedsys
153-
sol = solve(prob, Ipopt.Optimizer())
183+
sol = solve(prob, Ipopt.Optimizer(); print_level = 0)
154184
@test sol.minimum < -1e5
155185

156-
#inequality constraint, the bounds for constraints lcons !== ucons
157186
prob = OptimizationProblem(sys2, [x => 0.0, y => 0.0], [a => 1.0, b => 100.0],
158-
lcons = [-1.0, -1.0], ucons = [500.0, 500.0], grad = true,
159-
hess = true)
187+
grad = true, hess = true)
160188
@test prob.f.sys === sys2
161189
sol = solve(prob, IPNewton())
162190
@test sol.minimum < 1.0
163-
sol = solve(prob, Ipopt.Optimizer())
191+
sol = solve(prob, Ipopt.Optimizer(); print_level = 0)
164192
@test sol.minimum < 1.0
165193
end
166194

167-
@variables x
168-
o1 = (x - 1)^2
169-
c1 = [
170-
x ~ 1,
171-
]
172-
testdict = Dict(["test" => 1])
173-
sys1 = OptimizationSystem(o1, [x], [], name = :sys1, constraints = c1,
174-
metadata = testdict)
175-
@test get_metadata(sys1) == testdict
195+
@testset "metadata" begin
196+
@variables x
197+
o1 = (x - 1)^2
198+
c1 = [
199+
x ~ 1,
200+
]
201+
testdict = Dict(["test" => 1])
202+
sys1 = OptimizationSystem(o1, [x], [], name = :sys1, constraints = c1,
203+
metadata = testdict)
204+
@test get_metadata(sys1) == testdict
205+
end
206+
207+
@testset "non-convex problem with inequalities" begin
208+
@variables x[1:2] [bounds = (0.0, Inf)]
209+
@named sys = OptimizationSystem(x[1] + x[2], [x...], [];
210+
constraints = [
211+
1.0 x[1]^2 + x[2]^2,
212+
x[1]^2 + x[2]^2 2.0,
213+
])
214+
215+
prob = OptimizationProblem(sys, [x[1] => 2.0, x[2] => 0.0], [], grad = true,
216+
hess = true)
217+
sol = Optimization.solve(prob, Ipopt.Optimizer(); print_level = 0)
218+
@test sol.u [1, 0]
219+
@test prob.lb == [0.0, 0.0]
220+
@test prob.ub == [Inf, Inf]
221+
end
222+
223+
@testset "parameter bounds" begin
224+
@parameters c = 0.0
225+
@variables x y [bounds = (c, Inf)]
226+
@parameters a b
227+
loss = (a - x)^2 + b * (y - x^2)^2
228+
@named sys = OptimizationSystem(loss, [x, y], [a, b, c])
229+
prob = OptimizationProblem(sys, [x => 0.0, y => 0.0], [a => 1.0, b => 100.0],
230+
grad = true, hess = true)
231+
@test prob.lb == [-Inf, 0.0]
232+
@test prob.ub == [Inf, Inf]
233+
end

0 commit comments

Comments
 (0)