, , , , , , - , . :
x_t , t ( c1, c2, t1 t2).y_s , s ( c1 c2) .
, d_s , y_s s. 1 ( ), " ".
, , , . t c1, t1, (x_t = 1), c1 c1 1. , c1, c1 . c1 i1, i2, ..., in, c1 o1, o2, ..., om, x_t <= x_i1 + x_i2 + ... + x_in - x_o1 - x_o2 - ... - x_om. , , . s ( 0) s . s s1, s2, ..., sn, x_s1 + x_s2 + ... + x_sn = 1, .
, , y_s. , , y_s 0, t1, t2, ..., t_n, s, . y_s <= x_t1 + x_t2 + ... + x_tn.
Python. dists trains, ( , , , ). , , D E 3:
import pulp
dist = {("A", "B"): 1.0,
("B", "C"): 1.0,
("D", "E"): 3.0}
trains = [("A", "B", 1.0, 2.0),
("B", "C", 2.0, 3.0),
("C", "B", 3.5, 4.5),
("B", "A", 4.5, 5.5),
("D", "E", 1.0, 5.5)]
sources = set(list([t[0] for t in trains]))
x = pulp.LpVariable.dicts("x", trains, lowBound=0, upBound=1, cat=pulp.LpInteger)
y = pulp.LpVariable.dicts("y", dist.keys(), lowBound=0, upBound=1, cat=pulp.LpInteger)
s = pulp.LpVariable.dicts("s", sources, lowBound=0, upBound=1, cat=pulp.LpInteger)
mod = pulp.LpProblem("Train Optimization", pulp.LpMaximize)
mod += sum([dist[k] * y[k] for k in dist])
for t in trains:
mod += x[t] <= s[t[0]] + sum([x[k] for k in trains if k[1] == t[0] and k[3] <= t[2]]) - sum([x[k] for k in trains if k != t and k[0] == t[0] and k[2] <= t[2]])
mod += sum([s[k] for k in sources]) == 1
for k in dist:
mod += y[k] <= sum([x[t] for t in trains if (t[0] == k[0] and t[1] == k[1]) or (t[1] == k[0] and t[0] == k[1])])
mod.solve()
for t in trains:
print "Trip", t, "used:", x[t].value()
, :
Trip ('A', 'B', 1.0, 2.0) used: 0.0
Trip ('B', 'C', 2.0, 3.0) used: 0.0
Trip ('C', 'B', 3.5, 4.5) used: 0.0
Trip ('B', 'A', 4.5, 5.5) used: 0.0
Trip ('D', 'E', 1.0, 5.5) used: 1.0
A-B B-C:
dist = {("A", "B"): 2.0,
("B", "C"): 2.0,
("D", "E"): 3.0}
, , A-B-C ( , C-B B-A , , , ):
Trip ('A', 'B', 1.0, 2.0) used: 1.0
Trip ('B', 'C', 2.0, 3.0) used: 1.0
Trip ('C', 'B', 3.5, 4.5) used: 1.0
Trip ('B', 'A', 4.5, 5.5) used: 0.0
Trip ('D', 'E', 1.0, 5.5) used: 0.0