Powerco has three electric power plants that supply the needs of four cities.† Each power plant can supply the following numbers of kilowatt-hours (kwh) of electricity: plant 1—35 million; plant 2—50 million; plant 3—40 million (see Table 1). The peak power demands in these cities, which occur at the same time (2 P.M.), are as follows (in kwh): city 1—45 million; city 2—20 million; city 3—30 million; city 4—30 million. The costs of sending 1 million kwh of electricity from plant to city depend on the distance the electricity must travel. Formulate an LP to minimize the cost of meeting each city’s peak power demand.
City 1 | City 2 | City 3 | City 4 | Supply | |
---|---|---|---|---|---|
Plant 1 | 8 | 6 | 10 | 9 | 35 |
Plant 2 | 9 | 12 | 13 | 5 | 50 |
Plant 3 | 14 | 9 | 16 | 5 | 40 |
Demand | 45 | 20 | 30 | 30 | 30 |
In [2]:
from gurobipy import *
m = Model("Transporation Prolem")
NUMBER_OF_PLANTS = 3
NUMBER_OF_CITIES = 4
cost_matrix = [[8, 6, 10, 9], [9, 12, 13, 7], [14, 9 , 16 ,5]]
supply = [35, 50, 40]
demand = [45, 20, 30, 30]
variables = [
[m.addVar(vtype=GRB.CONTINUOUS,
obj = cost_matrix[row][column],
name="(node-%d%d)" % (row+1, column+1))
for column in range(NUMBER_OF_CITIES) ]
for row in range(NUMBER_OF_PLANTS)
]
m.modelSense = GRB.MINIMIZE
m.update()
for plant_number in range(NUMBER_OF_PLANTS):
m.addConstr(
quicksum(variables[plant_number][city_number] for city_number in range(NUMBER_OF_CITIES)) <= supply[plant_number],
"supply requirment plant - %d" % (plant_number + 1))
for city_number in range(NUMBER_OF_CITIES):
m.addConstr(
quicksum(variables[plant_number][city_number] for plant_number in range(NUMBER_OF_PLANTS)) >= demand[city_number],
"demand requirment plant - %d" % (city_number + 1))
m.optimize()
for v in m.getVars():
print (v.varName, v.x)
print (m.getObjective().getValue())