【问题标题】:Gurobi & Python:How to create a constraint which includes a variable and a kind of index(two indices simultaneous)Gurobi & Python:如何创建一个包含变量和一种索引的约束(同时两个索引)
【发布时间】:2020-07-24 06:53:01
【问题描述】:

我想把约束代码写成图片的内容。

#index:
J=['j1','j2','j3','j4','j5','j6','j7','j8','j9','j10','j11','j12','j13','j14','j15','j16','j17','j18','j19','j20', 'j21']

#variable of decision
F= m.addVars(J , vtype=GRB.BINARY,name="Fj")

我试图创建一个字母来代替“J+1”,但显然代码不应该有两种索引。

【问题讨论】:

  • 从二维数组J=[['j',1],['j',2]]...开始。

标签: python python-3.x indexing constraints gurobi


【解决方案1】:

这不是一条捷径,但它确实有效。

for j_index, j_value in enumerate(J[:-1]):
    m.addConstr(F[J[j_index+1]]<=F[j_value])

注意: Gurobi examples page can be useful.

【讨论】:

  • 亲爱的库尔阿格:感谢您的回答。我也在 GUROBI 论坛网站上问过这个问题。我想分享其他答案给你交流。您可以使用整数作为索引,即 #index J=[int(i)+1 for i in range(21)] #code b=len(J)-1 c=0 for j in J: c+=1 if c
  • 你的答案似乎更短。如果你接受我的回答,别忘了检查一下。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-02-06
  • 1970-01-01
  • 2021-11-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多