-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathtrnsport.py
More file actions
142 lines (118 loc) · 3.11 KB
/
trnsport.py
File metadata and controls
142 lines (118 loc) · 3.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
"""
## GAMSSOURCE: https://www.gams.com/latest/gamslib_ml/libhtml/gamslib_trnsport.html
## LICENSETYPE: Demo
## MODELTYPE: LP
## KEYWORDS: linear programming, transportation problem, scheduling
A Transportation Problem (TRNSPORT)
This problem finds a least cost shipping schedule that meets
requirements at markets and supplies at factories.
Dantzig, G B, Chapter 3.3. In Linear Programming and Extensions.
Princeton University Press, Princeton, New Jersey, 1963.
This formulation is described in detail in:
Rosenthal, R E, Chapter 2: A GAMS Tutorial. In GAMS: A User's Guide.
The Scientific Press, Redwood City, California, 1988.
The line numbers will not match those in the book because of these
comments.
"""
from __future__ import annotations
from gamspy import (
Container,
Equation,
Model,
Parameter,
Sense,
Set,
Sum,
Variable,
)
def main():
m = Container()
# Prepare data
distances = [
["seattle", "new-york", 2.5],
["seattle", "chicago", 1.7],
["seattle", "topeka", 1.8],
["san-diego", "new-york", 2.5],
["san-diego", "chicago", 1.8],
["san-diego", "topeka", 1.4],
]
capacities = [["seattle", 350], ["san-diego", 600]]
demands = [["new-york", 325], ["chicago", 300], ["topeka", 275]]
# Set
i = Set(
m,
name="i",
records=["seattle", "san-diego"],
description="canning plants",
)
j = Set(
m,
name="j",
records=["new-york", "chicago", "topeka"],
description="markets",
)
# Data
a = Parameter(
m,
name="a",
domain=i,
records=capacities,
description="capacity of plant i in cases",
)
b = Parameter(
m,
name="b",
domain=j,
records=demands,
description="demand at market j in cases",
)
d = Parameter(
m,
name="d",
domain=[i, j],
records=distances,
description="distance in thousands of miles",
)
c = Parameter(
m,
name="c",
domain=[i, j],
description="transport cost in thousands of dollars per case",
)
c[i, j] = 90 * d[i, j] / 1000
# Variable
x = Variable(
m,
name="x",
domain=[i, j],
type="Positive",
description="shipment quantities in cases",
)
# Equation
supply = Equation(
m,
name="supply",
domain=i,
description="observe supply limit at plant i",
)
demand = Equation(
m, name="demand", domain=j, description="satisfy demand at market j"
)
supply[i] = Sum(j, x[i, j]) <= a[i]
demand[j] = Sum(i, x[i, j]) >= b[j]
transport = Model(
m,
name="transport",
equations=m.getEquations(),
problem="LP",
sense=Sense.MIN,
objective=Sum((i, j), c[i, j] * x[i, j]),
)
transport.solve()
import math
assert math.isclose(transport.objective_value, 153.675000, rel_tol=0.001)
print(x.records)
print(transport.objective_value)
print(transport.status)
if __name__ == "__main__":
main()