Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
Flatland
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
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
sfwatergit
Flatland
Commits
2b292dcc
Commit
2b292dcc
authored
5 years ago
by
u214892
Browse files
Options
Downloads
Patches
Plain Diff
#62 increase unit test coverage
parent
419a8e9c
No related branches found
No related tags found
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
flatland/core/grid/grid4.py
+19
-0
19 additions, 0 deletions
flatland/core/grid/grid4.py
flatland/core/grid/rail_env_grid.py
+0
-13
0 additions, 13 deletions
flatland/core/grid/rail_env_grid.py
tests/test_flatland_core_transitions.py
+46
-6
46 additions, 6 deletions
tests/test_flatland_core_transitions.py
with
65 additions
and
19 deletions
flatland/core/grid/grid4.py
+
19
−
0
View file @
2b292dcc
...
...
@@ -49,6 +49,9 @@ class Grid4Transitions(Transitions):
# row,col delta for each direction
self
.
gDir2dRC
=
np
.
array
([[
-
1
,
0
],
[
0
,
1
],
[
1
,
0
],
[
0
,
-
1
]])
# These bits represent all the possible dead ends
self
.
maskDeadEnds
=
0b0010000110000100
def
get_type
(
self
):
return
np
.
uint16
...
...
@@ -210,3 +213,19 @@ class Grid4Transitions(Transitions):
def
get_direction_enum
(
self
)
->
IntEnum
:
return
Grid4TransitionsEnum
def
has_deadend
(
self
,
cell_transition
):
"""
Checks if one entry can only by exited by a turn-around.
"""
if
cell_transition
&
self
.
maskDeadEnds
>
0
:
return
True
else
:
return
False
def
remove_deadends
(
self
,
cell_transition
):
"""
Remove all turn-arounds (e.g. N-S, S-N, E-W,...).
"""
cell_transition
&=
cell_transition
&
(
~
self
.
maskDeadEnds
)
&
0xffff
return
cell_transition
This diff is collapsed.
Click to expand it.
flatland/core/grid/rail_env_grid.py
+
0
−
13
View file @
2b292dcc
...
...
@@ -43,9 +43,6 @@ class RailEnvTransitions(Grid4Transitions):
transitions
=
self
.
transition_list
)
# These bits represent all the possible dead ends
self
.
maskDeadEnds
=
0b0010000110000100
# create this to make validation faster
self
.
transitions_all
=
set
()
for
index
,
trans
in
enumerate
(
self
.
transitions
):
...
...
@@ -112,13 +109,3 @@ class RailEnvTransitions(Grid4Transitions):
True or False
"""
return
cell_transition
in
self
.
transitions_all
def
has_deadend
(
self
,
cell_transition
):
if
cell_transition
&
self
.
maskDeadEnds
>
0
:
return
True
else
:
return
False
def
remove_deadends
(
self
,
cell_transition
):
cell_transition
&=
cell_transition
&
(
~
self
.
maskDeadEnds
)
&
0xffff
return
cell_transition
This diff is collapsed.
Click to expand it.
tests/test_flatland_core_transitions.py
+
46
−
6
View file @
2b292dcc
...
...
@@ -4,18 +4,20 @@
"""
Tests for `flatland` package.
"""
import
numpy
as
np
from
flatland.core.grid.grid4
import
Grid4Transitions
from
flatland.core.grid.grid8
import
Grid8Transitions
from
flatland.core.grid.rail_env_grid
import
RailEnvTransitions
from
flatland.envs.env_utils
import
validate_new_transition
# remove whitespace in string; keep whitespace below for easier reading
def
rw
(
s
):
return
s
.
replace
(
"
"
,
""
)
def
test_rotate_railenv_transition
():
rail_env_transitions
=
RailEnvTransitions
()
# remove whitespace in string; keep whitespace below for easier reading
def
rw
(
s
):
return
s
.
replace
(
"
"
,
""
)
# TODO test all cases
transition_cycles
=
[
# empty cell - Case 0
...
...
@@ -68,9 +70,17 @@ def test_rotate_railenv_transition():
# int('1100110000110011', 2), \ # noqa: E800
# Case 6 - symmetrical
# int('0101001000000010', 2), \ # noqa: E800
# Case 7 - dead end
# int('0010000000000000', 2), \ # noqa: E800
# Case 7 - dead end
#
#
# |
[
int
(
rw
(
'
0010 0000 0000 0000
'
),
2
),
int
(
rw
(
'
0000 0001 0000 0000
'
),
2
),
int
(
rw
(
'
0000 0000 1000 0000
'
),
2
),
int
(
rw
(
'
0000 0000 0000 0100
'
),
2
),
],
]
for
index
,
cycle
in
enumerate
(
transition_cycles
):
...
...
@@ -206,3 +216,33 @@ def test_diagonal_transitions():
assert
(
diagonal_trans_env
.
rotate_transition
(
south_northeast_transition
,
180
)
==
north_southwest_transition
)
def
test_rail_env_has_deadend
():
deadends
=
set
([
int
(
rw
(
'
0010 0000 0000 0000
'
),
2
),
int
(
rw
(
'
0000 0001 0000 0000
'
),
2
),
int
(
rw
(
'
0000 0000 1000 0000
'
),
2
),
int
(
rw
(
'
0000 0000 0000 0100
'
),
2
)])
ret
=
RailEnvTransitions
()
transitions_all
=
ret
.
transitions_all
for
t
in
transitions_all
:
expected_has_deadend
=
t
in
deadends
actual_had_deadend
=
ret
.
has_deadend
(
t
)
assert
actual_had_deadend
==
expected_has_deadend
,
\
"
{} should be deadend = {}, actual = {}
"
.
format
(
t
,
)
def
test_rail_env_remove_deadend
():
ret
=
Grid4Transitions
([])
rail_env_deadends
=
set
([
int
(
rw
(
'
0010 0000 0000 0000
'
),
2
),
int
(
rw
(
'
0000 0001 0000 0000
'
),
2
),
int
(
rw
(
'
0000 0000 1000 0000
'
),
2
),
int
(
rw
(
'
0000 0000 0000 0100
'
),
2
)])
for
t
in
rail_env_deadends
:
expected_has_deadend
=
0
actual_had_deadend
=
ret
.
remove_deadends
(
t
)
assert
actual_had_deadend
==
expected_has_deadend
,
\
"
{} should be deadend = {}, actual = {}
"
.
format
(
t
,
)
assert
ret
.
remove_deadends
(
int
(
rw
(
'
0010 0001 1000 0100
'
),
2
))
==
0
assert
ret
.
remove_deadends
(
int
(
rw
(
'
0010 0001 1000 0110
'
),
2
))
==
int
(
rw
(
'
0000 0000 0000 0010
'
),
2
)
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