-
-
Notifications
You must be signed in to change notification settings - Fork 213
/
Copy pathoptimizationsystem.jl
390 lines (343 loc) · 13.1 KB
/
optimizationsystem.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
using ModelingToolkit, SparseArrays, Test, Optimization, OptimizationOptimJL,
OptimizationMOI, Ipopt, AmplNLWriter, Ipopt_jll, SymbolicIndexingInterface
using ModelingToolkit: get_metadata
@testset "basic" begin
@variables x y
@parameters a b
loss = (a - x)^2 + b * (y - x^2)^2
sys1 = OptimizationSystem(loss, [x, y], [a, b], name = :sys1)
cons2 = [x^2 + y^2 ~ 0, y * sin(x) - x ~ 0]
sys2 = OptimizationSystem(loss, [x, y], [a, b], name = :sys2, constraints = cons2)
@variables z
@parameters β
loss2 = sys1.x - sys2.y + z * β
combinedsys = complete(OptimizationSystem(loss2, [z], [β], systems = [sys1, sys2],
name = :combinedsys))
equations(combinedsys)
unknowns(combinedsys)
parameters(combinedsys)
calculate_gradient(combinedsys)
calculate_hessian(combinedsys)
generate_function(combinedsys)
generate_gradient(combinedsys)
generate_hessian(combinedsys)
hess_sparsity = ModelingToolkit.hessian_sparsity(sys1)
sparse_prob = OptimizationProblem(complete(sys1),
[x, y],
[a => 0.0, b => 0.0],
grad = true,
sparse = true)
@test sparse_prob.f.hess_prototype.rowval == hess_sparsity.rowval
@test sparse_prob.f.hess_prototype.colptr == hess_sparsity.colptr
u0 = [sys1.x => 1.0
sys1.y => 2.0
sys2.x => 3.0
sys2.y => 4.0
z => 5.0]
p = [sys1.a => 6.0
sys1.b => 7.0
sys2.a => 8.0
sys2.b => 9.0
β => 10.0]
prob = OptimizationProblem(combinedsys, u0, p, grad = true, hess = true, cons_j = true,
cons_h = true)
@test prob.f.sys === combinedsys
sol = solve(prob, Ipopt.Optimizer(); print_level = 0)
@test sol.objective < -1e5
end
@testset "inequality constraint" begin
@variables x y
@parameters a b
loss = (a - x)^2 + b * (y - x^2)^2
cons = [
x^2 + y^2 ≲ 1.0
]
@named sys = OptimizationSystem(loss, [x, y], [a, b], constraints = cons)
sys = complete(sys)
prob = OptimizationProblem(sys, [x => 0.0, y => 0.0], [a => 1.0, b => 1.0],
grad = true, hess = true, cons_j = true, cons_h = true)
@test prob.f.sys === sys
sol = solve(prob, IPNewton())
@test sol.objective < 1.0
sol = solve(prob, Ipopt.Optimizer(); print_level = 0)
@test sol.objective < 1.0
prob = OptimizationProblem(sys, [x => 0.0, y => 0.0], [a => 1.0, b => 1.0],
grad = false, hess = false, cons_j = false, cons_h = false)
sol = solve(prob, AmplNLWriter.Optimizer(Ipopt_jll.amplexe))
@test_skip sol.objective < 1.0
end
@testset "equality constraint" begin
@variables x y z
@parameters a b
loss = (a - x)^2 + b * z^2
cons = [1.0 ~ x^2 + y^2
z ~ y - x^2
z^2 + y^2 ≲ 1.0]
@named sys = OptimizationSystem(loss, [x, y, z], [a, b], constraints = cons)
sys = structural_simplify(sys)
prob = OptimizationProblem(sys, [x => 0.0, y => 0.0, z => 0.0], [a => 1.0, b => 1.0],
grad = true, hess = true, cons_j = true, cons_h = true)
sol = solve(prob, IPNewton())
@test sol.objective < 1.0
@test sol.u≈[0.808, -0.064] atol=1e-3
@test sol[x]^2 + sol[y]^2 ≈ 1.0
sol = solve(prob, Ipopt.Optimizer(); print_level = 0)
@test sol.objective < 1.0
@test sol.u≈[0.808, -0.064] atol=1e-3
@test sol[x]^2 + sol[y]^2 ≈ 1.0
prob = OptimizationProblem(sys, [x => 0.0, y => 0.0, z => 0.0], [a => 1.0, b => 1.0],
grad = false, hess = false, cons_j = false, cons_h = false)
sol = solve(prob, AmplNLWriter.Optimizer(Ipopt_jll.amplexe))
@test_skip sol.objective < 1.0
@test_skip sol.u≈[0.808, -0.064] atol=1e-3
@test_skip sol[x]^2 + sol[y]^2 ≈ 1.0
end
@testset "rosenbrock" begin
rosenbrock(x, p) = (p[1] - x[1])^2 + p[2] * (x[2] - x[1]^2)^2
x0 = zeros(2)
p = [1.0, 100.0]
f = OptimizationFunction(rosenbrock, Optimization.AutoSymbolics())
prob = OptimizationProblem(f, x0, p)
sol = solve(prob, Newton())
@test sol.u ≈ [1.0, 1.0]
end
# issue #819
@testset "Combined system name collisions" begin
@variables x y
@parameters a b
loss = (a - x)^2 + b * (y - x^2)^2
sys1 = OptimizationSystem(loss, [x, y], [a, b], name = :sys1)
sys2 = OptimizationSystem(loss, [x, y], [a, b], name = :sys1)
@variables z
@parameters β
loss2 = sys1.x - sys2.y + z * β
@test_throws ArgumentError OptimizationSystem(loss2, [z], [β], systems = [sys1, sys2])
end
@testset "observed variable handling" begin
@variables x y
@parameters a b
loss = (a - x)^2 + b * (y - x^2)^2
@variables OBS
@named sys2 = OptimizationSystem(loss, [x, y], [a, b]; observed = [OBS ~ x + y])
OBS2 = OBS
@test isequal(OBS2, @nonamespace sys2.OBS)
@unpack OBS = sys2
@test isequal(OBS2, OBS)
end
# nested constraints
@testset "nested systems" begin
@variables x y
@parameters a = 1
o1 = (x - a)^2
o2 = (y - 1 / 2)^2
c1 = [
x ~ 1
]
c2 = [
y ~ 1
]
sys1 = OptimizationSystem(o1, [x], [a], name = :sys1, constraints = c1)
sys2 = OptimizationSystem(o2, [y], [], name = :sys2, constraints = c2)
sys = complete(OptimizationSystem(0, [], []; name = :sys, systems = [sys1, sys2],
constraints = [sys1.x + sys2.y ~ 2], checks = false))
prob = OptimizationProblem(sys, [0.0, 0.0])
@test isequal(constraints(sys), vcat(sys1.x + sys2.y ~ 2, sys1.x ~ 1, sys2.y ~ 1))
@test isequal(equations(sys), (sys1.x - sys1.a)^2 + (sys2.y - 1 / 2)^2)
@test isequal(unknowns(sys), [sys1.x, sys2.y])
prob_ = remake(prob, u0 = [1.0, 0.0], p = [2.0])
@test isequal(prob_.u0, [1.0, 0.0])
@test isequal(prob_.p, [2.0])
prob_ = remake(prob, u0 = Dict(sys1.x => 1.0), p = Dict(sys1.a => 2.0))
@test isequal(prob_.u0, [1.0, 0.0])
@test isequal((prob_.p...,)[1], [2.0])
end
@testset "nested systems" begin
@variables x1 x2 x3 x4
@named sys1 = OptimizationSystem(x1, [x1], [])
@named sys2 = OptimizationSystem(x2, [x2], [], systems = [sys1])
@named sys3 = OptimizationSystem(x3, [x3], [], systems = [sys2])
@named sys4 = OptimizationSystem(x4, [x4], [], systems = [sys3])
@test isequal(equations(sys4), sys3.sys2.sys1.x1 + sys3.sys2.x2 + sys3.x3 + x4)
end
@testset "time dependent var" begin
@independent_variables t
@variables x(t) y
@parameters a b
loss = (a - x)^2 + b * (y - x^2)^2
sys1 = OptimizationSystem(loss, [x, y], [a, b], name = :sys1)
cons = [
x^2 + y^2 ≲ 1.0
]
sys2 = OptimizationSystem(loss, [x, y], [a, b], name = :sys2, constraints = cons)
@variables z
@parameters β
loss2 = sys1.x - sys2.y + z * β
combinedsys = complete(OptimizationSystem(loss2, [z], [β], systems = [sys1, sys2],
name = :combinedsys))
u0 = [sys1.x => 1.0
sys1.y => 2.0
sys2.x => 3.0
sys2.y => 4.0
z => 5.0]
p = [sys1.a => 6.0
sys1.b => 7.0
sys2.a => 8.0
sys2.b => 9.0
β => 10.0]
prob = OptimizationProblem(combinedsys, u0, p, grad = true, hess = true, cons_j = true,
cons_h = true)
@test prob.f.sys === combinedsys
@test_broken SciMLBase.successful_retcode(solve(prob,
Ipopt.Optimizer();
print_level = 0))
#=
@test sol.objective < -1e5
prob = OptimizationProblem(sys2, [x => 0.0, y => 0.0], [a => 1.0, b => 100.0],
grad = true, hess = true, cons_j = true, cons_h = true)
@test prob.f.sys === sys2
sol = solve(prob, IPNewton())
@test sol.objective < 1.0
sol = solve(prob, Ipopt.Optimizer(); print_level = 0)
@test sol.objective < 1.0
=#
end
@testset "metadata" begin
@variables x
o1 = (x - 1)^2
c1 = [
x ~ 1
]
testdict = Dict(["test" => 1])
sys1 = OptimizationSystem(o1, [x], [], name = :sys1, constraints = c1,
metadata = testdict)
@test get_metadata(sys1) == testdict
end
@testset "non-convex problem with inequalities" begin
@variables x[1:2] [bounds = (0.0, Inf)]
@named sys = OptimizationSystem(x[1] + x[2], [x...], [];
constraints = [
1.0 ≲ x[1]^2 + x[2]^2,
x[1]^2 + x[2]^2 ≲ 2.0
])
prob = OptimizationProblem(complete(sys), [x[1] => 2.0, x[2] => 0.0], [], grad = true,
hess = true, cons_j = true, cons_h = true)
sol = Optimization.solve(prob, Ipopt.Optimizer(); print_level = 0)
@test sol.u ≈ [1, 0]
@test prob.lb == [0.0, 0.0]
@test prob.ub == [Inf, Inf]
end
@testset "parameter bounds" begin
@parameters c = 0.0
@variables x y [bounds = (c, Inf)]
@parameters a b
loss = (a - x)^2 + b * (y - x^2)^2
@named sys = OptimizationSystem(loss, [x, y], [a, b, c])
prob = OptimizationProblem(complete(sys), [x => 0.0, y => 0.0], [a => 1.0, b => 100.0])
@test prob.lb == [-Inf, 0.0]
@test prob.ub == [Inf, Inf]
end
@testset "modelingtoolkitize" begin
@variables x₁ x₂
@parameters α₁ α₂
loss = (α₁ - x₁)^2 + α₂ * (x₂ - x₁^2)^2
cons = [
x₁^2 + x₂^2 ≲ 1.0
]
sys1 = complete(OptimizationSystem(loss,
[x₁, x₂],
[α₁, α₂],
name = :sys1,
constraints = cons))
prob1 = OptimizationProblem(sys1, [x₁ => 0.0, x₂ => 0.0], [α₁ => 1.0, α₂ => 100.0],
grad = true, hess = true, cons_j = true, cons_h = true)
sys2 = complete(modelingtoolkitize(prob1))
prob2 = OptimizationProblem(sys2, [x₁ => 0.0, x₂ => 0.0], [α₁ => 1.0, α₂ => 100.0],
grad = true, hess = true, cons_j = true, cons_h = true)
sol1 = Optimization.solve(prob1, Ipopt.Optimizer())
sol2 = Optimization.solve(prob2, Ipopt.Optimizer())
@test sol1.u ≈ sol2.u
end
@testset "#2323 keep symbolic expressions and xor condition on constraint bounds" begin
@variables x y
@parameters a b
loss = (a - x)^2 + b * (y - x^2)^2
@named sys = OptimizationSystem(loss, [x, y], [a, b], constraints = [x^2 + y^2 ≲ 0.0])
sys = complete(sys)
@test_throws Any OptimizationProblem(sys,
[x => 0.0, y => 0.0],
[a => 1.0, b => 100.0],
lcons = [0.0])
@test_throws Any OptimizationProblem(sys,
[x => 0.0, y => 0.0],
[a => 1.0, b => 100.0],
ucons = [0.0])
prob = OptimizationProblem(sys, [x => 0.0, y => 0.0], [a => 1.0, b => 100.0])
@test prob.f.expr isa Symbolics.Symbolic
@test all(prob.f.cons_expr[i].lhs isa Symbolics.Symbolic
for i in 1:length(prob.f.cons_expr))
end
@testset "Derivatives, iip and oop" begin
@variables x y
@parameters a b
loss = (a - x)^2 + b * (y - x^2)^2
cons2 = [x^2 + y^2 ~ 0, y * sin(x) - x ~ 0]
sys = complete(OptimizationSystem(
loss, [x, y], [a, b], name = :sys2, constraints = cons2))
prob = OptimizationProblem(sys, [x => 0.0, y => 0.0], [a => 1.0, b => 100.0],
grad = true, hess = true, cons_j = true, cons_h = true)
G1 = Array{Float64}(undef, 2)
H1 = Array{Float64}(undef, 2, 2)
J = Array{Float64}(undef, 2, 2)
H3 = [Array{Float64}(undef, 2, 2), Array{Float64}(undef, 2, 2)]
prob.f.grad(G1, [1.0, 1.0], [1.0, 100.0])
@test prob.f.grad([1.0, 1.0], [1.0, 100.0]) == G1
prob.f.hess(H1, [1.0, 1.0], [1.0, 100.0])
@test prob.f.hess([1.0, 1.0], [1.0, 100.0]) == H1
prob.f.cons_j(J, [1.0, 1.0], [1.0, 100.0])
@test prob.f.cons_j([1.0, 1.0], [1.0, 100.0]) == J
prob.f.cons_h(H3, [1.0, 1.0], [1.0, 100.0])
@test prob.f.cons_h([1.0, 1.0], [1.0, 100.0]) == H3
end
@testset "Passing `nothing` to `u0`" begin
@variables x = 1.0
@mtkbuild sys = OptimizationSystem((x - 3)^2, [x], [])
prob = @test_nowarn OptimizationProblem(sys, nothing)
@test_nowarn solve(prob, NelderMead())
end
@testset "Bounded unknowns are irreducible" begin
@variables x
@variables y [bounds = (-Inf, Inf)]
@variables z [bounds = (1.0, 2.0)]
obj = x^2 + y^2 + z^2
cons = [y ~ 2x
z ~ 2y]
@mtkbuild sys = OptimizationSystem(obj, [x, y, z], []; constraints = cons)
@test is_variable(sys, z)
@test !is_variable(sys, y)
@variables x[1:3] [bounds = ([-Inf, -1.0, -2.0], [Inf, 1.0, 2.0])]
obj = x[1]^2 + x[2]^2 + x[3]^2
cons = [x[2] ~ 2x[1] + 3, x[3] ~ x[1] + x[2]]
@mtkbuild sys = OptimizationSystem(obj, [x], []; constraints = cons)
@test length(unknowns(sys)) == 2
@test !is_variable(sys, x[1])
@test is_variable(sys, x[2])
@test is_variable(sys, x[3])
end
@testset "Constraints work with nonnumeric parameters" begin
@variables x
@parameters p f(::Real)
@mtkbuild sys = OptimizationSystem(
x^2 + f(x) * p, [x], [f, p]; constraints = [2.0 ≲ f(x) + p])
prob = OptimizationProblem(sys, [x => 1.0], [p => 1.0, f => (x -> 2x)])
@test abs(prob.f.cons(prob.u0, prob.p)[1]) ≈ 1.0
end
@testset "Variable discovery" begin
@variables x1 x2
@parameters p1 p2
@named sys1 = OptimizationSystem(x1^2; constraints = [p1 * x1 ≲ 2.0])
@named sys2 = OptimizationSystem(x2^2; constraints = [p2 * x2 ≲ 2.0], systems = [sys1])
@test isequal(only(unknowns(sys1)), x1)
@test isequal(only(parameters(sys1)), p1)
@test all(y -> any(x -> isequal(x, y), unknowns(sys2)), [x2, sys1.x1])
@test all(y -> any(x -> isequal(x, y), parameters(sys2)), [p2, sys1.p1])
end