简体   繁体   English

打印约束 Gurobi Python

[英]print constraints Gurobi Python

I'm using Gurobi in Python and for a given set S I'm adding the constraint as follows:我在 Python 中使用 Gurobi,对于给定的集合S ,我添加了如下约束:

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.我想为屏幕上集合SV的每个值打印这些约束。 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.例如,如果S={1,3,4}V= {1,2,3,4,5,6} ,那么我的约束将是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我希望将这个约束预先印在屏幕上。 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.最好的选择是调用Model.write()将模型导出为 LP 文件。

use print (model.display()) after calling model.optimize() function.在调用model.optimize()函数后使用print (model.display())

Else you can also use model.write(file_path) as suggested above by Greg否则,您也可以按照Greg上面的建议使用model.write(file_path)

Use model.write("file.lp").使用 model.write("file.lp")。 You can choose any name for file but the extension must be lp.您可以为文件选择任何名称,但扩展名必须是 lp。

Summing up what the others have mentioned:总结一下其他人提到的:


print to terminal: print (model.display())打印到终端: print (model.display())

Produces something like:产生类似的东西:

在此处输入图像描述


save to file: model.write("file_name.lp")保存到文件: model.write("file_name.lp")

Produces something like:产生类似的东西:

在此处输入图像描述

Where the.lp file can be opened with a text editor.可以使用文本编辑器打开 .lp 文件的位置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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