Skip to content

Commit

Permalink
commit monteCarlo
Browse files Browse the repository at this point in the history
  • Loading branch information
Shen368 committed Mar 10, 2021
1 parent 4cd5d18 commit f3a3995
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 21 deletions.
70 changes: 50 additions & 20 deletions Assets/Scripts/Algo/Monte Carlo/Monte Carlo.cs
Original file line number Diff line number Diff line change
@@ -1,45 +1,75 @@
using System.Collections;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MonteCarlo : MonoBehaviour
public class MonteCarlo : MarkovBase
{

// Pi(s) => dictionnaire de Istate (clef = Istate, value = int "action associée")
// -> initialisé à random
public Dictionary<IState, int> Pi_s;

// -> initialisé à random
private void RandomPolicy()
{
Pi_s = new Dictionary<IState, int>();

foreach (var state in m_states)
{
if (state.isFinal()) continue;
int action = m_actions[Utils.RandomGenerator.RandomNumber(0, m_actions.Count)];
Pi_s.Add(state, action);
}
}
// States

// Q(s,a) => Dictionary<(IState, int), int> (on stocke la moyenne des returns)
// -> initialisé à random
public Dictionary<IState, int> Q_s;
// -> initialisé à random

// Returns(s, a)
// -> initialiser à vide

// -> initialiser à vide
public List<IState> Return_s;

private void Start()
public MonteCarlo(List<IState> states, List<int> actions, ConvertMethod T) : base(states, actions, T)
{
// Pour chaque état, initialiser la politique avec une action random
// (Comme bcp de possibilité, un seul élément dans le dictionnaire)
}
// (Comme bcp de possibilité, un seul élément dans le dictionnaire)

// intitialize v_s with a random number
v_s = new Dictionary<IState, float>();
foreach (var state in m_states) v_s.Add(state, 0f);

// Loop forever (for each episode or game)
// Loop forever (for each episode or game)
// start from a random state

// creation d'une liste de (State Action Reward)

// generate random game until the end
// apply transition method on S0 & A0
// add An, Sn, Rn à la liste
// apply transition method on S0 & A0
// add An, Sn, Rn à la liste

// G = 0
// Loop for each step (on remonte les infos qu'on a obtenue (reward)) (on commence par l'avant derniére étape)
// G = gamma * G + R(t+1) (reward de l'itération suivante) (on va de la fin au debut) (la derniére en premmier)
// G = gamma * G + R(t+1) (reward de l'itération suivante) (on va de la fin au debut) (la derniére en premmier)

// Si on est pas deja passé par cet état
// on ajoute G à Returns(s, a)

// on ajoute la moyenne des Returns(s, a) dans Q(s,a)
// pour toute les actions possibles de l'état on ajoute l'action qui a la meilleure moyenne
}


public override int Think(IState state)
{

if (state.isFinal()) return 0;
return (int)Pi_s[state];

}



// Si on est pas deja passé par cet état
// on ajoute G à Returns(s, a)

// on ajoute la moyenne des Returns(s, a) dans Q(s,a)
// pour toute les actions possibles de l'état on ajoute l'action qui a la meilleure moyenne



}
2 changes: 1 addition & 1 deletion Assets/Scripts/Game/TicTacToe.cs
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ public static Cell Play(IState iState, int action, out IState newIState)

void Render()
{
for(int i = 0 ; i < 8 ; i++){
for(int i = 0 ; i < 9 ; i++){
SpawnTile(i % 3, i / 3, gameState.board[i]);
}
}
Expand Down

0 comments on commit f3a3995

Please sign in to comment.