与矩阵一起使用的Python Pulp [英] Python Pulp using with Matrices
问题描述
经过数年的Matlab,我对Python还是很陌生.我正在尝试使用Pulp设置整数线性程序.
I am still very new to Python, after years and years of Matlab. I am trying to use Pulp to set up an integer linear program.
给出一个数字数组:
{P[i]:i=1...N}
我想最大化:
sum( x_i P_i )
受约束
A x <= b
A_eq x = b_eq
并带有边界(基于矢量的边界)
and with bounds (vector based bounds)
LB <= x <= UB
但是,在纸浆中,我看不到如何正确进行向量声明.我正在使用:
In pulp however, I don't see how to do vector declarations properly. I was using:
RANGE = range(numpy.size(P))
x = pulp.LpVariable.dicts("x", LB_ind, UB_ind, "Integer")
,我只能输入单个范围(因此只能输入1个数字).
where I can only enter individual bounds (so only 1 number).
prob = pulp.LpProblem("Test", pulp.LpMaximize)
prob += pulp.lpSum([Prices[i]*Dispatch[i] for i in RANGE])
并且出于约束,我真的必须逐行执行此行吗?看来我缺少了一些东西.我将不胜感激.该文档讨论了一个简短的示例.就我而言,变量的数量是几千.
and for the constraints, do I really have to do this line per line? It seems that I am missing something. I would appreciate some help. The documentation discusses a short example. The number of variables in my case is a few thousand.
推荐答案
您可以在变量初始化后设置 lowBound 和 upBound.您可以使用
You can set the lowBound and upBound on variables after the initialization. You can create an array of variables with
LB[i] <= x[i] <= UB[i]
使用以下代码.
x = pulp.LpVariable.dicts("x", RANGE, cat="Integer")
for i in x.viewkeys():
x[i].lowBound = LB_ind[i]
x[i].upBound = UB_ind[i]
LpVariable.dict的第二个参数是决策变量的索引集,而不是它们的下限.
The second parameter to LpVariable.dict is the index set of the decision variables, not their lower bounds.
这篇关于与矩阵一起使用的Python Pulp的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!