如何在Python中使用Pulp将’or’放入约束中

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

How to put 'or' into contraints in Pulp in python

问题

Here is the translated part of your text:

  1. 我有一个问题,我需要找到三个给定电机的最佳成本。
  2. 电机1的范围是100 - 300
  3. 电机2的范围是400 - 1000
  4. 电机3的范围是50 - 250
  5. 它们的目标值是600
  6. 电机1的价格是5000
  7. 电机2的价格是5500
  8. 电机3的价格是5250
  9. 方程如下:
  10. 成本 = 电机1 * 5000 + 电机2 * 5500 + 电机3 * 5250
  11. 还有一个非常重要的部分,不是每个电机都需要运行。
  12. 我有一个Python代码,可以计算它,但我可以传递给它不需要包括每个电机的信息。
  13. 以下是代码:
  14. ```python
  15. from pulp import LpProblem, LpVariable, LpMinimize
  16. def find_lowest_cost():
  17. # 定义问题
  18. problem = LpProblem("电机优化", LpMinimize)
  19. # 定义决策变量
  20. x = LpVariable("电机1", lowBound=100, cat='Integer') # 电机1的功率
  21. y = LpVariable("电机2", lowBound=0, cat='Integer') # 电机2的功率
  22. z = LpVariable("电机3", lowBound=50, cat='Integer') # 电机3的功率
  23. # 定义目标函数(成本)
  24. problem += x * 5000 + y * 5500 + z * 5250
  25. # 定义约束条件
  26. problem += x >= 100 # 电机1下限
  27. problem += x <= 300 # 电机1上限
  28. problem += y >= 350 # 电机2下限
  29. problem += y <= 1000 # 电机2上限
  30. problem += z >= 50 # 电机3下限
  31. problem += z <= 250 # 电机3上限
  32. problem += x + y + z == 500 # 总功率约束
  33. # 解决问题
  34. problem.solve()
  35. # 获取最优解
  36. lowest_cost = problem.objective.value()
  37. best_combination = (x.value(), y.value(), z.value())
  38. return lowest_cost, best_combination
  39. cost, combination = find_lowest_cost()
  40. print("最低成本:", cost)
  41. print("电机组合:", combination)

我尝试在“定义约束条件”部分添加了'or',但没有帮助

  1. problem += x >= 100 or x == 0 # 电机1下限
  2. problem += x <= 300 # 电机1上限
  3. problem += y >= 350 or y == 0 # 电机2下限
  4. problem += y <= 1000 # 电机2上限
  5. problem += z >= 50 or z == 0 # 电机3下限
  6. problem += z <= 250 # 电机3上限
  7. problem += x + y + z == 500 # 总功率约束

所以我的问题是,如何将'OR'实现到我的代码中。

谢谢你提前。

英文:

I have a problem, where I have to find the optimal cost of 3 given motor.

Motor 1 has a range of 100 - 300
Motor 2 has a range of 400 - 1000
Motor 3 has a range of 50 - 250

They have a target value of 600

Motor 1 price is 5000
Motor 2 price is 5500
Motor 3 price is 5250

The equation looks like this:

Cost = Motor1 * 5000 + Motor2 * 5500 + Motor3 * 5250.

And a very important part, NOT every motor needs to run.

I have a python code, that can calculate it, but I can give it to it that not every motors needs to be inclued.
Here is the code:

  1. from pulp import LpProblem, LpVariable, LpMinimize
  2. def find_lowest_cost():
  3. # Define the problem
  4. problem = LpProblem(&quot;Motor Optimization&quot;, LpMinimize)
  5. # Define the decision variables
  6. x = LpVariable(&quot;Motor1&quot;, lowBound=100, cat=&#39;Integer&#39;) # Power of motor 1
  7. y = LpVariable(&quot;Motor2&quot;, lowBound=0, cat=&#39;Integer&#39;) # Power of motor 2
  8. z = LpVariable(&quot;Motor3&quot;, lowBound=50, cat=&#39;Integer&#39;) # Power of motor 3
  9. # Define the objective function (cost)
  10. problem += x * 5000 + y * 5500 + z * 5250
  11. # Define the constraints
  12. problem += x &gt;= 100 # Motor 1 lower bound
  13. problem += x &lt;= 300 # Motor 1 upper bound
  14. problem += y &gt;= 350 # Motor 2 lower bound
  15. problem += y &lt;= 1000 # Motor 2 upper bound
  16. problem += z &gt;= 50 # Motor 3 lower bound
  17. problem += z &lt;= 250 # Motor 3 upper bound
  18. problem += x + y + z == 500 # Total power constraint
  19. # Solve the problem
  20. problem.solve()
  21. # Retrieve the optimal solution
  22. lowest_cost = problem.objective.value()
  23. best_combination = (x.value(), y.value(), z.value())
  24. return lowest_cost, best_combination
  25. cost, combination = find_lowest_cost()
  26. print(&quot;Lowest cost:&quot;, cost)
  27. print(&quot;Motor combination:&quot;, combination)

I tried to add 'or' to the "Define the Constraints' part, but it did not help

  1. problem += x &gt;= 100 or x ==0 # Motor 1 lower bound
  2. problem += x &lt;= 300 # Motor 1 upper bound
  3. problem += y &gt;= 350 or y == 0 # Motor 2 lower bound
  4. problem += y &lt;= 1000 # Motor 2 upper bound
  5. problem += z &gt;= 50 or z == 0 # Motor 3 lower bound
  6. problem += z &lt;= 250 # Motor 3 upper bound
  7. problem += x + y + z == 500 # Total power constraint

So my Questions is, how to implement that 'OR' into my code.

Thank you in advance

答案1

得分: 0

我有一些假设:

  • 电机的功率是连续的,而不是积分的。
  • 观察您的变量界限中的最小值,而不是后来添加的冗余和不一致的约束。
  • 使用500作为目标,而不是600。

您需要像这样的二进制选择变量:

  1. from pulp import LpProblem, LpVariable, LpMinimize, LpContinuous, lpDot, LpBinary, lpSum
  2. powers = (
  3. LpVariable('Motor1', cat=LpContinuous, upBound=300),
  4. LpVariable('Motor2', cat=LpContinuous, upBound=1000),
  5. LpVariable('Motor3', cat=LpContinuous, upBound=250),
  6. )
  7. used = LpVariable.matrix(name='MotorUsed', cat=LpBinary, indices=range(len(powers)))
  8. problem = LpProblem(name='Motor_Optimization', sense=LpMinimize)
  9. problem.objective = lpDot(powers, (5000, 5500, 5250))
  10. problem.addConstraint(name='target', constraint=lpSum(powers) == 500)
  11. for power, power_min, use in zip(
  12. powers,
  13. (100, 0, 50),
  14. used,
  15. ):
  16. problem.addConstraint(power >= power_min*used)
  17. problem.addConstraint(power <= 1000*used)
  18. problem.solve()
  19. combination =

  20. print('Lowest cost:', problem.objective.value())

  21. print('Motor combination:', combination)

  1. Result - Optimal solution found
  2. Objective value: 2550000.00000000
  3. Enumerated nodes: 0
  4. Total iterations: 0
  5. Time (CPU seconds): 0.01
  6. Time (Wallclock seconds): 0.01
  7. Option for printingOptions changed from normal to all
  8. Total time (CPU seconds): 0.01 (Wallclock seconds): 0.01
  9. Lowest cost: 2550000.0
  10. Motor combination: [300.0, 0.0, 200.0]
英文:

I make some assumptions:

  • Continuous power of motors, not integral
  • Observe the minima in your variable bounds, not the redundant and inconsistent constraints added later
  • Use 500 as a target, not 600

You need binary selection variables, like this:

  1. from pulp import LpProblem, LpVariable, LpMinimize, LpContinuous, lpDot, LpBinary, lpSum
  2. powers = (
  3. LpVariable(&#39;Motor1&#39;, cat=LpContinuous, upBound=300),
  4. LpVariable(&#39;Motor2&#39;, cat=LpContinuous, upBound=1000),
  5. LpVariable(&#39;Motor3&#39;, cat=LpContinuous, upBound=250),
  6. )
  7. used = LpVariable.matrix(name=&#39;MotorUsed&#39;, cat=LpBinary, indices=range(len(powers)))
  8. problem = LpProblem(name=&#39;Motor_Optimization&#39;, sense=LpMinimize)
  9. problem.objective = lpDot(powers, (5000, 5500, 5250))
  10. problem.addConstraint(name=&#39;target&#39;, constraint=lpSum(powers) == 500)
  11. for power, power_min, use in zip(
  12. powers,
  13. (100, 0, 50),
  14. used,
  15. ):
  16. problem.addConstraint(power &gt;= power_min*used)
  17. problem.addConstraint(power &lt;= 1000*used)
  18. problem.solve()
  19. combination =

  20. print(&#39;Lowest cost:&#39;, problem.objective.value())

  21. print(&#39;Motor combination:&#39;, combination)

  1. Result - Optimal solution found
  2. Objective value: 2550000.00000000
  3. Enumerated nodes: 0
  4. Total iterations: 0
  5. Time (CPU seconds): 0.01
  6. Time (Wallclock seconds): 0.01
  7. Option for printingOptions changed from normal to all
  8. Total time (CPU seconds): 0.01 (Wallclock seconds): 0.01
  9. Lowest cost: 2550000.0
  10. Motor combination: [300.0, 0.0, 200.0]

huangapple
  • 本文由 发表于 2023年6月1日 20:12:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/76381732.html
匿名

发表评论

匿名网友

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

确定