The transportation problem

Allocation of passenger cars to trains to minimize cars required or car-miles run. Based on:

Fourer, D.M. Gay and Brian W. Kernighan, A Modeling Language for Mathematical Programming, https://ampl.com/REFS/amplmod.ps.gz Appendix D.

Originally contributed by Louis Luangkesorn, January 30, 2015.

using JuMP
import HiGHS
import Test

function example_transp()
    ORIG = ["GARY", "CLEV", "PITT"]
    DEST = ["FRA", "DET", "LAN", "WIN", "STL", "FRE", "LAF"]
    supply = [1_400, 2_600, 2_900]
    demand = [900, 1_200, 600, 400, 1_700, 1_100, 1_000]
    Test.@test sum(supply) == sum(demand)
    cost = [
        39 14 11 14 16 82 8
        27 9 12 9 26 95 17
        24 14 17 13 28 99 20
    ]
    model = Model(HiGHS.Optimizer)
    @variable(model, trans[1:length(ORIG), 1:length(DEST)] >= 0)
    @objective(
        model,
        Min,
        sum(
            cost[i, j] * trans[i, j] for i in 1:length(ORIG),
            j in 1:length(DEST)
        )
    )
    @constraints(
        model,
        begin
            [i in 1:length(ORIG)], sum(trans[i, :]) == supply[i]
            [j in 1:length(DEST)], sum(trans[:, j]) == demand[j]
        end
    )
    optimize!(model)
    Test.@test termination_status(model) == OPTIMAL
    Test.@test primal_status(model) == FEASIBLE_POINT
    Test.@test objective_value(model) == 196200.0
    println("The optimal solution is:")
    println(value.(trans))
    return
end

example_transp()
Presolving model
10 rows, 21 cols, 42 nonzeros
9 rows, 21 cols, 39 nonzeros
Presolve : Reductions: rows 9(-1); columns 21(-0); elements 39(-3)
Solving the presolved LP
Using EKK dual simplex solver - serial
  Iteration        Objective     Infeasibilities num(sum)
          0     0.0000000000e+00 Pr: 9(12900) 0s
         10     1.9620000000e+05 Pr: 0(0) 0s
Solving the original LP from the solution after postsolve
Model   status      : Optimal
Simplex   iterations: 10
Objective value     :  1.9620000000e+05
HiGHS run time      :          0.00
The optimal solution is:
[0.0 0.0 0.0 0.0 300.0 1100.0 0.0; 0.0 1200.0 600.0 400.0 0.0 0.0 400.0; 900.0 0.0 0.0 0.0 1400.0 0.0 600.0]

Tip

This tutorial was generated using Literate.jl. View the source .jl file on GitHub.