Commit 7aa6799c authored by LARIVIERE Axel's avatar LARIVIERE Axel
Browse files

ajout SARSA

parent 07f5446a
......@@ -2,26 +2,35 @@ import sys
from abc import abstractmethod
import time
import numpy as np
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
import random
import array as ar
class Game:
boardGame = [1, 1, 1, 1, 2, 2, 2, 2,
0] # boardGame[0-7] : kewai, boardGame[8] : putahi, 1 : pion joueur 1, 2 : pion joueur 2, 0 : vide
actualPlayer = 1 # 1 for player 1, 2 for player 2
actualPlayer = 1 # 1 for player 1, 2 for player 2
winner = 0
def __init__(self, joueur1, joueur2):
self.joueur1 = joueur1
self.joueur2 = joueur2
self.joueur1.setPlayerNumber(1)
self.joueur2.setPlayerNumber(2)
self.window = Window(self)
self.gameManager()
input()#fin du jeu
input() # fin du jeu
def getBoardGame(self):
return self.boardGame
def gameManager(self):
while(not self.isEndGame(self.actualPlayer)):
while (not self.isEndGame(self.actualPlayer)):
self.window.drawTheScene()
if self.actualPlayer == 1:
self.joueur1.takeDecision(self)
......@@ -29,41 +38,47 @@ class Game:
else:
self.joueur2.takeDecision(self)
self.actualPlayer = 1
self.boardGame = [1, 1, 1, 1, 2, 2, 2, 2, 0]
self.winner = 0
self.actualPlayer = 1
self.window.drawTheScene()
self.endGame()
def isEndGame(self,nextPlayer): #est ce la fin du jeu? est ce que le prochain joueur peut jouer?
for i in range(9): #pour chaque emplacement de pions
if(self.boardGame[i]==nextPlayer): #si c'est un pion au prochain joueur, peut il se déplacer?
if(self.emptyPlaceToMove(i)!=-1):
def isEndGame(self, nextPlayer): # est ce la fin du jeu? est ce que le prochain joueur peut jouer?
for i in range(9): # pour chaque emplacement de pions
if (self.boardGame[i] == nextPlayer): # si c'est un pion au prochain joueur, peut il se déplacer?
if (self.emptyPlaceToMove(i) != -1):
return False
return True #si aucun déplacement est possible, fin du jeu
return True # si aucun déplacement est possible, fin du jeu
def endGame(self):
if self.actualPlayer==1 :
winner=2
if self.actualPlayer == 1:
self.winner = 2
else:
winner=1
print("Player "+str(winner)+" win!")
self.winner = 1
print("Player " + str(self.winner) + " win!")
def movePawn(self, positionToMove):
positionToMove = positionToMove-1 # pour la position dans le tableau
positionToMove = positionToMove - 1 # pour la position dans le tableau
newPosition = self.emptyPlaceToMove(positionToMove)
if newPosition == -1: #si aucune place n'a été trouvé ou possible
if newPosition == -1: # si aucune place n'a été trouvé ou possible
return False
else : #on bouge le pion, son ancienne case est maintenant vide
else: # on bouge le pion, son ancienne case est maintenant vide
self.boardGame[newPosition] = self.boardGame[positionToMove]
self.boardGame[positionToMove] = 0
return True
def emptyPlaceToMove(self, positionToMove): #-1 if impossible
if not self.boardGame[positionToMove] == self.actualPlayer: return -1 #si on essaie de bouger autre chose que un de ses pions : False
if self.boardGame[8] == 0: # si la place au milieu est libre
def emptyPlaceToMove(self, positionToMove): # -1 if impossible
if not self.boardGame[
positionToMove] == self.actualPlayer: return -1 # si on essaie de bouger autre chose que un de ses pions : False
if self.boardGame[8] == 0: # si la place au milieu est libre
# seulement si un point adverse autours du joueur est présent
if (self.boardGame[(positionToMove+1)%8] == self.actualPlayer) and (self.boardGame[(positionToMove-1)%8] == self.actualPlayer): return -1
if (self.boardGame[(positionToMove + 1) % 8] == self.actualPlayer) and (
self.boardGame[(positionToMove - 1) % 8] == self.actualPlayer): return -1
return 8
else :
if(positionToMove == 8) : # on bouge le pion du milieu, on cherche la case vide
else:
if (positionToMove == 8): # on bouge le pion du milieu, on cherche la case vide
for x in range(8):
if self.boardGame[x] == 0:
return x
......@@ -73,38 +88,107 @@ class Game:
if self.boardGame[(positionToMove - 1) % 8] == 0: return (positionToMove - 1) % 8
return -1
class Player():
position = 0
@abstractmethod
def takeDecision(self,game):
def takeDecision(self, game):
pass
def setPlayerNumber(self, position):
self.position = position
class RealPlayer(Player):
def takeDecision(self,game):
print("\nAu tour du joueur "+str(game.actualPlayer)+", a vous de jouer")
def takeDecision(self, game):
print("\nAu tour du joueur " + str(game.actualPlayer) + ", a vous de jouer")
print("Voici le plateau : ")
for x in range(9):
print("position "+str(x+1)+" : "+str(game.boardGame[x]))
inputUser=0
impossibleMove=0
while(inputUser==0 or impossibleMove==1):
impossibleMove=0
print("position " + str(x + 1) + " : " + str(game.boardGame[x]))
inputUser = 0
impossibleMove = 0
while (inputUser == 0 or impossibleMove == 1):
impossibleMove = 0
try:
inputUser = int(input("Quelle position voulez vous bouger ?"))
except ValueError:
inputUser=0
inputUser = 0
print("Mauvaise saisie, veuillez entrer un nombre numérique")
if(game.movePawn(inputUser) == False):
impossibleMove=1 #le déplacement est impossible
if (game.movePawn(inputUser) == False):
impossibleMove = 1 # le déplacement est impossible
print("Déplacement impossible, veuillez choisir un autre pion")
class Agent(Player):
def takeDecision(self,game):
print("\nAu tour du joueur "+str(game.actualPlayer)+", l'IA joue")
pass #TODO IA
class Window(QMainWindow):
def greedy(self, Q, s):
return np.argmax(Q[s])
def eps_greedy(self, Q, s, eps):
if random.random() < eps:
return np.random.randint(Q.shape[1])
else:
return self.greedy(Q, s)
def SARSA(self, game, lr=0.01, num_episode=10000, eps=0.5, gamma=0.95, eps_decay=0.001):
n_a = ar.array[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
n_s = game.getBoardGame()
Q = np.zeros((n_s, n_a))
for ep in range(num_episode):
actual_board = game.getBoardGame()
played = False
total_reward = 0
if eps > 0.01:
eps -= eps_decay
index=0
action = self.eps_greedy(Q, actual_board, eps)
while not played:
played = game.movePawn(action)
new_board_game = game.getBoardGame()
new_action = self.eps_greedy(Q, new_board_game, eps)
if not played:
reward = -10
total_reward += reward
Q[actual_board][action] = Q[actual_board][action] + lr * (
reward + gamma * Q[new_board_game][new_action] - Q[actual_board][action])
actual_board = new_board_game
action = new_action
reward = 20
total_reward += reward
Q[actual_board][action] = Q[actual_board][action] + lr * (
reward + gamma * Q[new_board_game][new_action] - Q[actual_board][action])
if (game.winner == 1 and self.position == 1) or (game.winner == 2 and self.position == 2):
reward = 200
total_reward += reward
Q[actual_board][action] = Q[actual_board][action] + lr * (
reward + gamma * Q[new_board_game][new_action] - Q[actual_board][action])
elif (game.winner == 1 and self.position == 2) or (game.winner == 2 and self.position == 1):
reward = -100
total_reward += reward
Q[actual_board][action] = Q[actual_board][action] + lr * (
reward + gamma * Q[new_board_game][new_action] - Q[actual_board][action])
if (ep % 300) == 0:
print("Episode:{:5d} Eps:{:2.4f} Rew:{:2.4f}".format(ep, eps, total_reward))
def takeDecision(self, game):
print("\nAu tour du joueur " + str(game.actualPlayer) + ", l'IA joue")
pass # TODO IA
class Window(QMainWindow):
index = 0
def __init__(self, game):
super().__init__()
......@@ -113,82 +197,81 @@ class Window(QMainWindow):
# set the title
self.setWindowTitle("Mutorere")
# setting geometry
self.setGeometry(100, 100, 600, 500)
self.drawTheScene()
# creating a label widget
def drawTheScene(self):
#self.paintEvent()
# self.paintEvent()
# show all the widgets
self.update()
self.show()
def paintEvent(self, event):
self.index = 0
self.painter = QPainter(self)#1
self.painter.setPen(QPen(Qt.black, 4, Qt.SolidLine))
self.painter.drawText(250,50,"1")
self.painter = QPainter(self) # 1
self.painter.setPen(QPen(Qt.black, 4, Qt.SolidLine))
self.painter.drawText(250, 50, "1")
self.painter.setBrush(QBrush(self.chooseColor(), Qt.SolidPattern))
self.painter.drawEllipse(250, 50, 70, 70)
self.painter.setPen(QPen(Qt.black, 4, Qt.SolidLine))
self.painter.drawText(350,100,"2")
self.painter.setPen(QPen(Qt.black, 4, Qt.SolidLine))
self.painter.drawText(350, 100, "2")
self.painter.setBrush(QBrush(self.chooseColor(), Qt.SolidPattern))
self.painter.drawEllipse(350, 100, 70, 70)
self.painter.drawEllipse(350, 100, 70, 70)
self.painter.setPen(QPen(Qt.black, 4, Qt.SolidLine))
self.painter.drawText(400,200,"3")
self.painter.setPen(QPen(Qt.black, 4, Qt.SolidLine))
self.painter.drawText(400, 200, "3")
self.painter.setBrush(QBrush(self.chooseColor(), Qt.SolidPattern))
self.painter.drawEllipse(400, 200, 70, 70)
self.painter.drawEllipse(400, 200, 70, 70)
self.painter.setPen(QPen(Qt.black, 4, Qt.SolidLine))
self.painter.drawText(350,300,"4")
self.painter.setPen(QPen(Qt.black, 4, Qt.SolidLine))
self.painter.drawText(350, 300, "4")
self.painter.setBrush(QBrush(self.chooseColor(), Qt.SolidPattern))
self.painter.drawEllipse(350, 300, 70, 70)
self.painter.drawEllipse(350, 300, 70, 70)
self.painter.setPen(QPen(Qt.black, 4, Qt.SolidLine))
self.painter.drawText(250,350,"5")
self.painter.setPen(QPen(Qt.black, 4, Qt.SolidLine))
self.painter.drawText(250, 350, "5")
self.painter.setBrush(QBrush(self.chooseColor(), Qt.SolidPattern))
self.painter.drawEllipse(250, 350, 70, 70)
self.painter.drawEllipse(250, 350, 70, 70)
self.painter.setPen(QPen(Qt.black, 4, Qt.SolidLine))
self.painter.drawText(150,300,"6")
self.painter.setPen(QPen(Qt.black, 4, Qt.SolidLine))
self.painter.drawText(150, 300, "6")
self.painter.setBrush(QBrush(self.chooseColor(), Qt.SolidPattern))
self.painter.drawEllipse(150, 300, 70, 70)
self.painter.drawEllipse(150, 300, 70, 70)
self.painter.setPen(QPen(Qt.black, 4, Qt.SolidLine))
self.painter.drawText(100,200,"7")
self.painter.setPen(QPen(Qt.black, 4, Qt.SolidLine))
self.painter.drawText(100, 200, "7")
self.painter.setBrush(QBrush(self.chooseColor(), Qt.SolidPattern))
self.painter.drawEllipse(100, 200, 70, 70)
self.painter.drawEllipse(100, 200, 70, 70)
self.painter.setPen(QPen(Qt.black, 4, Qt.SolidLine))
self.painter.drawText(150,100,"8")
self.painter.setPen(QPen(Qt.black, 4, Qt.SolidLine))
self.painter.drawText(150, 100, "8")
self.painter.setBrush(QBrush(self.chooseColor(), Qt.SolidPattern))
self.painter.drawEllipse(150, 100, 70, 70)
self.painter.drawEllipse(150, 100, 70, 70)
self.painter.setPen(QPen(Qt.black, 4, Qt.SolidLine))
self.painter.drawText(250,200,"9")
self.painter.setPen(QPen(Qt.black, 4, Qt.SolidLine))
self.painter.drawText(250, 200, "9")
self.painter.setBrush(QBrush(self.chooseColor(), Qt.SolidPattern))
self.painter.drawEllipse(250, 200, 70, 70)
self.painter.end()
#self.painter.drawEllipse(180, 50, 100, 100)
self.painter.drawEllipse(250, 200, 70, 70)
self.painter.end()
# self.painter.drawEllipse(180, 50, 100, 100)
def chooseColor(self):
self.index+=1
if(self.game.boardGame[self.index-1]==0):
self.index += 1
if (self.game.boardGame[self.index - 1] == 0):
return Qt.white
if(self.game.boardGame[self.index-1]==1):
if (self.game.boardGame[self.index - 1] == 1):
return Qt.green
if(self.game.boardGame[self.index-1]==2):
if (self.game.boardGame[self.index - 1] == 2):
return Qt.red
if __name__ == "__main__":
app = QApplication(sys.argv)
Game(RealPlayer(),RealPlayer())
\ No newline at end of file
Game(Agent(), Agent())
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment