Reputation: 89
Is it possible to create an LpVariable in Python PULP across three variables?
So far, I've had success using the combination of two variables - here's an example using the combination of Basketball Player and Basketball Position. The optimizer will choose combinations of players+positions that each one will play at to maximize points. (Subject to constraints that are not written below)
decision_variable = pulp.LpVariable.dicts('PlayerPosition', (PlayerVar, PositionVar), lowBound = 0, upBound = 1, cat='Integer')
My question is -- is it possible to add a THIRD variable that the optimizer can optimize across? For example, say I want to optimize for two separate games at the same time --- is there any way to update the above code with something to the effect of (PlayerVar, PositionVar, GameVar)
instead of (PlayerVar, PositionVar)
?
I haven't had success with this thus far, but I'm fairly new to PULP and certainly not an optimization expert, so maybe I'm just missing something.
Upvotes: 0
Views: 544
Reputation: 11903
Sure. The indices can be arbitrarily dimensional. You'll probably want to gin up a few convenience sets to make this easier to write your constraints, etc. Here is an example for your 3-tuple index
import pulp
# SETS
players = {'Jordan', 'Abdul Jabar', 'Johnson'}
positions = {'FWD', 'CTR', 'GRD'}
games = {4,5,6}
# the full x-set
player_pos_game = {(player, pos, game)
for player in players
for pos in positions
for game in games}
# example of a convenience set you'll likely need for constraints...
# you'll likely want other to make your constraints
player_pos = {(player, pos) for player in players for pos in positions}
prob = pulp.LpProblem("game_assignments", sense=pulp.LpMinimize)
# VARS
assign = pulp.LpVariable.dicts("assign", player_pos_game, cat=pulp.LpBinary)
for t in assign:
print(t)
('Jordan', 'CTR', 4)
('Abdul Jabar', 'CTR', 5)
('Jordan', 'GRD', 4)
('Abdul Jabar', 'GRD', 5)
('Abdul Jabar', 'FWD', 6)
('Johnson', 'CTR', 4)
('Jordan', 'FWD', 5)
('Johnson', 'GRD', 4)
('Johnson', 'FWD', 5)
('Abdul Jabar', 'CTR', 4)
('Jordan', 'CTR', 6)
('Abdul Jabar', 'GRD', 4)
('Jordan', 'GRD', 6)
('Jordan', 'FWD', 4)
('Abdul Jabar', 'FWD', 5)
('Johnson', 'CTR', 6)
...
Upvotes: 1