[英]Specify variables bounds in pulp
I'm working on linear programming using pulp and what I ask for is how can I specify each items bounds in my list我正在使用纸浆进行线性编程,我要求的是如何指定列表中的每个项目边界
mylist=["x","y","z"]
I've created this:我创建了这个:
vars = LpVariable.dicts("vars", mylist, lowBound=0, cat='Continuous')
but it creates a global bounds for all of the items inside my list and what I want is for each item in my list但它为列表中的所有项目创建了一个全局范围,而我想要的是列表中的每个项目
I try this but it didn't work:我尝试了这个,但没有奏效:
x = LpVariable("x", lowBound=5, upBound=10, cat='Continuous')
THANKS谢谢
You just need to create individual constraints for the low/upper bounds if you want them to be different in pulp
.如果您希望它们在
pulp
中有所不同,您只需要为下限/上限创建单独的约束。 It's all the same to the solver.这对求解器来说都是一样的。
import pulp as plp
products = ['rice', 'veggies', 'fruit']
low_bounds = { 'rice': 5,
'veggies': 7,
'fruit': 2}
prob = plp.LpProblem('example')
x = plp.LpVariable.dicts('products', products, cat='Continuous')
for p in products:
prob += x[p] >= low_bounds[p]
print(prob)
MINIMIZE
None
SUBJECT TO
_C1: products_rice >= 5
_C2: products_veggies >= 7
_C3: products_fruit >= 2
VARIABLES
products_fruit free Continuous
products_rice free Continuous
products_veggies free Continuous
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.