Mohit Srinivasan
Mohit Srinivasan

Reputation: 1

2D decision variables in PuLP

I am new to PuLP, and I am trying to run an optimization problem where one of my decision variables is 2D. I'm a little confused as to how one can declare 2D decision variables as part of plp.LpVariable? As of now, this is how I am declaring the variable

a = { k : plp.LpVariable(name='a', lowBound=np.array([0, 0]), \
                         upBound=np.array([2, 3]), \
                         cat=plp.LpContinuous) for k in range(10)}

Thanks!

Upvotes: 0

Views: 1603

Answers (1)

kabdulla
kabdulla

Reputation: 5419

What you are looking for is the dicts method of the LpVariable class. This allows you to pass in multi-dimensional indexes to create an M x N, or M x N x O (etc.) set of variables.

Its use is illustrated in the pulp docs example of solving a sudoku puzzle: https://coin-or.github.io/pulp/CaseStudies/a_sudoku_problem.html?highlight=dicts

And the method itself is documented here: https://coin-or.github.io/pulp/technical/pulp.html?highlight=dicts#pulp.LpVariable.dicts

As far as I know the method cannot directly accept upperbounds and lowerbounds which are different for each variable, so you'd need to do something like:

up_bounds = [2,3]
a = pulp.LpVariable.dicts('a', range(2), lowBound=0)
for i in range(2):
    prob += a[i] <= up_bounds[i]

Upvotes: 1

Related Questions