In [1]:
%load_ext ampl

In [2]:
%%ampl
reset;

set Plants;
set Markets;

# Capacity of plant p in cases
param Capacity{p in Plants};

# Demand at market m in cases
param Demand{m in Markets};

# Distance in thousands of miles
param Distance{Plants, Markets};

# Freight in dollars per case per thousand miles
param Freight;

# Transport cost in thousands of dollars per case
param TransportCost{p in Plants, m in Markets} :=
    Freight * Distance[p, m] / 1000; 

# Shipment quantities in cases
var shipment{Plants, Markets} >= 0;

# Total transportation costs in thousands of dollars
minimize cost:
    sum{p in Plants, m in Markets} TransportCost[p, m] * shipment[p, m];

# Observe supply limit at plant p
s.t. supply{p in Plants}: sum{m in Markets} shipment[p, m] <= Capacity[p];

# Satisfy demand at market m
s.t. demand{m in Markets}: sum{p in Plants} shipment[p, m] >= Demand[m];

data;

set Plants := seattle san-diego;
set Markets := new-york chicago topeka;

param Capacity :=
    seattle   350
    san-diego 600;

param Demand :=
    new-york 325
    chicago  300
    topeka   275;

param Distance : new-york chicago topeka :=
    seattle        2.5      1.7     1.8
    san-diego      2.5      1.8     1.4;

param Freight := 90;

solve;


MINOS 5.51: optimal solution found.
4 iterations, objective 153.675

In [3]:
# Indexed AMPL parameters, variables, constraints, etc. act as
# Python dictionaries.
for s in shipment:
    print(s, shipment[s])


('seattle', 'topeka') 0.0
('seattle', 'chicago') 300.0
('san-diego', 'topeka') 275.0
('san-diego', 'chicago') 0.0
('san-diego', 'new-york') 275.0
('seattle', 'new-york') 50.0

In [4]:
# It is easy to visualize the data using matplotlib.
s = shipment.val
pos = arange(len(s))
barh(pos, s.values(), align='center')
yticks(pos, list(s.keys()))
show()



In [ ]: