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
3bceaab7
Commit
3bceaab7
authored
Jan 18, 2022
by
Philip Maas
Browse files
Options
Downloads
Patches
Plain Diff
Added mutation factor
parent
9a40da64
No related branches found
No related tags found
1 merge request
!1
Evaluations
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
brain.py
+3
-3
3 additions, 3 deletions
brain.py
main.py
+3
-1
3 additions, 1 deletion
main.py
population.py
+4
-3
4 additions, 3 deletions
population.py
walker.py
+4
-3
4 additions, 3 deletions
walker.py
with
14 additions
and
10 deletions
brain.py
+
3
−
3
View file @
3bceaab7
...
...
@@ -31,10 +31,10 @@ class Brain:
clone
.
directions
[
i
]
=
copy
.
copy
(
self
.
directions
[
i
])
return
clone
# mutates the brain by setting some of the directions to random movements
def
mutate
(
self
):
def
mutate
(
self
,
mutation_factor
):
# mutates the brain by setting some of the directions to random movements
for
i
in
range
(
len
(
self
.
directions
)):
if
random
.
random
()
<
0.1
:
# changes roughly 10% of the movements
if
random
.
random
()
<
=
mutation_factor
:
self
.
directions
[
i
]
=
np
.
random
.
uniform
(
-
1
,
1
,
4
)
def
save
(
self
):
...
...
This diff is collapsed.
Click to expand it.
main.py
+
3
−
1
View file @
3bceaab7
...
...
@@ -5,11 +5,13 @@ import matplotlib.pyplot as plt
INCREASE_BY
=
5
BRAIN_SIZE
=
50
POP_SIZE
=
50
MUTATION_FACTOR
=
0.1
# 0 <= x <= 1
GAME_CANCELLED
=
False
LOAD_BRAIN
=
True
RENDER_BEST
=
False
if
__name__
==
'
__main__
'
:
population
=
Population
(
POP_SIZE
,
BRAIN_SIZE
,
LOAD_BRAIN
)
population
=
Population
(
POP_SIZE
,
BRAIN_SIZE
,
MUTATION_FACTOR
,
LOAD_BRAIN
,
RENDER_BEST
)
while
GAME_CANCELLED
is
False
:
# this is our game
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
+
4
−
3
View file @
3bceaab7
...
...
@@ -9,9 +9,10 @@ MAX_STEPS = 1599 # after 1600 steps the Environment gives us a done anyway.
class
Population
:
def
__init__
(
self
,
size
,
brain_size
,
load_brain
):
def
__init__
(
self
,
size
,
brain_size
,
mutation_factor
,
load_brain
,
render_best
):
self
.
size
=
size
self
.
brain_size
=
brain_size
self
.
mutation_factor
=
mutation_factor
self
.
fitness_sum
=
0.0
self
.
gen
=
1
self
.
best_walker_index
=
0
# index of the best player in self.players
...
...
@@ -21,7 +22,7 @@ class Population:
self
.
envs
=
[]
self
.
fitnesses
=
None
for
i
in
range
(
self
.
size
):
self
.
walkers
.
append
(
Walker
(
self
.
brain_size
,
load_brain
))
self
.
walkers
.
append
(
Walker
(
self
.
brain_size
,
load_brain
,
render_best
))
self
.
reset_environments
()
if
load_brain
:
self
.
mutate_babies
()
...
...
@@ -89,7 +90,7 @@ class Population:
def
mutate_babies
(
self
):
# mutates all the brains of the babies
for
i
in
range
(
1
,
len
(
self
.
walkers
)):
# we don't want to mutate the champion's brain
self
.
walkers
[
i
].
brain
.
mutate
()
self
.
walkers
[
i
].
brain
.
mutate
(
self
.
mutation_factor
)
def
set_best_walker
(
self
):
# finds the player with the highest fitness and sets it as the best one
max_index
=
np
.
argmax
(
self
.
fitnesses
)
...
...
This diff is collapsed.
Click to expand it.
walker.py
+
4
−
3
View file @
3bceaab7
...
...
@@ -3,13 +3,14 @@ import gym
class
Walker
:
def
__init__
(
self
,
brain_size
,
load_brain
):
def
__init__
(
self
,
brain_size
,
load_brain
,
render_best
):
self
.
brain
=
Brain
(
brain_size
,
load_brain
)
# new brain with X instructions
self
.
dead
=
False
self
.
reached_goal
=
False
self
.
is_best
=
False
# true if this dot is the best dot from the previous generation
self
.
fitness
=
0.0
self
.
env
=
gym
.
make
(
'
BipedalWalker-v3
'
)
self
.
render_best
=
render_best
# self.pos = copy.copy(self.map.startpoint)
def
update
(
self
):
# moves the dot according to the brains directions
...
...
@@ -25,7 +26,7 @@ class Walker:
elif
done
is
True
:
self
.
reached_goal
=
True
self
.
fitness
+=
10000000
if
self
.
is_best
:
if
self
.
is_best
and
self
.
render_best
:
self
.
env
.
render
()
"""
def get_fitness(self):
...
...
@@ -40,7 +41,7 @@ class Walker:
self
.
env
.
reset
()
def
get_baby
(
self
):
baby
=
Walker
(
0
,
False
)
baby
=
Walker
(
0
,
False
,
self
.
render_best
)
baby
.
brain
=
self
.
brain
.
clone
()
# babies have the same brain as their parents
self
.
env
.
close
()
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