Skip to content

Commit

Permalink
Merge pull request #578 from SciML/docsup
Browse files Browse the repository at this point in the history
Fix mtk empty contraints creation and update rosenbrock example
  • Loading branch information
Vaibhavdixit02 authored Aug 23, 2023
2 parents 6fdfdcc + 4ae8c72 commit b3bbb58
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 17 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "Optimization"
uuid = "7f7a1694-90dd-40f0-9382-eb1efda571ba"
version = "3.16.0"
version = "3.17.0"

[deps]
ADTypes = "47edcb42-4c32-4615-8424-f2b9edc5f35b"
Expand Down
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)
else
cons = nothing
cons_j = nothing
cons_h = nothing
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)
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 b3bbb58

Please sign in to comment.