2

How does == work in Gurobi Python?

After I have created the needed variables (x[s,d,r]), I'm trying to make an if statement like x[s,d,r] == 1. The problem is, that this statement is always true so it does not matter if I set it to x[s,d,r]==0 or x[s,d,r]==2, it is always the same.

x[s,d,r] is a binary variable defined by x[s,d,r] = m.addVar(0.0,1.0,1.0,GRB.BINARY,"x_"+ s+"_"+ d +"_" + r).

z = 0.0
for s in students:
    for d in dates:
        if (s,d) in preferences:        
            if preferences[s,d]!=0:
                for r in rooms:
                    if (d,r) in tutorials:
                        if x[s,d,r]>0.001:
                            print('%s:%s:%s:%s '%(s,d,r,preferences[s,d]))
                            z = z + preferences[s,d]
                        else:
                            print('no')                                 

 m.setObjective((z), GRB.MAXIMIZE)

So if x[s,d,r]>0.001: is the part that always evaluates to "true".

kosti4592
  • 21
  • 3
  • `x[s,d,r]==0` and `x[s,d,r]==2` are equality tests, not assignments. You are not changing the value. Use a *single* `=` to assign: `x[s,d,r] = 0`. – Martijn Pieters Jul 04 '15 at 17:22
  • so this is my actual intend to check if the variable x[s,d,r] is set to one or still zero. By "x[s,d,r]==0" means that I have changed the if statement so the it checks if x == 0. – kosti4592 Jul 04 '15 at 17:23
  • 1
    Sorry this just seems like a confused question. `x[s,d,r]` is a gurobi variable. It's not a number. The solution value `x[s,d,r].x` is a number, but you need to optimize to have that. It's very unclear what you're trying to do. – Pete Cacioppi Jul 04 '15 at 20:52

2 Answers2

2

The == operator for grb.Expr returns a grb.Constraint object. As mentioned in this answer, the constraint object is always truthy, so you will always get the 'if' part.

Assuming you already optimized, you want the actual value of the x value in the optimized solution. That's given by the X attrubutes on a grb.Variable. So you should replace your code with

                        if x[s,d,r].X>0.001:

and it should work as you expect.

Community
  • 1
  • 1
David Nehme
  • 21,379
  • 8
  • 78
  • 117
0

To investigate this, you should evaluate x[s,d,r] == 1 in isolation in a Python shell. I believe you will find that the result is never a boolean, but rather another Gurobi object. (I'd expect something like LinExpr.)

Gurobi (wisely) overloads all the arithmetic operations for variables so as to make it easier to construct constraints. This results in very human readable model building code. (IMHO, nearly as good as OPL or AMPL, with all the other advantages of Python).

Are you trying to check to see if the solution value for this variable is 1? For that, I think you want x[s,d,r].x == 1, which will evaluate to a boolean. (But only if there is a solution for the model).

Pete Cacioppi
  • 880
  • 7
  • 11
  • Thanks for the answer. Using x[s,d,r].x == 1 as the solution does not work because of fact, that x[s,d,r] == 1 is a part of an value that should be optimized with GRB.MAXIMIZE, so the value of x[s,d,r] is still not evaluated. I'll try to post the soure in my fist post. – kosti4592 Jul 04 '15 at 17:37