# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# # The Best Learning Rate

# In this notebook, we are interested by the best learning rate (named **epsilon**). The following program will reach it by testing different epsilon values, and by getting their winning rate. First, let import the **AI_Tictactoe.py** program :

# %pylab inline

from AI_Tictactoe import *


# To find the best learning rate, the program will train different AI with different learning rates. The different learning rates are contained in **listEpsilon**, and the learning process is in the **training()** function. 

def training(nbTraining, epsilon):
        pp1 = Player('X', isIntelligent = False, learningRate = epsilon)     
        pp2 = Player('O', isIntelligent = False, learningRate = epsilon)
        
        pp1.experience = {}
        pp2.experience = {} #Force the experience to be null
        
        for i in range(nbTraining) :
            PlayTicTacToe(pp1,pp2, aiTraining = True)
        
        pp1.experience.update(pp2.experience)
        aiExperience = copy.deepcopy(pp1.experience)
        
        #pp1.Display_Experience()
        return aiExperience
        


listEpsilon = np.linspace(0,1,21)
print(listEpsilon)

# Then, we train different AI with the different learning rates. (**Warning** : this process can be long, in the current parametrization, the function PlayTicTacToe() is called **231 000** (= (10 000 + 1 000) * 21) 

# +
listWinningRate = []
experience = {}


for epsilon in listEpsilon :
    print('For epsilon = {}. '.format(epsilon), end='')
    experience = training(10000, epsilon)
    listWinningRate.append(WinningRate(experience))
        
    experience = {}
# -

# Finaly, we are looking for the best learning rate, by using **argmax()** function.

# +
plot(listEpsilon,listWinningRate)
bestLearningRate = listEpsilon[argmax(listWinningRate)]

print('The argmax is :', bestLearningRate)
print('It is the best learning rate we had for 10 000 traning games')
# -



StarcoderdataJupyterScriptsDedupFiltered