Skip to content

Commit

Permalink
refactor: update for MTKv9
Browse files Browse the repository at this point in the history
  • Loading branch information
AayushSabharwal committed Jan 25, 2024
1 parent 4da7756 commit 1c8978f
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 43 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ ForwardDiff = "0.10.26"
LinearAlgebra = "1.10"
Logging = "1.10"
LoggingExtras = "0.4, 1"
ModelingToolkit = "8.74"
ModelingToolkit = "8.74, 9"
Pkg = "1"
Printf = "1.10"
ProgressLogging = "0.1"
Expand Down
2 changes: 1 addition & 1 deletion lib/OptimizationMOI/Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ Symbolics = "0c5d862f-8b57-4792-8d23-62f2024744c7"
Ipopt_jll = "=300.1400.400"
Juniper = "0.9"
MathOptInterface = "1"
ModelingToolkit = "8.74"
ModelingToolkit = "9"
Optimization = "3.21"
Reexport = "1.2"
SymbolicIndexingInterface = "0.3"
Expand Down
8 changes: 4 additions & 4 deletions lib/OptimizationMOI/src/OptimizationMOI.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ using MathOptInterface
using Optimization.SciMLBase
using SymbolicIndexingInterface
using SparseArrays
import ModelingToolkit: parameters, states, varmap_to_vars, mergedefaults, toexpr
import ModelingToolkit: parameters, unknowns, varmap_to_vars, mergedefaults, toexpr
import ModelingToolkit
const MTK = ModelingToolkit
using Symbolics
Expand Down Expand Up @@ -182,12 +182,12 @@ end
"""
convert_to_expr(eq, sys; expand_expr = false, pairs_arr = expr_map(sys))
Converts the given symbolic expression to a Julia `Expr` and replaces all symbols, i.e. states and
Converts the given symbolic expression to a Julia `Expr` and replaces all symbols, i.e. unknowns and
parameters with `x[i]` and `p[i]`.
# Arguments:
- `eq`: Expression to convert
- `sys`: Reference to the system holding the parameters and states
- `sys`: Reference to the system holding the parameters and unknowns
- `expand_expr=false`: If `true` the symbolic expression is expanded first.
"""
function convert_to_expr(eq, expr_map; expand_expr = false)
Expand All @@ -206,7 +206,7 @@ function convert_to_expr(eq, expr_map; expand_expr = false)
end

function get_expr_map(sys)
dvs = ModelingToolkit.states(sys)
dvs = ModelingToolkit.unknowns(sys)

Check warning on line 209 in lib/OptimizationMOI/src/OptimizationMOI.jl

View check run for this annotation

Codecov / codecov/patch

lib/OptimizationMOI/src/OptimizationMOI.jl#L209

Added line #L209 was not covered by tests
ps = ModelingToolkit.parameters(sys)
return vcat([ModelingToolkit.toexpr(_s) => Expr(:ref, :x, i)
for (i, _s) in enumerate(dvs)],
Expand Down
37 changes: 0 additions & 37 deletions src/cache.jl
Original file line number Diff line number Diff line change
@@ -1,40 +1,3 @@

function Base.getproperty(cache::SciMLBase.AbstractOptimizationCache, x::Symbol)
if x in (:u0, :p)
return getfield(cache.reinit_cache, x)
end
return getfield(cache, x)
end

SciMLBase.has_reinit(cache::SciMLBase.AbstractOptimizationCache) = true
function SciMLBase.reinit!(cache::SciMLBase.AbstractOptimizationCache; p = missing,
u0 = missing)
if p === missing && u0 === missing
p, u0 = cache.p, cache.u0
else # at least one of them has a value
if p === missing
p = cache.p
end
if u0 === missing
u0 = cache.u0
end
if (eltype(p) <: Pair && !isempty(p)) || (eltype(u0) <: Pair && !isempty(u0)) # one is a non-empty symbolic map
hasproperty(cache.f, :sys) && hasfield(typeof(cache.f.sys), :ps) ||
throw(ArgumentError("This cache does not support symbolic maps with `remake`, i.e. it does not have a symbolic origin." *
" Please use `remake` with the `p` keyword argument as a vector of values, paying attention to parameter order."))
hasproperty(cache.f, :sys) && hasfield(typeof(cache.f.sys), :states) ||
throw(ArgumentError("This cache does not support symbolic maps with `remake`, i.e. it does not have a symbolic origin." *
" Please use `remake` with the `u0` keyword argument as a vector of values, paying attention to state order."))
p, u0 = SciMLBase.process_p_u0_symbolic(cache, p, u0)
end
end

cache.reinit_cache.p = p
cache.reinit_cache.u0 = u0

return cache
end

struct OptimizationCache{F, RC, LB, UB, LC, UC, S, O, D, P, C} <:
SciMLBase.AbstractOptimizationCache
f::F
Expand Down

0 comments on commit 1c8978f

Please sign in to comment.