简体   繁体   English

在 Python Gurobi 中删除变量

[英]Removing a variable in Python Gurobi

I am using Python/Gurobi to optimize a problem.我正在使用 Python/Gurobi 来优化问题。 I generated variables using GRBaddVar through this code:我通过以下代码使用 GRBaddVar 生成了变量:

x[1,i,j,t] = model.addVar(vtype="B", name="x(1,%s,%s,%s)" % (i,j,t)) 

I also want to remove some variables in order to save some space.我还想删除一些变量以节省一些空间。 The variable is removed when a certain condition is true.当某个条件为真时,该变量被删除。 Suppose the condition is when R = 1. To remove the variable I use the following code.假设条件是当 R = 1 时。要删除变量,我使用以下代码。

if R == 1:
    x[1,i,j,t] = model.delVars(vtype="B", name="x(1,%s,%s,%s)" % (i,j,t))

However, it resulted in the following error:但是,它导致了以下错误:

AttributeError: 'gurobipy.Model' object has no attribute 'delVars'

I have no idea what's wrong with the code since I just follow the documentation (attached below) from Gurobi.我不知道代码有什么问题,因为我只是按照 Gurobi 的文档(附在下面)。 Thanks for your help!谢谢你的帮助!

Gurobi 文档:addVar

Gurobi 文档:delVar

As David noted, you are looking at the documentation for the C API.正如 David 所指出的,您正在查看 C API 的文档。

In the Python API, you can remove a single variable with the Model.remove() method, using the Var object the argument.在 Python API 中,您可以使用Model.remove()方法删除单个变量,使用 Var 对象作为参数。 Eg,例如,

model.remove(x[1,i,j,t])

You can use this same method to remove, eg, a list of Var objects at once.您可以使用相同的方法一次删除,例如,Var 对象列表。

Note that you should call Model.update() before removing variables in this manner.请注意,您应该在以这种方式删除变量之前调用Model.update()

"You can use this same method to remove, eg, a list of Var objects at once." “您可以使用相同的方法一次删除例如 Var 对象列表。” Can anyone suggest how to remove multiple variables at once.谁能建议如何一次删除多个变量。 I have the variable to be removed in a dict/list format.我有要以字典/列表格式删除的变量。 I am trying to the below code, but error appears: GurobiError: Item to be removed not a Var, MVar, Constr, MConstr, SOS, QConstr, or GenConstr我正在尝试以下代码,但出现错误: GurobiError: Item to be removed not a Var, MVar, Consr, MConstr, SOS, QConstr, or GenConstr

model.remove(var_to_remove_dict.keys())

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

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