简体   繁体   English

Python纸浆与矩阵一起使用

[英]Python Pulp using with Matrices

I am still very new to Python, after years and years of Matlab. 经过数年和多年的Matlab,我仍然是Python的新手。 I am trying to use Pulp to set up an integer linear program. 我正在尝试使用Pulp来设置整数线性程序。

Given an array of numbers: 鉴于一系列数字:

{P[i]:i=1...N}

I want to maximize: 我想最大化:

sum( x_i P_i )

subject to the constraints 受限制

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

where I can only enter individual bounds (so only 1 number). 我只能输入个别边界(所以只有1个数字)。

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. 在我的情况下,变量的数量是几千。

You can set the lowBound and upBound on variables after the initialization. 初始化后,您可以在变量上设置lowBound和upBound。 You can create an array of variables with 您可以使用创建变量数组

LB[i] <= x[i] <= UB[i]

with the following code. 使用以下代码。

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]

The second parameter to LpVariable.dict is the index set of the decision variables, not their lower bounds. LpVariable.dict的第二个参数是决策变量的索引集,而不是它们的下限。

For the first question, you can do it like this in some other problem. 对于第一个问题,你可以在其他一些问题中这样做。

students = range(96)
group = range(24)

var = lp.LpVariable.dicts("if_i_in_group_j", ((i, j) for i in students for j in group),cat='binary')

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

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