Skip to content
Snippets Groups Projects
Commit 869270ea authored by markn92's avatar markn92
Browse files

test

parent 1c096115
No related branches found
No related tags found
No related merge requests found
...@@ -13,7 +13,7 @@ from math import inf ...@@ -13,7 +13,7 @@ from math import inf
import networkx as nx import networkx as nx
from evrouting.T import Node, SoC, Time, Result, EmptyResult, ConsumptionFunction from evrouting.T import Node, SoC, Time, Result, EmptyResult, ConsumptionFunction
from evrouting.utils import PriorityQueue from evrouting.utils import PriorityQueue
from evrouting.graph_tools import distance, consumption from evrouting.graph_tools import distance, consumption, DISTANCE_KEY, CONSUMPTION_KEY
from evrouting.charge.T import SoCFunction, Label from evrouting.charge.T import SoCFunction, Label
from evrouting.charge.utils import LabelPriorityQueue from evrouting.charge.utils import LabelPriorityQueue
from evrouting.charge.factories import ( from evrouting.charge.factories import (
...@@ -161,7 +161,10 @@ def _setup(G: nx.Graph, charging_stations: Set[Node], capacity: SoC, ...@@ -161,7 +161,10 @@ def _setup(G: nx.Graph, charging_stations: Set[Node], capacity: SoC,
# left at t (final_soc). The node becomes the new final node. # left at t (final_soc). The node becomes the new final node.
dummy_final_node: Node = len(G) dummy_final_node: Node = len(G)
G.add_node(dummy_final_node) G.add_node(dummy_final_node)
G.add_edge(t, dummy_final_node, weight=0, c=final_soc) G.add_edge(t, dummy_final_node, **{
DISTANCE_KEY: 0,
CONSUMPTION_KEY: final_soc
})
t = dummy_final_node t = dummy_final_node
# Init factories # Init factories
......
...@@ -72,7 +72,7 @@ class OSMGraph(nx.DiGraph): ...@@ -72,7 +72,7 @@ class OSMGraph(nx.DiGraph):
lat = s['lat'] lat = s['lat']
n = self.find_nearest((lat, lon), distance_limit=500) n = self.find_nearest((lat, lon), distance_limit=500)
if n is not None: if n is not None:
self.nodes[n][CHARGING_COEFFICIENT_KEY] = s['power'] self.nodes[n][CHARGING_COEFFICIENT_KEY] = s['power'] / 3.6 # in Wh/s
S.add(n) S.add(n)
self.charging_stations = S self.charging_stations = S
......
...@@ -8,7 +8,7 @@ from evrouting.T import Result ...@@ -8,7 +8,7 @@ from evrouting.T import Result
from evrouting.osm.imports import read_osm, OSMGraph, HAVERSINE_KEY from evrouting.osm.imports import read_osm, OSMGraph, HAVERSINE_KEY
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 from evrouting.graph_tools import CHARGING_COEFFICIENT_KEY, DISTANCE_KEY, CONSUMPTION_KEY
@pytest.fixture @pytest.fixture
...@@ -99,6 +99,7 @@ def test_shortest_route_dimensions(map_graph): ...@@ -99,6 +99,7 @@ def test_shortest_route_dimensions(map_graph):
def test_charge_shortest_route_dimensions(map_graph): def test_charge_shortest_route_dimensions(map_graph):
"""Full tank is enough to get there. Must be equal to shortest path."""
s = (51.75041438844966, 6.9332313537597665) s = (51.75041438844966, 6.9332313537597665)
t = (51.75657783347559, 7.000350952148438) t = (51.75657783347559, 7.000350952148438)
_s = map_graph.find_nearest(s) _s = map_graph.find_nearest(s)
...@@ -109,7 +110,10 @@ def test_charge_shortest_route_dimensions(map_graph): ...@@ -109,7 +110,10 @@ def test_charge_shortest_route_dimensions(map_graph):
def c(G, u, v): def c(G, u, v):
"""Returns consumption in Wh from u to v.""" """Returns consumption in Wh from u to v."""
return G[u][v][HAVERSINE_KEY] * consumption * 1000 try:
return G[u][v][HAVERSINE_KEY] * consumption
except KeyError:
return G[u][v][CONSUMPTION_KEY]
result = charge.routing.shortest_path( result = charge.routing.shortest_path(
G=map_graph, G=map_graph,
...@@ -122,4 +126,46 @@ def test_charge_shortest_route_dimensions(map_graph): ...@@ -122,4 +126,46 @@ def test_charge_shortest_route_dimensions(map_graph):
c=c c=c
) )
path = shortest_path(map_graph, _s, _t, car)
assert type(result) is Result
assert [n for n, t in result.charge_path] == path
def test_charge_shortest_route_stop(map_graph):
"""Full tank is enough to get there. Must be equal to shortest path."""
s = (51.75041438844966, 6.9332313537597665)
t = (51.75657783347559, 7.000350952148438)
_s = map_graph.find_nearest(s)
_t = map_graph.find_nearest(t)
consumption = 1 # kWh/km
cost_path = 6 * consumption * 1000 # distance * consumption in Wh
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]
result = charge.routing.shortest_path(
G=map_graph,
charging_stations=map_graph.charging_stations,
s=_s,
t=_t,
initial_soc=2000, # > cost_path
final_soc=0,
capacity=10000,
c=c
)
assert type(result) is Result assert type(result) is Result
charge_at = [t for n, t in result.charge_path if t > 0]
# charge once
assert len(charge_at) == 1
# Charge about 10 min
assert charge_at[0] < 600
assert charge_at[0] > 500
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