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
f0c8a527
Commit
f0c8a527
authored
5 years ago
by
markn92
Browse files
Options
Downloads
Patches
Plain Diff
sketch, wip
parent
d5391c9f
No related branches found
Branches containing commit
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
+60
-19
60 additions, 19 deletions
evrouting/gasstation/routing.py
tests/gasstation/test_gasstation_routing.py
+2
-1
2 additions, 1 deletion
tests/gasstation/test_gasstation_routing.py
with
62 additions
and
20 deletions
evrouting/gasstation/routing.py
+
60
−
19
View file @
f0c8a527
from
typing
import
Set
,
Callable
,
List
from
typing
import
Set
,
Callable
,
List
,
Any
import
networkx
as
nx
from
evrouting.T
import
Node
,
SoC
from
evrouting.graph_tools
import
CONSUMPTION_KEY
,
DISTANCE_KEY
from
evrouting.graph_tools
import
(
CONSUMPTION_KEY
,
DISTANCE_KEY
,
CHARGING_COEFFICIENT_KEY
)
Path
=
List
[
Node
]
DistFunction
=
Callable
[[
nx
.
Graph
,
Node
,
Node
],
Path
]
def
shortest_path
(
G
:
nx
.
Graph
,
s
,
t
,
b_0
:
float
,
b_t
:
float
,
U
:
float
):
"""
Calculates shortest path using a generalized gas station algorithm.
:param G: Input Graph
:param s: Start Node identifier
:param t: End Node identifier
:param b_0: Start SoC
:param b_t: End SoC
:param U: Capacity
:return:
"""
pass
def
dijkstra
(
G
:
nx
.
Graph
,
u
:
Node
,
v
:
Node
,
weight
:
str
=
'
weight
'
)
->
Path
:
def
dijkstra
(
G
:
nx
.
Graph
,
u
:
Any
,
v
:
Any
,
weight
:
str
=
'
weight
'
)
->
Path
:
return
nx
.
algorithms
.
shortest_path
(
G
,
u
,
v
,
weight
=
weight
)
...
...
@@ -31,6 +17,30 @@ def fold_path(G: nx.Graph, path: Path, weight: str):
return
sum
([
G
.
edges
[
u
,
v
][
weight
]
for
u
,
v
in
zip
(
path
[:
-
1
],
path
[
1
:])])
def
insert_temp_node
(
temp_node
:
Node
,
graph_core
:
nx
.
Graph
,
graph_contracted
:
nx
.
Graph
,
capacity
:
SoC
,
initial_soc
:
SoC
,
dist
:
DistFunction
=
dijkstra
):
# Add cheap charging station
graph_contracted
.
add_node
(
temp_node
,
**
{
CHARGING_COEFFICIENT_KEY
:
0
})
for
cs
in
graph_contracted
.
nodes
:
min_path
=
dist
(
graph_core
,
temp_node
,
cs
)
consumption
=
fold_path
(
graph_core
,
min_path
,
CONSUMPTION_KEY
)
distance
=
fold_path
(
graph_core
,
min_path
,
DISTANCE_KEY
)
if
consumption
<=
initial_soc
:
graph_contracted
.
add_edge
(
temp_node
,
cs
,
**
{
CONSUMPTION_KEY
:
capacity
-
initial_soc
+
consumption
,
DISTANCE_KEY
:
distance
}
)
def
contract_graph
(
G
:
nx
.
Graph
,
charging_stations
:
Set
[
Node
],
capacity
:
SoC
,
dist
:
DistFunction
=
dijkstra
)
->
nx
.
Graph
:
"""
...
...
@@ -56,7 +66,38 @@ def contract_graph(G: nx.Graph, charging_stations: Set[Node], capacity: SoC,
cs
,
n_cs
,
**
{
CONSUMPTION_KEY
:
consumption
,
DISTANCE_KEY
:
fold_path
(
G
,
min_path
,
weight
=
DISTANCE_KEY
)
DISTANCE_KEY
:
fold_path
(
G
,
min_path
,
weight
=
DISTANCE_KEY
)
}
)
return
H
def
extract_graph
(
G
:
nx
.
Graph
,
capacity
:
SoC
)
->
nx
.
Graph
:
pass
def
compose_result
(
G
:
nx
.
Graph
,
path
:
Path
)
->
dict
:
pass
def
shortest_path
(
G
:
nx
.
Graph
,
charging_stations
:
Set
[
Node
],
s
:
Node
,
t
:
Node
,
initial_soc
:
SoC
,
final_soc
:
SoC
,
capacity
:
SoC
)
->
Dict
:
"""
Calculates shortest path using a generalized gas station algorithm.
:param G: Input Graph
:param s: Start Node identifier
:param t: End Node identifier
:param b_0: Start SoC
:param b_t: End SoC
:param U: Capacity
:return:
"""
H
:
nx
.
Graph
=
contract_graph
(
G
,
charging_stations
,
capacity
)
insert_temp_node
(
s
,
G
,
H
,
capacity
,
initial_soc
)
insert_temp_node
(
t
,
G
,
H
,
capacity
,
final_soc
)
extracted_graph
=
extract_graph
(
H
,
capacity
)
path
:
Path
=
dijkstra
(
extract_graph
(
H
,
capacity
),
(
s
,
0
),
(
t
,
0
))
return
compose_result
(
extracted_graph
,
path
)
This diff is collapsed.
Click to expand it.
tests/gasstation/test_gasstation_routing.py
+
2
−
1
View file @
f0c8a527
import
networkx
as
nx
import
pytest
from
evrouting.gasstation.routing
import
contract_graph
,
dijkstra
,
fold_path
from
evrouting.gasstation.routing
import
(
shortest_path
,
contract_graph
,
dijkstra
,
fold_path
)
from
evrouting.graph_tools
import
label
,
CONSUMPTION_KEY
,
DISTANCE_KEY
from
tests.config
import
edge_case
,
get_graph
,
gasstation
,
init_config
...
...
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