From 1c8978f8b8675bb373e5194e69ec78926666fc31 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 25 Jan 2024 18:01:53 +0530 Subject: [PATCH] refactor: update for MTKv9 --- Project.toml | 2 +- lib/OptimizationMOI/Project.toml | 2 +- lib/OptimizationMOI/src/OptimizationMOI.jl | 8 ++--- src/cache.jl | 37 ---------------------- 4 files changed, 6 insertions(+), 43 deletions(-) diff --git a/Project.toml b/Project.toml index ed6e47638..3c35803e0 100644 --- a/Project.toml +++ b/Project.toml @@ -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" diff --git a/lib/OptimizationMOI/Project.toml b/lib/OptimizationMOI/Project.toml index 47a4585d3..aa27ad0af 100644 --- a/lib/OptimizationMOI/Project.toml +++ b/lib/OptimizationMOI/Project.toml @@ -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" diff --git a/lib/OptimizationMOI/src/OptimizationMOI.jl b/lib/OptimizationMOI/src/OptimizationMOI.jl index 6a30b7860..72f2cde17 100644 --- a/lib/OptimizationMOI/src/OptimizationMOI.jl +++ b/lib/OptimizationMOI/src/OptimizationMOI.jl @@ -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 @@ -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) @@ -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) ps = ModelingToolkit.parameters(sys) return vcat([ModelingToolkit.toexpr(_s) => Expr(:ref, :x, i) for (i, _s) in enumerate(dvs)], diff --git a/src/cache.jl b/src/cache.jl index a7a856ba6..a638fac86 100644 --- a/src/cache.jl +++ b/src/cache.jl @@ -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