使用子集

时间:2017-10-02 14:24:34

标签: python gurobi

我试图通过Python 2.7.12在Gurobi 7.0.2中添加以下约束。

\sum_{i\in I_j}c_i x_i \leq f_j~~~\forall j\in I

我有一个J列表:

J

[u'Capability Investments',
 u'Facilities',
 u'Mission #1',
 u'Mission #2',
 u'New Program Readiness',
 u'Site Support',
 u'Transformation',
 u'Waste Mgt']

我的名单是:

I

[u'Capability Investments', u'Capability Investments', u'Capability Investments',
u'Capability Investments', u'Capability Investments', u'Site Support',
u'Site Support', u'Site Support', u'Site Support', u'Site Support', u'Site Support', 
u'Site Support', u'Site Support', u'Site Support', u'Site Support', u'Site Support', 
u'Site Support', u'Site Support', u'Site Support', u'Site Support', u'Site Support',
u'Site Support', u'Site Support', u'Facilities', u'Facilities', u'Facilities',
u'Facilities', u'Facilities', u'Facilities', u'Facilities', u'Facilities',
u'Facilities', u'Facilities', u'Facilities', u'Facilities', u'Facilities', u'Facilities',
u'Facilities', u'Facilities', u'Facilities', u'Facilities', u'Facilities', u'Facilities', 
u'Facilities', u'Facilities', u'Facilities', u'Facilities', u'Facilities', u'Facilities',
u'Facilities', u'Facilities', u'Facilities', u'Facilities', u'Facilities', u'Facilities',
u'Facilities', u'Facilities', u'Facilities', u'Facilities', u'Facilities', u'Facilities',
u'Facilities', u'Facilities', u'Facilities', u'Facilities', u'Facilities', u'Facilities',
u'Facilities', u'Facilities', u'Facilities', u'Facilities', u'Facilities', u'Facilities',
u'Facilities', u'Facilities', u'Facilities', u'Facilities', u'Facilities', u'Facilities',
u'Facilities', u'Facilities', u'Facilities', u'Facilities', u'Facilities', u'Facilities',
u'Facilities', u'Facilities', u'Facilities', u'Facilities', u'Facilities', u'Facilities',
u'Facilities', u'Facilities', u'Facilities', u'Facilities', u'Facilities', u'Facilities',
u'Facilities', u'Facilities', u'Facilities', u'Facilities', u'Facilities', u'Facilities',
u'Facilities', u'Facilities', u'Facilities', u'Facilities', u'Facilities', u'Facilities',
u'Facilities', u'Facilities', u'Facilities', u'Facilities', u'Facilities', u'Facilities',
u'Facilities', u'Facilities', u'Facilities', u'Facilities', u'Facilities', u'Facilities',
u'Facilities', u'Facilities', u'Facilities', u'Facilities', u'Facilities', u'Facilities',
u'Facilities', u'Facilities', u'Facilities', u'Facilities', u'Facilities', u'Facilities',
u'Facilities', u'Facilities', u'Facilities', u'Facilities', u'Facilities', u'Facilities',
u'Facilities', u'Facilities', u'Facilities', u'Facilities', u'Facilities', u'Facilities',
u'Facilities', u'Facilities', u'Facilities', u'Facilities', u'Facilities', u'Facilities',
u'Facilities', u'Facilities', u'Facilities', u'Facilities', u'Facilities', u'Facilities',
u'Facilities', u'Facilities', u'Facilities', u'Facilities', u'Facilities', u'Facilities',
u'Mission #2', u'Mission #2', u'Mission #2', u'Mission #2', u'Waste Mgt', u'Waste Mgt',
u'Waste Mgt', u'Waste Mgt', u'Waste Mgt', u'Waste Mgt', u'Waste Mgt', u'Waste Mgt',
u'Waste Mgt', u'Waste Mgt', u'Waste Mgt', u'Waste Mgt', u'Waste Mgt', u'Transformation',
u'Transformation', u'Transformation', u'Transformation', u'Transformation',
u'Capability Investments', u'Mission #2', u'Mission #2', u'Mission #1', u'Mission #1', 
u'Mission #1', u'Mission #1', u'Mission #1', u'Mission #1', u'Facilities',
u'New Program Readiness', u'New Program Readiness', u'New Program Readiness',
u'Transformation', u'Site Support', u'Site Support', u'Site Support', u'Site Support',
u'Site Support', u'Site Support', u'Mission #1']

我失败的尝试:

for i in I:
    for j in J:
        if i == j:
            m.addConstr(quicksum(c[i]*x[i])<=f[j])

失败的第一个原因是我没有枚举我的列表,因此,这种约束加法导致与变量定义冲突,即我的x变量定义为x [0],x [1],x [2 ],x [3] ....所以,我不能真正使用这些名称&#34;能力投资,设施等&#34;作为我的索引。任何贡献都可以解决我的问题。

1 个答案:

答案 0 :(得分:0)

帖子顶部的公式写为

for j in J:
   m.addConstr(quicksum([c[i] * x[i] for i in I[j]]) <= f[j])

假设您的意思是按J索引集合j