0

I solved the MIP in Gurobi Python Interface. After solving the optimization model, I need to sort some of the decision variables in descending order and do some modifications/calculations at the end of my model. Then, run my model again and get the D.V. value. I need to repeat this process several times. However, when I use 'sorted' syntax, I get the error saying: a={} a= sorted(x[i,j,k],reverse=True ) TypeError: 'Var' object is not iterable I also would like to know if I have a decision variable like: gurbi.Var x3,2,1 (value 1.0), is it posibble to have indexes (3,2,4) in a matrix and corresponding value 1.0 in other matrix? I am new to gurobi and was wondering if you can help me. Regards

hsi
  • 5
  • 7
  • What is the data structure you are using for the decision variables x? How are you generating them? – Greg Glockner Nov 27 '16 at 19:27
  • I am using dictionary. like the following code: x = {} for i in range(3): for j in range(2): for t in range(80): x[(i + 1, j + 1, t + 1)] = m.addVar(lb=0, ub=GRB.INFINITY, vtype=GRB.INTEGER, name="x%d,%d,%d" % (i + 1, j + 1, t + 1)) – hsi Nov 27 '16 at 21:35

1 Answers1

1

The Python sorted() function takes a mutable sequence (like a list) and sorts it according to a key or comparison function. The most Pythonic option is to create a list of Gurobi Var objects and sort them according to their solution values. For example, to sort the variables in descending order of their optimal values, try:

vars = m.getVars()
vars.sort(key=lambda y:y.X, reverse=True)

where the lambda function retrieves the solution value (X attribute) for the Var object. Now, if you want to sort a list of keys, then try:

keys = # your list of keys ...
keys.sort(key=lambda k:x[k].X, reverse=True)

where x is your dictionary of variables.

Community
  • 1
  • 1
Greg Glockner
  • 5,433
  • 2
  • 20
  • 23
  • Thank you very much Dr. Glockner. It really helped. Regards – hsi Nov 29 '16 at 17:34
  • Good, then you should mark that this answer solves your question. – Greg Glockner Nov 29 '16 at 19:31
  • Now that I have sorted values and keys of my variables, and decided on choosing one of the values in my sorted vars list, how can I access corresponding key. I need that key. I tried different way. However, it is said object value is not iterable. for example, I have: (4.0,3.0,1.0) vars with (3, 1, 45), (2, 1, 80), (3, 1, 37) keys. If I choose 3.0 in vars, how can I access (2,1,80) in keys? Regards – hsi Dec 01 '16 at 16:29
  • You should either associate the key with the Var object, or sort the list of keys rather than the list of Vars. – Greg Glockner Dec 01 '16 at 21:25
  • I tried sorting keys like: Capacitykeys.sort(key=lambda k: capacity[k].X, reverse=True). However, using them in a loop to do some modification such as x[i,j,t] -= 1, I have error saying "Capacitykeys.sort(key=lambda k: capacity[k].X, reverse=True) AttributeError: 'gurobipy.LinExpr' object has no attribute 'X' ". I would like to mention I have different variables such as x, y ,z and I want to modify only x values many time after optimization, then update the MIP again each time. I appreciate your help. Regards – hsi Dec 15 '16 at 14:58
  • Looks like you are not properly modifying the variable; you are changing x[i,j,t] to a linear expression. – Greg Glockner Dec 15 '16 at 15:31
  • Thank you! Is there any way I can only modify the value of x[i,j,t]? I tried x[i,j,t].X -= 1, but again no changes/modifications and same error. Regards – hsi Dec 15 '16 at 15:40
  • Decision variables in Gurobi are objects. These objects have attributes to represent different things, like the objective coefficient (Obj), upper and lower bounds (UB, LB), whether they are continuous or integer (VType) and the optimal solution (X). Fixing (constraining) a decision variable is different than computing something based on its optimal solution. The best way to fix a variable is to set its upper and lower bounds to the desired value. – Greg Glockner Dec 15 '16 at 15:46