-
Notifications
You must be signed in to change notification settings - Fork 3
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
Showing
16 changed files
with
963 additions
and
292 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
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,6 @@ | ||
name "astar-cities" | ||
description "An example illustrating an A* search." | ||
copyright "Copyright © 2016, Trent Clarke" | ||
license "BSL-1.0" | ||
authors "Trent Clarke" | ||
dependency "anansi" version="master" path="../../" |
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,123 @@ | ||
/** | ||
* An example using the anansi A* search algorithm. Ported from the | ||
* boost graph library's example. | ||
*/ | ||
import anansi, | ||
anansi.algorithms.astar; | ||
import std.math : sqrt; | ||
import std.algorithm : reverse; | ||
import std.stdio : write, writeln; | ||
import std.container; | ||
|
||
/** | ||
* The city data associated wth a given vertex. | ||
*/ | ||
struct City { | ||
this(string n, real latitude, real longitude) { | ||
name = n; | ||
lat = latitude; | ||
lng = longitude; | ||
} | ||
|
||
string name; | ||
real lat; | ||
real lng; | ||
} | ||
|
||
alias Graph = AdjacencyList!(VecS, VecS, DirectedS, City, real); | ||
alias Vertex = Graph.VertexDescriptor; | ||
|
||
/** | ||
* An exception type that signals that a path through the graph has been found. | ||
*/ | ||
class FoundTarget : Throwable { | ||
this() { super("found it"); } | ||
} | ||
|
||
struct AStarGoalVisitor { | ||
mixin NullAstarVisitor!Graph; | ||
|
||
Vertex _target; | ||
|
||
this(Vertex target) { | ||
_target = target; | ||
} | ||
|
||
void examineVertex(ref const(Graph) g, Vertex v) { | ||
if (v == _target) { | ||
throw new FoundTarget; | ||
} | ||
} | ||
} | ||
|
||
|
||
void main() { | ||
Graph g; | ||
|
||
auto troy = g.addVertex(City("Troy", 42.73, 73.68)); | ||
auto lake_placid = g.addVertex(City("Lake Placid", 44.28, 73.99)); | ||
auto plattsborough = g.addVertex(City("Plattsborough", 44.70, 73.46)); | ||
auto massena = g.addVertex(City("Massena", 44.93, 74.89)); | ||
auto watertown = g.addVertex(City("Watertown", 43.97, 75.91)); | ||
auto utica = g.addVertex(City("Utica", 43.10, 75.23)); | ||
auto syracuse = g.addVertex(City("Syracuse", 43.04, 76.14)); | ||
auto rochester = g.addVertex(City("Rochester", 43.17, 77.61)); | ||
auto buffalo = g.addVertex(City("Buffalo", 42.89, 78.86)); | ||
auto ithaca = g.addVertex(City("Ithaca", 42.44, 76.50)); | ||
auto binghamton = g.addVertex(City("Binghamon", 42.10, 75.91)); | ||
auto woodstock = g.addVertex(City("Woodstock", 42.04, 74.11)); | ||
auto new_york = g.addVertex(City("New York", 40.67, 73.94)); | ||
|
||
g.addEdge(troy, utica, 93); | ||
g.addEdge(troy, lake_placid, 134); | ||
g.addEdge(troy, plattsborough, 143); | ||
g.addEdge(lake_placid, plattsborough, 65); | ||
g.addEdge(plattsborough, massena, 115); | ||
g.addEdge(lake_placid, massena, 113); | ||
g.addEdge(massena, watertown, 117); | ||
g.addEdge(watertown, utica, 116); | ||
g.addEdge(watertown, syracuse, 74); | ||
g.addEdge(utica, syracuse, 56); | ||
g.addEdge(syracuse, rochester, 84); | ||
g.addEdge(rochester, buffalo, 73); | ||
g.addEdge(syracuse, ithaca, 69); | ||
g.addEdge(ithaca, binghamton, 70); | ||
g.addEdge(ithaca, rochester, 116); | ||
g.addEdge(binghamton, troy, 147); | ||
g.addEdge(binghamton, woodstock, 173); | ||
g.addEdge(binghamton, new_york, 183); | ||
g.addEdge(syracuse, binghamton, 74); | ||
g.addEdge(woodstock, troy, 71); | ||
g.addEdge(woodstock, new_york, 124); | ||
|
||
auto src = woodstock; | ||
auto target = watertown; | ||
|
||
real heuristic(Vertex v) { | ||
auto dx = g[target].lng - g[v].lng; | ||
auto dy = g[target].lat - g[v].lat; | ||
return sqrt(dx * dx + dy * dy); | ||
} | ||
|
||
auto predecessor = new Graph.VertexDescriptor[g.vertexCount]; | ||
|
||
try { | ||
aStarSearch(g, | ||
src, | ||
&heuristic, | ||
g, | ||
predecessor, | ||
AStarGoalVisitor(target)); | ||
} | ||
catch(FoundTarget) { | ||
writeln("Found a path"); | ||
auto path = extractPath(predecessor, target); | ||
|
||
write(g[path[0]].name); | ||
foreach (n; path[1..$]) { | ||
write(" -> ", g[n].name); | ||
} | ||
|
||
writeln(""); | ||
} | ||
} |
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
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
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
Oops, something went wrong.