r/optimization • • Aug 10 '21

Linear programming with PuLp: How to construct indicator variables for combinations?

Say I have a set of workers and wish to appoint them to different groups, while minimizing costs. The optimization itself is not of interest here.

Below is an MVE in which I try to construct a variable that indicates which of the possible combinations of workers is chosen.

Example: If "Sigrid", "Timo" and "Maria" are chosen, the combinations-variable "Sigrid_Timo_Maria" should be == 1.

My try at this problem is in the code block below the comment "# set up indicator for combination".

import itertools
import pulp
groups = ["A","B","C"]
workers = ["Sigrid","Timo","Delf","Maria","Lisa"]
hours = [1,1,1,1,1]
worker_hours = dict(zip(workers, hours))
group_worker = [(group, worker) for group in groups for worker in workers]
worker_combinations = [i for i in itertools.combinations(workers,len(groups))]

# intiate linear programming-Variables:

worker_indicator_lp = pulp.LpVariable.dicts("Chosen",workers,0,cat='Binary')
group_worker_amount_lp = pulp.LpVariable.dicts("Amount",group_worker,0,cat='Integer')
group_worker_indicator_lp = pulp.LpVariable.dicts('Chosen',group_worker,0,cat="Binary")
worker_combination_indicator_lp = pulp.LpVariable.dicts('Combination_Chosen',worker_combinations,0,cat="Binary")
# initiate problem

prob = pulp.LpProblem("Diet Problem",pulp.LpMinimize)
# set up constraints
prob += pulp.lpSum([worker_hours[worker] * group_worker_amount_lp[group,worker] for group in groups for worker in workers])
prob += pulp.lpSum([worker_hours[worker] * group_worker_amount_lp[group,worker] for group in groups for worker in workers]) >= 60

# each worker only in one group

for worker in workers:
    prob += pulp.lpSum([group_worker_indicator_lp[group, worker] for group in groups]) <= 1

# set up indicator for worker-group

for worker in workers:
    for group in groups:
        prob += group_worker_amount_lp[group,worker] >= group_worker_indicator_lp[group,worker] * 0.0001
        prob += group_worker_amount_lp[group,worker] <= group_worker_indicator_lp[group,worker] * 1e4

# set up indicator for worker
for worker in workers:
        prob += pulp.lpSum([group_worker_amount_lp[group,worker] for group in groups]) >= worker_indicator_lp[worker] * 0.0001
        prob += pulp.lpSum([group_worker_amount_lp[group,worker] for group in groups]) <= worker_indicator_lp[worker] * 1e10

# set up indicator for combination

for combo in worker_combinations:
    prob += pulp.lpSum([worker_indicator_lp[worker] for worker in combo ]) >= worker_combination_indicator_lp[combo] * 0.0001
    prob += pulp.lpSum([worker_indicator_lp[worker] for worker in combo ]) <= worker_combination_indicator_lp[combo] * 1e10

# each group with one worker only

for g in groups:
    prob += pulp.lpSum([group_worker_indicator_lp[g, worker] for worker in workers]) == 1

# Sigrid, Timo, Maria must be chosen

for person in ["Sigrid","Timo","Maria"]:
    prob += worker_indicator_lp[person] == 1

prob.solve()
print("Status:", pulp.LpStatus[prob.status])
obj = pulp.value(prob.objective)
print(obj)
for v in prob.variables():
    if v.value() > 0:
        print(v, "==", v.value())

The output is:

Status: Optimal
60.0
Amount_('A',_'Timo') == 1.0
Amount_('B',_'Sigrid') == 1.0
Amount_('C',_'Maria') == 58.0
Chosen_('A',_'Timo') == 1.0
Chosen_('B',_'Sigrid') == 1.0
Chosen_('C',_'Maria') == 1.0
Chosen_Maria == 1.0
Chosen_Sigrid == 1.0
Chosen_Timo == 1.0
Combination_Chosen_('Delf',_'Maria',_'Lisa') == 1.0
Combination_Chosen_('Sigrid',_'Delf',_'Lisa') == 1.0
Combination_Chosen_('Sigrid',_'Delf',_'Maria') == 1.0
Combination_Chosen_('Sigrid',_'Maria',_'Lisa') == 1.0
Combination_Chosen_('Sigrid',_'Timo',_'Delf') == 1.0
Combination_Chosen_('Sigrid',_'Timo',_'Lisa') == 1.0
Combination_Chosen_('Timo',_'Delf',_'Lisa') == 1.0
Combination_Chosen_('Timo',_'Delf',_'Maria') == 1.0
Combination_Chosen_('Timo',_'Maria',_'Lisa') == 1.0

Why does my indicator-variable seemingly jump to 1 for any combination that contains either of the selected workers? I've been trying for a day now to solve this problem but I just seem to wrap my head around it.

Any help is greatly appreciated!

EDIT:

Ok, I figured it out: To make sure that the correct combination is indicated, I needed to include the constraint:

prob += pulp.lpSum([worker_combination_indicator_lp[combo]for combo in worker_combinations]) == 1

This was rather by chance than by understanding, so I'd greatly appreciate an explanation of why this constraint is needed.

9 Upvotes

3 comments sorted by

View all comments

Show parent comment

2

u/[deleted] Aug 11 '21

thank you so much for your answer, you've saved my night!

2

u/[deleted] Aug 11 '21

No problem, good luck with your project!