我的目标函数是: f(x)=a*500*价格类别x+b*500*价格类别y
其中pricecat_x是
1300,其中0<;x\u数量<;=250
1400,其中251<;=xï数量>;500
价格是
1420,其中0<;y\u数量<;400
1390,其中401<;y\u数量<;=500
xu数量允许范围为(250400) y数量的允许范围为(100250)
我已经写了我的代码如下。你知道吗
我的目标是优化a
和b
,即x
和y
的数量。你知道吗
我在跑下面的线路时碰到了一个路障:
problem += - a*Q_demand*pricecat_x - b*Q_demand* pricecat_y
Traceback (most recent call last):
File "<ipython-input-120-207ebdebfbb6>", line 1, in <module>
problem += - a*Q_demand*pricecat_x - b*Q_demand* pricecat_y
File "C:\Anaconda\envs\tfflow\lib\site-packages\pulp\pulp.py", line 790, in __mul__
if other != 0:
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
想知道是否有人能教你如何用Python执行分段线性规划?你知道吗
非常感谢。你知道吗
import numpy as np
from pulp import *
problem = LpProblem("Volume Aportionment", LpMaximize)
## Constants
# Supplier 1
VA_xmin = 0.5 #min VA - Supplier 1
VA_xmax = 0.8 #max VA - Supplier 1
# Supplier 2
VA_ymin = 0.2 #min VA - Supplier 2
VA_ymax = 0.5 #max VA - Supplier 2
Q_demand = 500
# Pricing Tiers
P1 = np.linspace(0, 500, 51)
pricecat_x = np.piecewise(P1, [P1 <= 250, P1 >= 251], [1324.17, 1309.17])
P2 = np.linspace(0, 500, 51)
pricecat_y = np.piecewise(P2, [P2 <= 400, P2 >= 401], [1419.09, 1394.09])
#pricecat_x = 1305
#pricecat_y = 1342
# define Variables
a = LpVariable(name = "VA1", lowBound=VA_xmin, upBound=VA_xmax, cat="Continuous")
b = LpVariable(name = "VA2", lowBound=VA_ymin, upBound=VA_ymax, cat="Continuous")
# constraints
problem += (a+b)*Q_demand >=500
# objective function
#problem += - weight_x*pricecat_x - weight_y* pricecat_y
problem += - a*Q_demand*pricecat_x - b*Q_demand* pricecat_y
print(problem)
# solving
problem.solve()
for v in problem.variables():
print(v.name, "=", v.varValue)
目前没有回答
相关问题 更多 >
编程相关推荐