Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mtkize behind if and try blocks #676

Merged
merged 3 commits into from
Jan 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions ext/OptimizationMTKExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ function Optimization.instantiate_function(f, x, adtype::AutoModelingToolkit, p,
cons_hess_prototype = f.cons_hess_prototype,
expr = Optimization.symbolify(f.expr),
cons_expr = Optimization.symbolify.(f.cons_expr),
sys = sys,
observed = f.observed)
end

Expand Down Expand Up @@ -93,6 +94,7 @@ function Optimization.instantiate_function(f, cache::Optimization.ReInitCache,
cons_hess_prototype = f.cons_hess_prototype,
expr = Optimization.symbolify(f.expr),
cons_expr = Optimization.symbolify.(f.cons_expr),
sys = sys,
observed = f.observed)
end

Expand Down
10 changes: 9 additions & 1 deletion lib/OptimizationMOI/src/OptimizationMOI.jl
Original file line number Diff line number Diff line change
Expand Up @@ -273,9 +273,17 @@
maxtime::Union{Number, Nothing} = nothing,
abstol::Union{Number, Nothing} = nothing,
reltol::Union{Number, Nothing} = nothing,
mtkize = false,
kwargs...)
cache = if MOI.supports(_create_new_optimizer(opt), MOI.NLPBlock())
MOIOptimizationNLPCache(prob, opt; maxiters, maxtime, abstol, reltol, kwargs...)
MOIOptimizationNLPCache(prob,

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

View check run for this annotation

Codecov / codecov/patch

lib/OptimizationMOI/src/OptimizationMOI.jl#L279

Added line #L279 was not covered by tests
opt;
maxiters,
maxtime,
abstol,
reltol,
mtkize,
kwargs...)
else
MOIOptimizationCache(prob, opt; maxiters, maxtime, abstol, reltol, kwargs...)
end
Expand Down
57 changes: 43 additions & 14 deletions lib/OptimizationMOI/src/nlp.jl
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@

function MOIOptimizationNLPCache(prob::OptimizationProblem,
opt;
mtkize = false,
callback = nothing,
kwargs...)
reinit_cache = Optimization.ReInitCache(prob.u0, prob.p) # everything that can be changed via `reinit`
Expand Down Expand Up @@ -139,8 +140,13 @@
lcons = prob.lcons === nothing ? fill(T(-Inf), num_cons) : prob.lcons
ucons = prob.ucons === nothing ? fill(T(Inf), num_cons) : prob.ucons

if f.sys isa SymbolicIndexingInterface.SymbolCache{Nothing, Nothing, Nothing}
sys = MTK.modelingtoolkitize(prob)
if f.sys isa SymbolicIndexingInterface.SymbolCache{Nothing, Nothing, Nothing} && mtkize
try
sys = MTK.modelingtoolkitize(prob)

Check warning on line 145 in lib/OptimizationMOI/src/nlp.jl

View check run for this annotation

Codecov / codecov/patch

lib/OptimizationMOI/src/nlp.jl#L143-L145

Added lines #L143 - L145 were not covered by tests
catch err
throw(ArgumentError("Automatic symbolic expression generation with ModelingToolkit failed with error: $err.

Check warning on line 147 in lib/OptimizationMOI/src/nlp.jl

View check run for this annotation

Codecov / codecov/patch

lib/OptimizationMOI/src/nlp.jl#L147

Added line #L147 was not covered by tests
Try by setting `mtkize = false` instead if the solver doesn't require symbolic expressions."))
end
if !isnothing(prob.p) && !(prob.p isa SciMLBase.NullParameters)
unames = variable_symbols(sys)
pnames = parameter_symbols(sys)
Expand All @@ -156,22 +162,32 @@
obj_expr = sysprob.f.expr
cons_expr = sysprob.f.cons_expr
else
sys = f.sys
sys = f.sys isa SymbolicIndexingInterface.SymbolCache{Nothing, Nothing, Nothing} ?

Check warning on line 165 in lib/OptimizationMOI/src/nlp.jl

View check run for this annotation

Codecov / codecov/patch

lib/OptimizationMOI/src/nlp.jl#L165

Added line #L165 was not covered by tests
nothing : f.sys
obj_expr = f.expr
cons_expr = f.cons_expr
end

expr_map = get_expr_map(sys)
expr = convert_to_expr(obj_expr, expr_map; expand_expr = false)
expr = repl_getindex!(expr)
cons = MTK.constraints(sys)
_cons_expr = Vector{Expr}(undef, length(cons))
for i in eachindex(cons)
_cons_expr[i] = repl_getindex!(convert_to_expr(cons_expr[i],
expr_map;
expand_expr = false))
if sys === nothing
expr = obj_expr
_cons_expr = cons_expr

Check warning on line 173 in lib/OptimizationMOI/src/nlp.jl

View check run for this annotation

Codecov / codecov/patch

lib/OptimizationMOI/src/nlp.jl#L171-L173

Added lines #L171 - L173 were not covered by tests
else
expr_map = get_expr_map(sys)
expr = convert_to_expr(obj_expr, expr_map; expand_expr = false)
expr = repl_getindex!(expr)
cons = MTK.constraints(sys)
@show cons
_cons_expr = Vector{Expr}(undef, length(cons))
for i in eachindex(cons)
_cons_expr[i] = repl_getindex!(convert_to_expr(cons_expr[i],

Check warning on line 182 in lib/OptimizationMOI/src/nlp.jl

View check run for this annotation

Codecov / codecov/patch

lib/OptimizationMOI/src/nlp.jl#L175-L182

Added lines #L175 - L182 were not covered by tests
expr_map;
expand_expr = false))
end

Check warning on line 185 in lib/OptimizationMOI/src/nlp.jl

View check run for this annotation

Codecov / codecov/patch

lib/OptimizationMOI/src/nlp.jl#L185

Added line #L185 was not covered by tests
end

@show expr
@show _cons_expr

Check warning on line 189 in lib/OptimizationMOI/src/nlp.jl

View check run for this annotation

Codecov / codecov/patch

lib/OptimizationMOI/src/nlp.jl#L188-L189

Added lines #L188 - L189 were not covered by tests

evaluator = MOIOptimizationNLPEvaluator(f,
reinit_cache,
prob.lb,
Expand Down Expand Up @@ -387,11 +403,24 @@
function MOI.constraint_expr(evaluator::MOIOptimizationNLPEvaluator, i)
# expr has the form f(x,p) == 0 or f(x,p) <= 0
cons_expr = deepcopy(evaluator.cons_expr[i].args[2])
compop = Symbol(evaluator.cons_expr[i].args[1])
repl_getindex!(cons_expr)
_replace_parameter_indices!(cons_expr, evaluator.p)
_replace_variable_indices!(cons_expr)
return Expr(:call, compop, cons_expr, 0.0)
lb, ub = Float64(evaluator.lcons[i]), Float64(evaluator.ucons[i])
@show lb
@show ub
@show cons_expr
if lb == ub
return Expr(:call, :(==), cons_expr, lb)

Check warning on line 414 in lib/OptimizationMOI/src/nlp.jl

View check run for this annotation

Codecov / codecov/patch

lib/OptimizationMOI/src/nlp.jl#L409-L414

Added lines #L409 - L414 were not covered by tests
else
if lb == -Inf
return Expr(:call, :(<=), cons_expr, ub)
elseif ub == Inf
return Expr(:call, :(>=), cons_expr, lb)

Check warning on line 419 in lib/OptimizationMOI/src/nlp.jl

View check run for this annotation

Codecov / codecov/patch

lib/OptimizationMOI/src/nlp.jl#L416-L419

Added lines #L416 - L419 were not covered by tests
else
return Expr(:call, :between, cons_expr, lb, ub)

Check warning on line 421 in lib/OptimizationMOI/src/nlp.jl

View check run for this annotation

Codecov / codecov/patch

lib/OptimizationMOI/src/nlp.jl#L421

Added line #L421 was not covered by tests
end
end
end

function _add_moi_variables!(opt_setup, evaluator::MOIOptimizationNLPEvaluator)
Expand Down
Loading