plot the results by gurobi python

1,193 views
Skip to first unread message

peyman iravani

unread,
Sep 28, 2017, 11:05:12 AM9/28/17
to Gurobi Optimization
Hi

I have MILP problem which is solved by gurobi python

I could see the computed values by print command 

but when I use matplotlib.pyplot it says that :

TypeError: float() argument must be a string or a number

I casted my variables to int but still I have the same problem

I really appreciate if you could help me 

Thanks for your collaboration and consideration 

Yours faithfully 

Peyman 

Tobias Achterberg

unread,
Sep 28, 2017, 12:01:27 PM9/28/17
to gur...@googlegroups.com
Maybe you used the Gurobi variable object (say, v) directly instead of passing
its solution value v.X to the methods?

If you have, for example,

v = m.addVar(...)

then you cannot pass v to non-Gurobi functions, because v is a Gurobi variable,
which is an object that is unknown to non-Gurobi packages. After having solved
your model with m.optimize() you can use v.X (the "X" attribute of the Gurobi
variable object), which is a number.


Regards,

Tobias

peyman iravani

unread,
Sep 29, 2017, 6:01:40 AM9/29/17
to Gurobi Optimization
Thanks for your response 

I attached my code 

I have another problem: I want to implement on the whole array but when I see the result it just showed the result for two last indexes and the rest are zero 

Thanks 

cheers 

Peyman
test_DR_fourthVersion.py

Tobias Achterberg

unread,
Sep 29, 2017, 8:22:54 AM9/29/17
to gur...@googlegroups.com
It looks like you are missing some OPTmodel.addConstr() calls. For example, you have code

Pbess_flex_[i] = Pbess_in_[i] + Pbess_out_[i]

All these objects are Gurobi Variables. If you do this, then Pbess_flex_[i] will be
assigned the result of "Pbess_in_[i] + Pbess_out_[i]". Because the latter two are Gurobi
variables that overload the "+" operator, the result will be a Gurobi linear expression.
Hence, after this assignment, Pbess_flex_[i] is no longer a Gurobi variable but a linear
expression.

I guess what you want is

OPTmodel.addConstr(Pbess_flex_[i] == Pbess_in_[i] + Pbess_out_[i])


I suggest to start with a toy example (like using range of 1 instead of 10) and then look
at your model (e.g., OPTmodel.write("mymodel.lp")). Then check whether it looks correct.


Regards,

Tobias

peyman iravani

unread,
Oct 1, 2017, 8:24:08 AM10/1/17
to Gurobi Optimization
Thanks for your reply 

I changed my code and now it working properly, in mymodel.lp all elements have value but when I want to plot the value it says that has no attribute 'X'

Cheers

Peyman 

Tobias Achterberg

unread,
Oct 1, 2017, 6:08:27 PM10/1/17
to gur...@googlegroups.com
I don't understand what you mean with "in mymodel.lp all elements have value". The *.lp
file does not store the solution, it only stores the model.

What is the plot call that gives this error?


Tobias

peyman iravani

unread,
Oct 3, 2017, 10:49:32 AM10/3/17
to Gurobi Optimization
Thanks for your response
 
I uploaded my model with related sol and lp file

I want to plot for example Phvac_flex  
model.zip

Michael Winkler

unread,
Oct 4, 2017, 1:46:41 PM10/4/17
to Gurobi Optimization
Hi Peyman,

I'm not sure where the solution file but it does not correspond to your problem file. Your model is infeasible, that's why you cannot access a solution. So you should always check the status of the optimization before accessing the X attribute.

If you want to know one reason why your model is infeasible you can use the IIS feature, e.g., see http://www.gurobi.com/documentation/7.5/refman/py_model_computeiis.html.

In your case the IIS is:


Minimize
  - 94949.461 Constant
Subject To
 CON2: PHVAC_flex_198 - PHVAC_up__198 - PHVAC_down__198 = 0
 CON3: PHVAC_down__198 = 0
 CON4: PHVAC_up__198 = 19.66388916666667
 R4351: 3.14 PHVAC_flex_198 + Tdc_i_198 = 22.90832917405333
Bounds
 PHVAC_flex_198 free
 PHVAC_up__198 free
 PHVAC_down__198 free
 Constant = 1
End

which simplified reads as "61.74461198333 + Tdc_i_198 = 22.90832917405333" but since your variable "Tdc_i_198" is non-negative this is infeasible.

Best, Michael

peyman iravani

unread,
Oct 6, 2017, 5:48:01 AM10/6/17
to Gurobi Optimization
Dear Michael

Thanks for your resonse

I  checked again my model Now it is feasible and all compnent have values ,
I checked for X attribute , for first index it says the value and for the rest it says that "Nontype" object has no attribute X
but I could the value of all index of the array in command prompt

Cheers

Peyman
Reply all
Reply to author
Forward
0 new messages