0

Firstly, Sry fo the bad title of this question I simply don't know a better one. If you have a better one => Tell me!

So my problem is that I would like to write a simplex solver in Python by myself to deeply understand how they work.

Therefore, I would like to have something like this in my program:

 m.addConstr(x[0] <= 7)

Which basically should add a constraint to my model m. This works in Gurobi which is just wonderful cause it's so easy to read. The problem is that x[0] has to be an object where I itself can define what should happen when there is an inequality or equality or whatever, right?

I am happy to figure out most of the stuff by myself would just like to get an idea how this works.

2 Answers 2

1

It looks like you want to overload the comparison operators of whatever objects you're working with. So if Foo is the class of x[0] in your example, then you could write it like this:

class Foo:
    def __gt__(self, other):
        # construct and return some kind of constraint object

    def __lt__(self, other):
        # likewise

These special methods (__gt__, __ge__, __lt__, __le__, __ne__ and __eq__) are called for the left-hand object in a comparison relation. So if you have x > y, then the __gt__ method on x is called with y as an argument.

Sign up to request clarification or add additional context in comments.

Comments

1

I don't think it should be your first concern to come up with an elegant input syntax. You should rather implement the simplex algorithm first.

I suggest, you handle the input by writing a parser for the two standard formats for linear programming problems: .lp and .mps

If you still want to know how to implement proper expression handling in Python I recommend you have a look at PySCIPOpt since this is exactly doing what you want and you can inspect the entire source code.

1 Comment

I'm thinking about doing most of the stuff inline in Python as it is done in Gurobi normally​. Then you have a lot of freedom. I'll have a look at the .lp syntax. Thanks for the links!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.