[英]LP Programming with python using Pulp library
I have a linear programming problem where I need to minimise the cost of manufacturing a number of items in the span of n months.我有一个线性规划问题,我需要在 n 个月的跨度内最小化制造许多项目的成本。 Xi is the variable for each amount of items manufactured corresponding to month i.
Xi 是对应于第 i 个月制造的每种物品数量的变量。 Now, I want to include a constraint where if Xi > 0, then a number A is going to be added to the objective function.
现在,我想包括一个约束条件,如果 Xi > 0,那么数字 A 将被添加到目标 function。
Obviously this can't be done with a boolean expression inside a for loop for example since Xi is a class object from the pulp library.显然,这不能通过 for 循环中的 boolean 表达式来完成,因为 Xi 是 pulp 库中的 class object。 Does anybody know how to help me?
有人知道如何帮助我吗?
Docplex is not working Docplex 不工作
Thank you so much.太感谢了。
x = [LpVariable(name=f"x{i}", lowBound=0) for i in range(0, 12)]
# standards
manufacturing_time_per_unit = 1/3
cost_of_hour = 12
storage_cost_per_unit = 3
# these are monthly
cost_of_raw_materials_per_unit = [11, 10, 13, 9, 8, 7,
10, 12, 12, 10, 9]
demand = [150, 200, 100, 300, 200,
400, 300, 250, 150, 200, 300, 350]
avalaible_hours = [250, 250, 200, 150, 200, 200,
150, 200, 250, 150, 150, 200]
cost_sum = 0
stored = [100]
for i in range(1, 13):
cost_constraint = manufacturing_time_per_unit*x[i-1] <= avalaible_hours[i-1]
model += cost_constraint
demand_constraint = x[i-1] + stored >= demand[i-1]
model += demand_constraint
stored.append(x[i-1] + stored - demand[i-1])
cost_sum += manufacturing_time_per_unit*x[i-1]+stored[i-1]*storage_cost_per_unit
storage_constraint = x[i-1] != 0
#if x[i-1]>0:
# cost_sum += 1000
model += cost_sum
model.solve()
Add a binary variable y[i]
with y[i]=0 => x[i]=0
.添加一个二进制变量
y[i]
和y[i]=0 => x[i]=0
。 (This implies x[i]>0 => y[i]=1
.) Ie (这意味着
x[i]>0 => y[i]=1
。)即
min sum(i, 1000*y[i])
x[i] <= U*y[i]
x[i] >= 0
y[i] ∈ {0,1}
Here U is an upper bound on x[i].这里 U 是 x[i] 的上界。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.