Transporation Problem

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())


Optimize a model with 7 rows, 12 columns and 24 nonzeros
Coefficient statistics:
  Matrix range     [1e+00, 1e+00]
  Objective range  [5e+00, 2e+01]
  Bounds range     [0e+00, 0e+00]
  RHS range        [2e+01, 5e+01]
Presolve time: 1.75s
Presolved: 7 rows, 12 columns, 24 nonzeros

Iteration    Objective       Primal Inf.    Dual Inf.      Time
       0    0.0000000e+00   1.250000e+02   0.000000e+00      2s
       7    1.0200000e+03   0.000000e+00   0.000000e+00      2s

Solved in 7 iterations and 1.78 seconds
Optimal objective  1.020000000e+03
(node-11) 0.0
(node-12) 10.0
(node-13) 25.0
(node-14) 0.0
(node-21) 45.0
(node-22) 0.0
(node-23) 5.0
(node-24) 0.0
(node-31) 0.0
(node-32) 10.0
(node-33) 0.0
(node-34) 30.0
1020.0