#!/usr/bin/env python
import sys
from pulp import LpVariable, LpBinary, lpSum, LpProblem, LpMinimize
from facility_ex1 import REQUIREMENT, PRODUCTS
from facility_ex1 import LOCATIONS, CAPACITY
try:
from facility_ex1 import FIXED_COST
except ImportError:
FIXED_COST = [1 for i in LOCATIONS]
try:
from facility_ex1 import ASSIGNMENTS
except ImportError:
ASSIGNMENTS = [(i, j) for i in LOCATIONS for j in PRODUCTS]
try:
from facility_ex1 import ASSIGNMENT_COSTS
except ImportError:
ASSIGNMENT_COSTS = dict((i, 0) for i in ASSIGNMENTS)
display_mode = 'xdot'
prob = LpProblem("Facility Location", LpMinimize)
assign_vars = LpVariable.dicts("x", ASSIGNMENTS, 0, 1, LpBinary)
use_vars = LpVariable.dicts("y", LOCATIONS, 0, 1, LpBinary)
debug_print = False
debug_print_lp = False
prob += (lpSum(use_vars[i] * FIXED_COST[i] for i in LOCATIONS) +
lpSum(assign_vars[j] * ASSIGNMENT_COSTS[j] for j in ASSIGNMENTS),
"min")
# assignment constraints
for j in PRODUCTS:
prob += lpSum(assign_vars[(i, j)] for i in LOCATIONS) == 1
# Aggregate capacity constraints
for i in LOCATIONS:
prob += lpSum(assign_vars[(i, j)] * REQUIREMENT[j]
for j in PRODUCTS) <= CAPACITY * use_vars[i]
# Disaggregate capacity constraints
for i, j in ASSIGNMENTS:
prob += assign_vars[(i, j)] <= use_vars[i]
prob.solve()
# print solution
print "Optimal solution found!"
print "************************************"
for i in LOCATIONS:
if use_vars[i].varValue > 0:
print "Location ", i, " is assigned: ",
print [j for j in PRODUCTS if assign_vars[(i, j)].varValue > 0]
print "************************************"
print