'TempConstr'对象不可迭代
希望有人能指出以下代码中错误的方向。我试图最大限度地减少进行调查的总体成本,但出现了大量错误。
N_investors = 2000
groups = [1,2,3,4]
telephone = pd.Series([15, 12, 20, 18], index = groups)
personal = pd.Series([35, 30, 50, 40], index = groups)
methods = [1,2] #1 is telephone #2 is personal
mod = gp.Model('Gladstone')
mod.setParam('LogToConsole', 0)
mod.setParam('OutputFlag', 0)
#Add variables
x = mod.addVars([(i,j)for i in groups for j in methods], lb=0, vtype=GRB.CONTINUOUS) #the total number of people being surveyed in group i by method j
g = mod.addVars(groups, lb=0, vtype=GRB.CONTINUOUS, name = 'g') #the number of people being surveyed in group i
y = mod.addVars(methods, lb=0, vtype=GRB.CONTINUOUS, name = 'y') #the number of people getting surveyed by method j
mod.addConstr( (g[1] + g[2]) >= 0.5 * (gp.quicksum(x[i,j] for j in methods for i in groups)))
mod.addConstr(y[2] >= (0.25 * (gp.quicksum(x[i,j] for j in methods for i in groups))))
mod.addConstr((g[2] + g[4]) <= 0.4 *(gp.quicksum(x[i,j] for j in methods for i in groups)))
mod.addConstrs((g[i] for i in groups >= 0.1 * (gp.quicksum(x[i,j] for j in methods for i in groups))))
mod.addConstrs((g[i] for i in groups <= 0.5 * (gp.quicksum(x[i,j] for j in methods for i in groups))))
mod.addConstrs((gp.quicksum(x[i,j]for i in groups for j in methods) == N_investors))
mod.addConstrs(((g[i] for i in groups) + (y[j] for j in methods)) == x[i,j] for i in groups for j in methods)
mod.addConstr((0.5*g[1] >= y[2]))
mod.addConstr((0.25*(g[1] + g[2]) <= y[2]))
mod.addConstr((g[i] == x[i,1] + x[i,2]))
#objective
costs = gp.quicksum((telephone[i] * x[i,1] for i in groups) + gp.quicksum(personal[i] * x[i,0] for i in groups))
mod.setObjective(costs, sense = GRB.MINIMIZE)
mod.update()
mod.optimise()
print(mod.objval)
print('minimum cost:', mod.objval)
我得到的错误是:
TypeError Traceback (most recent call last)
\<ipython-input-120-285afeb1ec31\> in \<module\>
25 mod.addConstr((g\[2\] + g\[4\]) \<= 0.4 \*(gp.quicksum(x\[i,j\] for j in methods for i in groups)))
26
\---\> 27 mod.addConstrs((g\[i\] for i in groups \>= 0.1 \* (gp.quicksum(x\[i,j\] for j in methods for i in groups))))
28
29 mod.addConstrs((g\[i\] for i in groups \<= 0.5 \* (gp.quicksum(x\[i,j\] for j in methods for i in groups))))
TypeError: 'TempConstr' object is not iterable
我尝试将其设置为一个大的 for 循环,但这似乎也不起作用。我不确定我是否使用了 Gp.Quicksum 错误或者这里发生了什么!
Hoping someone could point me in the direction of the error in the following code. I am trying to minimise the overall cost of taking a survey but am having loads of errors pop up.
N_investors = 2000
groups = [1,2,3,4]
telephone = pd.Series([15, 12, 20, 18], index = groups)
personal = pd.Series([35, 30, 50, 40], index = groups)
methods = [1,2] #1 is telephone #2 is personal
mod = gp.Model('Gladstone')
mod.setParam('LogToConsole', 0)
mod.setParam('OutputFlag', 0)
#Add variables
x = mod.addVars([(i,j)for i in groups for j in methods], lb=0, vtype=GRB.CONTINUOUS) #the total number of people being surveyed in group i by method j
g = mod.addVars(groups, lb=0, vtype=GRB.CONTINUOUS, name = 'g') #the number of people being surveyed in group i
y = mod.addVars(methods, lb=0, vtype=GRB.CONTINUOUS, name = 'y') #the number of people getting surveyed by method j
mod.addConstr( (g[1] + g[2]) >= 0.5 * (gp.quicksum(x[i,j] for j in methods for i in groups)))
mod.addConstr(y[2] >= (0.25 * (gp.quicksum(x[i,j] for j in methods for i in groups))))
mod.addConstr((g[2] + g[4]) <= 0.4 *(gp.quicksum(x[i,j] for j in methods for i in groups)))
mod.addConstrs((g[i] for i in groups >= 0.1 * (gp.quicksum(x[i,j] for j in methods for i in groups))))
mod.addConstrs((g[i] for i in groups <= 0.5 * (gp.quicksum(x[i,j] for j in methods for i in groups))))
mod.addConstrs((gp.quicksum(x[i,j]for i in groups for j in methods) == N_investors))
mod.addConstrs(((g[i] for i in groups) + (y[j] for j in methods)) == x[i,j] for i in groups for j in methods)
mod.addConstr((0.5*g[1] >= y[2]))
mod.addConstr((0.25*(g[1] + g[2]) <= y[2]))
mod.addConstr((g[i] == x[i,1] + x[i,2]))
#objective
costs = gp.quicksum((telephone[i] * x[i,1] for i in groups) + gp.quicksum(personal[i] * x[i,0] for i in groups))
mod.setObjective(costs, sense = GRB.MINIMIZE)
mod.update()
mod.optimise()
print(mod.objval)
print('minimum cost:', mod.objval)
The Error I am getting is:
TypeError Traceback (most recent call last)
\<ipython-input-120-285afeb1ec31\> in \<module\>
25 mod.addConstr((g\[2\] + g\[4\]) \<= 0.4 \*(gp.quicksum(x\[i,j\] for j in methods for i in groups)))
26
\---\> 27 mod.addConstrs((g\[i\] for i in groups \>= 0.1 \* (gp.quicksum(x\[i,j\] for j in methods for i in groups))))
28
29 mod.addConstrs((g\[i\] for i in groups \<= 0.5 \* (gp.quicksum(x\[i,j\] for j in methods for i in groups))))
TypeError: 'TempConstr' object is not iterable
I have tried making it a large for loop but this doesn't seem to work either. I am unsure if I am using Gp.Quicksum
wrong or what is happening here!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为
mod.addConstrs((gp.quicksum(x[i,j]for i in groups for j inmethods) == N_investors))
行包含一个错误。addConstrs
期望生成器,但== N_investors
肯定会阻止这种情况。这是一个括号更清晰匹配的版本:我不知道你想要做什么(也不知道
gp.quicksum
做什么),但也许你可以只提供一个包含结果的列表平等:I think the line
mod.addConstrs((gp.quicksum(x[i,j]for i in groups for j in methods) == N_investors))
contains a mistake.addConstrs
expect a generator but the== N_investors
certainly prevent that. Here is a version where the parenthesis are more clearly matched:I do not know what you meant to do (nor what
gp.quicksum
does), but maybe you can just provide a list containing the result of the equality: