如何确保我的约束能够在数学上与 LpVariable 协同工作?

huangapple go评论63阅读模式
英文:

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(&quot;Optimal Number of Bank Tellers&quot;, pulp.LpMinimize)

x = pulp.LpVariable(&quot;Number of Tellers&quot;, lowBound = 0, cat=&#39;Integer&#39;)

prob += x * (16*4 + 14*4)/8 , &quot;Total Cost of Labor&quot;

for i in [28, 35, 21, 46, 32, 14, 24, 32]:
    prob += i / x &lt;= 1/8, &quot;Service Level Constraint for Time Slot {}&quot;.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 &lt;= 1/8

is obviously nonlinear. PuLP is only for linear models. Of course, you could write:

 i &lt;= x * (1/8)

which makes this linear.

Actually, there is no need to generate all these constraints. We can do with just one:

 x &gt;= 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.

huangapple
  • 本文由 发表于 2023年2月10日 07:32:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/75405542.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定