Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
ba
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
markn92
ba
Commits
8fd9de9c
Commit
8fd9de9c
authored
4 years ago
by
markn92
Browse files
Options
Downloads
Patches
Plain Diff
rusable state graph
parent
d9ab9974
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
evrouting/gasstation/routing.py
+32
-12
32 additions, 12 deletions
evrouting/gasstation/routing.py
tests/gasstation/test_transformations.py
+17
-1
17 additions, 1 deletion
tests/gasstation/test_transformations.py
with
49 additions
and
13 deletions
evrouting/gasstation/routing.py
+
32
−
12
View file @
8fd9de9c
...
@@ -20,10 +20,16 @@ def insert_start_node(s: Node,
...
@@ -20,10 +20,16 @@ def insert_start_node(s: Node,
graph_extended
:
nx
.
DiGraph
,
graph_extended
:
nx
.
DiGraph
,
capacity
:
SoC
,
capacity
:
SoC
,
initial_soc
:
SoC
,
initial_soc
:
SoC
,
f
:
AccessFunctions
=
AccessFunctions
()
f
:
AccessFunctions
=
AccessFunctions
(),
added_nodes
=
None
)
->
nx
.
DiGraph
:
)
->
nx
.
DiGraph
:
"""
Insert s into extended graph an create states and edges as necessary.
"""
"""
Insert s into extended graph an create states and edges as necessary.
"""
if
added_nodes
is
None
:
added_nodes
=
[]
added_nodes
.
append
((
s
,
initial_soc
))
graph_extended
.
add_node
((
s
,
initial_soc
))
graph_extended
.
add_node
((
s
,
initial_soc
))
v
:
Node
v
:
Node
for
v
in
gas_stations
:
for
v
in
gas_stations
:
try
:
try
:
...
@@ -38,7 +44,8 @@ def insert_start_node(s: Node,
...
@@ -38,7 +44,8 @@ def insert_start_node(s: Node,
d
=
f
.
path_distance
(
graph_core
,
shortest_p
)
d
=
f
.
path_distance
(
graph_core
,
shortest_p
)
c_v
=
f
.
charging_coefficient
(
graph_core
,
v
)
c_v
=
f
.
charging_coefficient
(
graph_core
,
v
)
g
=
initial_soc
-
w
g
=
initial_soc
-
w
if
(
v
,
g
)
not
in
graph_extended
.
nodes
:
added_nodes
.
append
((
v
,
g
))
graph_extended
.
add_edge
((
s
,
initial_soc
),
(
v
,
g
),
weight
=
d
)
graph_extended
.
add_edge
((
s
,
initial_soc
),
(
v
,
g
),
weight
=
d
)
for
u
in
graph_contracted
.
neighbors
(
v
):
for
u
in
graph_contracted
.
neighbors
(
v
):
c_u
=
f
.
charging_coefficient
(
graph_contracted
,
u
)
c_u
=
f
.
charging_coefficient
(
graph_contracted
,
u
)
...
@@ -66,10 +73,16 @@ def insert_final_node(t: Node,
...
@@ -66,10 +73,16 @@ def insert_final_node(t: Node,
graph_extended
:
nx
.
DiGraph
,
graph_extended
:
nx
.
DiGraph
,
capacity
:
SoC
,
capacity
:
SoC
,
final_soc
:
SoC
,
final_soc
:
SoC
,
f
:
AccessFunctions
=
AccessFunctions
()
f
:
AccessFunctions
=
AccessFunctions
(),
added_nodes
=
None
)
->
nx
.
DiGraph
:
)
->
nx
.
DiGraph
:
"""
Insert terminal node into extended graph an create states and edges as necessary.
"""
"""
Insert terminal node into extended graph an create states and edges as necessary.
"""
if
added_nodes
is
None
:
added_nodes
=
[]
graph_extended
.
add_node
((
t
,
final_soc
))
graph_extended
.
add_node
((
t
,
final_soc
))
added_nodes
.
append
((
t
,
final_soc
))
u
:
Node
u
:
Node
for
u
in
gas_stations
:
for
u
in
gas_stations
:
try
:
try
:
...
@@ -242,6 +255,8 @@ def shortest_path(G: nx.Graph,
...
@@ -242,6 +255,8 @@ def shortest_path(G: nx.Graph,
charge_path
=
[(
n
,
0
)
for
n
in
_path
]
charge_path
=
[(
n
,
0
)
for
n
in
_path
]
)
)
added_nodes
=
[]
contracted_graph
:
nx
.
Graph
=
contracted_graph
or
contract_graph
(
G
,
charging_stations
,
capacity
,
f
)
contracted_graph
:
nx
.
Graph
=
contracted_graph
or
contract_graph
(
G
,
charging_stations
,
capacity
,
f
)
extended_graph
=
extended_graph
or
state_graph
(
contracted_graph
,
capacity
,
f
)
extended_graph
=
extended_graph
or
state_graph
(
contracted_graph
,
capacity
,
f
)
...
@@ -253,7 +268,8 @@ def shortest_path(G: nx.Graph,
...
@@ -253,7 +268,8 @@ def shortest_path(G: nx.Graph,
graph_extended
=
extended_graph
,
graph_extended
=
extended_graph
,
capacity
=
capacity
,
capacity
=
capacity
,
initial_soc
=
initial_soc
,
initial_soc
=
initial_soc
,
f
=
f
f
=
f
,
added_nodes
=
added_nodes
)
)
extended_graph
=
insert_final_node
(
extended_graph
=
insert_final_node
(
...
@@ -263,17 +279,21 @@ def shortest_path(G: nx.Graph,
...
@@ -263,17 +279,21 @@ def shortest_path(G: nx.Graph,
graph_extended
=
extended_graph
,
graph_extended
=
extended_graph
,
capacity
=
capacity
,
capacity
=
capacity
,
final_soc
=
final_soc
,
final_soc
=
final_soc
,
f
=
f
f
=
f
,
added_nodes
=
added_nodes
)
)
try
:
try
:
path
:
List
[
State
]
=
nx
.
shortest_path
(
extended_graph
,
(
s
,
initial_soc
),
(
t
,
final_soc
))
path
:
List
[
State
]
=
nx
.
shortest_path
(
extended_graph
,
(
s
,
initial_soc
),
(
t
,
final_soc
))
except
nx
.
NetworkXNoPath
:
except
nx
.
NetworkXNoPath
:
return
EmptyResult
()
res
=
EmptyResult
()
else
:
res
=
compose_result
(
graph_core
=
G
,
extended_graph
=
extended_graph
,
path
=
path
,
f
=
f
)
return
compose_result
(
extended_graph
.
remove_nodes_from
(
added_nodes
)
graph_core
=
G
,
return
res
extended_graph
=
extended_graph
,
path
=
path
,
f
=
f
)
This diff is collapsed.
Click to expand it.
tests/gasstation/test_transformations.py
+
17
−
1
View file @
8fd9de9c
...
@@ -7,7 +7,8 @@ from evrouting.gasstation.routing import (
...
@@ -7,7 +7,8 @@ from evrouting.gasstation.routing import (
state_graph
,
state_graph
,
insert_final_node
,
insert_final_node
,
insert_start_node
,
insert_start_node
,
compose_result
compose_result
,
shortest_path
)
)
from
evrouting.graph_tools
import
(
from
evrouting.graph_tools
import
(
label
,
label
,
...
@@ -325,6 +326,21 @@ class TestIntegration(Integration):
...
@@ -325,6 +326,21 @@ class TestIntegration(Integration):
assert
len
(
inserted_t
.
edges
)
==
11
assert
len
(
inserted_t
.
edges
)
==
11
assert
inserted_t
.
edges
[
u
,
v
][
'
weight
'
]
==
weight
assert
inserted_t
.
edges
[
u
,
v
][
'
weight
'
]
==
weight
def
test_shortest_path_reset_state_graph
(
self
,
graph
,
graph_config
,
extended_graph
,
contracted_graph
):
before
=
len
(
extended_graph
.
nodes
)
shortest_path
(
G
=
graph
,
charging_stations
=
graph_config
[
'
charging_stations
'
],
s
=
graph_config
[
'
s
'
],
t
=
graph_config
[
'
t
'
],
initial_soc
=
graph_config
[
'
initial_soc
'
],
final_soc
=
graph_config
[
'
final_soc
'
],
capacity
=
graph_config
[
'
capacity
'
],
extended_graph
=
extended_graph
,
contracted_graph
=
contracted_graph
)
assert
before
==
len
(
extended_graph
.
nodes
)
class
TestResult
(
Integration
):
class
TestResult
(
Integration
):
def
test_compose_result
(
self
,
graph
,
inserted_t
):
def
test_compose_result
(
self
,
graph
,
inserted_t
):
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment