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

Fix chaining of MILP constraints #14540

Closed
nathanncohen mannequin opened this issue May 6, 2013 · 42 comments
Closed

Fix chaining of MILP constraints #14540

nathanncohen mannequin opened this issue May 6, 2013 · 42 comments

Comments

@nathanncohen
Copy link
Mannequin

nathanncohen mannequin commented May 6, 2013

sage: p = MixedIntegerLinearProgram()
sage: b = p.new_variable()
sage: int(1) <= b[0] <= int(2)
x_0 <= 2
sage: p = MixedIntegerLinearProgram()
sage: b = p.new_variable()
sage: float(0) <= b[0] <= int(1) <= b[1] <= float(2)
1 <= x_1 <= 2
sage: p = MixedIntegerLinearProgram()
sage: b = p.new_variable()
sage: b[0] <= 555*b[1] >= 2
2 <= x_0 <= 555*x_1

This can never work due to the way Python handles chained comparisons:

sage: p = MixedIntegerLinearProgram()
sage: b = p.new_variable()
sage: b[0] <= 3 <= 4
True

Depends on #20478

CC: @vbraun @dimpase @mkoeppe @videlec @tscrim

Component: linear programming

Author: Jeroen Demeyer

Branch/Commit: 96e133e

Reviewer: Matthias Koeppe, Travis Scrimshaw

Issue created by migration from https://trac.sagemath.org/ticket/14540

@nathanncohen nathanncohen mannequin added this to the sage-5.11 milestone May 6, 2013
@nathanncohen nathanncohen mannequin self-assigned this May 6, 2013
@dimpase
Copy link
Member

dimpase commented May 6, 2013

comment:1

In a script - do you mean a Python script, not Sage script, right?

@nathanncohen
Copy link
Mannequin Author

nathanncohen mannequin commented May 6, 2013

comment:2

Well, in any script that does not wrap integers with Integer(). So a .py that you load with %runfile is ok !

Nathann

@dimpase
Copy link
Member

dimpase commented May 7, 2013

comment:3

Replying to @nathanncohen:

Well, in any script that does not wrap integers with Integer(). So a .py that you load with %runfile is ok !

Nathann

and it's not only int, but float, too, that gives this problem.

@jdemeyer jdemeyer modified the milestones: sage-5.11, sage-5.12 Aug 13, 2013
@sagetrac-vbraun-spam sagetrac-vbraun-spam mannequin modified the milestones: sage-6.1, sage-6.2 Jan 30, 2014
@sagetrac-vbraun-spam sagetrac-vbraun-spam mannequin modified the milestones: sage-6.2, sage-6.3 May 6, 2014
@sagetrac-vbraun-spam sagetrac-vbraun-spam mannequin modified the milestones: sage-6.3, sage-6.4 Aug 10, 2014
@jdemeyer

This comment has been minimized.

@jdemeyer jdemeyer changed the title MILP constraints are silently misunderstood MILP constraints do no deal with Python ints properly Apr 19, 2016
@jdemeyer jdemeyer modified the milestones: sage-6.4, sage-7.2 Apr 19, 2016
@jdemeyer

This comment has been minimized.

@jdemeyer
Copy link

comment:10

I'm investigating this.

@jdemeyer

This comment has been minimized.

@jdemeyer jdemeyer changed the title MILP constraints do no deal with Python ints properly MILP constraints do no deal with Python types properly Apr 19, 2016
@jdemeyer
Copy link

Author: Jeroen Demeyer

@jdemeyer
Copy link

@jdemeyer
Copy link

Commit: ba7613b

@jdemeyer
Copy link

New commits:

ba7613bFix chained inequalities with non-Sage types

@dimpase
Copy link
Member

dimpase commented Apr 19, 2016

New commits:

ba7613bFix chained inequalities with non-Sage types

@mkoeppe
Copy link
Contributor

mkoeppe commented Apr 21, 2016

comment:25

It seems to work, except for bizarre behavior if one does try to chain in comparisons between constants.

sage: p = MixedIntegerLinearProgram()
sage: b = p.new_variable()
sage: b[0] <= 2 <= 2
True
sage: 0<=b[0]<=b[0]<=b[0]<=1
0 <= x_0 <= 2 <= x_0 <= x_0 <= 1

(The documentation should probably explain what is expected to work and what is not!)

I note that the unrelated comparison-hack code for SR silently throws away chained inequalities.

sage: var('x')
x
sage: x <= 2 <= 3
x <= 2
sage: x <= 2 <= x
x <= 2

Also, should add_constraint allow the user to add tautological constraints like this:

sage: p = MixedIntegerLinearProgram()
sage: p.add_constraint(0<=0)
ValueError: argument must be a linear function or constraint, got True

Someone should probably review your changes to Parent before this ticket is set to positive_review.

@jdemeyer
Copy link

comment:26

Replying to @mkoeppe:

It seems to work, except for bizarre behavior if one does try to chain in comparisons between constants.

sage: p = MixedIntegerLinearProgram()
sage: b = p.new_variable()
sage: b[0] <= 2 <= 2
True
sage: 0<=b[0]<=b[0]<=b[0]<=1
0 <= x_0 <= 2 <= x_0 <= x_0 <= 1

There are theoretical limits to what can work without fundamentally changing Python (or the preparser if you want to go that way), that's why it is called a "hack".

I could add a bit of documentation, although a full explanation would probably be too technical.

@mkoeppe
Copy link
Contributor

mkoeppe commented Apr 22, 2016

comment:27

Replying to @jdemeyer:

I could add a bit of documentation, although a full explanation would probably be too technical.

It would suffice to explain that you can chain linear expressions with the same comparison, and also use constants at the very left hand side and the very right hand side. If the user tries anything beyond that, there's either an error or weirdness.

@jdemeyer
Copy link

comment:28

Replying to @mkoeppe:

Replying to @jdemeyer:

I could add a bit of documentation, although a full explanation would probably be too technical.

It would suffice to explain that you can chain linear expressions with the same comparison, and also use constants at the very left hand side and the very right hand side. If the user tries anything beyond that, there's either an error or weirdness.

Constants in the middle also work, just not two constants next to eachother. For example, b[0] <= 0 <= b[1] works as expected.

@mkoeppe
Copy link
Contributor

mkoeppe commented Apr 22, 2016

comment:29

I know, it was just a suggestion how you could simplify the story when you write that documentation.

@jdemeyer
Copy link

Changed reviewer from Matthias Köppe to Matthias Koeppe

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Apr 22, 2016

Branch pushed to git repo; I updated commit sha1. New commits:

e8d8e67Improve documentation

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Apr 22, 2016

Changed commit from 682ba3e to e8d8e67

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Apr 22, 2016

Branch pushed to git repo; I updated commit sha1. This was a forced push. New commits:

6e10798Improve documentation

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Apr 22, 2016

Changed commit from e8d8e67 to 6e10798

@mkoeppe
Copy link
Contributor

mkoeppe commented Apr 22, 2016

comment:33

Looks good to me, but the changes to Parent still need independent review.

@tscrim
Copy link
Collaborator

tscrim commented Apr 24, 2016

Changed reviewer from Matthias Koeppe to Matthias Koeppe, Travis Scrimshaw

@tscrim
Copy link
Collaborator

tscrim commented Apr 24, 2016

comment:35

The changes in Parent look good to me. If everything else is good, then this would be a positive review.

@mkoeppe
Copy link
Contributor

mkoeppe commented Apr 24, 2016

comment:36

Thanks, Travis!

@mkoeppe
Copy link
Contributor

mkoeppe commented Apr 24, 2016

comment:38

The patchbot complains about decreased coverage, though

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Apr 25, 2016

Changed commit from 6e10798 to 96e133e

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Apr 25, 2016

Branch pushed to git repo; I updated commit sha1. New commits:

96e133eImprove `__cinit__` and add doctests

@jdemeyer
Copy link

comment:42

Thanks!

@vbraun
Copy link
Member

vbraun commented Apr 26, 2016

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

No branches or pull requests

5 participants