Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
L
LearningEnvironment
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Deploy
Releases
Container registry
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Armin Co
LearningEnvironment
Commits
e09a4dd0
Commit
e09a4dd0
authored
4 years ago
by
Armin Co
Browse files
Options
Downloads
Patches
Plain Diff
Refactoring
parent
ab185eb3
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
.gitignore
+2
-1
2 additions, 1 deletion
.gitignore
environment_wrapper.py
+11
-8
11 additions, 8 deletions
environment_wrapper.py
main.py
+11
-9
11 additions, 9 deletions
main.py
with
24 additions
and
18 deletions
.gitignore
+
2
−
1
View file @
e09a4dd0
__pycache__
__pycache__
saved_agents
saved_agents
*.png
\ No newline at end of file
This diff is collapsed.
Click to expand it.
environment_wrapper.py
+
11
−
8
View file @
e09a4dd0
...
@@ -24,18 +24,18 @@ def one_episode(environment, agent, render, learn, max_steps=3000):
...
@@ -24,18 +24,18 @@ def one_episode(environment, agent, render, learn, max_steps=3000):
score
=
0
score
=
0
state
=
reset
(
environment
)
state
=
reset
(
environment
)
for
_
in
range
(
max_steps
):
for
_
in
range
(
max_steps
):
action
=
agent
.
get_action
(
state
)
if
render
:
if
render
:
environment
.
render
()
environment
.
render
()
action
=
agent
.
get_action
(
state
)
following_state
,
reward
,
done
,
_
=
step
(
environment
,
action
)
following_state
,
reward
,
done
,
_
=
step
(
environment
,
action
)
agent
.
remember
(
state
,
action
,
reward
,
following_state
,
done
)
agent
.
remember
(
state
,
action
,
reward
,
following_state
,
done
)
score
+=
reward
score
+=
reward
state
=
following_state
state
=
following_state
if
learn
:
if
learn
:
loss
=
agent
.
learn
()
agent
.
learn
()
if
done
:
if
done
:
if
learn
:
if
learn
:
print
(
loss
)
agent
.
learn
(
offline
=
True
)
break
break
return
score
return
score
...
@@ -60,16 +60,19 @@ def run(environment, agent, episodes, render=True, learn=True):
...
@@ -60,16 +60,19 @@ def run(environment, agent, episodes, render=True, learn=True):
score_history
=
[]
score_history
=
[]
avg_score_history
=
[]
avg_score_history
=
[]
for
_
in
tqdm
(
range
(
episodes
),
desc
=
"
Training
"
,
unit
=
"
Episode
"
):
pbar
=
trange
(
episodes
,
desc
=
'
Score [actual, average]: [0, 0]
'
,
unit
=
"
Episodes
"
)
for
_
in
pbar
:
score
=
one_episode
(
environment
,
agent
,
render
,
learn
)
score
=
one_episode
(
environment
,
agent
,
render
,
learn
)
score_history
.
append
(
score
)
score_history
.
append
(
score
)
is_solved
=
np
.
mean
(
score_history
[
-
10
0
:])
is_solved
=
np
.
mean
(
score_history
[
-
5
0
:])
avg_score_history
.
append
(
is_solved
)
avg_score_history
.
append
(
is_solved
)
if
is_solved
>
2
35
and
learn
:
if
is_solved
>
2
00
and
learn
:
break
break
print
(
"
Score [actual, average]: [{0:.2f}, {1:.2f}]
\n
"
.
format
(
score
,
is_solved
))
desc
=
(
"
Score [actual, average]: [{0:.2f}, {1:.2f}]
"
.
format
(
score
,
is_solved
))
pbar
.
set_description
(
desc
)
pbar
.
refresh
()
return
score_history
,
avg_score_history
return
score_history
,
avg_score_history
...
...
This diff is collapsed.
Click to expand it.
main.py
+
11
−
9
View file @
e09a4dd0
...
@@ -6,7 +6,7 @@ import os
...
@@ -6,7 +6,7 @@ import os
import
atexit
import
atexit
import
gym
import
gym
from
agents
import
D
QAgent
from
agents
import
QAgent
import
environment_wrapper
as
ew
import
environment_wrapper
as
ew
# Allow GPU usage or force tensorflow to use the CPU.
# Allow GPU usage or force tensorflow to use the CPU.
...
@@ -21,18 +21,20 @@ if __name__ == '__main__':
...
@@ -21,18 +21,20 @@ if __name__ == '__main__':
env
=
gym
.
make
(
'
LunarLander-v2
'
)
env
=
gym
.
make
(
'
LunarLander-v2
'
)
# 2. Create a learning agent
# 2. Create a learning agent
marvin
=
D
QAgent
(
env
.
action_space
.
n
,
env
.
observation_space
.
shape
[
0
],
'
double_test
'
)
marvin
=
QAgent
(
env
.
action_space
.
n
,
env
.
observation_space
.
shape
[
0
],
'
from_scratch
'
)
# (2.5) *optional* Load agent memory and/or net from disk.
# (2.5) *optional* Load agent memory and/or net from disk.
marvin
.
load
(
'
saved_agents/large_memory/large_memory
'
,
net
=
False
)
LOAD_MEMORIES
=
False
LOAD_ANN
=
False
marvin
.
load
(
'
saved_agents/agent/agent
'
,
net
=
LOAD_ANN
,
memory
=
LOAD_MEMORIES
)
# 3. Set your configurations for the run.
# 3. Set your configurations for the run.
RENDER
=
Tru
e
RENDER
=
Fals
e
LEARNING
=
True
LEARNING
=
True
LEARN_ONLINE
=
Fals
e
LEARN_ONLINE
=
Tru
e
LEARN_OFFLINE
=
Tru
e
LEARN_OFFLINE
=
Fals
e
RUN_EPISODES
=
2
00
RUN_EPISODES
=
5
00
LEARN_OFFLINE_EPOCHS
=
1
00
LEARN_OFFLINE_EPOCHS
=
5
00
SAVE_PATH
=
"
./saved_agents
"
SAVE_PATH
=
"
./saved_agents
"
# Register an *atexit* callback,
# Register an *atexit* callback,
...
@@ -59,4 +61,4 @@ if __name__ == '__main__':
...
@@ -59,4 +61,4 @@ if __name__ == '__main__':
# Show the result of the runl.
# Show the result of the runl.
if
RENDER
:
if
RENDER
:
ew
.
process_logs
(
avg_score
,
loss
)
ew
.
process_logs
(
avg_score
,
loss
,
title
=
marvin
.
name
)
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