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 9501f74
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 16 deletions.
21 changes: 13 additions & 8 deletions docs/src/examples/rosenbrock.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,23 @@ 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 +122,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
28 changes: 20 additions & 8 deletions ext/OptimizationMTKExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,17 @@ 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 38 in ext/OptimizationMTKExt.jl

View check run for this annotation

Codecov / codecov/patch

ext/OptimizationMTKExt.jl#L35-L38

Added lines #L35 - L38 were not covered by tests
else
cons = nothing
cons_j = nothing
cons_h = nothing

Check warning on line 42 in ext/OptimizationMTKExt.jl

View check run for this annotation

Codecov / codecov/patch

ext/OptimizationMTKExt.jl#L40-L42

Added lines #L40 - L42 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 +77,17 @@ 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)

cons_h = (res, θ) -> f.cons_h(res, θ, 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 84 in ext/OptimizationMTKExt.jl

View check run for this annotation

Codecov / codecov/patch

ext/OptimizationMTKExt.jl#L82-L84

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

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 9501f74

Please sign in to comment.