Skip to content

Commit

Permalink
Try to get it to work
Browse files Browse the repository at this point in the history
  • Loading branch information
Vaibhavdixit02 committed Sep 25, 2023
1 parent 8c68093 commit 2c78244
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 11 deletions.
2 changes: 1 addition & 1 deletion lib/OptimizationAccelerated/Manifest.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
6 changes: 3 additions & 3 deletions lib/OptimizationAccelerated/src/OptimizationAccelerated.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 9 additions & 7 deletions lib/OptimizationMOI/src/moi.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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) &&

Check warning on line 18 in lib/OptimizationMOI/src/moi.jl

View check run for this annotation

Codecov / codecov/patch

lib/OptimizationMOI/src/moi.jl#L17-L18

Added lines #L17 - L18 were not covered by tests
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)),

Check warning on line 23 in lib/OptimizationMOI/src/moi.jl

View check run for this annotation

Codecov / codecov/patch

lib/OptimizationMOI/src/moi.jl#L22-L23

Added lines #L22 - L23 were not covered by tests
f.sys; expand_expr = false, expr_map))

cons = MTK.constraints(prob.f.sys)
cons = MTK.constraints(f.sys)

Check warning on line 26 in lib/OptimizationMOI/src/moi.jl

View check run for this annotation

Codecov / codecov/patch

lib/OptimizationMOI/src/moi.jl#L26

Added line #L26 was not covered by tests
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,

Check warning on line 35 in lib/OptimizationMOI/src/moi.jl

View check run for this annotation

Codecov / codecov/patch

lib/OptimizationMOI/src/moi.jl#L35

Added line #L35 was not covered by tests
Optimization.ReInitCache(prob.u0, prob.p),
prob.lb,
prob.ub,
Expand Down

0 comments on commit 2c78244

Please sign in to comment.