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
0304a317
Commit
0304a317
authored
5 years ago
by
u214892
Browse files
Options
Downloads
Patches
Plain Diff
use set() instead of list to speed up is_node_in_list (O(n) -> O(1))
parent
504c44d4
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
flatland/envs/env_utils.py
+21
-18
21 additions, 18 deletions
flatland/envs/env_utils.py
with
21 additions
and
18 deletions
flatland/envs/env_utils.py
+
21
−
18
View file @
0304a317
"""
"""
Definition of the RailEnv environment and related level-generation functions.
Definition of the RailEnv environment and related level-generation functions.
Generator functions are functions that take width, height and num_resets as arguments and return
Generator functions are functions that take width, height and num_resets as arguments and return
a GridTransitionMap object.
a GridTransitionMap object.
"""
"""
import
numpy
as
np
import
numpy
as
np
# from flatland.core.env import Environment
# from flatland.core.env import Environment
# from flatland.envs.observations import TreeObsForRailEnv
# from flatland.envs.observations import TreeObsForRailEnv
...
@@ -90,6 +91,9 @@ class AStarNode():
...
@@ -90,6 +91,9 @@ class AStarNode():
def
__eq__
(
self
,
other
):
def
__eq__
(
self
,
other
):
return
self
.
pos
==
other
.
pos
return
self
.
pos
==
other
.
pos
def
__hash__
(
self
):
return
hash
(
self
.
pos
)
def
update_if_better
(
self
,
other
):
def
update_if_better
(
self
,
other
):
if
other
.
g
<
self
.
g
:
if
other
.
g
<
self
.
g
:
self
.
parent
=
other
.
parent
self
.
parent
=
other
.
parent
...
@@ -106,30 +110,29 @@ def a_star(rail_trans, rail_array, start, end):
...
@@ -106,30 +110,29 @@ def a_star(rail_trans, rail_array, start, end):
rail_shape
=
rail_array
.
shape
rail_shape
=
rail_array
.
shape
start_node
=
AStarNode
(
None
,
start
)
start_node
=
AStarNode
(
None
,
start
)
end_node
=
AStarNode
(
None
,
end
)
end_node
=
AStarNode
(
None
,
end
)
open_list
=
[]
open_list
=
set
()
closed_list
=
[]
closed_list
=
set
()
open_list
.
add
(
start_node
)
open_list
.
append
(
start_node
)
# this could be optimized
# this could be optimized
def
is_node_in_list
(
node
,
the_list
):
def
is_node_in_list
(
node
,
the_list
):
for
o_node
in
the_list
:
if
node
in
the_list
:
if
node
==
o_node
:
return
node
return
o_node
return
None
return
None
while
len
(
open_list
)
>
0
:
while
len
(
open_list
)
>
0
:
# get node with current shortest est. path (lowest f)
# get node with current shortest est. path (lowest f)
current_node
=
open_list
[
0
]
current_node
=
None
current_index
=
0
for
index
,
item
in
enumerate
(
open_list
):
for
index
,
item
in
enumerate
(
open_list
):
if
current_node
is
None
:
current_node
=
item
continue
if
item
.
f
<
current_node
.
f
:
if
item
.
f
<
current_node
.
f
:
current_node
=
item
current_node
=
item
current_index
=
index
# pop current off open list, add to closed list
# pop current off open list, add to closed list
open_list
.
pop
(
current_
i
nde
x
)
open_list
.
remove
(
current_n
o
de
)
closed_list
.
a
ppen
d
(
current_node
)
closed_list
.
a
d
d
(
current_node
)
# found the goal
# found the goal
if
current_node
==
end_node
:
if
current_node
==
end_node
:
...
@@ -150,9 +153,9 @@ def a_star(rail_trans, rail_array, start, end):
...
@@ -150,9 +153,9 @@ def a_star(rail_trans, rail_array, start, end):
for
new_pos
in
[(
0
,
-
1
),
(
0
,
1
),
(
-
1
,
0
),
(
1
,
0
)]:
for
new_pos
in
[(
0
,
-
1
),
(
0
,
1
),
(
-
1
,
0
),
(
1
,
0
)]:
node_pos
=
(
current_node
.
pos
[
0
]
+
new_pos
[
0
],
current_node
.
pos
[
1
]
+
new_pos
[
1
])
node_pos
=
(
current_node
.
pos
[
0
]
+
new_pos
[
0
],
current_node
.
pos
[
1
]
+
new_pos
[
1
])
if
node_pos
[
0
]
>=
rail_shape
[
0
]
or
\
if
node_pos
[
0
]
>=
rail_shape
[
0
]
or
\
node_pos
[
0
]
<
0
or
\
node_pos
[
0
]
<
0
or
\
node_pos
[
1
]
>=
rail_shape
[
1
]
or
\
node_pos
[
1
]
>=
rail_shape
[
1
]
or
\
node_pos
[
1
]
<
0
:
node_pos
[
1
]
<
0
:
continue
continue
# validate positions
# validate positions
...
@@ -186,7 +189,7 @@ def a_star(rail_trans, rail_array, start, end):
...
@@ -186,7 +189,7 @@ def a_star(rail_trans, rail_array, start, end):
continue
continue
# add the child to the open list
# add the child to the open list
open_list
.
a
ppen
d
(
child
)
open_list
.
a
d
d
(
child
)
# no full path found
# no full path found
if
len
(
open_list
)
==
0
:
if
len
(
open_list
)
==
0
:
...
@@ -324,7 +327,7 @@ def get_rnd_agents_pos_tgt_dir_on_rail(rail, num_agents):
...
@@ -324,7 +327,7 @@ def get_rnd_agents_pos_tgt_dir_on_rail(rail, num_agents):
for
m
in
valid_movements
:
for
m
in
valid_movements
:
new_position
=
get_new_position
(
agents_position
[
i
],
m
[
1
])
new_position
=
get_new_position
(
agents_position
[
i
],
m
[
1
])
if
m
[
0
]
not
in
valid_starting_directions
and
\
if
m
[
0
]
not
in
valid_starting_directions
and
\
_path_exists
(
rail
,
new_position
,
m
[
0
],
agents_target
[
i
]):
_path_exists
(
rail
,
new_position
,
m
[
0
],
agents_target
[
i
]):
valid_starting_directions
.
append
(
m
[
0
])
valid_starting_directions
.
append
(
m
[
0
])
if
len
(
valid_starting_directions
)
==
0
:
if
len
(
valid_starting_directions
)
==
0
:
...
...
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