Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
Bipedal Walker Evo
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
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Philip Maas
Bipedal Walker Evo
Commits
d94e96c2
Commit
d94e96c2
authored
Jan 18, 2022
by
Philip Maas
Browse files
Options
Downloads
Patches
Plain Diff
Added functionality to load and save a brain
parent
3a796e17
Branches
Branches containing commit
No related tags found
1 merge request
!1
Evaluations
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
brain.py
+15
-4
15 additions, 4 deletions
brain.py
main.py
+2
-3
2 additions, 3 deletions
main.py
population.py
+9
-10
9 additions, 10 deletions
population.py
walker.py
+9
-5
9 additions, 5 deletions
walker.py
with
35 additions
and
22 deletions
brain.py
+
15
−
4
View file @
d94e96c2
import
numpy
as
np
import
numpy
as
np
import
random
import
random
import
copy
import
copy
import
pickle
class
Brain
:
class
Brain
:
def
__init__
(
self
,
size
):
def
__init__
(
self
,
size
,
load_brain
):
self
.
directions
=
[]
self
.
directions
=
[]
self
.
step
=
0
self
.
step
=
0
if
load_brain
:
self
.
load
()
else
:
self
.
increase_moves
(
size
)
self
.
increase_moves
(
size
)
def
get_move
(
self
):
def
get_move
(
self
):
...
@@ -22,7 +26,7 @@ class Brain:
...
@@ -22,7 +26,7 @@ class Brain:
# returns a copy of the given brain
# returns a copy of the given brain
def
clone
(
self
):
def
clone
(
self
):
clone
=
Brain
(
len
(
self
.
directions
))
clone
=
Brain
(
len
(
self
.
directions
)
,
False
)
for
i
in
range
(
len
(
self
.
directions
)):
for
i
in
range
(
len
(
self
.
directions
)):
clone
.
directions
[
i
]
=
copy
.
copy
(
self
.
directions
[
i
])
clone
.
directions
[
i
]
=
copy
.
copy
(
self
.
directions
[
i
])
return
clone
return
clone
...
@@ -33,8 +37,15 @@ class Brain:
...
@@ -33,8 +37,15 @@ class Brain:
if
random
.
random
()
<
0.1
:
# changes roughly 10% of the movements
if
random
.
random
()
<
0.1
:
# changes roughly 10% of the movements
self
.
directions
[
i
]
=
np
.
random
.
uniform
(
-
1
,
1
,
4
)
self
.
directions
[
i
]
=
np
.
random
.
uniform
(
-
1
,
1
,
4
)
def
save
(
self
):
with
open
(
'
best_brain
'
,
'
wb
'
)
as
fp
:
pickle
.
dump
(
self
.
directions
,
fp
)
def
load
(
self
):
with
open
(
'
best_brain
'
,
'
rb
'
)
as
fp
:
self
.
directions
=
pickle
.
load
(
fp
)
if
__name__
==
'
__main__
'
:
# for debugging
if
__name__
==
'
__main__
'
:
# for debugging
brain_inst
=
Brain
(
100
)
brain_inst
=
Brain
(
100
,
True
)
print
(
brain_inst
.
directions
)
print
(
brain_inst
.
directions
)
print
(
len
(
brain_inst
.
directions
))
print
(
len
(
brain_inst
.
directions
))
This diff is collapsed.
Click to expand it.
main.py
+
2
−
3
View file @
d94e96c2
import
gym
from
population
import
Population
from
population
import
Population
import
time
import
time
import
numpy
as
np
import
matplotlib.pyplot
as
plt
import
matplotlib.pyplot
as
plt
INCREASE_BY
=
5
INCREASE_BY
=
5
BRAIN_SIZE
=
50
BRAIN_SIZE
=
50
POP_SIZE
=
50
POP_SIZE
=
50
GAME_CANCELLED
=
False
GAME_CANCELLED
=
False
LOAD_BRAIN
=
True
if
__name__
==
'
__main__
'
:
if
__name__
==
'
__main__
'
:
population
=
Population
(
POP_SIZE
,
BRAIN_SIZE
)
population
=
Population
(
POP_SIZE
,
BRAIN_SIZE
,
LOAD_BRAIN
)
while
GAME_CANCELLED
is
False
:
# this is our game
while
GAME_CANCELLED
is
False
:
# this is our game
if
population
.
all_players_finished
():
# this is our genetic algorithm after one generation of players
if
population
.
all_players_finished
():
# this is our genetic algorithm after one generation of players
...
...
This diff is collapsed.
Click to expand it.
population.py
+
9
−
10
View file @
d94e96c2
import
numpy
as
np
import
numpy
as
np
from
walker
import
Walker
import
gym
import
random
import
random
import
logging
import
logging
import
copy
import
copy
from
walker
import
Walker
MAX_STEPS
=
1
0000
MAX_STEPS
=
1
599
# after 1600 steps the Environment gives us a done anyway.
class
Population
:
class
Population
:
def
__init__
(
self
,
size
,
brain_size
):
def
__init__
(
self
,
size
,
brain_size
,
load_brain
):
self
.
size
=
size
self
.
size
=
size
self
.
brain_size
=
brain_size
self
.
brain_size
=
brain_size
self
.
fitness_sum
=
0.0
self
.
fitness_sum
=
0.0
...
@@ -23,13 +21,14 @@ class Population:
...
@@ -23,13 +21,14 @@ class Population:
self
.
envs
=
[]
self
.
envs
=
[]
self
.
fitnesses
=
None
self
.
fitnesses
=
None
for
i
in
range
(
self
.
size
):
for
i
in
range
(
self
.
size
):
self
.
envs
.
append
(
gym
.
make
(
'
BipedalWalker-v3
'
))
self
.
walkers
.
append
(
Walker
(
self
.
brain_size
,
load_brain
))
self
.
walkers
.
append
(
Walker
(
self
.
envs
[
i
],
self
.
brain_size
))
self
.
reset_environments
()
self
.
reset_environments
()
if
load_brain
:
self
.
mutate_babies
()
def
reset_environments
(
self
):
def
reset_environments
(
self
):
for
env
in
self
.
env
s
:
for
walker
in
self
.
walker
s
:
env
.
rese
t
()
walker
.
reset_environmen
t
()
def
update
(
self
):
def
update
(
self
):
for
walker
in
self
.
walkers
:
for
walker
in
self
.
walkers
:
...
@@ -62,7 +61,7 @@ class Population:
...
@@ -62,7 +61,7 @@ class Population:
#new_walkers.append(Walker(self.envs[i], self.brain_size))
#new_walkers.append(Walker(self.envs[i], self.brain_size))
self
.
calculate_fitness_sum
()
self
.
calculate_fitness_sum
()
self
.
set_best_walker
()
self
.
set_best_walker
()
self
.
walkers
[
self
.
best_walker_index
].
brain
.
save
()
# the champion lives on
# the champion lives on
new_walkers
=
[
self
.
walkers
[
self
.
best_walker_index
].
get_baby
()]
new_walkers
=
[
self
.
walkers
[
self
.
best_walker_index
].
get_baby
()]
new_walkers
[
0
].
is_best
=
True
new_walkers
[
0
].
is_best
=
True
...
...
This diff is collapsed.
Click to expand it.
walker.py
+
9
−
5
View file @
d94e96c2
from
brain
import
Brain
from
brain
import
Brain
import
gym
class
Walker
:
class
Walker
:
def
__init__
(
self
,
env
,
brain_size
):
def
__init__
(
self
,
brain_size
,
load_brain
):
self
.
brain
=
Brain
(
brain_size
)
# new brain with X instructions
self
.
brain
=
Brain
(
brain_size
,
load_brain
)
# new brain with X instructions
self
.
dead
=
False
self
.
dead
=
False
self
.
reached_goal
=
False
self
.
reached_goal
=
False
self
.
is_best
=
False
# true if this dot is the best dot from the previous generation
self
.
is_best
=
False
# true if this dot is the best dot from the previous generation
self
.
fitness
=
0.0
self
.
fitness
=
0.0
self
.
env
=
env
self
.
env
=
gym
.
make
(
'
BipedalWalker-v3
'
)
# self.pos = copy.copy(self.map.startpoint)
# self.pos = copy.copy(self.map.startpoint)
def
update
(
self
):
# moves the dot according to the brains directions
def
update
(
self
):
# moves the dot according to the brains directions
...
@@ -36,7 +36,11 @@ class Walker:
...
@@ -36,7 +36,11 @@ class Walker:
self.fitness = 1 / (self.map.get_closest_distance(self.pos[X], self.pos[Y]) ** 2)
self.fitness = 1 / (self.map.get_closest_distance(self.pos[X], self.pos[Y]) ** 2)
return self.fitness
"""
return self.fitness
"""
def
reset_environment
(
self
):
self
.
env
.
reset
()
def
get_baby
(
self
):
def
get_baby
(
self
):
baby
=
Walker
(
self
.
env
,
0
)
baby
=
Walker
(
0
,
False
)
baby
.
brain
=
self
.
brain
.
clone
()
# babies have the same brain as their parents
baby
.
brain
=
self
.
brain
.
clone
()
# babies have the same brain as their parents
self
.
env
.
close
()
return
baby
return
baby
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