I am solving transportation problem using PULP.
I have a list of suppliers (supply) and list of customers (demand). The supply is always more than demand and in some cases not every supplier is chosen by PULP. However sometimes i need specific supplier to be chosen regardless of whether it is the optimal option or not. I understand that it should be added as a constraint, but i do not know how to modify my script.
prob = LpProblem("transportationproblem", LpMaximize)
routes = [(s,c) for s in suppliers for c in customers]
vars = LpVariable.dicts("Route", (suppliers, customers), 0, None, LpInteger)
prob += (
lpSum([vars[s][c] * costs[s][c] for (s, c) in routes]),
"sum_of_transp_cost"
)
for s in suppliers:
prob += (
lpSum([vars[s][c] for c in customers]) <= supply[s],
f"sum_of_products_from_suppliers_{s}",
)
for c in customers:
prob += (
lpSum([vars[s][c] for s in suppliers]) == demand[c],
f"sum_of_products_to_customers{c}"
)
I tried to add this constraint, but solver is not working correctly
for s in suppliers:
if s == "specific supplier":
prob += (
lpSum([vars[s][c] for c in customers]) == supply[s],
f"sum_of_products_from_suppliers_{s}",
)
Your problem is probably that you're double-applying a constraint to the supplier row that you want to be "specific". This works: