Python Pulp using with Matrices
Asked Answered
C

2

20

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.

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

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.

Czernowitz answered 11/10, 2011 at 15:14 Comment(2)
As I recall in PuLP, you do have to add each constraint individually (line-by-line).Clamworm
I have the same question. I know this is old. I would greatly appreciate a satisfying answer!Manufactory
S
10

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]

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.

Swearword answered 6/4, 2012 at 4:58 Comment(0)
S
4

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')
Sisterly answered 15/3, 2018 at 5:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.