-
Notifications
You must be signed in to change notification settings - Fork 81
Added vertex property inspectors. #107
base: master
Are you sure you want to change the base?
Changes from 1 commit
ef73302
9f41e05
7a3dd16
5304455
8c0e900
9821140
7091ea2
298e9a1
91f69aa
8074764
0025cad
52900f3
2056bc2
1175ff0
42af312
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -101,6 +101,50 @@ Both edge types implement the following methods: | |
|
|
||
| A custom edge type ``E{V}`` which is constructible by ``E(index::Int, s::V, t::V)`` and implements the above methods is usable in the ``VectorIncidenceList`` parametric type. Construct such a list with ``inclist(V,E{V})``, where E and V are your vertex and edge types. See test/inclist.jl for an example. | ||
|
|
||
| Vertex Properties | ||
| --------------- | ||
|
|
||
| Many algorithms use a property of an vertex such as amount of a | ||
| resource provided or required by that vertex as input. As the | ||
| algorithms do not mandate any structure for the vertex types, these | ||
| vertex properties can be passed through to the algorithm by an | ||
| ``VertexPropertyInspector``. An ``VertexPropertyInspector`` when | ||
| passed to the ``vertex_property`` method along with an vertex and a | ||
| graph, will return that property of an vertex. | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Minor English nitpick: it's "a vertex" not "an vertex" (only use "an" when the following word starts with a vowel sound). |
||
|
|
||
| All vertex property inspectors should be declared as a subtype of | ||
| ``AbstractVertexPropertyInspector{T}`` where ``T`` is the type of the | ||
| vertex property. The vertex propery inspector should respond to the | ||
| following methods. | ||
|
|
||
| .. py::function:: vertex_property(i, e, g) | ||
|
|
||
| returns the vertex property of vertex ``v`` in graph ``g`` selected by | ||
| inspector ``i``. | ||
|
|
||
| .. py::function:: vertex_property_requirement(i, g) | ||
|
|
||
| checks that graph ``g`` implements the interface(s) necessary for | ||
| inspector ``i`` | ||
|
|
||
| Three vertex property inspectors are provided | ||
| ``ConstantVertexPropertyInspector``, ``VectorVertexPropertyInspector`` and | ||
| ``AttributeVertexPropertyInspector``. | ||
|
|
||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. And |
||
| ``ConstantVertexPropertyInspector(c)`` constructs an vertex property | ||
| inspector that returns the constant ``c`` for each vertex. | ||
|
|
||
| ``VectorVertexPropertyInspector(vec)`` constructs an vertex property | ||
| inspector that returns ``vec[vertex_index(v, g)]``. It requires that | ||
| ``g`` implement the ``vertex_map`` interface. | ||
|
|
||
| ``FunctionVertexPropertyInspector(func)`` constructs an vertex property | ||
| inspector that returns the result of ``func(v)`` from an ``ExVertex``. | ||
| ``AttributeVertexPropertyInspector`` requires that the graph implements | ||
| the ``vertex_map`` interface. | ||
|
|
||
|
|
||
|
|
||
| Edge Properties | ||
| --------------- | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -37,6 +37,13 @@ module Graphs | |
| ConstantEdgePropertyInspector, AttributeEdgePropertyInspector, | ||
| edge_property, edge_property_requirement, | ||
|
|
||
| AbstractVertexPropertyInspector, ConstantVertexPropertyInspector, | ||
| FunctionVertexPropertyInspector, VectorVertexPropertyInspector, | ||
| vertex_property_requirement, vertex_property, | ||
|
|
||
| AbstractVertexColormap, VectorVertexColormap, HashVertexColormap, | ||
| vertex_colormap_requirement, setindex!, getindex, | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As long as ( |
||
|
|
||
| # edge_list | ||
| GenericEdgeList, EdgeList, simple_edgelist, edgelist, | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -20,9 +20,9 @@ export shortest_path | |
| function a_star_impl!{V,D}( | ||
| graph::AbstractGraph{V},# the graph | ||
| frontier, # an initialized heap containing the active vertices | ||
| colormap::Vector{Int}, # an (initialized) color-map to indicate status of vertices | ||
| colormap::AbstractVertexColormap{Int}, # an (initialized) color-map to indicate status of vertices | ||
| edge_dists::AbstractEdgePropertyInspector{D}, # cost of each edge | ||
| heuristic::Function, # heuristic fn (under)estimating distance to target | ||
| heuristic::AbstractVertexPropertyInspector{Int}, # heuristic fn (under)estimating distance to target | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It would be useful to have floating-point weights. Maybe |
||
| t::V) # the end vertex | ||
|
|
||
| while !isempty(frontier) | ||
|
|
@@ -33,16 +33,16 @@ function a_star_impl!{V,D}( | |
|
|
||
| for edge in out_edges(u, graph) | ||
| v = target(edge) | ||
| if colormap[v] < 2 | ||
| colormap[v] = 1 | ||
| if colormap[v,graph] < 2 | ||
| colormap[v,graph] = 1 | ||
| new_path = cat(1, path, edge) | ||
| path_cost = cost_so_far + edge_property(edge_dists, edge, graph) | ||
| enqueue!(frontier, | ||
| (path_cost, new_path, v), | ||
| path_cost + heuristic(v)) | ||
| path_cost + vertex_property(heuristic,v, graph)) | ||
| end | ||
| end | ||
| colormap[u] = 2 | ||
| colormap[u,graph] = 2 | ||
| end | ||
| nothing | ||
| end | ||
|
|
@@ -53,12 +53,17 @@ function shortest_path{V,E,D}( | |
| edge_dists::AbstractEdgePropertyInspector{D}, # cost of each edge | ||
| s::V, # the start vertex | ||
| t::V, # the end vertex | ||
| heuristic::Function = n -> 0) | ||
| heuristic::AbstractVertexPropertyInspector{Int} = ConstantVertexPropertyInspector(0)) | ||
|
|
||
| # heuristic (under)estimating distance to target | ||
| frontier = PriorityQueue{(D,Array{E,1},V),D}() | ||
| frontier[(zero(D), E[], s)] = zero(D) | ||
| colormap = zeros(Int, num_vertices(graph)) | ||
| colormap[s] = 1 | ||
| if implements_vertex_map(graph) | ||
| colormap = VectorVertexColormap{Int}(zeros(Int, num_vertices(graph))) | ||
| else | ||
| colormap = HashVertexColormap{Int}(Dict{V,Int}()) | ||
| end | ||
| colormap[s,graph] = 1 | ||
| a_star_impl!(graph, frontier, colormap, edge_dists, heuristic, t) | ||
| end | ||
|
|
||
|
|
@@ -67,8 +72,10 @@ function shortest_path{V,E,D}( | |
| edge_dists::Vector{D}, # cost of each edge | ||
| s::V, # the start vertex | ||
| t::V, # the end vertex | ||
| heuristic::Function = n -> 0) | ||
| fheuristic::Function = n -> 0) | ||
| edge_len::AbstractEdgePropertyInspector{D} = VectorEdgePropertyInspector(edge_dists) | ||
|
|
||
| heuristic = FunctionVertexPropertyInspector{Int}(fheuristic) | ||
| shortest_path(graph, edge_len, s, t, heuristic) | ||
| end | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"An" on these two lines should also be "a".