1
我使用紙漿模塊來解決我的一些優化問題。用整數,它就像一個魅力。我想知道紙漿是否可以用真正的數字工作。我認爲這被稱爲混合整數編程。在Python中使用實數紙漿
這是我爲我的項目構建的示例優化問題。
prob = pulp.LpProblem("example", pulp.LpMinimize)
# Variable represent number of times device i is used
n1 = pulp.LpVariable("n1", 1, 5, pulp.LpInteger)
n2 = pulp.LpVariable("n2", 0, max_exp, pulp.LpInteger)
n3 = pulp.LpVariable("n3", 0, max_exp, pulp.LpInteger)
n4 = pulp.LpVariable("n4", 0, max_exp, pulp.LpInteger)
n5 = pulp.LpVariable("n5", 0, max_exp, pulp.LpInteger)
# The objective function that we want to minimize: the total cost
prob += n1 * Device1[-1] + n2 * Device2[-1] + n3 * Device3[-1] + n4 * Device4[-1] + n5 * Device5[-1], "Minimize total cost"
# Constraint that we use no more than max. devices per controller device
prob += n2 + n3 + n4 + n5 <= n1*max_exp
#Constraint (total_UI >= (target_AI + target_BI))
prob += n1 * Device1[0] + n2 * Device2[0] + n3 * Device3[0] + n4 * Device4[0] + n5 * Device5[0] >= (Target[0]+Target[2])
#Constraint ((total_UO + total_BO) >= target_BO)
prob += n1 * Device1[1] + n2 * Device2[1] + n3 * Device3[1] + n4 * Device4[1] + n5 * Device5[1] + n1 * Device1[3] + n2 * Device2[3] + n3 * Device3[3] + n4 * Device4[3] + n5 * Device5[3]>= (Target[3])
#Constraint [total_UO + total_AO + total_BO - target_BO] >= target_AO
prob += n1 * Device1[1] + n2 * Device2[1] + n3 * Device3[1] + n4 * Device4[1] + n5 * Device5[1] + n1 * Device1[2] + n2 * Device2[2] + n3 * Device3[2] + n4 * Device4[2] + n5 * Device5[2] + n1 * Device1[3] + n2 * Device2[3] + n3 * Device3[3] + n4 * Device4[3] + n5 * Device5[3] >= (Target[1]+Target[3])
# Actually solve the problem, this calls GLPK so you need it installed
prob.solve()
讓我們假設DeviceX [-1]是一個實數。你能做出所需的改變嗎?
在此先感謝。
你能關閉它嗎?或重寫爲一個問題,然後發佈並接受你自己的答案。 – smci 2011-07-04 10:42:09
好的。你是對的。我已經忘記了。 – Shansal 2011-07-04 11:04:39