简体   繁体   中英

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!

Welcome to SO! 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.

It's 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]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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