An issue with a constraint involving len() method

54 views
Skip to first unread message

lahlou aziz

unread,
Feb 4, 2018, 2:10:06 AM2/4/18
to Gurobi Optimization
Hi,

I am having some difficulties to spot where does an error comes from regarding a constraint I have designed and implemented in Python.

I would like to implement a affinity constraint where for some request r I have to condition some elements of r to be put within on the same location. Each request contains two sets one embodies the set of all items in r while the second embodies a subset of items of r. Say r = [items = {1,2,3,4} , affinity = {2,4}].

req[r].__getitem__(2) represent the data structure of affinity set while req[r].__getitem__(0)[0] represent the items set.

A[r,k,j] is a decision variable = 1 if element u of r is located on x. So given the request r and the set of affinity items I want to make sure that for a given location x and a request r the number of elements of affinity r are put within the same location x while others wherever.

I implemented the constraint but when looking to the my_model_before_solving.lp file, it does not appear ! Does it have to do with the len method (I wanted to the concept of cardinality of a set) ? Could anyone shed some lights ?

Here is a piece of a relevant code :

my_model.addConstrs(
(quicksum(A_r_k_j[r - 1, k, j]
for j in phy_server_id) == len(req[r].__getitem__(2))
for r in R
for k in req[r].__getitem__(2) if k not in req[r].__getitem__(0)[0]),
"Constraint10")

Thanks,
Regards

Robert Luce

unread,
Feb 6, 2018, 2:41:30 PM2/6/18
to Gurobi Optimization
Hello Aziz,

I'm not quite sure I can follow your problem description here.  But if you already suspect that the RHS value produced by the len method in the given snippet is a source of trouble, the easiest procedure would be to debug your python script on your side.  Try starting you script with -mpdb and check whether all the pieces of the constraint look like you would expect.

https://docs.python.org/2/library/pdb.html

Robert

lahlou aziz

unread,
Feb 6, 2018, 3:01:56 PM2/6/18
to Gurobi Optimization
Hi Robert,

Thank you for your reply, but the thing is that I checked already the constraint mathematically and it works. It seems like it is ignored by Gurobi.
Any idea why ?

Thanks
Aziz

Robert Luce

unread,
Feb 6, 2018, 3:08:10 PM2/6/18
to Gurobi Optimization
Hello Aziz,

can you post a minimal, but yet fully functional python snippet that shows the behaviour that you are observing?  Ideally you would end up with something like

m = Model()
x
= m.addVars(2)
m
.addConstr(x[0] + x[1] <= .5)
m
.write('a.lp')

where now 'a.lp' doesn't show the added constraint.  Please try as hard as possible to remove all model artefacts not needed to demonstrate your observed behaviour, and simplify all involved data as much as possible.

Robert

lahlou aziz

unread,
Feb 8, 2018, 12:17:39 AM2/8/18
to Gurobi Optimization

Hi Robert,

I am having an instance of the well known assignment problem that is forming the core of my optimization model. The whole code I am not able to share it here for non disclosure agreement considerations.
However, please find here the constraint that I expressed mathematically and hope this will help and clarify well my issue as I described it above in my first post.

Regards,
Aziz

Robert Luce

unread,
Feb 8, 2018, 4:57:27 AM2/8/18
to Gurobi Optimization
Hello Aziz,

from the description you showed in your original post it is not possible to reproduce the behavior you are reporting.  Without anything concrete at hand all I can do is recommending the following.

0) Add each constraint individually (addConstr) instead of batches (addConstrs).  This reduces the risk of programming errors in your script.
1) Make sure that the LHS of each constraint evaluates to a LinExpr object.
2) Make sure that the RHS of each constraint evaluates to a number.
3) Try to keep intermediate object references for easier debugging, i.e., write

c = x[0] + x[1] <= .5
m
.addConstr(c)

instead of

m.addConstr(x[0] + x[1] <= .5)

so that you can inspect the constraint object in question more easily from within pdb (see earlier post about that).


Robert

lahlou aziz

unread,
Feb 8, 2018, 11:05:34 AM2/8/18
to Gurobi Optimization
Hi Robert,

Thank you for your nice answer, I will follow your recommandations and see how it goes,

Regards,
Aziz
Reply all
Reply to author
Forward
0 new messages