Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 3 additions & 5 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ Logging = "1.10"
LoggingExtras = "0.4, 1"
Lux = "1.12.4"
MLUtils = "0.4"
ModelingToolkit = "10"
ModelingToolkit = "10.23"
Mooncake = "0.4.138"
Optim = ">= 1.4.1"
OptimizationBase = "2"
Expand All @@ -57,10 +57,9 @@ Random = "1.10"
Reexport = "1.2"
ReverseDiff = "1"
SafeTestsets = "0.1"
SciMLBase = "2.82"
SciMLBase = "2.104"
SciMLSensitivity = "7"
SparseArrays = "1.10"
SparseDiffTools = "2"
Symbolics = "6"
TerminalLoggers = "0.1"
Test = "1.10"
Expand Down Expand Up @@ -96,7 +95,6 @@ ReverseDiff = "37e2e3b7-166d-5795-8a7a-e32c996b4267"
SafeTestsets = "1bc83da4-3b8d-516f-aca4-4fe02f6d838f"
SciMLSensitivity = "1ed8b502-d754-442c-8d5d-10ac956f44a1"
SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf"
SparseDiffTools = "47a9eef4-7e08-11e9-0b38-333d64bd3804"
Symbolics = "0c5d862f-8b57-4792-8d23-62f2024744c7"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
Tracker = "9f7883ad-71c0-57eb-9f7f-b5c9e6d3789c"
Expand All @@ -106,5 +104,5 @@ Mooncake = "da2b9cff-9c12-43a0-ae48-6db2b0edb7d6"
[targets]
test = ["Aqua", "BenchmarkTools", "Boltz", "ComponentArrays", "DiffEqFlux", "Enzyme", "FiniteDiff", "Flux", "ForwardDiff",
"Ipopt", "IterTools", "Lux", "MLUtils", "ModelingToolkit", "Optim", "OptimizationMOI", "OptimizationOptimJL", "OptimizationOptimisers",
"OrdinaryDiffEqTsit5", "Pkg", "Random", "ReverseDiff", "SafeTestsets", "SciMLSensitivity", "SparseArrays", "SparseDiffTools",
"OrdinaryDiffEqTsit5", "Pkg", "Random", "ReverseDiff", "SafeTestsets", "SciMLSensitivity", "SparseArrays",
"Symbolics", "Test", "Tracker", "Zygote", "Mooncake"]
2 changes: 1 addition & 1 deletion docs/Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ Lux = "1"
MLUtils = "0.4.4"
Manifolds = "0.9"
Manopt = "0.4"
ModelingToolkit = "10"
ModelingToolkit = "10.23"
NLPModels = "0.21"
NLPModelsTest = "0.10"
NLopt = "0.6, 1"
Expand Down
1 change: 1 addition & 0 deletions lib/OptimizationBBO/src/OptimizationBBO.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ module OptimizationBBO

using Reexport
import Optimization
import Optimization: OptimizationBase
import BlackBoxOptim, Optimization.SciMLBase
import Optimization.SciMLBase: MultiObjectiveOptimizationFunction

Expand Down
4 changes: 2 additions & 2 deletions lib/OptimizationBase/Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,11 @@ ForwardDiff = "0.10.26, 1"
LinearAlgebra = "1.9, 1.10"
MLDataDevices = "1"
MLUtils = "0.4"
ModelingToolkit = "9, 10"
ModelingToolkit = "10.23"
PDMats = "0.11"
Reexport = "1.2"
ReverseDiff = "1.14"
SciMLBase = "2"
SciMLBase = "2.104"
SparseConnectivityTracer = "0.6, 1"
SparseMatrixColorings = "0.4"
SymbolicAnalysis = "0.3"
Expand Down
65 changes: 34 additions & 31 deletions lib/OptimizationBase/ext/OptimizationMTKExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -21,24 +21,25 @@ function OptimizationBase.instantiate_function(
num_cons))))
#sys = ModelingToolkit.structural_simplify(sys)
# don't need to pass `x` or `p` since they're defaults now
f = OptimizationProblem(sys, nothing; grad = g, hess = h,
mtkprob = OptimizationProblem(sys, nothing; grad = g, hess = h,
sparse = true, cons_j = cons_j, cons_h = cons_h,
cons_sparse = true).f
cons_sparse = true)
f = mtkprob.f

grad = (G, θ, args...) -> f.grad(G, θ, p, args...)
grad = (G, θ, args...) -> f.grad(G, θ, mtkprob.p, args...)

hess = (H, θ, args...) -> f.hess(H, θ, p, args...)
hess = (H, θ, args...) -> f.hess(H, θ, mtkprob.p, args...)

hv = function (H, θ, v, args...)
res = (eltype(θ)).(f.hess_prototype)
res = similar(f.hess_prototype, eltype(θ))
hess(res, θ, args...)
H .= res * v
end

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)
cons = (res, θ) -> f.cons(res, θ, mtkprob.p)
cons_j = (J, θ) -> f.cons_j(J, θ, mtkprob.p)
cons_h = (res, θ) -> f.cons_h(res, θ, mtkprob.p)
else
cons = nothing
cons_j = nothing
Expand Down Expand Up @@ -72,24 +73,24 @@ function OptimizationBase.instantiate_function(
num_cons))))
#sys = ModelingToolkit.structural_simplify(sys)
# don't need to pass `x` or `p` since they're defaults now
f = OptimizationProblem(sys, nothing; grad = g, hess = h,
mtkprob = OptimizationProblem(sys, nothing; grad = g, hess = h,
sparse = true, cons_j = cons_j, cons_h = cons_h,
cons_sparse = true).f
cons_sparse = true)
f = mtkprob.f

grad = (G, θ, args...) -> f.grad(G, θ, cache.p, args...)
grad = (G, θ, args...) -> f.grad(G, θ, mtkprob.p, args...)

hess = (H, θ, args...) -> f.hess(H, θ, cache.p, args...)
hess = (H, θ, args...) -> f.hess(H, θ, mtkprob.p, args...)

hv = function (H, θ, v, args...)
res = (eltype(θ)).(f.hess_prototype)
res = similar(f.hess_prototype, eltype(θ))
hess(res, θ, args...)
H .= res * v
end

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)
cons = (res, θ) -> f.cons(res, θ, mtkprob.p)
cons_j = (J, θ) -> f.cons_j(J, θ, mtkprob.p)
cons_h = (res, θ) -> f.cons_h(res, θ, mtkprob.p)
else
cons = nothing
cons_j = nothing
Expand Down Expand Up @@ -121,13 +122,14 @@ function OptimizationBase.instantiate_function(
num_cons))))
#sys = ModelingToolkit.structural_simplify(sys)
# don't need to pass `x` or `p` since they're defaults now
f = OptimizationProblem(sys, nothing; grad = g, hess = h,
mtkprob = OptimizationProblem(sys, nothing; grad = g, hess = h,
sparse = false, cons_j = cons_j, cons_h = cons_h,
cons_sparse = false).f
cons_sparse = false)
f = mtkprob.f

grad = (G, θ, args...) -> f.grad(G, θ, p, args...)
grad = (G, θ, args...) -> f.grad(G, θ, mtkprob.p, args...)

hess = (H, θ, args...) -> f.hess(H, θ, p, args...)
hess = (H, θ, args...) -> f.hess(H, θ, mtkprob.p, args...)

hv = function (H, θ, v, args...)
res = ArrayInterface.zeromatrix(θ)
Expand All @@ -136,9 +138,9 @@ function OptimizationBase.instantiate_function(
end

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)
cons = (res, θ) -> f.cons(res, θ, mtkprob.p)
cons_j = (J, θ) -> f.cons_j(J, θ, mtkprob.p)
cons_h = (res, θ) -> f.cons_h(res, θ, mtkprob.p)
else
cons = nothing
cons_j = nothing
Expand Down Expand Up @@ -172,13 +174,14 @@ function OptimizationBase.instantiate_function(
num_cons))))
#sys = ModelingToolkit.structural_simplify(sys)
# don't need to pass `x` or `p` since they're defaults now
f = OptimizationProblem(sys, nothing; grad = g, hess = h,
mtkprob = OptimizationProblem(sys, nothing; grad = g, hess = h,
sparse = false, cons_j = cons_j, cons_h = cons_h,
cons_sparse = false).f
cons_sparse = false)
f = mtkprob.f

grad = (G, θ, args...) -> f.grad(G, θ, cache.p, args...)
grad = (G, θ, args...) -> f.grad(G, θ, mtkprob.p, args...)

hess = (H, θ, args...) -> f.hess(H, θ, cache.p, args...)
hess = (H, θ, args...) -> f.hess(H, θ, mtkprob.p, args...)

hv = function (H, θ, v, args...)
res = ArrayInterface.zeromatrix(θ)
Expand All @@ -187,9 +190,9 @@ function OptimizationBase.instantiate_function(
end

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)
cons = (res, θ) -> f.cons(res, θ, mtkprob.p)
cons_j = (J, θ) -> f.cons_j(J, θ, mtkprob.p)
cons_h = (res, θ) -> f.cons_h(res, θ, mtkprob.p)
else
cons = nothing
cons_j = nothing
Expand Down
18 changes: 9 additions & 9 deletions lib/OptimizationBase/src/function.jl
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,11 @@ function OptimizationBase.instantiate_function(
cons_vjp = f.cons_vjp === nothing ? nothing : (res, x) -> f.cons_vjp(res, x, p)
cons_h = f.cons_h === nothing ? nothing : (res, x) -> f.cons_h(res, x, p)
hess_prototype = f.hess_prototype === nothing ? nothing :
convert.(eltype(x), f.hess_prototype)
similar(f.hess_prototype, eltype(x))
cons_jac_prototype = f.cons_jac_prototype === nothing ? nothing :
convert.(eltype(x), f.cons_jac_prototype)
similar(f.cons_jac_prototype, eltype(x))
cons_hess_prototype = f.cons_hess_prototype === nothing ? nothing :
[convert.(eltype(x), f.cons_hess_prototype[i])
[similar(f.cons_hess_prototype[i], eltype(x))
for i in 1:num_cons]
expr = symbolify(f.expr)
cons_expr = symbolify.(f.cons_expr)
Expand Down Expand Up @@ -90,11 +90,11 @@ function OptimizationBase.instantiate_function(
cons_vjp = f.cons_vjp === nothing ? nothing : (res, x) -> f.cons_vjp(res, x, cache.p)
cons_h = f.cons_h === nothing ? nothing : (res, x) -> f.cons_h(res, x, cache.p)
hess_prototype = f.hess_prototype === nothing ? nothing :
convert.(eltype(cache.u0), f.hess_prototype)
similar(f.hess_prototype, eltype(cache.u0))
cons_jac_prototype = f.cons_jac_prototype === nothing ? nothing :
convert.(eltype(cache.u0), f.cons_jac_prototype)
similar(f.cons_jac_prototype, eltype(cache.u0))
cons_hess_prototype = f.cons_hess_prototype === nothing ? nothing :
[convert.(eltype(cache.u0), f.cons_hess_prototype[i])
[similar(f.cons_hess_prototype[i], eltype(cache.u0))
for i in 1:num_cons]
expr = symbolify(f.expr)
cons_expr = symbolify.(f.cons_expr)
Expand Down Expand Up @@ -196,11 +196,11 @@ function OptimizationBase.instantiate_function(
end
end
hess_prototype = f.hess_prototype === nothing ? nothing :
convert.(eltype(x), f.hess_prototype)
similar(f.hess_prototype, eltype(x))
cons_jac_prototype = f.cons_jac_prototype === nothing ? nothing :
convert.(eltype(x), f.cons_jac_prototype)
similar(f.cons_jac_prototype, eltype(x))
cons_hess_prototype = f.cons_hess_prototype === nothing ? nothing :
[convert.(eltype(x), f.cons_hess_prototype[i])
[similar(f.cons_hess_prototype[i], eltype(x))
for i in 1:num_cons]
expr = symbolify(f.expr)
cons_expr = symbolify.(f.cons_expr)
Expand Down
2 changes: 1 addition & 1 deletion lib/OptimizationIpopt/Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ SymbolicIndexingInterface = "2efcf032-c050-4f8e-a9bb-153293bab1f5"
[compat]
Ipopt = "1.10.3"
LinearAlgebra = "1.10.0"
ModelingToolkit = "10.20"
ModelingToolkit = "10.23"
Optimization = "4.3.0"
SciMLBase = "2.90.0"
SparseArrays = "1.10.0"
Expand Down
8 changes: 4 additions & 4 deletions lib/OptimizationIpopt/src/cache.jl
Original file line number Diff line number Diff line change
Expand Up @@ -94,13 +94,13 @@ function IpoptCache(prob, opt;
J = if isnothing(f.cons_jac_prototype)
zeros(T, num_cons, n)
else
convert.(T, f.cons_jac_prototype)
similar(f.cons_jac_prototype, T)
end
lagh = !isnothing(f.lag_hess_prototype)
H = if lagh # lag hessian takes precedence
convert.(T, f.lag_hess_prototype)
similar(f.lag_hess_prototype, T)
elseif !isnothing(f.hess_prototype)
convert.(T, f.hess_prototype)
similar(f.hess_prototype, T)
else
zeros(T, n, n)
end
Expand All @@ -109,7 +109,7 @@ function IpoptCache(prob, opt;
elseif isnothing(f.cons_hess_prototype)
Matrix{T}[zeros(T, n, n) for i in 1:num_cons]
else
[convert.(T, f.cons_hess_prototype[i]) for i in 1:num_cons]
[similar(f.cons_hess_prototype[i], T) for i in 1:num_cons]
end
lcons = prob.lcons === nothing ? fill(T(-Inf), num_cons) : prob.lcons
ucons = prob.ucons === nothing ? fill(T(Inf), num_cons) : prob.ucons
Expand Down
4 changes: 2 additions & 2 deletions lib/OptimizationMOI/Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ Ipopt_jll = "300.1400"
Juniper = "0.9"
LinearAlgebra = "1"
MathOptInterface = "1"
ModelingToolkit = "10"
ModelingToolkit = "10.23"
NLopt = "1"
Optimization = "4.4"
Reexport = "1.2"
Expand All @@ -31,7 +31,7 @@ SparseArrays = "1.6"
SymbolicIndexingInterface = "0.3"
Symbolics = "6"
Test = "1.6"
Zygote = "0.6"
Zygote = "0.6, 0.7"
julia = "1.10"

[extras]
Expand Down
10 changes: 5 additions & 5 deletions lib/OptimizationMOI/src/nlp.jl
Original file line number Diff line number Diff line change
Expand Up @@ -123,17 +123,17 @@ function MOIOptimizationNLPCache(prob::OptimizationProblem,
end
T = eltype(prob.u0)
n = length(prob.u0)

J = if isnothing(f.cons_jac_prototype)
zeros(T, num_cons, n)
else
convert.(T, f.cons_jac_prototype)
similar(f.cons_jac_prototype, T)
end
lagh = !isnothing(f.lag_hess_prototype)

H = if lagh # lag hessian takes precedence
convert.(T, f.lag_hess_prototype)
similar(f.lag_hess_prototype, T)
elseif !isnothing(f.hess_prototype)
convert.(T, f.hess_prototype)
similar(f.hess_prototype, T)
else
zeros(T, n, n)
end
Expand All @@ -142,7 +142,7 @@ function MOIOptimizationNLPCache(prob::OptimizationProblem,
elseif isnothing(f.cons_hess_prototype)
Matrix{T}[zeros(T, n, n) for i in 1:num_cons]
else
[convert.(T, f.cons_hess_prototype[i]) for i in 1:num_cons]
[similar(f.cons_hess_prototype[i], T) for i in 1:num_cons]
end
lcons = prob.lcons === nothing ? fill(T(-Inf), num_cons) : prob.lcons
ucons = prob.ucons === nothing ? fill(T(Inf), num_cons) : prob.ucons
Expand Down
4 changes: 2 additions & 2 deletions lib/OptimizationOptimJL/src/OptimizationOptimJL.jl
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ function SciMLBase.__solve(cache::OptimizationCache{
isnothing(cache.f.hess_prototype) ?
Optim.NLSolversBase.alloc_H(cache.u0,
real(zero(u0_type))) :
convert.(u0_type, cache.f.hess_prototype))
similar(cache.f.hess_prototype, u0_type))
end

opt_args = __map_optimizer_args(cache, cache.opt, callback = _cb,
Expand Down Expand Up @@ -414,7 +414,7 @@ function SciMLBase.__solve(cache::OptimizationCache{
isnothing(cache.f.hess_prototype) ?
Optim.NLSolversBase.alloc_H(cache.u0,
real(zero(u0_type))) :
convert.(u0_type, cache.f.hess_prototype))
similar(cache.f.hess_prototype, u0_type))
else
Optim.OnceDifferentiable(_loss, gg, fg!, cache.u0,
real(zero(u0_type)),
Expand Down
2 changes: 1 addition & 1 deletion src/auglag.jl
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ function SciMLBase.__solve(cache::OptimizationCache{
function aug_grad(G, θ, p)
cache.f.grad(G, θ, p)
if !isnothing(cache.f.cons_jac_prototype)
J = Float64.(cache.f.cons_jac_prototype)
J = similar(cache.f.cons_jac_prototype, Float64)
else
J = zeros((length(cache.lcons), length(θ)))
end
Expand Down
2 changes: 1 addition & 1 deletion src/lbfgsb.jl
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ function SciMLBase.__solve(cache::OptimizationCache{
function aug_grad(G, θ)
cache.f.grad(G, θ)
if !isnothing(cache.f.cons_jac_prototype)
J = Float64.(cache.f.cons_jac_prototype)
J = similar(cache.f.cons_jac_prototype, Float64)
else
J = zeros((length(cache.lcons), length(θ)))
end
Expand Down
6 changes: 3 additions & 3 deletions test/ADtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ end
end

@testset "No constraint" begin
for adtype in [AutoEnzyme(), AutoForwardDiff(), AutoZygote(), AutoReverseDiff(),
@testset "$adtype" for adtype in [AutoEnzyme(), AutoForwardDiff(), AutoZygote(), AutoReverseDiff(),
AutoFiniteDiff(), AutoModelingToolkit(), AutoSparseForwardDiff(),
AutoSparseReverseDiff(), AutoSparse(AutoZygote()), AutoModelingToolkit(true, true), AutoMooncake()]
optf = OptimizationFunction(rosenbrock, adtype)
Expand Down Expand Up @@ -71,7 +71,7 @@ end
end

@testset "One constraint" begin
for adtype in [AutoEnzyme(), AutoForwardDiff(), AutoZygote(), AutoReverseDiff(),
@testset "$adtype" for adtype in [AutoEnzyme(), AutoForwardDiff(), AutoZygote(), AutoReverseDiff(),
AutoFiniteDiff(), AutoModelingToolkit(), AutoSparseForwardDiff(),
AutoSparseReverseDiff(), AutoSparse(AutoZygote()), AutoModelingToolkit(true, true), AutoMooncake()]
cons = (res, x, p) -> (res[1] = x[1]^2 + x[2]^2 - 1.0; return nothing)
Expand All @@ -92,7 +92,7 @@ end
end

@testset "Two constraints" begin
for adtype in [AutoForwardDiff(), AutoZygote(), AutoReverseDiff(),
@testset "$adtype" for adtype in [AutoForwardDiff(), AutoZygote(), AutoReverseDiff(),
AutoFiniteDiff(), AutoModelingToolkit(), AutoSparseForwardDiff(),
AutoSparseReverseDiff(), AutoSparse(AutoZygote()), AutoModelingToolkit(true, true), AutoMooncake()]
function con2_c(res, x, p)
Expand Down
Loading