Skip to content

Commit

Permalink
Remove ModelingToolkit from dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
RXGottlieb committed Jun 3, 2024
1 parent 1f0a5a3 commit 8bcc454
Show file tree
Hide file tree
Showing 7 changed files with 219 additions and 228 deletions.
2 changes: 0 additions & 2 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"
DocStringExtensions = "ffbed154-4ef7-542d-bbb7-c09d3a79fcae"
Graphs = "86223c79-3864-5bf0-83f7-82e725a168b6"
IfElse = "615f187c-cbe4-4ef1-ba3b-2fcf58d6d173"
ModelingToolkit = "961ee093-0014-501f-94e3-6117800e7a78"
SymbolicUtils = "d1185830-fcd6-423d-90d6-eec64667417b"
Symbolics = "0c5d862f-8b57-4792-8d23-62f2024744c7"

Expand All @@ -18,7 +17,6 @@ CUDA = "5"
DocStringExtensions = "0.8 - 0.9"
Graphs = "1"
IfElse = "0.1.0 - 0.1.1"
ModelingToolkit = "8"
SymbolicUtils = "1"
Symbolics = "5"
julia = "1.6"
Expand Down
8 changes: 1 addition & 7 deletions src/SourceCodeMcCormick.jl
Original file line number Diff line number Diff line change
@@ -1,18 +1,13 @@

module SourceCodeMcCormick

# using ModelingToolkit
import ModelingToolkit: Equation, ODESystem, @named, toparam, iv_from_nested_derivative, value, collect_vars!
using Symbolics
using SymbolicUtils.Code
using IfElse
using DocStringExtensions
using Graphs
using CUDA
import Dates

import SymbolicUtils: BasicSymbolic, exprtype, SYM, TERM, ADD, MUL, POW, DIV

import SymbolicUtils: BasicSymbolic, exprtype, SYM, TERM, ADD, MUL, POW, DIV

"""
Expand All @@ -29,10 +24,9 @@ abstract type AbstractTransform end
"""
transform_rule(::AbstractTransform, ::Equation)
transform_rule(::AbstractTransform, ::Vector{Equation})
transform_rule(::AbstractTransform, ::ModelingToolkit.ODESystem)
transform_rule(::AbstractTransform, ::Num)
Apply the desired transformation to a given expression, equation, set of equations, or ODESystem.
Apply the desired transformation to a given expression, equation, or set of equations.
If the `AbstractTransform` is an `IntervalTransform`, all symbols are split into lower and upper
bounds, and the number of expressions/equations is initially doubled. `SourceCodeMcCormick` uses
the auxiliary variable method, so factorable expressions may be separated into multiple expressions
Expand Down
40 changes: 20 additions & 20 deletions src/interval/interval.jl
Original file line number Diff line number Diff line change
Expand Up @@ -30,27 +30,27 @@ function var_names(::IntervalTransform, a::BasicSymbolic)
end
end

function translate_initial_conditions(::IntervalTransform, prob::ODESystem, new_eqs::Vector{Equation})
vars, params = extract_terms(new_eqs)
var_defaults = Dict{Any, Any}()
param_defaults = Dict{Any, Any}()
# function translate_initial_conditions(::IntervalTransform, prob::ODESystem, new_eqs::Vector{Equation})
# vars, params = extract_terms(new_eqs)
# var_defaults = Dict{Any, Any}()
# param_defaults = Dict{Any, Any}()

for (key, val) in prob.defaults
name_lo = String(get_name(key))*"_"*"lo"
name_hi = String(get_name(key))*"_"*"hi"
for i in vars
if in(String(i.f.name), (name_lo, name_hi))
var_defaults[i] = val
end
end
for i in params
if in(String(i.name), (name_lo, name_hi))
param_defaults[i] = val
end
end
end
return var_defaults, param_defaults
end
# for (key, val) in prob.defaults
# name_lo = String(get_name(key))*"_"*"lo"
# name_hi = String(get_name(key))*"_"*"hi"
# for i in vars
# if in(String(i.f.name), (name_lo, name_hi))
# var_defaults[i] = val
# end
# end
# for i in params
# if in(String(i.name), (name_lo, name_hi))
# param_defaults[i] = val
# end
# end
# end
# return var_defaults, param_defaults
# end


"""
Expand Down
84 changes: 42 additions & 42 deletions src/relaxation/relaxation.jl
Original file line number Diff line number Diff line change
Expand Up @@ -34,51 +34,51 @@ function var_names(::McCormickIntervalTransform, a::Any)
end


function translate_initial_conditions(::McCormickTransform, prob::ODESystem, new_eqs::Vector{Equation})
vars, params = extract_terms(new_eqs)
var_defaults = Dict{Any, Any}()
param_defaults = Dict{Any, Any}()
# function translate_initial_conditions(::McCormickTransform, prob::ODESystem, new_eqs::Vector{Equation})
# vars, params = extract_terms(new_eqs)
# var_defaults = Dict{Any, Any}()
# param_defaults = Dict{Any, Any}()

for (key, val) in prob.defaults
name_cv = String(get_name(key))*"_"*"cv"
name_cc = String(get_name(key))*"_"*"cc"
for i in vars
if String(i.f.name)==name_cv || String(i.f.name)==name_cc
var_defaults[i] = val
end
end
for i in params
if String(i.name)==name_cv || String(i.name)==name_cc
param_defaults[i] = val
end
end
end
return var_defaults, param_defaults
end
# for (key, val) in prob.defaults
# name_cv = String(get_name(key))*"_"*"cv"
# name_cc = String(get_name(key))*"_"*"cc"
# for i in vars
# if String(i.f.name)==name_cv || String(i.f.name)==name_cc
# var_defaults[i] = val
# end
# end
# for i in params
# if String(i.name)==name_cv || String(i.name)==name_cc
# param_defaults[i] = val
# end
# end
# end
# return var_defaults, param_defaults
# end

function translate_initial_conditions(::McCormickIntervalTransform, prob::ODESystem, new_eqs::Vector{Equation})
vars, params = extract_terms(new_eqs)
var_defaults = Dict{Any, Any}()
param_defaults = Dict{Any, Any}()
# function translate_initial_conditions(::McCormickIntervalTransform, prob::ODESystem, new_eqs::Vector{Equation})
# vars, params = extract_terms(new_eqs)
# var_defaults = Dict{Any, Any}()
# param_defaults = Dict{Any, Any}()

for (key, val) in prob.defaults
name_lo = String(get_name(key))*"_"*"lo"
name_hi = String(get_name(key))*"_"*"hi"
name_cv = String(get_name(key))*"_"*"cv"
name_cc = String(get_name(key))*"_"*"cc"
for i in vars
if in(String(i.f.name), (name_lo, name_hi, name_cv, name_cc))
var_defaults[i] = val
end
end
for i in params
if in(String(i.name), (name_lo, name_hi, name_cv, name_cc))
param_defaults[i] = val
end
end
end
return var_defaults, param_defaults
end
# for (key, val) in prob.defaults
# name_lo = String(get_name(key))*"_"*"lo"
# name_hi = String(get_name(key))*"_"*"hi"
# name_cv = String(get_name(key))*"_"*"cv"
# name_cc = String(get_name(key))*"_"*"cc"
# for i in vars
# if in(String(i.f.name), (name_lo, name_hi, name_cv, name_cc))
# var_defaults[i] = val
# end
# end
# for i in params
# if in(String(i.name), (name_lo, name_hi, name_cv, name_cc))
# param_defaults[i] = val
# end
# end
# end
# return var_defaults, param_defaults
# end

# A symbolic way of evaluating the line segment between (xL, zL) and (xU, zU) at x (returns IfElse block)
line_expr(x, xL, xU, zL, zU) = IfElse.ifelse(zU > zL, (zL*(xU - x) + zU*(x - xL))/(xU - xL), zU)
Expand Down
2 changes: 1 addition & 1 deletion src/relaxation/rules.jl
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ function transform_rule(::McCormickTransform, ::typeof(*), zL, zU, zcv, zcc, xL,
# # This returns mult_kernel(-x,-y). Swap x with flipped x and y with flipped
# # y, and since both are flipped, no need to worry about negative signs
# cv = max(yL*xcc + xL*ycc - xL*yL, yU*xcc + xU*ycc - xU*yU)
# cc = min(yU*xcv + xL*ycv - xL*yU, yL*xcv + xL*ycv - xU*yL)
# cc = min(yU*xcv + xL*ycv - xL*yU, yL*xcv + xU*ycv - xU*yL)


# # [x-,ym]
Expand Down
104 changes: 52 additions & 52 deletions src/transform/transform.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,58 +3,58 @@ include(joinpath(@__DIR__, "binarize.jl"))
include(joinpath(@__DIR__, "factor.jl"))


function apply_transform(transform::T, prob::ODESystem; constants::Vector{Num}=Num[]) where T<:AbstractTransform

# Factorize all model equations to generate a new set of equations

genparam(get_name(prob.iv.val))

equations = Equation[]
for eqn in prob.eqs
current = length(equations)
factor(eqn.rhs, eqs=equations)
if length(equations) > current
push!(equations, Equation(eqn.lhs, equations[end].rhs))
deleteat!(equations, length(equations)-1)
else
index = findall(x -> isequal(x.rhs, eqn.rhs), equations)
push!(equations, Equation(eqn.lhs, equations[index[1]].lhs))
end
end

# Apply transform rules to the factored equations to make the final equation set
new_equations = Equation[]
for a in equations
zn = var_names(transform, zstr(a))
if string(xstr(a)) in string.(constants)
xn = (xstr(a), xstr(a), xstr(a), xstr(a))
else
xn = var_names(transform, xstr(a))
end
if isone(arity(a))
targs = (transform, op(a), zn..., xn...)
else
if string(ystr(a)) in string.(constants)
yn = (ystr(a), ystr(a), ystr(a), ystr(a))
else
yn = var_names(transform, ystr(a))
end
targs = (transform, op(a), zn..., xn..., yn...)
end
new = transform_rule(targs...)
for i in new
push!(new_equations, i)
end
end

# Copy model start points to the newly transformed variables
var_defaults, param_defaults = translate_initial_conditions(transform, prob, new_equations)

# Use the transformed equations and new start points to generate a new ODE system
@named new_sys = ODESystem(new_equations, defaults=merge(var_defaults, param_defaults))

return new_sys
end
# function apply_transform(transform::T, prob::ODESystem; constants::Vector{Num}=Num[]) where T<:AbstractTransform

# # Factorize all model equations to generate a new set of equations

# genparam(get_name(prob.iv.val))

# equations = Equation[]
# for eqn in prob.eqs
# current = length(equations)
# factor(eqn.rhs, eqs=equations)
# if length(equations) > current
# push!(equations, Equation(eqn.lhs, equations[end].rhs))
# deleteat!(equations, length(equations)-1)
# else
# index = findall(x -> isequal(x.rhs, eqn.rhs), equations)
# push!(equations, Equation(eqn.lhs, equations[index[1]].lhs))
# end
# end

# # Apply transform rules to the factored equations to make the final equation set
# new_equations = Equation[]
# for a in equations
# zn = var_names(transform, zstr(a))
# if string(xstr(a)) in string.(constants)
# xn = (xstr(a), xstr(a), xstr(a), xstr(a))
# else
# xn = var_names(transform, xstr(a))
# end
# if isone(arity(a))
# targs = (transform, op(a), zn..., xn...)
# else
# if string(ystr(a)) in string.(constants)
# yn = (ystr(a), ystr(a), ystr(a), ystr(a))
# else
# yn = var_names(transform, ystr(a))
# end
# targs = (transform, op(a), zn..., xn..., yn...)
# end
# new = transform_rule(targs...)
# for i in new
# push!(new_equations, i)
# end
# end

# # Copy model start points to the newly transformed variables
# var_defaults, param_defaults = translate_initial_conditions(transform, prob, new_equations)

# # Use the transformed equations and new start points to generate a new ODE system
# @named new_sys = ODESystem(new_equations, defaults=merge(var_defaults, param_defaults))

# return new_sys
# end

function apply_transform(transform::T, eqn_vector::Vector{Equation}; constants::Vector{Num}=Num[]) where T<:AbstractTransform

Expand Down
Loading

0 comments on commit 8bcc454

Please sign in to comment.