machines = ['m1','m2'] lastjobs = ['j0','j1','j2','j3','j4','j5'] jobs = ['j1','j2','j3','j4','j5'] x = m.addVars(machines,lastjobs,jobs, vtype=GRB.BINARY, name="x") s = m.addVars(machines,lastjobs,jobs, lb=0, name="s") z= m.addVar(name="c_max") Z = {i : gp.quicksum(x[i,j,k]*s[i,j,k] for j in lastjobs for k in jobs if j!=k) m.addConstrs(Z[i] <= z for i in machines) 如何使得x的输出值不是{'m1','j1','j0'}之类的,而是{'m1,j1,j0'}之类的
时间: 2023-08-06 16:13:44 浏览: 122
您可以使用字符串拼接的方式来生成变量名,具体代码如下:
```
x = {}
s = {}
for i in machines:
for j in lastjobs:
for k in jobs:
x[i,j,k] = m.addVar(vtype=GRB.BINARY, name="x_" + i + "_" + j + "_" + k)
s[i,j,k] = m.addVar(lb=0, name="s_" + i + "_" + j + "_" + k)
z = m.addVar(name="c_max")
Z = {i : gp.quicksum(x[i,j,k]*s[i,j,k] for j in lastjobs for k in jobs if j!=k) for i in machines}
m.addConstrs(Z[i] <= z for i in machines)
```
这样生成的变量名可以直接输出为字符串类型的变量名,例如:"x_m1_j1_j0"。
阅读全文