This repository has been archived by the owner on Apr 4, 2020. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Marcus Olsson
committed
Oct 25, 2014
0 parents
commit 18f3251
Showing
5 changed files
with
171 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
The MIT License (MIT) | ||
|
||
Copyright (c) 2014 Marcus Olsson | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in | ||
all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
THE SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
web: pathfinder |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
pathfinder | ||
========== | ||
|
||
The routing context from the original DDD Sample Application, written in Go. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package main | ||
|
||
import ( | ||
"net/http" | ||
"os" | ||
|
||
"github.com/gorilla/mux" | ||
"github.com/marcusolsson/pathfinder" | ||
"gopkg.in/unrolled/render.v1" | ||
) | ||
|
||
func main() { | ||
|
||
r := render.New(render.Options{IndentJSON: true}) | ||
router := mux.NewRouter() | ||
|
||
router.HandleFunc("/paths", func(w http.ResponseWriter, req *http.Request) { | ||
from := req.URL.Query().Get("from") | ||
to := req.URL.Query().Get("to") | ||
|
||
if len(from) == 0 || len(to) == 0 { | ||
r.JSON(w, http.StatusBadRequest, map[string]interface{}{"error": "missing parameters"}) | ||
return | ||
} | ||
|
||
r.JSON(w, http.StatusOK, pathfinder.FindShortestPath(from, to)) | ||
}).Methods("GET") | ||
|
||
http.Handle("/", router) | ||
|
||
http.ListenAndServe(":"+os.Getenv("PORT"), nil) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
package pathfinder | ||
|
||
import ( | ||
"math/rand" | ||
"time" | ||
) | ||
|
||
// TransitEdge is a direct transportation between two locations. | ||
type TransitEdge struct { | ||
VoyageNumber string `json:"voyage"` | ||
FromUNLocode string `json:"origin"` | ||
ToUNLocode string `json:"destination"` | ||
FromTime time.Time `json:"departure"` | ||
ToTime time.Time `json:"arrival"` | ||
} | ||
|
||
// TransitPath is a series of transit edges. | ||
type TransitPath struct { | ||
Edges []TransitEdge `json:"edges"` | ||
} | ||
|
||
// FindShortestPath computes the shortest paths between two locations. | ||
func FindShortestPath(origin, destination string) []TransitPath { | ||
start := time.Now() | ||
date := nextDate(start) | ||
|
||
v := allVertices(origin, destination) | ||
|
||
candidates := make([]TransitPath, 3+rand.Intn(3)) | ||
for i := range candidates { | ||
v = randChunk(v) | ||
|
||
var edges []TransitEdge | ||
|
||
edges, date = appendEdge(edges, origin, v[0], date) | ||
|
||
for j := 0; j < len(v)-1; j++ { | ||
edges, date = appendEdge(edges, v[j], v[j+1], date) | ||
} | ||
|
||
edges, _ = appendEdge(edges, v[len(v)-1], destination, date) | ||
|
||
candidates[i] = TransitPath{Edges: edges} | ||
} | ||
|
||
return candidates | ||
} | ||
|
||
func appendEdge(edges []TransitEdge, curr, next string, date time.Time) ([]TransitEdge, time.Time) { | ||
var ( | ||
from = nextDate(date) | ||
to = nextDate(from) | ||
) | ||
|
||
edges = append(edges, TransitEdge{ | ||
VoyageNumber: randVoyageNumber(), | ||
FromUNLocode: curr, | ||
ToUNLocode: next, | ||
FromTime: from, | ||
ToTime: to, | ||
}) | ||
|
||
return edges, nextDate(to) | ||
} | ||
|
||
func allVertices(origin, destination string) []string { | ||
locations := []string{"CNHKG", "AUMEL", "SESTO", "FIHEL", "USCHI", "JNTKO", "DEHAM", "CNSHA", "NLRTM", "SEGOT", "CNHGH", "USNYC", "USDAL"} | ||
for i, l := range locations { | ||
if l == origin { | ||
locations = append(locations[:i], locations[i+1:]...) | ||
} | ||
} | ||
for i, l := range locations { | ||
if l == destination { | ||
locations = append(locations[:i], locations[i+1:]...) | ||
} | ||
} | ||
return locations | ||
} | ||
|
||
func randVoyageNumber() string { | ||
switch rand.Intn(5) { | ||
case 0: | ||
return "0100S" | ||
case 1: | ||
return "0200T" | ||
case 2: | ||
return "0300A" | ||
case 3: | ||
return "0301S" | ||
} | ||
return "0400S" | ||
} | ||
|
||
func randChunk(locations []string) []string { | ||
// TODO: Shuffle the locations first for even more randomness. | ||
|
||
t := len(locations) | ||
|
||
var c int | ||
if t > 4 { | ||
c = 1 + rand.Intn(5) | ||
} else { | ||
c = t | ||
} | ||
|
||
return locations[:c] | ||
} | ||
|
||
func nextDate(t time.Time) time.Time { | ||
n := time.Duration(rand.Intn(1000) - 500) | ||
return t.Add(24 * time.Hour).Add(n * time.Minute) | ||
} |