简体   繁体   中英

print constraints Gurobi Python

I'm using Gurobi in Python and for a given set S I'm adding the constraint as follows:

for i in S:
  m.addConstr(quicksum(x[i,j] for j in (set(V) - set(S))) >= 2)

I want to print these constraints for each value of the sets S and V on the screen. For an example, if S={1,3,4} and V= {1,2,3,4,5,6} , then, my constraint will be x[1,2]+x[1,5]+x[1,6]+x[3,2]+x[3,5]+x[3,6]+x[4,2]+x[4,5]+x[4,6]>=2 I want this constraint to be preinted on the screen. Can someone please help me to do it?

There is no built-in function to do this. Your best option is to call Model.write() to export the model as an LP file.

use print (model.display()) after calling model.optimize() function.

Else you can also use model.write(file_path) as suggested above by Greg

Use model.write("file.lp"). You can choose any name for file but the extension must be lp.

Summing up what the others have mentioned:


print to terminal: print (model.display())

Produces something like:

在此处输入图像描述


save to file: model.write("file_name.lp")

Produces something like:

在此处输入图像描述

Where the.lp file can be opened with a text editor.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM