1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245
|
###############################################################################
# Amna
###############################################################################
"""
N = Niveau (Répondre à N) (De l'état N à ...)
xp = Experience (Data / Données / Cerveau)
P = player (La personne qui jou en ce momant)
"""
### INI ###
terrain = list([0,0,0,0,0,0,0,0,0]) #Map morpion
# Experience / Cerveau
xp = list([])
for a in range(10):
xp.append([])
for b in range(9):
xp[a].append([(b+1),0])
"""
Map morpion :
|1|2|3|
|4|5|6|
|7|8|9|
Plan de la base de donnée de l'IA / Cerveau :
[
Si le joueur adverse choisi 0 (Tu jou en premier) : [[1, 0], [2, 0], [3, 0], [4, 0], [5, 0], [6, 0], [7, 0], [8, 0], [9, 0]],
Si le joueur adverse choisi 1 : [[(Alors jouer 1)>1, (Niveau d'envie de jouer 1)>0], [(Alors jouer 2)>2, (Niveau d'envie de jouer 2)>0], [3, 0], [4, 0], [5, 0], [6, 0], [7, 0], [8, 0], [9, 0]],
Si le joueur adverse choisi 2 : [[1, 0], [2, 0], [3, 0], [4, 0], [5, 0], [6, 0], [7, 0], [8, 0], [9, 0]],
[[1, 0], [2, 0], [3, 0], [4, 0], [5, 0], [6, 0], [7, 0], [8, 0], [9, 0]],
[[1, 0], [2, 0], [3, 0], [4, 0], [5, 0], [6, 0], [7, 0], [8, 0], [9, 0]],
[[1, 0], [2, 0], [3, 0], [4, 0], [5, 0], [6, 0], [7, 0], [8, 0], [9, 0]],
[[1, 0], [2, 0], [3, 0], [4, 0], [5, 0], [6, 0], [7, 0], [8, 0], [9, 0]],
[[1, 0], [2, 0], [3, 0], [4, 0], [5, 0], [6, 0], [7, 0], [8, 0], [9, 0]],
[[1, 0], [2, 0], [3, 0], [4, 0], [5, 0], [6, 0], [7, 0], [8, 0], [9, 0]],
[[1, 0], [2, 0], [3, 0], [4, 0], [5, 0], [6, 0], [7, 0], [8, 0], [9, 0]]
]
"""
### IMP ###
import random
import os #Pour la commande cls de system pour clear l'affichage pour le chargement
### DEF ###
def choisir(N):
randomlist = list([]) #Liste pour tout les choix qui ont la même valleurs qu'il faudra départager grance a l'aleatoire
best = list([0,0])
choix = xp[N].copy() #Copie de la base de donnée pour ne pas la modifier
for i in range(9): # Tri des choix possible
if terrain[8-i] == 1 or terrain[8-i] == 2:
choix.remove(choix[8-i]) #Suppretion des choix impossible
# Recherche du meilleur choix
for i in range(len(choix)):
if choix[i][1] > best[1]:
best = choix[i]
randomlist = list([])
if choix[i][1] == best[1]:
randomlist.append(choix[i])
# Si il y a confrontation alors place au random
if len(randomlist) > 0:
best = randomlist[random.randint(0,(len(randomlist)-1))]
# Return le nom de la case (entre 1 et 9)
return best[0]
def choisirALL(): #Pour choisir premier tour
most = list([])
for N in range(10):
randomlist = list([])
best = list([0,0])
choix = xp[N].copy()
for i in range(9): # Tri des choix possible
if terrain[8-i] == 1 or terrain[8-i] == 2:
choix.remove(choix[8-i])
# Recherche du meilleur choix
for i in range(len(choix)):
if choix[i][1] > best[1]:
best = choix[i]
randomlist = list([])
if choix[i][1] == best[1]:
randomlist.append(choix[i])
# Si il y a confrontation alors place au random
if len(randomlist) > 0:
best = randomlist[random.randint(0,(len(randomlist)-1))]
# Ajoute a la liste des meilleur
most.append(best)
choix = most
for i in range(len(choix)):
if choix[i][1] > best[1]:
best = choix[i]
randomlist = list([])
if choix[i][1] == best[1]:
randomlist.append(choix[i])
# Si il y a confrontation alors place au random
if len(randomlist) > 0:
best = randomlist[random.randint(0,(len(randomlist)-1))]
# Return le nom de la case (entre 1 et 9)
return best[0]
def Gagner():
global terrain
if ((terrain[0]==1 and terrain[1]==1 and terrain[2]==1) or
(terrain[0]==1 and terrain[4]==1 and terrain[8]==1) or
(terrain[0]==1 and terrain[3]==1 and terrain[6]==1) or
(terrain[1]==1 and terrain[4]==1 and terrain[7]==1) or
(terrain[2]==1 and terrain[5]==1 and terrain[8]==1) or
(terrain[2]==1 and terrain[4]==1 and terrain[6]==1) or
(terrain[3]==1 and terrain[4]==1 and terrain[5]==1) or
(terrain[6]==1 and terrain[7]==1 and terrain[8]==1)):
return True
elif ((terrain[0]==2 and terrain[1]==2 and terrain[2]==2) or
(terrain[0]==2 and terrain[4]==2 and terrain[8]==2) or
(terrain[0]==2 and terrain[3]==2 and terrain[6]==2) or
(terrain[1]==2 and terrain[4]==2 and terrain[7]==2) or
(terrain[2]==2 and terrain[5]==2 and terrain[8]==2) or
(terrain[2]==2 and terrain[4]==2 and terrain[6]==2) or
(terrain[3]==2 and terrain[4]==2 and terrain[5]==2) or
(terrain[6]==2 and terrain[7]==2 and terrain[8]==2)):
return True
else:
return False
#############
# Algorytme #
#####################################
learningRate = 0.01 #Learning rate pété
long = 1000000 #Nombre de partie d'apprentissage
barre = list([]) #Barre de chargement
for app in range(long):
if (app/long)*100 == int((app/long)*100):
os.system("cls")
barre.append("|")
print("Apprentisage :",int((app/long)*100),"% /",app,"parties jouées")
print("---------------------------------------------------------------------------------------------") #Top la classe XD
print(''.join(barre))
print("---------------------------------------------------------------------------------------------")
print("")
print("Learning rate :", learningRate)
print("")
print(terrain[0],terrain[1],terrain[2])
print(terrain[3],terrain[4],terrain[5])
print(terrain[6],terrain[7],terrain[8])
print("")
for i in range(9): #Montre les données
print(xp[i])
"""
#Fonction d'avencement du learning rate durant la partie
if learningRate < 1:
learningRate += learningRate/(100000)
elif learningRate > 1:
learningRate = 1
"""
historique = list([]) #Historique de la partie pour la backpropagation
terrain = list([0,0,0,0,0,0,0,0,0]) #Nettoyage du terrain
P = 1 #Joueur 1 commence
### Démarage de la partie ###
for i in range(10):
if sum(terrain) > 12: #Anti bug du terrain remplis
"""
print("BUG : Terrain rempli (Non resolu)")
print(terrain[0],terrain[1],terrain[2])
print(terrain[3],terrain[4],terrain[5])
print(terrain[6],terrain[7],terrain[8])
"""
break
if random.random() > learningRate: #Esay de faire quelque chose avec le learning rate
choix = xp[0].copy()
for i in range(9): # Tri des choix possible
if terrain[8-i] == 1 or terrain[8-i] == 2:
choix.remove(choix[8-i])
choix = choix[random.randint(0,(len(choix)-1))][0] #Choix aléatoire pour cartographier le jeu
else:
if len(historique) > 0: #Savoir si on jou en premier ou pas
choix = choisir(historique[-1])
else:
choix = choisirALL()
historique.append(choix) #Sauvegarde dans l'historique
if P == 1: #Switch entre joueur 1 et 2
terrain[choix-1] = 1
elif P == -1:
terrain[choix-1] = 2
P *= -1
# System de reward surement foireux
if Gagner():
xp[historique[-2]-1][historique[-1]-1][1] = xp[historique[-2]-1][historique[-1]-1][1]+learningRate*(5)
xp[historique[-3]-1][historique[-2]-1][1] = xp[historique[-3]-1][historique[-2]-1][1]+learningRate*(-5)
xp[historique[-4]-1][historique[-3]-1][1] = xp[historique[-4]-1][historique[-3]-1][1]+learningRate*(xp[historique[-2]-1][historique[-1]-1][1]-xp[historique[-4]-1][historique[-3]-1][1])
xp[historique[-5]-1][historique[-4]-1][1] = xp[historique[-5]-1][historique[-4]-1][1]+learningRate*(xp[historique[-3]-1][historique[-2]-1][1]-xp[historique[-5]-1][historique[-4]-1][1])
if not len(historique) > 5: break
xp[historique[-6]-1][historique[-5]-1][1] = xp[historique[-6]-1][historique[-5]-1][1]+learningRate*(xp[historique[-4]-1][historique[-3]-1][1]-xp[historique[-6]-1][historique[-5]-1][1])
if not len(historique) > 6: break
xp[historique[-7]-1][historique[-6]-1][1] = xp[historique[-7]-1][historique[-6]-1][1]+learningRate*(xp[historique[-5]-1][historique[-4]-1][1]-xp[historique[-7]-1][historique[-6]-1][1])
if not len(historique) > 7: break
xp[historique[-8]-1][historique[-7]-1][1] = xp[historique[-8]-1][historique[-7]-1][1]+learningRate*(xp[historique[-6]-1][historique[-5]-1][1]-xp[historique[-8]-1][historique[-7]-1][1])
if not len(historique) > 8: break
xp[historique[-9]-1][historique[-8]-1][1] = xp[historique[-9]-1][historique[-8]-1][1]+learningRate*(xp[historique[-7]-1][historique[-6]-1][1]-xp[historique[-9]-1][historique[-8]-1][1])
if not len(historique) > 9: break
xp[historique[-10]-1][historique[-9]-1][1] = xp[historique[-10]-1][historique[-9]-1][1]+learningRate*(xp[historique[-8]-1][historique[-7]-1][1]-xp[historique[-10]-1][historique[-9]-1][1])
break
if i == 9: #Egalité
xp[historique[-2]-1][historique[-1]-1][1] += learningRate*(0)
xp[historique[-3]-1][historique[-2]-1][1] += learningRate*(0)
xp[historique[-4]-1][historique[-3]-1][1] = xp[historique[-4]-1][historique[-3]-1][1]+learningRate*(xp[historique[-2]-1][historique[-1]-1][1]-xp[historique[-4]-1][historique[-3]-1][1])
xp[historique[-5]-1][historique[-4]-1][1] = xp[historique[-5]-1][historique[-4]-1][1]+learningRate*(xp[historique[-3]-1][historique[-2]-1][1]-xp[historique[-5]-1][historique[-4]-1][1])
xp[historique[-6]-1][historique[-5]-1][1] = xp[historique[-6]-1][historique[-5]-1][1]+learningRate*(xp[historique[-4]-1][historique[-3]-1][1]-xp[historique[-6]-1][historique[-5]-1][1])
xp[historique[-7]-1][historique[-6]-1][1] = xp[historique[-7]-1][historique[-6]-1][1]+learningRate*(xp[historique[-5]-1][historique[-4]-1][1]-xp[historique[-7]-1][historique[-6]-1][1])
xp[historique[-8]-1][historique[-7]-1][1] = xp[historique[-8]-1][historique[-7]-1][1]+learningRate*(xp[historique[-6]-1][historique[-5]-1][1]-xp[historique[-8]-1][historique[-7]-1][1])
xp[historique[-9]-1][historique[-8]-1][1] = xp[historique[-9]-1][historique[-8]-1][1]+learningRate*(xp[historique[-7]-1][historique[-6]-1][1]-xp[historique[-9]-1][historique[-8]-1][1])
xp[historique[-10]-1][historique[-9]-1][1] = xp[historique[-10]-1][historique[-9]-1][1]+learningRate*(xp[historique[-8]-1][historique[-7]-1][1]-xp[historique[-10]-1][historique[-9]-1][1])
break
print("")
print("A vous de jouer")
print("")
print("0 1 2")
print("3 4 5")
print("6 7 8")
print("")
while True:
terrain = list([0,0,0,0,0,0,0,0,0])
for i in range(9):
print("Etat du jeu :")
print(terrain[0],terrain[1],terrain[2])
print(terrain[3],terrain[4],terrain[5])
print(terrain[6],terrain[7],terrain[8])
print("")
ch = int(input("Choix : "))
terrain[ch] = 1
if Gagner():
print("Vous avez gagner :")
print(terrain[0],terrain[1],terrain[2])
print(terrain[3],terrain[4],terrain[5])
print(terrain[6],terrain[7],terrain[8])
break
terrain[choisir(ch)-1] = 2
if Gagner():
print("Vous avez perdu :")
print(terrain[0],terrain[1],terrain[2])
print(terrain[3],terrain[4],terrain[5])
print(terrain[6],terrain[7],terrain[8])
break
print("")
print("-------------------------") |
Partager