diff --git a/lib/OptimizationAccelerated/Manifest.toml b/lib/OptimizationAccelerated/Manifest.toml index 6d2665096..128067990 100644 --- a/lib/OptimizationAccelerated/Manifest.toml +++ b/lib/OptimizationAccelerated/Manifest.toml @@ -905,7 +905,7 @@ version = "3.19.0" [[deps.OptimizationMOI]] deps = ["Ipopt_jll", "MathOptInterface", "ModelingToolkit", "Optimization", "Reexport", "SparseArrays", "Symbolics"] -git-tree-sha1 = "0e6f35532f6b433818760ec9db5eaf623b8cba52" +path = "../OptimizationMOI" uuid = "fd9f6733-72f4-499f-8506-86b2bdd0dea1" version = "0.1.15" diff --git a/lib/OptimizationAccelerated/src/OptimizationAccelerated.jl b/lib/OptimizationAccelerated/src/OptimizationAccelerated.jl index c183ef9c0..85a94db3d 100644 --- a/lib/OptimizationAccelerated/src/OptimizationAccelerated.jl +++ b/lib/OptimizationAccelerated/src/OptimizationAccelerated.jl @@ -59,13 +59,13 @@ function SciMLBase.__solve(cache::OptimizationCache{ consjaccache .= zero(eltype(xₖ)) cache.f.cons_j(consjaccache, xₖ) for j in 1:length(cache.lcons) - if conscache[j] < 0 + if conscache[j] < ϵ_const push!(w, -α*conscache[j] - ϵ*min(dot(consjaccache[j,:], uₖ) + α*conscache[j], 0)) push!(W, consjaccache[j,:]) end end - quadprob = OptimizationProblem(OptimizationFunction((v, p =nothing) -> (v - rₖ).^2, Optimization.AutoModelingToolkit(), cons = (res, v, p = nothing) -> res .= W .* v - w), uₖ, lcons = zeros(length(w))) - + quadprob = OptimizationProblem(OptimizationFunction((v, p =nothing) -> (v .- rₖ).^2, Optimization.AutoModelingToolkit(), cons = (res, v, p = nothing) -> res .= (W .* v) .- w), uₖ, lcons = zeros(length(w)), ucons = fill(Inf, length(w))) + uₖ = solve(quadprob, OSQP.Optimizer()).u xₖ = xₖ + Tₖ*uₖ if norm(uₖ, 1) < ϵ_sol diff --git a/lib/OptimizationMOI/src/moi.jl b/lib/OptimizationMOI/src/moi.jl index 3546637a4..d859ae6dc 100644 --- a/lib/OptimizationMOI/src/moi.jl +++ b/lib/OptimizationMOI/src/moi.jl @@ -13,24 +13,26 @@ struct MOIOptimizationCache{F <: OptimizationFunction, RC, LB, UB, I, S, EX, end function MOIOptimizationCache(prob::OptimizationProblem, opt; kwargs...) - isnothing(prob.f.sys) && + # _f = Optimization.instantiate_function(prob.f, prob.u0, prob.f.adtype, prob.p, length(prob.ucons)) + f = Optimization.instantiate_function(prob.f, prob.u0, Optimization.AutoModelingToolkit(), prob.p, length(prob.ucons)) + isnothing(f.sys) && throw(ArgumentError("Expected an `OptimizationProblem` that was setup via an `OptimizationSystem`, consider `modelingtoolkitize(prob).`")) # TODO: check if the problem is at most bilinear, i.e. affine and or quadratic terms in two variables - expr_map = get_expr_map(prob.f.sys) - expr = repl_getindex!(convert_to_expr(MTK.subs_constants(MTK.objective(prob.f.sys)), - prob.f.sys; expand_expr = false, expr_map)) + expr_map = get_expr_map(f.sys) + expr = repl_getindex!(convert_to_expr(MTK.subs_constants(MTK.objective(f.sys)), + f.sys; expand_expr = false, expr_map)) - cons = MTK.constraints(prob.f.sys) + cons = MTK.constraints(f.sys) cons_expr = Vector{Expr}(undef, length(cons)) Threads.@sync for i in eachindex(cons) Threads.@spawn cons_expr[i] = repl_getindex!(convert_to_expr(Symbolics.canonical_form(MTK.subs_constants(cons[i])), - prob.f.sys; + f.sys; expand_expr = false, expr_map)) end - return MOIOptimizationCache(prob.f, + return MOIOptimizationCache(f, Optimization.ReInitCache(prob.u0, prob.p), prob.lb, prob.ub,