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
f2fc90e2
Commit
f2fc90e2
authored
4 years ago
by
markn92
Browse files
Options
Downloads
Patches
Plain Diff
tests
parent
16c827e2
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
evrouting/gasstation/routing.py
+22
-24
22 additions, 24 deletions
evrouting/gasstation/routing.py
tests/config.py
+21
-0
21 additions, 0 deletions
tests/config.py
tests/gasstation/test_transformations.py
+106
-3
106 additions, 3 deletions
tests/gasstation/test_transformations.py
with
149 additions
and
27 deletions
evrouting/gasstation/routing.py
+
22
−
24
View file @
f2fc90e2
...
@@ -23,28 +23,26 @@ def fold_path(G: nx.Graph, path: Path, weight: str):
...
@@ -23,28 +23,26 @@ 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
:])])
return
sum
([
G
.
edges
[
u
,
v
][
weight
]
for
u
,
v
in
zip
(
path
[:
-
1
],
path
[
1
:])])
def
insert_temp_node
(
temp_node
:
Node
,
def
insert_start_node
(
s
:
Node
,
graph_core
:
nx
.
Graph
,
graph_core
:
nx
.
Graph
,
graph_contracted
:
nx
.
Graph
,
gas_stations
:
Set
[
Node
],
capacity
:
SoC
,
graph_extended
:
nx
.
DiGraph
,
initial_soc
:
SoC
,
capacity
:
SoC
,
dist
:
DistFunction
=
dijkstra
initial_soc
:
SoC
,
):
dist
:
DistFunction
=
dijkstra
# Add cheap charging station
)
->
nx
.
DiGraph
:
graph_contracted
.
add_node
(
temp_node
,
**
{
CHARGING_COEFFICIENT_KEY
:
0
})
return
graph_extended
for
cs
in
graph_contracted
.
nodes
:
min_path
=
dist
(
graph_core
,
temp_node
,
cs
)
def
insert_final_node
(
t
:
Node
,
consumption
=
fold_path
(
graph_core
,
min_path
,
CONSUMPTION_KEY
)
graph_core
:
nx
.
Graph
,
distance
=
fold_path
(
graph_core
,
min_path
,
DISTANCE_KEY
)
gas_stations
:
Set
[
Node
],
if
consumption
<=
initial_soc
:
graph_extended
:
nx
.
DiGraph
,
graph_contracted
.
add_edge
(
capacity
:
SoC
,
temp_node
,
cs
,
initial_soc
:
SoC
,
**
{
dist
:
DistFunction
=
dijkstra
CONSUMPTION_KEY
:
capacity
-
initial_soc
+
consumption
,
)
->
nx
.
DiGraph
:
DISTANCE_KEY
:
distance
return
graph_extended
}
)
def
contract_graph
(
G
:
nx
.
Graph
,
charging_stations
:
Set
[
Node
],
capacity
:
SoC
,
def
contract_graph
(
G
:
nx
.
Graph
,
charging_stations
:
Set
[
Node
],
capacity
:
SoC
,
...
@@ -136,8 +134,8 @@ def shortest_path(G: nx.Graph, charging_stations: Set[Node], s: Node, t: Node,
...
@@ -136,8 +134,8 @@ def shortest_path(G: nx.Graph, charging_stations: Set[Node], s: Node, t: Node,
S
:
nx
.
Graph
=
contract_graph
(
G
,
charging_stations
,
capacity
)
S
:
nx
.
Graph
=
contract_graph
(
G
,
charging_stations
,
capacity
)
H
=
state_graph
(
S
,
capacity
)
H
=
state_graph
(
S
,
capacity
)
insert_
temp
_node
(
s
,
G
,
H
,
capacity
,
initial_soc
)
insert_
start
_node
(
s
,
G
,
charging_stations
,
H
,
capacity
,
initial_soc
)
insert_
temp
_node
(
t
,
G
,
H
,
capacity
,
final_soc
)
insert_
final
_node
(
t
,
G
,
charging_stations
,
H
,
capacity
,
final_soc
)
path
:
Path
=
dijkstra
(
H
,
(
s
,
0
),
(
t
,
0
))
path
:
Path
=
dijkstra
(
H
,
(
s
,
0
),
(
t
,
0
))
...
...
This diff is collapsed.
Click to expand it.
tests/config.py
+
21
−
0
View file @
f2fc90e2
...
@@ -93,6 +93,27 @@ edge_case_start_node_no_cs = {
...
@@ -93,6 +93,27 @@ edge_case_start_node_no_cs = {
]
]
}
}
gasstation_complete
=
{
'
beta_s
'
:
0
,
'
beta_t
'
:
0
,
'
U
'
:
4
,
'
s
'
:
0
,
'
t
'
:
2
,
'
nodes
'
:
[
TemplateNode
(),
TemplateNode
(
charging_coeff
=
1
),
TemplateNode
(
charging_coeff
=
2
),
TemplateNode
(),
],
'
edges
'
:
[
TemplateEdge
(
0
,
1
,
distance
=
6
,
consumption
=
3
),
TemplateEdge
(
0
,
2
,
distance
=
2
,
consumption
=
1
),
TemplateEdge
(
1
,
2
,
distance
=
6
,
consumption
=
3
),
TemplateEdge
(
1
,
3
,
distance
=
4
,
consumption
=
2
),
TemplateEdge
(
2
,
3
,
distance
=
8
,
consumption
=
4
),
]
}
def
get_graph
(
config
:
dict
)
->
nx
.
Graph
:
def
get_graph
(
config
:
dict
)
->
nx
.
Graph
:
G
=
nx
.
Graph
()
G
=
nx
.
Graph
()
...
...
This diff is collapsed.
Click to expand it.
tests/gasstation/test_transformations.py
+
106
−
3
View file @
f2fc90e2
import
networkx
as
nx
import
networkx
as
nx
import
pytest
import
pytest
from
evrouting.T
import
ChargingCoefficient
from
evrouting.gasstation.routing
import
(
from
evrouting.gasstation.routing
import
(
contract_graph
,
contract_graph
,
dijkstra
,
dijkstra
,
fold_path
,
fold_path
,
get_possible_arriving_soc
,
get_possible_arriving_soc
,
state_graph
state_graph
,
insert_final_node
,
insert_start_node
)
)
from
evrouting.graph_tools
import
(
from
evrouting.graph_tools
import
(
label
,
label
,
CONSUMPTION_KEY
,
CONSUMPTION_KEY
,
DISTANCE_KEY
,
DISTANCE_KEY
,
CHARGING_COEFFICIENT_KEY
CHARGING_COEFFICIENT_KEY
,
consumption
,
distance
,
charging_cofficient
)
)
from
tests.config
import
edge_case
,
gasstation
,
init_config
from
tests.config
import
edge_case
,
gasstation
,
init_config
,
gasstation_complete
class
TestDjikstra
:
class
TestDjikstra
:
...
@@ -236,3 +242,100 @@ class TestStateGraph(MinimalExamples):
...
@@ -236,3 +242,100 @@ class TestStateGraph(MinimalExamples):
H
:
nx
.
Graph
=
state_graph
(
G
,
self
.
U
)
H
:
nx
.
Graph
=
state_graph
(
G
,
self
.
U
)
assert
len
(
H
.
nodes
)
==
0
assert
len
(
H
.
nodes
)
==
0
assert
len
(
H
.
edges
)
==
0
assert
len
(
H
.
edges
)
==
0
class
TestIntegration
:
@pytest.fixture
def
graph_config
(
self
):
return
init_config
(
gasstation_complete
)
@pytest.fixture
def
graph
(
self
,
graph_config
):
return
graph_config
[
'
G
'
]
@pytest.fixture
def
contracted_graph
(
self
,
graph_config
):
return
contract_graph
(
G
=
graph_config
[
'
G
'
],
charging_stations
=
graph_config
[
'
charging_stations
'
],
capacity
=
graph_config
[
'
capacity
'
]
)
@pytest.fixture
def
extended_graph
(
self
,
contracted_graph
,
graph_config
):
return
state_graph
(
G
=
contracted_graph
,
capacity
=
graph_config
[
'
capacity
'
]
)
@pytest.fixture
def
inserted_s
(
self
,
extended_graph
,
graph_config
):
return
insert_start_node
(
s
=
0
,
graph_core
=
graph_config
[
'
G
'
],
gas_stations
=
graph_config
[
'
charging_stations
'
],
graph_extended
=
extended_graph
,
capacity
=
graph_config
[
'
capacity
'
],
initial_soc
=
4
)
@pytest.fixture
def
inserted_t
(
self
,
inserted_s
,
graph_config
):
return
insert_final_node
(
t
=
3
,
graph_core
=
graph_config
[
'
G
'
],
gas_stations
=
graph_config
[
'
charging_stations
'
],
graph_extended
=
inserted_s
,
capacity
=
graph_config
[
'
capacity
'
],
initial_soc
=
4
)
def
test_contracted_graph
(
self
,
contracted_graph
):
G
=
contracted_graph
assert
len
(
G
.
nodes
)
==
2
assert
len
(
G
.
edges
)
==
1
assert
G
.
edges
[
1
,
2
][
CONSUMPTION_KEY
]
==
3
assert
G
.
edges
[
1
,
2
][
DISTANCE_KEY
]
==
6
@pytest.mark.parametrize
(
'
u, v, weight
'
,
[
((
1
,
0
),
(
2
,
1
),
4
*
1
+
6
),
((
2
,
0
),
(
1
,
0
),
3
*
2
+
6
),
((
2
,
1
),
(
1
,
0
),
2
*
2
+
6
),
])
def
test_extended_graph
(
self
,
extended_graph
,
graph
,
u
,
v
,
weight
):
assert
len
(
extended_graph
.
nodes
)
==
3
assert
len
(
extended_graph
.
edges
)
==
3
assert
extended_graph
.
edges
[
u
,
v
][
'
weight
'
]
==
weight
@pytest.mark.parametrize
(
'
u, v, weight
'
,
[
((
1
,
0
),
(
2
,
1
),
4
*
1
+
6
),
((
2
,
0
),
(
1
,
0
),
3
*
2
+
6
),
((
2
,
1
),
(
1
,
0
),
2
*
2
+
6
),
((
0
,
4
),
(
2
,
3
),
2
),
((
0
,
4
),
(
1
,
1
),
6
),
((
1
,
1
),
(
2
,
1
),
(
4
-
1
)
*
1
+
6
)
])
def
test_s_insert
(
self
,
inserted_s
,
u
,
v
,
weight
):
assert
set
(
inserted_s
.
nodes
)
==
{(
0
,
4
),
(
2
,
3
),
(
1
,
1
),
(
2
,
1
),
(
1
,
0
),
(
2
,
0
)}
assert
len
(
inserted_s
.
edges
)
==
6
assert
inserted_s
.
edges
[
u
,
v
][
'
weight
'
]
==
weight
@pytest.mark.parametrize
(
'
u, v, weight
'
,
[
((
1
,
0
),
(
2
,
1
),
4
*
1
+
6
),
((
2
,
0
),
(
1
,
0
),
3
*
2
+
6
),
((
2
,
1
),
(
1
,
0
),
2
*
2
+
6
),
((
0
,
4
),
(
2
,
3
),
2
),
((
0
,
4
),
(
1
,
1
),
6
),
((
1
,
1
),
(
2
,
1
),
(
4
-
1
)
*
1
+
6
),
((
2
,
3
),
(
3
,
0
),
1
*
2
+
8
),
((
1
,
1
),
(
3
,
0
),
1
*
1
+
4
),
((
2
,
1
),
(
3
,
0
),
3
*
2
+
8
),
((
1
,
0
),
(
3
,
0
),
2
*
1
+
4
),
((
2
,
0
),
(
3
,
0
),
4
*
2
+
8
),
])
def
test_t_insert
(
self
,
inserted_t
,
u
,
v
,
weight
):
assert
set
(
inserted_t
.
nodes
)
==
{(
0
,
4
),
(
2
,
3
),
(
1
,
1
),
(
2
,
1
),
(
1
,
0
),
(
2
,
0
),
(
3
,
0
)}
assert
len
(
inserted_t
.
edges
)
==
11
assert
inserted_t
.
edges
[
u
,
v
][
'
weight
'
]
==
weight
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