Graph with distance and travel time: find the largest km in 24 hours (with restrictions)

In two weeks I will participate in a contest in which people have to travel more than kilometers on a train in the Netherlands. Everyone has 24 hours to travel, and the person with the longest distance wins. However, you can only travel through each section. For example, if you are traveling from Rotterdam to Amsterdam and back from Amsterdam to The Hague, most will not count since you were already there. If you come to the same section twice, your kilometers will not be counted. To get the best route, I want to use the power of algorithms :).

To find the best route, I decided to use Python and use the package networkxto get the visualization of the Dutch railways. So far, so good, but now comes the interesting part: the algorithm. Given a schedule with all sections of the railway and distances, how can you solve the problem? Here is a graph, without distances.

enter image description here

It seems to me that this is a combination of the traveling salesman problem (except that you can visit cities several times), optimizing the maximum flow and some inverted Dijkstra algorithm: p. Is there an existing algorithm that can solve this? Or do I need to build something myself? If the latter, is it a bit of a bounce of a good approach?

+4
source share
2 answers

, , , , , , - , . :

  • 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)

# Objective
mod += sum([dist[k] * y[k] for k in dist])

# Feasibility
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

# Valid y variables
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])])

# Solve
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
+3

, , ( ) 24 . , , . , . ( ) .

EDIT:

/ ( ) . , , , , ( , ).

+1

All Articles