Skip to content

Commit

Permalink
fixes tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ValentinKaisermayer committed Oct 21, 2022
1 parent c4d761f commit d3ed888
Showing 1 changed file with 65 additions and 59 deletions.
124 changes: 65 additions & 59 deletions test/optimizationsystem.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,59 +2,58 @@ using ModelingToolkit, SparseArrays, Test, Optimization, OptimizationOptimJL,
OptimizationMOI, Ipopt, AmplNLWriter, Ipopt_jll
using ModelingToolkit: get_metadata

@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 = OptimizationSystem(loss2, [z], [β], systems = [sys1, sys2],
name = :combinedsys)

equations(combinedsys)
states(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(sys1, [x, y], [a, b], 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)
@test prob.f.sys === combinedsys
sol = solve(prob, Ipopt.Optimizer())
@test sol.minimum < -1e5
@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 = OptimizationSystem(loss2, [z], [β], systems = [sys1, sys2],
name = :combinedsys)

equations(combinedsys)
states(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(sys1, [x, y], [a, b], 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)
@test prob.f.sys === combinedsys
sol = solve(prob, Ipopt.Optimizer(); print_level = 0)
@test sol.minimum < -1e5
end

@testset "inequality constraint" begin
@variables x y
@parameters a b
loss = (a - x)^2 + b * (y - x^2)^2
cons = [
-1 x^2 + y^2,
x^2 + y^2 500,
1 y * sin(x) - x,
y * sin(x) - x 500,
x^2 + y^2 1.0,
]
@named sys = OptimizationSystem(loss, [x, y], [a, b], constraints = cons)

Expand All @@ -63,7 +62,7 @@ sol = solve(prob, Ipopt.Optimizer())
@test prob.f.sys === sys
sol = solve(prob, IPNewton())
@test sol.minimum < 1.0
sol = solve(prob, Ipopt.Optimizer())
sol = solve(prob, Ipopt.Optimizer(); print_level = 0)
@test sol.minimum < 1.0
sol = solve(prob, AmplNLWriter.Optimizer(Ipopt_jll.amplexe))
@test sol.minimum < 1.0
Expand All @@ -79,13 +78,16 @@ end
grad = true, hess = true)
sol = solve(prob, IPNewton())
@test sol.minimum < 1.0
@test sol.u[0.808, 0.589] atol=1e-3
@test sol[x]^2 + sol[y]^2 1.0
sol = solve(prob, Ipopt.Optimizer())
sol = solve(prob, Ipopt.Optimizer(); print_level = 0)
@test sol.minimum < 1.0
@test sol[x]^2 + sol[y]^2 [1.0]
@test sol.u[0.808, 0.589] atol=1e-3
@test sol[x]^2 + sol[y]^2 1.0
sol = solve(prob, AmplNLWriter.Optimizer(Ipopt_jll.amplexe))
@test sol.minimum < 1.0
@test sol[x]^2 + sol[y]^2 [1.0]
@test sol.u[0.808, 0.589] atol=1e-3
@test sol[x]^2 + sol[y]^2 1.0
end

@testset "rosenbrock" begin
Expand All @@ -100,7 +102,14 @@ 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

Expand Down Expand Up @@ -143,10 +152,7 @@ end
sys1 = OptimizationSystem(loss, [x, y], [a, b], name = :sys1)

cons = [
-1 x^2 + y^2,
x^2 + y^2 500,
1 y * sin(x) - x,
y * sin(x) - x 500,
x^2 + y^2 1.0,
]
sys2 = OptimizationSystem(loss, [x, y], [a, b], name = :sys2, constraints = cons)

Expand All @@ -169,15 +175,15 @@ end

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

prob = OptimizationProblem(sys2, [x => 0.0, y => 0.0], [a => 1.0, b => 100.0],
grad = true, hess = true)
@test prob.f.sys === sys2
sol = solve(prob, IPNewton())
@test sol.minimum < 1.0
sol = solve(prob, Ipopt.Optimizer())
sol = solve(prob, Ipopt.Optimizer(); print_level = 0)
@test sol.minimum < 1.0
end

Expand All @@ -197,8 +203,8 @@ end
@variables x[1:2] [bounds = (0.0, Inf)]
@named sys = OptimizationSystem(x[1] + x[2], [x...], [];
constraints = [
1 x[1]^2 + x[2]^2,
x[1]^2 + x[2]^2 2,
1.0 x[1]^2 + x[2]^2,
x[1]^2 + x[2]^2 2.0,
])

prob = OptimizationProblem(sys, [x[1] => 2.0, x[2] => 0.0], [], grad = true,
Expand Down

0 comments on commit d3ed888

Please sign in to comment.