From fcb3f2ad3c310f59c5fd347bb0bae968a74fb131 Mon Sep 17 00:00:00 2001
From: maberet <maberet@turing.local.isima.fr>
Date: Tue, 28 Jun 2022 17:32:05 +0200
Subject: [PATCH]  ajout des fichiers qlearn

---
 .../jeu_appren_par_renfo/src/main.c           |  3 +
 .../jeu_appren_par_renfo/src/main.h           |  1 +
 .../jeu_appren_par_renfo/src/qlearn.c         | 99 +++++++++++++++++++
 .../jeu_appren_par_renfo/src/qlearn.h         | 32 ++++++
 4 files changed, 135 insertions(+)
 create mode 100644 travail_de_groupe/jeu_appren_par_renfo/src/qlearn.c
 create mode 100644 travail_de_groupe/jeu_appren_par_renfo/src/qlearn.h

diff --git a/travail_de_groupe/jeu_appren_par_renfo/src/main.c b/travail_de_groupe/jeu_appren_par_renfo/src/main.c
index 7ca6b39..84d2097 100644
--- a/travail_de_groupe/jeu_appren_par_renfo/src/main.c
+++ b/travail_de_groupe/jeu_appren_par_renfo/src/main.c
@@ -5,6 +5,9 @@ int game_state;
 
 
 int main(){
+
+    float ***** Q = allocateAndInitiateQ();
+    writeQ(Q);
     running = 1;
     game_state = GAME;
     readMapFromFile("map.txt");
diff --git a/travail_de_groupe/jeu_appren_par_renfo/src/main.h b/travail_de_groupe/jeu_appren_par_renfo/src/main.h
index 884eef4..8b5b70c 100644
--- a/travail_de_groupe/jeu_appren_par_renfo/src/main.h
+++ b/travail_de_groupe/jeu_appren_par_renfo/src/main.h
@@ -17,6 +17,7 @@
 #include "render.h"
 #include "gest_event.h"
 #include "map.h"
+#include "qlearn.h"
 
 
 #define MENU 0
diff --git a/travail_de_groupe/jeu_appren_par_renfo/src/qlearn.c b/travail_de_groupe/jeu_appren_par_renfo/src/qlearn.c
new file mode 100644
index 0000000..65f18f1
--- /dev/null
+++ b/travail_de_groupe/jeu_appren_par_renfo/src/qlearn.c
@@ -0,0 +1,99 @@
+#include "qlearn.h"
+
+void moveAgent(agent_t * agent, int choice){
+    switch (choice)
+    {
+    case BACK:
+        agent->x += 1*agent->speed; //Avancer 
+        break;
+
+    case FOWARD:
+        agent->x -= 1*agent->speed; // reculer 
+        break;
+
+    case UP:
+        agent->y += 1*agent->speed;
+        break;
+
+    case DOWN:
+        agent->y -= 1*agent->speed;
+        break;
+    case WAIT: 
+        break; 
+    }
+}
+
+float ***** allocateAndInitiateQ(){
+    int i,j,k,l,m;
+    
+    float ***** q = malloc(sizeof(float ****) * NUMBER_ZONE_RECEIVER); /// alloc player zone 
+    if (q==NULL)
+    {
+        printf("problème d'allocation \n");
+        exit(1);
+    }
+
+    for(i = 0; i <  NUMBER_ZONE_RECEIVER; i++){  
+        q[i] = malloc(sizeof(float ***) * NUMBER_ZONE_SHOOTER ); // alloc shooter zone 
+        if (q[i]==NULL)
+        {
+            printf("problème d'allocation \n");            
+            exit(1);
+        }
+
+        for(j = 0; j< NUMBER_ZONE_SHOOTER; j++){
+            q[i][j] = malloc(sizeof(float **) * 3 ); // alloc angle hauteur 
+
+            if (q[i][j]==NULL)
+            {
+                printf("problème d'allocation \n");            
+                exit(1);
+            }
+
+            for(k = 0; k <3 ; k++){
+                q[i][j][k] = malloc(sizeof(float *) * 5 ); // alloc angle plat 
+
+                if (q[i][j][k]==NULL)
+                {
+                    printf("problème d'allocation \n");            
+                    exit(1);
+                }
+                for(l = 0; l<5 ; l++){
+                    q[i][j][k][l] = malloc(sizeof(float ) * 5); //alloc action 
+
+                    if (q[i][j][k][l]==NULL)
+                    {
+                        printf("problème d'allocation \n");            
+                        exit(1);
+                    }
+                    for (m=0;m <5;m++){
+                        q[i][j][k][l][m]=0;
+                    }
+                        
+                }
+            }
+        }
+    }
+    return q;
+}
+
+
+void writeQ(float *****Q){
+    int i, j, k, l, m ;
+    FILE * fp = fopen("q.txt", "w+");
+    for(i = 0; i < NUMBER_ZONE_RECEIVER; i++){
+        for(j = 0; j < NUMBER_ZONE_SHOOTER; j++){
+            for(k = 0; k < 3; k++){
+                for(l= 0; l < 5; l++){
+                    for(m= 0; m <5; m++){
+                         fprintf(fp, "%f ", Q[i][j][k][l][m]);
+                    }
+                }
+            }
+            fprintf(fp, "\n");
+        }
+        fprintf(fp, "\n");
+    }
+    fflush(fp);
+    fclose(fp);
+}
\ No newline at end of file
diff --git a/travail_de_groupe/jeu_appren_par_renfo/src/qlearn.h b/travail_de_groupe/jeu_appren_par_renfo/src/qlearn.h
new file mode 100644
index 0000000..deffca1
--- /dev/null
+++ b/travail_de_groupe/jeu_appren_par_renfo/src/qlearn.h
@@ -0,0 +1,32 @@
+#ifndef Q_LEARN
+#define Q_LEARN
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <time.h>   
+//#include "ball.h"
+#include "math.h"
+#include "map.h"
+
+#define NUMBER_ZONE_SHOOTER 4
+#define NUMBER_ZONE_RECEIVER 4
+
+#define FOWARD 0//<--
+#define BACK 1 //-->
+#define UP 2
+#define DOWN 3
+#define WAIT 4 
+
+typedef struct agent {
+    int x;
+    int y;
+    int heigth;
+    int weight;
+    int speed; 
+} agent_t;
+
+
+void moveAgent(agent_t * agent, int choice);
+float ***** allocateAndInitiateQ();
+void writeQ(float *****Q);
+#endif
\ No newline at end of file
-- 
GitLab