HLPL / VerTeX Modelling Language Documentation

← lessons

Transportation — two sources

Supply and demand both bite.

Chapter: VIII — Transportation · lesson id transportation-two-source · practice lesson


Two sources with supplies {50, 60} and three destinations with demands {30, 40, 40}. Total supply 110 = total demand 110 (balanced).

Variables x_{i,j} = units shipped from source i to destination j. Two families of constraints: sources can't ship more than their supply, destinations must get at least their demand. Minimise total transport cost.

Predict before running

Which source-destination pairs will the solver favour?

Starter code

model transport
set I = {1, 2}
set J = {1, 2, 3}
const c = {{3, 5, 7}, {6, 4, 2}}, forall i in I, forall j in J
const supply = {50, 60}, forall i in I
const demand = {30, 40, 40}, forall j in J
var 0 <= x, forall i in I, forall j in J
constr sum_{j in J}{x_{i,j}} = supply_{i}, forall i in I
constr sum_{i in I}{x_{i,j}} = demand_{j}, forall j in J
min sum_{i in I}{sum_{j in J}{c_{i,j} * x_{i,j}}}
end

import elytica
def main():
  elytica.init_model("transport")
  elytica.run_model("transport")
  for i in [1,2]:
    for j in [1,2,3]:
      name = "x"+str(i)+","+str(j)
      print(name, "=", elytica.get_variable_value("transport", name))
  print("obj =", elytica.get_best_primal_bound("transport"))
  return 0

Hint

Costs are [[3, 5, 7], [6, 4, 2]]. Source 1 likes dest 1; source 2 likes dest 3.

What counts as correct

Mastery variant

Two sources, three destinations. Different sizes, different cheapest cells.

model transport_v
set I = {1, 2}
set J = {1, 2, 3}
const c = {{2, 6, 4}, {5, 3, 7}}, forall i in I, forall j in J
const supply = {40, 50}, forall i in I
const demand = {25, 30, 35}, forall j in J
var 0 <= x, forall i in I, forall j in J
constr sum_{j in J}{x_{i,j}} = supply_{i}, forall i in I
constr sum_{i in I}{x_{i,j}} = demand_{j}, forall j in J
min sum_{i in I}{sum_{j in J}{c_{i,j} * x_{i,j}}}
end

import elytica
def main():
  elytica.init_model("transport_v")
  elytica.run_model("transport_v")
  for i in [1,2]:
    for j in [1,2,3]:
      name = "x"+str(i)+","+str(j)
      print(name, "=", elytica.get_variable_value("transport_v", name))
  print("obj =", elytica.get_best_primal_bound("transport_v"))
  return 0

Read this through MCP: read with path: "lessons/transportation-two-source", or resource hlpl://lessons/transportation-two-source.