routing

package module
v0.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 21, 2020 License: MIT Imports: 6 Imported by: 0

README

Simple Timetable Routing

This package provides a simple algorithm to compute fastest routes in public transportation networks. This project was mainly meant as a study project to understand the algorithm and to experiment with different data models for public transportation networks in Go.

This implementation is not suited for production usage, it may still have bugs and is not at all optimized. As of now, the implementation assumes a minimum change time at stations of five minutes. It is planed to make this constant customizable.

Quick Guide:

  1. Define some stops with Id and name:
    mainStation := NewStop("MS","Main Station")
    docksAE := NewStop("DAE","Docks A–E")
    docksFG := NewStop("DFG", "Docks F and G")
    historicMall := NewStop( "HM","Historic Mall")
    schusterStreet := NewStop("SS","Schuster Street")
    marketPlace := NewStop("MP", "Market Place")
    airport := NewStop("AR","Airport")
    northAvenue := NewStop("NA","North Avenue")
    chalet := NewStop("CH", "Chalet")
    northEnd := NewStop("NE" ,"North End")
    
  2. Define your lines:
    blueLine := &Line{Id: "#0000FF", Name: "Blue Line", startStop: mainStation}
    redLine := &Line{Id: "#FF0000", Name: "Red Line", startStop: northEnd}
    
  3. Define events on the stops:
       mainStation.Events = []Event{
         {Departure: "8:02", Line: blueLine, TravelTime: 2 * time.Minute, NextStop: northAvenue, LineSegment: blueLine.Segments[2]},
         {Departure: "8:05", Line: redLine, TravelTime: 3 * time.Minute, NextStop: historicMall, LineSegment: redLine.Segments[0]}
         ...
       }   
    
  4. Create a timetable and query it:
       timetable := NewTimetable([]*Stop[mainstation, ...])
       connection := timetable.query(historicMall, chalet, time.Now())
    

Implementation Details

The package implements the time-dependent variant of Dijkstra's Algorithm. The time-dependent model is a classical solution for finding routes in public transportation networks. For a thorough explanation see for example this paper ("Time-Dependent Route Planning" by Daniel Delling and Dorothea Wagner).

The advantage of the time-dependent approach is that the original algorithm by Dijstrka must hardly be changed. The first step is to build a graph that resembles the real network graph: Every station is represented by a vertex and if there is at least one line connecting two stations, then there is an edge between the stations in the graph.

In normal Dijstrka the weight of the edge w(e) is constant. In the time-dependent model, the weight changes over time, thus we have a weight function of w(e,t) where t is the time. Given two stations u and v which are connected by lines A and B. If a train of line A departures at 14:30 and a train of line B departures at 14:40 and the travel time to v are three minutes for both lines, then w((u,v), 14:25) is eight minutes and w((u,v), 14:39) is four minutes.

My implementation is (at the moment) not focussed on performance, but rather was meant to be a working method for route finding in public networks. Thus, my implementation does neither make use of fibonnaci heaps in the plain Dijsktra algorithm, nor is the implementation of w(e,t) very sophisticated.

License

See LICENSE file.

Documentation

Overview

Package routing offers an algorithm to find the fastest route in a public transportation network.

The cornerstone of the package is the Timetable type which contains all information of the public transportation network.

Users of this package have to define Stops – physical locations where vehicles stop. A stop must have an Id and a list of departure events. All events consist of a departure time, a reference to the line they belong to, and information about the travel time to the next stop and which stop this is. The list of stops is used to create a timetable. This timetable can then be queried for shortest routes.

The departure times are given in the format "15:04" (resp. "HH:MM"). Queries always contain a real date and time (time.Time type). The departure times are then interpreted to take place at the certain date. In order to simulate timetables spanning more than one day, departure times can also be given four hours bigger than 23 o'clock, e.g. 26:34 means 02:34 on the second day.

Index

Constants

This section is empty.

Variables

View Source
var TimeRegex = regexp.MustCompile("^([0-9]+):?([0-5][0-9])$")

TimeRegex is used to validate time strings.

Functions

This section is empty.

Types

type Connection

type Connection struct {
	Arrival time.Time
	Legs    []Leg
}

Connection is the result of a route computation. It contains the arrival time as well a slice of legs which describe the lines that have to be taken at certain stations in order to reach the target station.

type Event

type Event struct {
	Departure  Time
	Line       *Line
	NextStop   *Stop
	TravelTime time.Duration
}

Event describes the departure of a certain line's vehicle at a station. The segment property points to the line segment that describes the journey to the next stop after the event's stop.

type Leg

type Leg struct {
	Line      *Line
	FirstStop *Stop
	LastStop  *Stop
}

Leg is a part of a journey during which there is no change of lines. A leg has the first stop, a last stop and a line.

type Line

type Line struct {
	Id   string
	Name string
	// contains filtered or unexported fields
}

Line represents a line in a public transportation network. It consists of an Id, which should be unique among all lines. Variants of lines (e.g. additional stops in the rush hour) should be modeled with additional lines.

type Stop

type Stop struct {
	Id     string
	Name   string
	Events []Event
}

Stop is a physical stop where a public transport vehicle stops and lets passengers enter and exit. The Id of the stop must be unique.

func NewStop

func NewStop(id, name string) *Stop

NewStop creates a new stop with the given id and name and an empty events slice.

type Time

type Time string

Time is a string data type that can be interpreted as simple time (without date). The time string should always match the TimeRegex, otherwise a panic may be risen.

Examples: 12:04, 14:34, 28:23 are all valid times

func CreateTime

func CreateTime(hour, minute int) Time

CreateTime creates a time string from a given hour and minute.

type Timetable

type Timetable struct {
	// contains filtered or unexported fields
}

Timetable contains all routing information in a public transport network. Timetables should be created with the NewTimetable function.

func NewTimetable

func NewTimetable(stops []*Stop) Timetable

NewTimetable creates a new timetable containing the passed stops. The stops contain all relevant information about the transport network (arrivals, departures, and lines).

func (*Timetable) Query

func (t *Timetable) Query(source *Stop, target *Stop, start time.Time) *Connection

Query computes the fastest route between source and target with the specified start time. If there is no connection, then nil is returned.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL