Skip to content

Nonlinear: change how constraints are parsed #1817

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

Closed
odow opened this issue Apr 25, 2022 · 2 comments · Fixed by #1818
Closed

Nonlinear: change how constraints are parsed #1817

odow opened this issue Apr 25, 2022 · 2 comments · Fixed by #1818
Labels
Project: next-gen nonlinear support Issues relating to nonlinear support

Comments

@odow
Copy link
Member

odow commented Apr 25, 2022

To minimize differences when refactoring, we kept the same parsing behavior as JuMP. That means, constraints like x^2 <= 1 get parsed into x^2 - 1 <= 0.0:

function _expr_to_constraint(expr::Expr)
if isexpr(expr, :comparison)
@assert expr.args[2] == expr.args[4]
@assert expr.args[2] in (:<=, :>=)
lhs, body, rhs =
_normalize_constraint_expr(expr.args[1], expr.args[3], expr.args[5])
return body, MOI.Interval(lhs, rhs)
end
lhs, rhs = _normalize_constraint_expr(expr.args[2], expr.args[3])
if expr.args[1] == :<=
return :($lhs - $rhs), MOI.LessThan(0.0)
elseif expr.args[1] == :>=
return :($lhs - $rhs), MOI.GreaterThan(0.0)
else
@assert expr.args[1] == :(==)
return :($lhs - $rhs), MOI.EqualTo(0.0)
end
end

Changing this behavior would lead to more informative RHSs being passed to the solvers, and remove an additional operation. However, doing so would break JuMP's tests and printing, which have things like this:

model = Nonlinear.Model()
x = MOI.VariableIndex(1)
input = :($x^2 + 1 <= 1.0)
c = Nonlinear.add_constraint(model, input)
@test model[c].set == MOI.LessThan(0.0)
evaluator = Nonlinear.Evaluator(model)
MOI.initialize(evaluator, [:ExprGraph])
@test MOI.constraint_expr(evaluator, 1) == :((x[$x]^2 + 1) - 1.0 <= 0.0)

As suggested by @blegat #1804 (comment), one option is to remove the ability to specify constraints as a <= b, and instead force add_constraint(model, f, set), and then make JuMP keep the existing behavior.

@blegat
Copy link
Member

blegat commented Apr 25, 2022

constraints like x^2 <= 1 get parsed into x^2 - 1 <= 0.0

That's the opposite right?

Tha's a good argument, we don't want MOI to have to do these choices. Everything is explicit in MOI

@odow
Copy link
Member Author

odow commented Apr 25, 2022

That's the opposite right?

No, at the moment that's what JuMP does.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Project: next-gen nonlinear support Issues relating to nonlinear support
Development

Successfully merging a pull request may close this issue.

2 participants