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

adds Inequality #717

Merged
merged 13 commits into from
Oct 10, 2022
Merged

Conversation

ValentinKaisermayer
Copy link
Contributor

follow up on #630

Indented use-case:

using ModelingToolkit
using Symbolics

@variables x y
@parameters a b
objective_func = (a - x)^2 + b * (y - x^2)^2
cons = [
    x^2 + y^2  0.8
    0  x * y
    x * y  5 
]
sys = OptimizationSystem(objective_func, [x, y], [a, b], name = :sys, constraints = cons)

julia> cons
3-element Vector{Inequality}:
 x^2 + y^2  0.8
 0  x*y
 x*y  5

@codecov-commenter
Copy link

codecov-commenter commented Aug 29, 2022

Codecov Report

Merging #717 (8d3eb33) into master (ab7e1aa) will decrease coverage by 72.79%.
The diff coverage is 0.00%.

@@            Coverage Diff             @@
##           master    #717       +/-   ##
==========================================
- Coverage   75.82%   3.03%   -72.80%     
==========================================
  Files          23      24        +1     
  Lines        2792    2737       -55     
==========================================
- Hits         2117      83     -2034     
- Misses        675    2654     +1979     
Impacted Files Coverage Δ
src/Symbolics.jl 0.00% <ø> (-71.43%) ⬇️
src/equations.jl 0.00% <0.00%> (-51.03%) ⬇️
src/inequality.jl 0.00% <0.00%> (ø)
src/groebner_basis.jl 0.00% <0.00%> (-100.00%) ⬇️
src/semipoly.jl 0.00% <0.00%> (-91.31%) ⬇️
src/linear_algebra.jl 0.00% <0.00%> (-89.91%) ⬇️
src/array-lib.jl 0.00% <0.00%> (-80.56%) ⬇️
src/diff.jl 0.00% <0.00%> (-80.29%) ⬇️
src/arrays.jl 0.00% <0.00%> (-78.27%) ⬇️
src/complex.jl 0.00% <0.00%> (-75.00%) ⬇️
... and 14 more

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

@ValentinKaisermayer
Copy link
Contributor Author

Should we add a compound inequality in order to represent a <= x <= b?

@shashi
Copy link
Member

shashi commented Aug 31, 2022

Can't you use an & term?

@ValentinKaisermayer
Copy link
Contributor Author

ValentinKaisermayer commented Aug 31, 2022

a <= x constructs an inequality. Would a <= x & x <= b construct simply two inequalities or would it be a separate type? It seems that the Optimization.jl interface expects this form.

BTW the & is implicit already. All constraints have to hold simultaneously.

@ValentinKaisermayer ValentinKaisermayer marked this pull request as ready for review August 31, 2022 18:36
@shashi
Copy link
Member

shashi commented Sep 2, 2022

Would a <= x & x <= b construct simply two inequalities or would it be a separate type?

If Inequality behaved like an istree (see https://symbolicutils.juliasymbolics.org/interface/) and is subtype of Symbolic then it should automatically construct a Term with & as the operation and the two inequalities as the arguments. Feel free to add a new type if that is too generic!

@ValentinKaisermayer
Copy link
Contributor Author

I have followed the implementation of Equation. Is there any reason why it is not an isterm?

@ValentinKaisermayer
Copy link
Contributor Author

I guess it would be somewhat important to be able to simplify inequalities into this form. Otherwise for e.g. Ipopt we would have double the amount of inequality constraints.

@dpsanders
Copy link

I believe that you can't write a <= x <= b due to how Julia parses this. I think there may be an issue about that?

@ValentinKaisermayer
Copy link
Contributor Author

However, this could be handled via a the constructor, doesn't it?

<=(a,b) = Inequality(a, b, leq)
<=(a,b::Inequality) = IntervalInequality(a, b.lhs, b.rhs, leq)

@dpsanders
Copy link

Yes but only if you write a <= (b <= c) with parentheses.

The problem is with the way Julia parses a <= b <= c:

julia> Meta.@lower a <= b <= c
:($(Expr(:thunk, CodeInfo(
    @ none within `top-level scope`
1%1 = a <= b
└──      goto #3 if not %1
2%3 = b <= c
└──      return %3
3return false
))))

@ValentinKaisermayer
Copy link
Contributor Author

@shashi If you are fine with the interface right now, can we merge this PR and leave a term interface or separate type as a follow-up?

@shashi
Copy link
Member

shashi commented Sep 16, 2022

Sure we can merge this :)

@ValentinKaisermayer
Copy link
Contributor Author

should fix #740 as well

@shashi shashi merged commit 61bf8a9 into JuliaSymbolics:master Oct 10, 2022
@ValentinKaisermayer ValentinKaisermayer deleted the vk-inequality branch October 11, 2022 11:17
@@ -126,6 +126,10 @@ for T in [:Num, :Complex, :Number], S in [:Num, :Complex, :Number]
end
end

canonical_form(eq::Equation) = eq.lhs - eq.rhs ~ 0

get_variables(eq::Equation) = unique(vcat(get_variables(eq.lhs), get_variables(eq.rhs)))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ValentinKaisermayer, @YingboMa, @shashi Just note this is a breaking change as previously get_variables only applied to the rhs. It also means get_variables is no longer consistent with get_variables!. Please be aware of this in making any new Symbolics releases. (It may not have any practical effect -- so far I haven't found anything that really relied on that behavior but I could have missed something in ModelingToolkit or Catalyst...)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess there has been a release already and this was included, so it's a bit late to make sure the release is flagged as breaking...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How can it be breaking if get_variables(eq::Equation) is added and not altered?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was previously (intentionally) defined with a different behavior:

function get_variables!(vars, e::Equation, varlist=nothing)

get_variables(e, varlist=nothing) = get_variables!([], e, varlist)

The new dispatch changes this behavior.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see. We can remove it, it is after all just for convenience and not used in MTK. Only get_variables!: https://github.com/SciML/ModelingToolkit.jl/blob/28b36c8f9932acfc6c54737c51437d35b192b0d7/src/systems/dependency_graphs.jl#L43
However, I must say, that without proper documentation it is not clear why only the rhs is considered. I guess it assumes 0~f(x) or D(x)~f(x), but just by convention.
https://symbolics.juliasymbolics.org/stable/manual/expression_manipulation/#Symbolics.get_variables

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the change makes sense given ModelingToolkit is now allowing more flexibility in equation structure, but the in-place version should probably get updated for consistency. I agree the behavior should get documented in either case though!

x - y ≲ 0
```
"""
function ≲(lhs, rhs)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add a one-line description for how to type ? For example, Base.MathConstants.pi.

Symbolics.jl is not in SciML, but SciMLStyle doesn't allow Unicode to be used in public APIs.
See SciMLStyle: General Naming Principles.
So it is probably better to change the function name.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See #787

So it is probably better to change the function name.

Yeah, but to what?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants