Skip to content

Commit

Permalink
Handle constraint bounds and some fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Vaibhavdixit02 committed Jan 10, 2024
1 parent 3fb0445 commit 5f1300f
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 11 deletions.
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
44 changes: 33 additions & 11 deletions lib/OptimizationMOI/src/nlp.jl
Original file line number Diff line number Diff line change
Expand Up @@ -167,17 +167,26 @@ function MOIOptimizationNLPCache(prob::OptimizationProblem,
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 172 in lib/OptimizationMOI/src/nlp.jl

View check run for this annotation

Codecov / codecov/patch

lib/OptimizationMOI/src/nlp.jl#L170-L172

Added lines #L170 - L172 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 181 in lib/OptimizationMOI/src/nlp.jl

View check run for this annotation

Codecov / codecov/patch

lib/OptimizationMOI/src/nlp.jl#L174-L181

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

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

View check run for this annotation

Codecov / codecov/patch

lib/OptimizationMOI/src/nlp.jl#L184

Added line #L184 was not covered by tests
end

@show expr
@show _cons_expr

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

View check run for this annotation

Codecov / codecov/patch

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

Added lines #L187 - L188 were not covered by tests

evaluator = MOIOptimizationNLPEvaluator(f,
reinit_cache,
prob.lb,
Expand Down Expand Up @@ -393,11 +402,24 @@ end
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 413 in lib/OptimizationMOI/src/nlp.jl

View check run for this annotation

Codecov / codecov/patch

lib/OptimizationMOI/src/nlp.jl#L408-L413

Added lines #L408 - L413 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 418 in lib/OptimizationMOI/src/nlp.jl

View check run for this annotation

Codecov / codecov/patch

lib/OptimizationMOI/src/nlp.jl#L415-L418

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

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

View check run for this annotation

Codecov / codecov/patch

lib/OptimizationMOI/src/nlp.jl#L420

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

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

0 comments on commit 5f1300f

Please sign in to comment.