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
|
###############################################################################
# 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 i in range(2):
xp.append([])
for a in range(10):
xp[i].append([])
for b in range(9):
xp[i][a].append([(b+1),5])
"""
Map morpion :
|1|2|3|
|4|5|6|
|7|8|9|
Plan de la base de donnée de l'IA / Cerveau TOUT BEAU et avec 2 partie du 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]]
][
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
from os import system #Pour la commande cls de system pour clear l'affichage pour le chargement
from math import exp #Pour la fonction Sigmoïde
### DEF ###
def sigmoid(x): #Magnifique
return 1/(1+exp(-x))
def choisir(J,N): #SO smart :)
global learningRate
global xp
J -= 1 #Pour que ça donne la possition dans la liste [J]
S = 0 #Somme
temp = 0 #Valleur d'ajustement pour la proba
best = list([0,0]) #Pour l'issue rnd = 100 (P=1%)
for i in range(len(xp[J][N])):
S += xp[J][N][i][1]
rnd = random.random()*100
for i in range(len(xp[J][N])):
if rnd >= temp and rnd < temp+xp[J][N][i][1]/S*100/(xp[J][N][i][1]/learningRate):
return xp[J][N][i][1]
temp += xp[J][N][i][1]/S*100/(xp[J][N][i][1]/learningRate)
if xp[J][N][i][1]/S*100/(xp[J][N][i][1]/learningRate) > best[0]:
best[0] = xp[J][N][i][1]/S*100/(xp[J][N][i][1]/learningRate)
best[1] = xp[J][N][i][1]
#print("/!\ Erreur de choix /!\ => Issue possible : rnd = 100")
if rnd == 100:
#print("Issue confirmée : rnd = 100")
return best[1]
print("/!\CRITICAL/!\ => Issue NON confirmée : rnd n'est pas égale a 100")
return best[1]
"""
Ruines :
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(J):
global terrain
if ((terrain[0]==J and terrain[1]==J and terrain[2]==J) or
(terrain[0]==J and terrain[4]==J and terrain[8]==J) or
(terrain[0]==J and terrain[3]==J and terrain[6]==J) or
(terrain[1]==J and terrain[4]==J and terrain[7]==J) or
(terrain[2]==J and terrain[5]==J and terrain[8]==J) or
(terrain[2]==J and terrain[4]==J and terrain[6]==J) or
(terrain[3]==J and terrain[4]==J and terrain[5]==J) or
(terrain[6]==J and terrain[7]==J and terrain[8]==J)):
return True
else:
return False
#############
# Algorytme #
#####################################
learningRate = 0.01
long = 100000 #Nombre de partie d'apprentissage
barre = list([]) #Barre de chargement
for app in range(long):
learingRate = sigmoid(app/100) #WaW
if (app/long)*100 == int((app/long)*100): #View
os.system("cls")
barre.append("|")
print("Apprentisage :",int((app/long)*100),"% /",app,"parties jouées")
print("---------------------------------------------------------------------------------------------") #Top la classe :)
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])
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):
#Jeu#
if len(historique) > 0:
historique.append(choisir(P,historique[-1])) #Attention ne pas faire le con : ce historique[-1] et pas pareil que
terrain[historique[-1]-1] = P #Celui la
else:
historique.append(choisir(P,0)) #P est forcément 1 mais on sait jamais si je suis fou et que g envi que le joueur 2 jou en premier XD
terrain[historique[0]-1] = P
#Inversement du P#
if P == 1:
P = 2
else:
P = 1
#backpropag#
if gagner(1):
"""
Je veut que l'envie evolue avec une fonction sigmoid qui va de 0.00000001 a 9.999... Donc la souce (Dernier choix) = sigmoid(x) (x += 1 a chaque fois que l'ia fini avec ce coup) et que le coup jouer avent evolu de sigmoid(sigmoid(x)) et avent en sigmoid(sigmoid(sigmoid(x)))...
"""
""" Y'a mieux
if xp[0][historique[-3]][historique[-2]] + learningRate > 10: #Bisou
xp[0][historique[-3]][historique[-2]] = 10
else:
xp[0][historique[-3]][historique[-2]] += learningRate
if xp[0][historique[-3]][historique[-2]] - learningRate < 0: #Claque
xp[0][historique[-3]][historique[-2]] = 0
else:
xp[0][historique[-3]][historique[-2]] -= learningRate
""" |
Partager