简体   繁体   English

在Python中使用实数纸浆

[英]Pulp with Real Numbers in Python

I use pulp module to solve my some optimization problems. 我使用纸浆模块来解决一些优化问题。 With integer numbers, it works like a charm. 使用整数,它就像一个符咒。 I wonder if it is possible for pulp to work with real numbers. 我想知道纸浆是否可以与实数一起使用。 I think this is called mixed integer programming. 我认为这称为混合整数编程。

Here is a sample optimization problem I build for my project. 这是我为项目构建的示例优化问题。

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()

Lets assume DeviceX[-1] be a real number. 假设DeviceX [-1]为实数。 Could you make the required changes? 您可以进行必要的更改吗?

Thanks in advance. 提前致谢。

SOLVED 解决了

Actually, I didnt need to do anything. 实际上,我不需要执行任何操作。 Just supplied the real numbers and it worked. 只需提供真实数字即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM