英文:
How can I make sure my constraint can operate mathematically with the LpVariable?
问题
我正在尝试使用pulp库的类来解决一个LP问题。我在将约束条件添加到我的代码中时遇到问题。
导入相关的类并从CSV文件中读取后,我写道:
prob = pulp.LpProblem("Optimal Number of Bank Tellers", pulp.LpMinimize)
x = pulp.LpVariable("Number of Tellers", lowBound=0, cat='Integer')
prob += x * (16*4 + 14*4)/8 , "Total Cost of Labor"
for i in [28, 35, 21, 46, 32, 14, 24, 32]:
prob += i / x <= 1/8, "Service Level Constraint for Time Slot {}".format(i)
prob.solve()
不幸的是,我不太明白为什么会出现错误消息,指出'int'和'LpVariable'是不支持的操作数类型。
否则,我应该如何正确建模我的约束条件?我在这里到底做错了什么?
英文:
I am trying to use the pulb libraries classes to solve a LP-Problem. I am having problems implementing the constraint into my code.
After importing the relevant classes and reading from my CSV file I wrote:
prob = pulp.LpProblem("Optimal Number of Bank Tellers", pulp.LpMinimize)
x = pulp.LpVariable("Number of Tellers", lowBound = 0, cat='Integer')
prob += x * (16*4 + 14*4)/8 , "Total Cost of Labor"
for i in [28, 35, 21, 46, 32, 14, 24, 32]:
prob += i / x <= 1/8, "Service Level Constraint for Time Slot {}".format(i)
prob.solve()
Unfortunately I don't quite understand why I get the error message, that 'int' and 'LpVariable' are an unsupported operand type.
How would I correctly model my constraint otherwise? What exactly did I do wrong here?
答案1
得分: 0
i / x <= 1/8 is obviously nonlinear. PuLP is only for linear models. Of course, you could write:
i <= x * (1/8)
which makes this linear.
Actually, there is no need to generate all these constraints. We can do with just one:
x >= 8*max([28, 35, 21, 46, 32, 14, 24, 32])
Finally, it is slightly better to specify this as a lower bound on x directly.
英文:
i / x <= 1/8
is obviously nonlinear. PuLP is only for linear models. Of course, you could write:
i <= x * (1/8)
which makes this linear.
Actually, there is no need to generate all these constraints. We can do with just one:
x >= 8*max([28, 35, 21, 46, 32, 14, 24, 32])
Finally, it is slightly better to specify this as a lower bound on x directly.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论