Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
M
Machine_Learning
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Deploy
Releases
Package registry
Model registry
Operate
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor 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
Dellandrea Emmanuel
Machine_Learning
Commits
125f00f5
Commit
125f00f5
authored
4 months ago
by
Dellandrea Emmanuel
Browse files
Options
Downloads
Patches
Plain Diff
Ajout TD5
parent
aa4c6ee8
No related branches found
No related tags found
No related merge requests found
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
TD/TD5/ML-TD5.pdf
+0
-0
0 additions, 0 deletions
TD/TD5/ML-TD5.pdf
TD/TD5/q_learning.py
+129
-0
129 additions, 0 deletions
TD/TD5/q_learning.py
with
129 additions
and
0 deletions
TD/TD5/ML-TD5.pdf
0 → 100644
+
0
−
0
View file @
125f00f5
File added
This diff is collapsed.
Click to expand it.
TD/TD5/q_learning.py
0 → 100644
+
129
−
0
View file @
125f00f5
import
numpy
as
np
def
choix_action
(
epsilon
,
s
,
R
,
Q
):
"""
Renvoie l
'
action sélectionnée pour l
'
état s. Epsilon est une valeur seuil entre 0 et 1 permettant
soit de faire de l
'
exploration (si tirage aléatoire supérieur au seuil), soit de faire
de l
'
exploitation (si tirage aléatoire inférieur au seuil)
Parametres
----------
epsilon : seuil (entre 0 et 1) pour choisir entre exploration et exploitation afin de sélectionner une action
s : état courant
R : matrice des récompenses immédiates
Q : matrice représentant la fonction action-valeur
Retour
-------
a : action sélectionnée
"""
#########################################################
##### A compléter (et supprimer l'instruction pass) #####
#########################################################
pass
# return a
def
calcule_q
(
s
,
a
,
alpha
,
gamma
,
R
,
Q
):
"""
Calcule la nouvelle valeur de la fonction action valeur pour l
'
état s et l
'
action a
Parametres
----------
s : état courant
a : action choisie
alpha : vitesse d
'
apprentissage
gamma : facteur d
'
actualisation
R : matrice des récompenses immédiates
Q : matrice représentant la fonction action-valeur
Retour
-------
q : nouvelle valeur de la fonction action valeur pour l
'
état s et l
'
action a
"""
#########################################################
##### A compléter (et supprimer l'instruction pass) #####
#########################################################
pass
# return q
def
politique
(
s
,
Q
):
"""
Détermine la meilleure action connaissant l
'
état S et la fonction action valeur Q
Parametres
----------
s : état courant
Q : matrice représentant la fonction action-valeur
Retour
-------
a : action sélectionnée
"""
#########################################################
##### A compléter (et supprimer l'instruction pass) #####
#########################################################
pass
# return a
# ===================== Partie 1: Initialisation des données =====================
alpha
=
1
gamma
=
0.8
nb_episodes
=
1000
epsilon
=
0.
R
=
[
[
-
1
,
-
1
,
-
1
,
-
1
,
0
,
-
1
],
[
-
1
,
-
1
,
-
1
,
0
,
-
1
,
100
],
[
-
1
,
-
1
,
-
1
,
0
,
-
1
,
-
1
],
[
-
1
,
0
,
0
,
-
1
,
0
,
-
1
],
[
0
,
-
1
,
-
1
,
0
,
-
1
,
100
],
[
-
1
,
0
,
-
1
,
-
1
,
0
,
100
],
]
R
=
np
.
array
(
R
)
# Matrice des récompenses immédiates
etat_final
=
5
Q
=
np
.
zeros
(
R
.
shape
)
# Matrice représentant la fonction action-valeur
nb_etats
=
nb_actions
=
R
.
shape
[
0
]
# ===================== Partie 2: Apprentissage de la fonction action-valeur Q =====================
print
(
"
Apprentissage de la fonction Q
"
)
for
i
in
range
(
nb_episodes
):
print
(
"
Episode :
"
,
i
)
s
=
np
.
random
.
randint
(
0
,
nb_etats
)
print
(
"
Etat initial :
"
,
s
)
while
True
:
a
=
choix_action
(
epsilon
,
s
,
R
,
Q
)
print
(
"
action choisie :
"
,
a
)
Q
[
s
,
a
]
=
calcule_q
(
s
,
a
,
alpha
,
gamma
,
R
,
Q
)
if
s
==
etat_final
:
break
s
=
a
print
(
"
Matrice Q_value :
"
,
Q
)
Q
=
Q
/
np
.
max
(
Q
)
*
100
print
(
"
Matrice Q_value normalisée :
"
,
Q
)
# ===================== Partie 3: Exploitation de la politique d'action apprise =====================
print
(
"
Exploitation
"
)
s
=
2
# choix d'un état initial
print
(
"
Etat initial :
"
,
s
)
while
s
!=
etat_final
:
a
=
politique
(
s
,
Q
)
s
=
a
# nouvel état
print
(
"
nouvel état :
"
,
s
)
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