Skip to content

Commit

Permalink
Fix mtk empty contraints creation and update rosenbrock example
Browse files Browse the repository at this point in the history
  • Loading branch information
Vaibhavdixit02 committed Aug 23, 2023
1 parent 6fdfdcc commit b3f6a9c
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 16 deletions.
20 changes: 12 additions & 8 deletions docs/src/examples/rosenbrock.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,22 @@ optf = OptimizationFunction(rosenbrock, Optimization.AutoForwardDiff(); cons = c
prob = OptimizationProblem(optf, x0, _p, lcons = [-Inf], ucons = [0.25^2])
sol = solve(prob, IPNewton()) # -Inf < cons_circ(sol.u, _p) = 0.25^2
## Evolutionary.jl Solvers
using OptimizationEvolutionary
sol = solve(prob, CMAES(μ = 40, λ = 100), abstol = 1e-15) # -Inf < cons_circ(sol.u, _p) = 0.25^2
## IPOPT through OptimizationMOI
using OptimizationMOI, Ipopt
function con2_c(res, x, p)
res .= [x[1]^2 + x[2]^2, x[2] * sin(x[1]) - x[1]]
end
optf = OptimizationFunction(rosenbrock, Optimization.AutoForwardDiff(); cons = con2_c)
optf = OptimizationFunction(rosenbrock, Optimization.AutoZygote(); cons = con2_c)
prob = OptimizationProblem(optf, x0, _p, lcons = [-Inf, -Inf], ucons = [Inf, Inf])
sol = solve(prob, IPNewton())
sol = solve(prob, Ipopt.Optimizer())
# Now let's switch over to OptimizationOptimisers with reverse-mode AD
Expand All @@ -112,12 +121,7 @@ sol = solve(prob, Opt(:LD_LBFGS, 2))
prob = OptimizationProblem(optf, x0, _p, lb = [-1.0, -1.0], ub = [0.8, 0.8])
sol = solve(prob, Opt(:LD_LBFGS, 2))
# sol = solve(prob, Opt(:G_MLSL_LDS, 2), nstart=2, local_method = Opt(:LD_LBFGS, 2), maxiters=10000)
## Evolutionary.jl Solvers
using OptimizationEvolutionary
sol = solve(prob, CMAES(μ = 40, λ = 100), abstol = 1e-15) # -1.0 ≤ x[1], x[2] ≤ 0.8
sol = solve(prob, Opt(:G_MLSL_LDS, 2), local_method = Opt(:LD_LBFGS, 2), maxiters = 10000) #a global optimizer with random starts of local optimization
## BlackBoxOptim.jl Solvers
Expand Down
26 changes: 18 additions & 8 deletions ext/OptimizationMTKExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,16 @@ function Optimization.instantiate_function(f, x, adtype::AutoModelingToolkit, p,
H .= res * v
end

cons = (res, θ) -> f.cons(res, θ, p)

cons_j = (J, θ) -> f.cons_j(J, θ, p)
if !isnothing(f.cons)
cons = (res, θ) -> f.cons(res, θ, p)
cons_j = (J, θ) -> f.cons_j(J, θ, p)
cons_h = (res, θ) -> f.cons_h(res, θ, p)

Check warning on line 37 in ext/OptimizationMTKExt.jl

View check run for this annotation

Codecov / codecov/patch

ext/OptimizationMTKExt.jl#L34-L37

Added lines #L34 - L37 were not covered by tests
else
cons = nothing
cons_j = nothing
cons_h = nothing

Check warning on line 41 in ext/OptimizationMTKExt.jl

View check run for this annotation

Codecov / codecov/patch

ext/OptimizationMTKExt.jl#L39-L41

Added lines #L39 - L41 were not covered by tests
end

cons_h = (res, θ) -> f.cons_h(res, θ, p)
return OptimizationFunction{true}(f.f, adtype; grad = grad, hess = hess, hv = hv,
cons = cons, cons_j = cons_j, cons_h = cons_h,
hess_prototype = f.hess_prototype,
Expand Down Expand Up @@ -71,11 +76,16 @@ function Optimization.instantiate_function(f, cache::Optimization.ReInitCache,
H .= res * v
end

cons = (res, θ) -> f.cons(res, θ, cache.p)

cons_j = (J, θ) -> f.cons_j(J, θ, cache.p)
if !isnothing(f.cons)
cons = (res, θ) -> f.cons(res, θ, cache.p)
cons_j = (J, θ) -> f.cons_j(J, θ, cache.p)
cons_h = (res, θ) -> f.cons_h(res, θ, cache.p)

Check warning on line 82 in ext/OptimizationMTKExt.jl

View check run for this annotation

Codecov / codecov/patch

ext/OptimizationMTKExt.jl#L80-L82

Added lines #L80 - L82 were not covered by tests
else
cons = nothing
cons_j = nothing
cons_h = nothing
end

cons_h = (res, θ) -> f.cons_h(res, θ, cache.p)
return OptimizationFunction{true}(f.f, adtype; grad = grad, hess = hess, hv = hv,
cons = cons, cons_j = cons_j, cons_h = cons_h,
hess_prototype = f.hess_prototype,
Expand Down

0 comments on commit b3f6a9c

Please sign in to comment.