Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
Vaibhavdixit02 committed Dec 14, 2023
1 parent 822b677 commit 8ab2e6f
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 18 deletions.
34 changes: 17 additions & 17 deletions lib/OptimizationMOI/src/OptimizationMOI.jl
Original file line number Diff line number Diff line change
Expand Up @@ -116,25 +116,25 @@ function __moi_status_to_ReturnCode(status::MOI.TerminationStatusCode)
end
end

function get_expr_map(prob, f)
pairs_arr = if prob.p isa SciMLBase.NullParameters
[Meta.parse(string(_s)) => Expr(:ref, :x, i) for (i, _s) in enumerate(f.syms)]
else
vcat([Symbol(_s) => Expr(:ref, :x, i) for (i, _s) in enumerate(f.syms)],
[Symbol(_s) => Expr(:ref, p, i) for (i, _p) in enumerate(f.paramsyms)])
end
return pairs_arr
end
# function get_expr_map(prob, f)
# pairs_arr = if prob.p isa SciMLBase.NullParameters
# [Meta.parse(string(_s)) => Expr(:ref, :x, i) for (i, _s) in enumerate(f.syms)]
# else
# vcat([Symbol(_s) => Expr(:ref, :x, i) for (i, _s) in enumerate(f.syms)],
# [Symbol(_s) => Expr(:ref, p, i) for (i, _p) in enumerate(f.paramsyms)])
# end
# return pairs_arr
# end

"""
Substitute variables and parameters with canonical names x and p respectively.
"""
function rep_pars_vals!(e::Expr, p)
rep_pars_vals!.(e.args, Ref(p))
replace!(e.args, p...)
end
# """
# Substitute variables and parameters with canonical names x and p respectively.
# """
# function rep_pars_vals!(e::Expr, p)
# rep_pars_vals!.(e.args, Ref(p))
# replace!(e.args, p...)
# end

function rep_pars_vals!(e, p) end
# function rep_pars_vals!(e, p) end

"""
Replaces every expression `:x[i]` with `:x[MOI.VariableIndex(i)]`
Expand Down
12 changes: 11 additions & 1 deletion lib/OptimizationMOI/src/moi.jl
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@ function MOIOptimizationCache(prob::OptimizationProblem, opt; kwargs...)
throw(ArgumentError("Expected an `OptimizationProblem` that was setup via an `OptimizationSystem`, or AutoModelingToolkit ad choice"))

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#L23

Added line #L23 was not covered by tests
end
end

# 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)
expr_map = get_expr_map(prob.f.sys)
expr = repl_getindex!(convert_to_expr(f.expr, expr_map; expand_expr = false))

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

View check run for this annotation

Codecov / codecov/patch

lib/OptimizationMOI/src/moi.jl#L29

Added line #L29 was not covered by tests

cons = MTK.constraints(f.sys)

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

View check run for this annotation

Codecov / codecov/patch

lib/OptimizationMOI/src/moi.jl#L31

Added line #L31 was not covered by tests
Expand Down Expand Up @@ -448,3 +449,12 @@ function convert_to_expr(eq, expr_map; expand_expr = false)
expr = symbolify!(expr)
return expr
end

function get_expr_map(sys)
dvs = ModelingToolkit.states(sys)
ps = ModelingToolkit.parameters(sys)
return vcat([ModelingToolkit.toexpr(_s) => Expr(:ref, :x, i)

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

View check run for this annotation

Codecov / codecov/patch

lib/OptimizationMOI/src/moi.jl#L453-L456

Added lines #L453 - L456 were not covered by tests
for (i, _s) in enumerate(dvs)],
[ModelingToolkit.toexpr(_p) => Expr(:ref, :p, i)
for (i, _p) in enumerate(ps)])
end

0 comments on commit 8ab2e6f

Please sign in to comment.