Skip to content
Snippets Groups Projects
Commit 1223d498 authored by markn92's avatar markn92
Browse files

fixed return type

parent 869270ea
No related branches found
No related tags found
No related merge requests found
...@@ -12,6 +12,7 @@ LABEL_KEY = 'label' ...@@ -12,6 +12,7 @@ LABEL_KEY = 'label'
CHARGING_COEFFICIENT_KEY = 'c' CHARGING_COEFFICIENT_KEY = 'c'
DISTANCE_KEY = 'weight' DISTANCE_KEY = 'weight'
CONSUMPTION_KEY = 'c' CONSUMPTION_KEY = 'c'
HAVERSINE_KEY = 'haversine'
def node_convert(n: TemplateNode) -> NodeData: def node_convert(n: TemplateNode) -> NodeData:
...@@ -36,3 +37,7 @@ def charging_cofficient(G: nx.Graph, n: Node) -> ChargingCoefficient: ...@@ -36,3 +37,7 @@ def charging_cofficient(G: nx.Graph, n: Node) -> ChargingCoefficient:
def label(G: nx.Graph, u: Node) -> str: def label(G: nx.Graph, u: Node) -> str:
return G.nodes[u][LABEL_KEY] return G.nodes[u][LABEL_KEY]
def sum_weights(G, path, weight) -> float:
return sum(G[u][v][weight] for u, v in zip(path[:-1], path[1:]))
...@@ -19,15 +19,13 @@ import itertools ...@@ -19,15 +19,13 @@ import itertools
import networkx as nx import networkx as nx
import rtree import rtree
from evrouting.graph_tools import CHARGING_COEFFICIENT_KEY, DISTANCE_KEY from evrouting.graph_tools import CHARGING_COEFFICIENT_KEY, DISTANCE_KEY, HAVERSINE_KEY
from evrouting.osm.const import ms_to_kmh from evrouting.osm.const import ms_to_kmh
from evrouting.osm.profiles import speed from evrouting.osm.profiles import speed
from evrouting.osm.routing import point, haversine_distance from evrouting.osm.routing import point, haversine_distance
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
HAVERSINE_KEY = 'haversine'
class OSMGraph(nx.DiGraph): class OSMGraph(nx.DiGraph):
""" """
......
...@@ -3,6 +3,10 @@ from math import radians, cos, sin, asin, sqrt ...@@ -3,6 +3,10 @@ from math import radians, cos, sin, asin, sqrt
import networkx as nx import networkx as nx
from evrouting.T import ConsumptionFunction, Result, EmptyResult
from evrouting.graph_tools import (
DISTANCE_KEY, HAVERSINE_KEY, CONSUMPTION_KEY, sum_weights
)
from evrouting.osm.const import ms_to_kmh from evrouting.osm.const import ms_to_kmh
lat = float lat = float
...@@ -30,7 +34,7 @@ def haversine_distance(lon1, lat1, lon2, lat2, unit_m=True): ...@@ -30,7 +34,7 @@ def haversine_distance(lon1, lat1, lon2, lat2, unit_m=True):
return c * r return c * r
def shortest_path(G, s: point, t: point, profile): def shortest_path(G, s: point, t: point, profile) -> Result:
"""Calc A* shortest path.""" """Calc A* shortest path."""
def dist(u, v): def dist(u, v):
...@@ -42,7 +46,15 @@ def shortest_path(G, s: point, t: point, profile): ...@@ -42,7 +46,15 @@ def shortest_path(G, s: point, t: point, profile):
unit_m=True unit_m=True
) / profile['maxspeed'] * ms_to_kmh ) / profile['maxspeed'] * ms_to_kmh
return nx.astar_path(G, s, t, heuristic=dist) try:
path = nx.astar_path(G, s, t, heuristic=dist)
except nx.NetworkXNoPath:
return EmptyResult()
return Result(
trip_time=sum_weights(G, path, DISTANCE_KEY),
charge_path=[(n, 0) for n in path]
)
def to_coordinates(G, path): def to_coordinates(G, path):
...@@ -59,3 +71,19 @@ def to_coordinates(G, path): ...@@ -59,3 +71,19 @@ def to_coordinates(G, path):
return lon, lat return lon, lat
return list(map(get_coordinates, path)) return list(map(get_coordinates, path))
def consumption_function_factory(consumption: float) -> ConsumptionFunction:
"""
:param consumption: in kWh/km
:return: consi
"""
def c(G, u, v):
"""Returns consumption in Wh from u to v."""
try:
return G[u][v][HAVERSINE_KEY] * consumption
except KeyError:
return G[u][v][CONSUMPTION_KEY]
return c
...@@ -5,10 +5,12 @@ import pytest ...@@ -5,10 +5,12 @@ import pytest
from evrouting import charge from evrouting import charge
from evrouting.T import Result from evrouting.T import Result
from evrouting.osm.imports import read_osm, OSMGraph, HAVERSINE_KEY from evrouting.osm.imports import read_osm, OSMGraph
from evrouting.osm.profiles import car from evrouting.osm.profiles import car
from evrouting.osm.routing import shortest_path from evrouting.osm.routing import shortest_path
from evrouting.graph_tools import CHARGING_COEFFICIENT_KEY, DISTANCE_KEY, CONSUMPTION_KEY from evrouting.graph_tools import (
CHARGING_COEFFICIENT_KEY, DISTANCE_KEY, CONSUMPTION_KEY, HAVERSINE_KEY
)
@pytest.fixture @pytest.fixture
...@@ -48,7 +50,7 @@ def test_insert_charging_stations_close(graph): ...@@ -48,7 +50,7 @@ def test_insert_charging_stations_close(graph):
graph.insert_charging_stations(S) graph.insert_charging_stations(S)
assert graph.nodes[0][CHARGING_COEFFICIENT_KEY] == 22.0 assert graph.nodes[0][CHARGING_COEFFICIENT_KEY] == 22.0 / 3.6
assert CHARGING_COEFFICIENT_KEY not in graph.nodes[1] assert CHARGING_COEFFICIENT_KEY not in graph.nodes[1]
...@@ -58,7 +60,7 @@ def test_insert_charging_stations_eq(graph): ...@@ -58,7 +60,7 @@ def test_insert_charging_stations_eq(graph):
graph.insert_charging_stations(S) graph.insert_charging_stations(S)
assert graph.nodes[0][CHARGING_COEFFICIENT_KEY] == 22.0 assert graph.nodes[0][CHARGING_COEFFICIENT_KEY] == 22.0 / 3.6
assert CHARGING_COEFFICIENT_KEY not in graph.nodes[1] assert CHARGING_COEFFICIENT_KEY not in graph.nodes[1]
...@@ -77,8 +79,8 @@ def test_shortest_route(map_graph): ...@@ -77,8 +79,8 @@ def test_shortest_route(map_graph):
"4955446051", "4955446051",
"418009799" "418009799"
] ]
result = shortest_path(map_graph, _s, _t, car)
assert route == shortest_path(map_graph, _s, _t, car) assert route == [n for n, t in result.charge_path]
def test_shortest_route_dimensions(map_graph): def test_shortest_route_dimensions(map_graph):
...@@ -87,8 +89,8 @@ def test_shortest_route_dimensions(map_graph): ...@@ -87,8 +89,8 @@ def test_shortest_route_dimensions(map_graph):
_s = map_graph.find_nearest(s) _s = map_graph.find_nearest(s)
_t = map_graph.find_nearest(t) _t = map_graph.find_nearest(t)
path = shortest_path(map_graph, _s, _t, car) result = shortest_path(map_graph, _s, _t, car)
path = [n for n, t in result.charge_path]
time = sum([map_graph[u][v][DISTANCE_KEY] for u, v in zip(path[:-1], path[1:])]) time = sum([map_graph[u][v][DISTANCE_KEY] for u, v in zip(path[:-1], path[1:])])
distance = sum([map_graph[u][v][HAVERSINE_KEY] for u, v in zip(path[:-1], path[1:])]) distance = sum([map_graph[u][v][HAVERSINE_KEY] for u, v in zip(path[:-1], path[1:])])
...@@ -129,7 +131,7 @@ def test_charge_shortest_route_dimensions(map_graph): ...@@ -129,7 +131,7 @@ def test_charge_shortest_route_dimensions(map_graph):
path = shortest_path(map_graph, _s, _t, car) path = shortest_path(map_graph, _s, _t, car)
assert type(result) is Result assert type(result) is Result
assert [n for n, t in result.charge_path] == path assert result.charge_path == path.charge_path
def test_charge_shortest_route_stop(map_graph): def test_charge_shortest_route_stop(map_graph):
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment