2025-08-01 20:45:57 +02:00
|
|
|
use crate::dqn::dqn_common;
|
2025-06-22 18:34:36 +02:00
|
|
|
use burn::{prelude::Backend, tensor::Tensor};
|
2025-06-22 15:42:55 +02:00
|
|
|
use burn_rl::base::{Action, Environment, Snapshot, State};
|
2025-06-22 18:34:36 +02:00
|
|
|
use rand::{thread_rng, Rng};
|
|
|
|
|
use store::{GameEvent, GameState, PlayerId, PointsRules, Stage, TurnStage};
|
2025-06-22 15:42:55 +02:00
|
|
|
|
|
|
|
|
/// État du jeu Trictrac pour burn-rl
|
|
|
|
|
#[derive(Debug, Clone, Copy)]
|
|
|
|
|
pub struct TrictracState {
|
2025-06-28 22:18:39 +02:00
|
|
|
pub data: [f32; 36], // Représentation vectorielle de l'état du jeu
|
2025-06-22 15:42:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl State for TrictracState {
|
|
|
|
|
type Data = [f32; 36];
|
|
|
|
|
|
|
|
|
|
fn to_tensor<B: Backend>(&self) -> Tensor<B, 1> {
|
|
|
|
|
Tensor::from_floats(self.data, &B::Device::default())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn size() -> usize {
|
|
|
|
|
36
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl TrictracState {
|
|
|
|
|
/// Convertit un GameState en TrictracState
|
|
|
|
|
pub fn from_game_state(game_state: &GameState) -> Self {
|
2025-06-28 22:18:39 +02:00
|
|
|
let state_vec = game_state.to_vec_float();
|
|
|
|
|
let mut data = [0.0; 36];
|
2025-06-22 18:34:36 +02:00
|
|
|
|
2025-06-22 15:42:55 +02:00
|
|
|
// Copier les données en s'assurant qu'on ne dépasse pas la taille
|
|
|
|
|
let copy_len = state_vec.len().min(36);
|
2025-06-28 22:18:39 +02:00
|
|
|
data[..copy_len].copy_from_slice(&state_vec[..copy_len]);
|
2025-06-22 18:34:36 +02:00
|
|
|
|
2025-06-22 15:42:55 +02:00
|
|
|
TrictracState { data }
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Actions possibles dans Trictrac pour burn-rl
|
|
|
|
|
#[derive(Debug, Clone, Copy, PartialEq)]
|
|
|
|
|
pub struct TrictracAction {
|
|
|
|
|
pub index: u32,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Action for TrictracAction {
|
|
|
|
|
fn random() -> Self {
|
|
|
|
|
use rand::{thread_rng, Rng};
|
|
|
|
|
let mut rng = thread_rng();
|
|
|
|
|
TrictracAction {
|
|
|
|
|
index: rng.gen_range(0..Self::size() as u32),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn enumerate() -> Vec<Self> {
|
|
|
|
|
(0..Self::size() as u32)
|
|
|
|
|
.map(|index| TrictracAction { index })
|
|
|
|
|
.collect()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn size() -> usize {
|
2025-07-08 21:58:15 +02:00
|
|
|
1252
|
2025-06-22 15:42:55 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl From<u32> for TrictracAction {
|
|
|
|
|
fn from(index: u32) -> Self {
|
|
|
|
|
TrictracAction { index }
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl From<TrictracAction> for u32 {
|
|
|
|
|
fn from(action: TrictracAction) -> u32 {
|
|
|
|
|
action.index
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Environnement Trictrac pour burn-rl
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
|
pub struct TrictracEnvironment {
|
2025-06-22 21:25:45 +02:00
|
|
|
pub game: GameState,
|
2025-06-22 18:34:36 +02:00
|
|
|
active_player_id: PlayerId,
|
2025-06-22 15:42:55 +02:00
|
|
|
opponent_id: PlayerId,
|
|
|
|
|
current_state: TrictracState,
|
|
|
|
|
episode_reward: f32,
|
|
|
|
|
step_count: usize,
|
2025-06-22 21:25:45 +02:00
|
|
|
pub visualized: bool,
|
2025-06-22 15:42:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Environment for TrictracEnvironment {
|
|
|
|
|
type StateType = TrictracState;
|
|
|
|
|
type ActionType = TrictracAction;
|
|
|
|
|
type RewardType = f32;
|
|
|
|
|
|
2025-08-02 12:42:32 +02:00
|
|
|
const MAX_STEPS: usize = 700; // Limite max pour éviter les parties infinies
|
2025-06-22 15:42:55 +02:00
|
|
|
|
|
|
|
|
fn new(visualized: bool) -> Self {
|
2025-06-22 18:34:36 +02:00
|
|
|
let mut game = GameState::new(false);
|
|
|
|
|
|
2025-06-22 15:42:55 +02:00
|
|
|
// Ajouter deux joueurs
|
2025-06-22 18:34:36 +02:00
|
|
|
game.init_player("DQN Agent");
|
|
|
|
|
game.init_player("Opponent");
|
|
|
|
|
let player1_id = 1;
|
|
|
|
|
let player2_id = 2;
|
|
|
|
|
|
2025-07-26 09:37:54 +02:00
|
|
|
// Commencer la partie
|
|
|
|
|
game.consume(&GameEvent::BeginGame { goes_first: 1 });
|
|
|
|
|
|
2025-06-22 18:34:36 +02:00
|
|
|
let current_state = TrictracState::from_game_state(&game);
|
2025-06-22 15:42:55 +02:00
|
|
|
TrictracEnvironment {
|
|
|
|
|
game,
|
|
|
|
|
active_player_id: player1_id,
|
|
|
|
|
opponent_id: player2_id,
|
|
|
|
|
current_state,
|
|
|
|
|
episode_reward: 0.0,
|
|
|
|
|
step_count: 0,
|
|
|
|
|
visualized,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn state(&self) -> Self::StateType {
|
|
|
|
|
self.current_state
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn reset(&mut self) -> Snapshot<Self> {
|
|
|
|
|
// Réinitialiser le jeu
|
2025-06-22 18:34:36 +02:00
|
|
|
self.game = GameState::new(false);
|
|
|
|
|
self.game.init_player("DQN Agent");
|
|
|
|
|
self.game.init_player("Opponent");
|
|
|
|
|
|
2025-06-22 21:25:45 +02:00
|
|
|
// Commencer la partie
|
|
|
|
|
self.game.consume(&GameEvent::BeginGame { goes_first: 1 });
|
|
|
|
|
|
2025-06-22 18:34:36 +02:00
|
|
|
self.current_state = TrictracState::from_game_state(&self.game);
|
2025-06-22 15:42:55 +02:00
|
|
|
self.episode_reward = 0.0;
|
|
|
|
|
self.step_count = 0;
|
|
|
|
|
|
2025-06-22 18:34:36 +02:00
|
|
|
Snapshot::new(self.current_state, 0.0, false)
|
2025-06-22 15:42:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn step(&mut self, action: Self::ActionType) -> Snapshot<Self> {
|
|
|
|
|
self.step_count += 1;
|
2025-06-22 18:34:36 +02:00
|
|
|
|
2025-06-22 15:42:55 +02:00
|
|
|
// Convertir l'action burn-rl vers une action Trictrac
|
2025-06-22 18:34:36 +02:00
|
|
|
let trictrac_action = self.convert_action(action, &self.game);
|
|
|
|
|
|
2025-06-22 15:42:55 +02:00
|
|
|
let mut reward = 0.0;
|
|
|
|
|
let mut terminated = false;
|
2025-06-22 18:34:36 +02:00
|
|
|
|
2025-06-22 15:42:55 +02:00
|
|
|
// Exécuter l'action si c'est le tour de l'agent DQN
|
2025-06-22 18:34:36 +02:00
|
|
|
if self.game.active_player_id == self.active_player_id {
|
2025-06-22 15:42:55 +02:00
|
|
|
if let Some(action) = trictrac_action {
|
|
|
|
|
match self.execute_action(action) {
|
|
|
|
|
Ok(action_reward) => {
|
|
|
|
|
reward = action_reward;
|
|
|
|
|
}
|
|
|
|
|
Err(_) => {
|
|
|
|
|
// Action invalide, pénalité
|
|
|
|
|
reward = -1.0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
// Action non convertible, pénalité
|
|
|
|
|
reward = -0.5;
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-06-22 18:34:36 +02:00
|
|
|
|
2025-06-22 21:25:45 +02:00
|
|
|
// Faire jouer l'adversaire (stratégie simple)
|
|
|
|
|
while self.game.active_player_id == self.opponent_id && self.game.stage != Stage::Ended {
|
|
|
|
|
reward += self.play_opponent_if_needed();
|
|
|
|
|
}
|
2025-06-22 18:34:36 +02:00
|
|
|
|
|
|
|
|
// Vérifier si la partie est terminée
|
|
|
|
|
let done = self.game.stage == Stage::Ended
|
|
|
|
|
|| self.game.determine_winner().is_some()
|
|
|
|
|
|| self.step_count >= Self::MAX_STEPS;
|
|
|
|
|
|
|
|
|
|
if done {
|
2025-06-22 15:42:55 +02:00
|
|
|
terminated = true;
|
|
|
|
|
// Récompense finale basée sur le résultat
|
2025-06-22 18:34:36 +02:00
|
|
|
if let Some(winner_id) = self.game.determine_winner() {
|
2025-06-22 15:42:55 +02:00
|
|
|
if winner_id == self.active_player_id {
|
2025-06-22 18:34:36 +02:00
|
|
|
reward += 100.0; // Victoire
|
2025-06-22 15:42:55 +02:00
|
|
|
} else {
|
2025-06-22 18:34:36 +02:00
|
|
|
reward -= 50.0; // Défaite
|
2025-06-22 15:42:55 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-06-22 18:34:36 +02:00
|
|
|
|
2025-06-22 15:42:55 +02:00
|
|
|
// Mettre à jour l'état
|
2025-06-22 18:34:36 +02:00
|
|
|
self.current_state = TrictracState::from_game_state(&self.game);
|
2025-06-22 15:42:55 +02:00
|
|
|
self.episode_reward += reward;
|
2025-06-22 18:34:36 +02:00
|
|
|
|
2025-06-22 15:42:55 +02:00
|
|
|
if self.visualized && terminated {
|
2025-06-22 18:34:36 +02:00
|
|
|
println!(
|
|
|
|
|
"Episode terminé. Récompense totale: {:.2}, Étapes: {}",
|
|
|
|
|
self.episode_reward, self.step_count
|
|
|
|
|
);
|
2025-06-22 15:42:55 +02:00
|
|
|
}
|
|
|
|
|
|
2025-06-22 18:34:36 +02:00
|
|
|
Snapshot::new(self.current_state, reward, terminated)
|
2025-06-22 15:42:55 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl TrictracEnvironment {
|
|
|
|
|
/// Convertit une action burn-rl vers une action Trictrac
|
2025-06-22 18:34:36 +02:00
|
|
|
fn convert_action(
|
|
|
|
|
&self,
|
|
|
|
|
action: TrictracAction,
|
|
|
|
|
game_state: &GameState,
|
2025-07-25 17:26:02 +02:00
|
|
|
) -> Option<dqn_common::TrictracAction> {
|
|
|
|
|
dqn_common::TrictracAction::from_action_index(action.index.try_into().unwrap())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Convertit l'index d'une action au sein des actions valides vers une action Trictrac
|
|
|
|
|
fn convert_valid_action_index(
|
|
|
|
|
&self,
|
|
|
|
|
action: TrictracAction,
|
|
|
|
|
game_state: &GameState,
|
2025-07-08 21:58:15 +02:00
|
|
|
) -> Option<dqn_common::TrictracAction> {
|
|
|
|
|
use dqn_common::get_valid_actions;
|
2025-06-22 18:34:36 +02:00
|
|
|
|
2025-06-22 15:42:55 +02:00
|
|
|
// Obtenir les actions valides dans le contexte actuel
|
2025-06-22 18:34:36 +02:00
|
|
|
let valid_actions = get_valid_actions(game_state);
|
|
|
|
|
|
2025-06-22 15:42:55 +02:00
|
|
|
if valid_actions.is_empty() {
|
|
|
|
|
return None;
|
|
|
|
|
}
|
2025-06-22 18:34:36 +02:00
|
|
|
|
2025-06-22 15:42:55 +02:00
|
|
|
// Mapper l'index d'action sur une action valide
|
|
|
|
|
let action_index = (action.index as usize) % valid_actions.len();
|
2025-06-22 18:34:36 +02:00
|
|
|
Some(valid_actions[action_index].clone())
|
2025-06-22 15:42:55 +02:00
|
|
|
}
|
2025-06-22 18:34:36 +02:00
|
|
|
|
2025-06-22 15:42:55 +02:00
|
|
|
/// Exécute une action Trictrac dans le jeu
|
2025-06-22 18:34:36 +02:00
|
|
|
fn execute_action(
|
|
|
|
|
&mut self,
|
2025-07-08 21:58:15 +02:00
|
|
|
action: dqn_common::TrictracAction,
|
2025-06-22 18:34:36 +02:00
|
|
|
) -> Result<f32, Box<dyn std::error::Error>> {
|
2025-07-08 21:58:15 +02:00
|
|
|
use dqn_common::TrictracAction;
|
2025-06-22 18:34:36 +02:00
|
|
|
|
2025-06-22 15:42:55 +02:00
|
|
|
let mut reward = 0.0;
|
2025-06-22 18:34:36 +02:00
|
|
|
|
|
|
|
|
let event = match action {
|
2025-06-22 15:42:55 +02:00
|
|
|
TrictracAction::Roll => {
|
2025-06-22 18:34:36 +02:00
|
|
|
// Lancer les dés
|
|
|
|
|
reward += 0.1;
|
|
|
|
|
Some(GameEvent::Roll {
|
|
|
|
|
player_id: self.active_player_id,
|
|
|
|
|
})
|
2025-06-22 15:42:55 +02:00
|
|
|
}
|
2025-06-22 18:34:36 +02:00
|
|
|
// TrictracAction::Mark => {
|
|
|
|
|
// // Marquer des points
|
|
|
|
|
// let points = self.game.
|
|
|
|
|
// reward += 0.1 * points as f32;
|
|
|
|
|
// Some(GameEvent::Mark {
|
|
|
|
|
// player_id: self.active_player_id,
|
|
|
|
|
// points,
|
|
|
|
|
// })
|
|
|
|
|
// }
|
2025-06-22 15:42:55 +02:00
|
|
|
TrictracAction::Go => {
|
2025-06-22 18:34:36 +02:00
|
|
|
// Continuer après avoir gagné un trou
|
2025-08-02 12:42:32 +02:00
|
|
|
reward += 0.4;
|
2025-06-22 18:34:36 +02:00
|
|
|
Some(GameEvent::Go {
|
|
|
|
|
player_id: self.active_player_id,
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
TrictracAction::Move {
|
|
|
|
|
dice_order,
|
|
|
|
|
from1,
|
|
|
|
|
from2,
|
|
|
|
|
} => {
|
|
|
|
|
// Effectuer un mouvement
|
|
|
|
|
let (dice1, dice2) = if dice_order {
|
|
|
|
|
(self.game.dice.values.0, self.game.dice.values.1)
|
|
|
|
|
} else {
|
|
|
|
|
(self.game.dice.values.1, self.game.dice.values.0)
|
|
|
|
|
};
|
|
|
|
|
let mut to1 = from1 + dice1 as usize;
|
|
|
|
|
let mut to2 = from2 + dice2 as usize;
|
|
|
|
|
|
|
|
|
|
// Gestion prise de coin par puissance
|
|
|
|
|
let opp_rest_field = 13;
|
|
|
|
|
if to1 == opp_rest_field && to2 == opp_rest_field {
|
|
|
|
|
to1 -= 1;
|
|
|
|
|
to2 -= 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let checker_move1 = store::CheckerMove::new(from1, to1).unwrap_or_default();
|
|
|
|
|
let checker_move2 = store::CheckerMove::new(from2, to2).unwrap_or_default();
|
|
|
|
|
|
2025-08-02 12:42:32 +02:00
|
|
|
reward += 0.4;
|
2025-06-22 18:34:36 +02:00
|
|
|
Some(GameEvent::Move {
|
|
|
|
|
player_id: self.active_player_id,
|
|
|
|
|
moves: (checker_move1, checker_move2),
|
|
|
|
|
})
|
2025-06-22 15:42:55 +02:00
|
|
|
}
|
2025-06-22 18:34:36 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Appliquer l'événement si valide
|
|
|
|
|
if let Some(event) = event {
|
|
|
|
|
if self.game.validate(&event) {
|
|
|
|
|
self.game.consume(&event);
|
|
|
|
|
|
|
|
|
|
// Simuler le résultat des dés après un Roll
|
|
|
|
|
if matches!(action, TrictracAction::Roll) {
|
|
|
|
|
let mut rng = thread_rng();
|
|
|
|
|
let dice_values = (rng.gen_range(1..=6), rng.gen_range(1..=6));
|
|
|
|
|
let dice_event = GameEvent::RollResult {
|
|
|
|
|
player_id: self.active_player_id,
|
|
|
|
|
dice: store::Dice {
|
|
|
|
|
values: dice_values,
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
if self.game.validate(&dice_event) {
|
|
|
|
|
self.game.consume(&dice_event);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
// Pénalité pour action invalide
|
|
|
|
|
reward -= 2.0;
|
2025-06-22 15:42:55 +02:00
|
|
|
}
|
|
|
|
|
}
|
2025-06-22 18:34:36 +02:00
|
|
|
|
2025-06-22 15:42:55 +02:00
|
|
|
Ok(reward)
|
|
|
|
|
}
|
2025-06-22 18:34:36 +02:00
|
|
|
|
2025-06-22 15:42:55 +02:00
|
|
|
/// Fait jouer l'adversaire avec une stratégie simple
|
2025-06-22 18:34:36 +02:00
|
|
|
fn play_opponent_if_needed(&mut self) -> f32 {
|
|
|
|
|
let mut reward = 0.0;
|
|
|
|
|
|
2025-06-22 15:42:55 +02:00
|
|
|
// Si c'est le tour de l'adversaire, jouer automatiquement
|
2025-06-22 18:34:36 +02:00
|
|
|
if self.game.active_player_id == self.opponent_id && self.game.stage != Stage::Ended {
|
|
|
|
|
// Utiliser la stratégie default pour l'adversaire
|
2025-07-08 21:58:15 +02:00
|
|
|
use crate::strategy::default::DefaultStrategy;
|
2025-06-22 18:34:36 +02:00
|
|
|
use crate::BotStrategy;
|
|
|
|
|
|
|
|
|
|
let mut default_strategy = DefaultStrategy::default();
|
|
|
|
|
default_strategy.set_player_id(self.opponent_id);
|
|
|
|
|
if let Some(color) = self.game.player_color_by_id(&self.opponent_id) {
|
|
|
|
|
default_strategy.set_color(color);
|
|
|
|
|
}
|
|
|
|
|
*default_strategy.get_mut_game() = self.game.clone();
|
|
|
|
|
|
|
|
|
|
// Exécuter l'action selon le turn_stage
|
|
|
|
|
let event = match self.game.turn_stage {
|
|
|
|
|
TurnStage::RollDice => GameEvent::Roll {
|
|
|
|
|
player_id: self.opponent_id,
|
|
|
|
|
},
|
|
|
|
|
TurnStage::RollWaiting => {
|
|
|
|
|
let mut rng = thread_rng();
|
|
|
|
|
let dice_values = (rng.gen_range(1..=6), rng.gen_range(1..=6));
|
|
|
|
|
GameEvent::RollResult {
|
|
|
|
|
player_id: self.opponent_id,
|
|
|
|
|
dice: store::Dice {
|
|
|
|
|
values: dice_values,
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
TurnStage::MarkAdvPoints | TurnStage::MarkPoints => {
|
|
|
|
|
let opponent_color = store::Color::Black;
|
|
|
|
|
let dice_roll_count = self
|
|
|
|
|
.game
|
|
|
|
|
.players
|
|
|
|
|
.get(&self.opponent_id)
|
|
|
|
|
.unwrap()
|
|
|
|
|
.dice_roll_count;
|
|
|
|
|
let points_rules =
|
|
|
|
|
PointsRules::new(&opponent_color, &self.game.board, self.game.dice);
|
|
|
|
|
let points = points_rules.get_points(dice_roll_count).0;
|
|
|
|
|
reward -= 0.3 * points as f32; // Récompense proportionnelle aux points
|
|
|
|
|
|
|
|
|
|
GameEvent::Mark {
|
|
|
|
|
player_id: self.opponent_id,
|
|
|
|
|
points,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
TurnStage::HoldOrGoChoice => {
|
|
|
|
|
// Stratégie simple : toujours continuer
|
|
|
|
|
GameEvent::Go {
|
|
|
|
|
player_id: self.opponent_id,
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-06-22 21:25:45 +02:00
|
|
|
TurnStage::Move => GameEvent::Move {
|
|
|
|
|
player_id: self.opponent_id,
|
|
|
|
|
moves: default_strategy.choose_move(),
|
|
|
|
|
},
|
2025-06-22 18:34:36 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if self.game.validate(&event) {
|
|
|
|
|
self.game.consume(&event);
|
2025-06-22 15:42:55 +02:00
|
|
|
}
|
|
|
|
|
}
|
2025-06-22 18:34:36 +02:00
|
|
|
reward
|
2025-06-22 15:42:55 +02:00
|
|
|
}
|
2025-06-22 18:34:36 +02:00
|
|
|
}
|