From 066b5f3ceb43055e469acb26667be65138088adc Mon Sep 17 00:00:00 2001 From: maberet <maberet@ada.local.isima.fr> Date: Wed, 29 Jun 2022 16:53:48 +0200 Subject: [PATCH] commit des piles et zones fonctionnelles --- .../jeu_appren_par_renfo/map.txt | 7 - .../jeu_appren_par_renfo/src/ball.c | 40 ++++- .../jeu_appren_par_renfo/src/ball.h | 22 ++- .../jeu_appren_par_renfo/src/main.c | 10 +- .../jeu_appren_par_renfo/src/map.h | 2 +- .../jeu_appren_par_renfo/src/qlearn.c | 150 ++++++++++++------ .../jeu_appren_par_renfo/src/qlearn.h | 31 ++-- 7 files changed, 181 insertions(+), 81 deletions(-) diff --git a/travail_de_groupe/jeu_appren_par_renfo/map.txt b/travail_de_groupe/jeu_appren_par_renfo/map.txt index 6dc328c..a5b149e 100644 --- a/travail_de_groupe/jeu_appren_par_renfo/map.txt +++ b/travail_de_groupe/jeu_appren_par_renfo/map.txt @@ -8,11 +8,4 @@ 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 -1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 -1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 -1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 -1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 -1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 -1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 -1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 \ No newline at end of file diff --git a/travail_de_groupe/jeu_appren_par_renfo/src/ball.c b/travail_de_groupe/jeu_appren_par_renfo/src/ball.c index 80878b2..d8ee945 100644 --- a/travail_de_groupe/jeu_appren_par_renfo/src/ball.c +++ b/travail_de_groupe/jeu_appren_par_renfo/src/ball.c @@ -1,21 +1,47 @@ #include "ball.h" - //ball_t ball; int trajectoireAntoine[NUMBERPOINT_TRAJEC][2]; -void initBall() -{ -} +point_t initCanon (point_t canon){ + canon.x=(1+rand()%14)*BLOCK_SIZE; + canon.y=(1+rand()%8)*BLOCK_SIZE; + return canon; +} + +point_t initDropPoint (point_t dropPoint){ + dropPoint.x=(16+rand()%14)*BLOCK_SIZE; + dropPoint.y=(1+rand()%8)*BLOCK_SIZE; + return dropPoint; +} -float defineAngle(int canonX, int canonY, int xDropPoint, int yDropPoint) +float defineAngleF(int canonX, int canonY, int xDropPoint, int yDropPoint) { float distance; float angleSin; + canonX=canonX/BLOCK_SIZE; + canonY=canonY/BLOCK_SIZE; + xDropPoint= xDropPoint/BLOCK_SIZE; + yDropPoint= yDropPoint/BLOCK_SIZE; + distance = sqrtf(powf((float)(xDropPoint - canonX), 2) + powf((float)(yDropPoint - canonY), 2)); - angleSin = asinf(distance / (xDropPoint - canonX)); + angleSin = asinf(((float) (yDropPoint - canonY))/distance); return angleSin; } +float defineAngleH(int xCanon, int xDropPoint){ + int xNet =15 ; + int yNet= 4; + float angle ; + float yTemp; + xCanon= xCanon/BLOCK_SIZE; + xDropPoint=xDropPoint/BLOCK_SIZE; + + yTemp = lagrangeInterpolation((float)(xCanon+(xDropPoint-xCanon)/4), xCanon , 2,xNet , yNet , xDropPoint, 0 ); + angle= atanf( (float)(yTemp/ (xCanon+(xDropPoint-xCanon)/4) )) ; // possible faute ? + + return angle; +} + /* * Fonction qui prend une valeur de x et 3 points. Elle * renvoie la coordonnée y liée à la valeur de x sur la @@ -25,7 +51,7 @@ float defineAngle(int canonX, int canonY, int xDropPoint, int yDropPoint) * Point n°2 (filet ) : int xf, int yf * Point n°3 (target) : int xt, int yt */ -int lagrangeInterpolation(float xp, int xd, int yd, int xf, int yf, int xt, int yt) +float lagrangeInterpolation(float xp, int xd, int yd, int xf, int yf, int xt, int yt) { float x[4], y[4], yp = 0, p; int i, j, n; diff --git a/travail_de_groupe/jeu_appren_par_renfo/src/ball.h b/travail_de_groupe/jeu_appren_par_renfo/src/ball.h index d55aca2..6a9c5f4 100644 --- a/travail_de_groupe/jeu_appren_par_renfo/src/ball.h +++ b/travail_de_groupe/jeu_appren_par_renfo/src/ball.h @@ -4,14 +4,17 @@ #include <stdlib.h> #include <time.h> #include <stdio.h> -#include <math.h> +#include <math.h> +#include "render.h" + + #define NUMBERPOINT_TRAJEC 50 typedef enum { - false, - true + false, true + } booleen_t; typedef struct ball @@ -23,13 +26,20 @@ typedef struct ball float speed; } ball_t; +typedef struct point{ + int x; + int y; +} point_t ; + //extern ball_t ball; extern int trajectoireAntoine[NUMBERPOINT_TRAJEC][2]; -void initBall(); -float defineAngle(int, int, int, int); +point_t initCanon (point_t canon); +point_t initDropPoint (point_t dropPoint); +float defineAngleF(int, int, int, int); +float defineAngleH(int xCanon, int xDropPoint); -int lagrangeInterpolation(float, int, int, int, int, int, int); +float lagrangeInterpolation(float, int, int, int, int, int, int); void calculTrajectoireAntoine2(int, int, int, int, int, int); #endif \ No newline at end of file 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 f16624f..a9db2c8 100644 --- a/travail_de_groupe/jeu_appren_par_renfo/src/main.c +++ b/travail_de_groupe/jeu_appren_par_renfo/src/main.c @@ -4,16 +4,14 @@ int running; int game_state; -int main(){ - agent_t * agent; - int i ; +int main(){ float ***** Q = allocateAndInitiateQ(); + srand ( time(NULL)); - agent =initAgent(); + + writeQ(Q); running = 1; - i=setReward( agent->x , agent->y, 0); - printf("%d \n ",i ); game_state = GAME; readMapFromFile("map.txt"); initPlayer(); diff --git a/travail_de_groupe/jeu_appren_par_renfo/src/map.h b/travail_de_groupe/jeu_appren_par_renfo/src/map.h index b6ec095..e39b445 100644 --- a/travail_de_groupe/jeu_appren_par_renfo/src/map.h +++ b/travail_de_groupe/jeu_appren_par_renfo/src/map.h @@ -5,7 +5,7 @@ #define MAP_WIDTH 31 -#define MAP_HEIGHT 18 +#define MAP_HEIGHT 11 extern int map[MAP_HEIGHT][MAP_WIDTH]; diff --git a/travail_de_groupe/jeu_appren_par_renfo/src/qlearn.c b/travail_de_groupe/jeu_appren_par_renfo/src/qlearn.c index aabe304..b481d7f 100644 --- a/travail_de_groupe/jeu_appren_par_renfo/src/qlearn.c +++ b/travail_de_groupe/jeu_appren_par_renfo/src/qlearn.c @@ -7,7 +7,7 @@ agent_t * initAgent (){ exit (1); } agent->x=(16+rand()%14)*BLOCK_SIZE; - agent->y=(1+rand()%14)*BLOCK_SIZE; + agent->y=(1+rand()%8)*BLOCK_SIZE; agent->high=2*BLOCK_SIZE; agent->weight=2*BLOCK_SIZE; agent->speed = 1; @@ -131,10 +131,21 @@ int convertIntoZone(int xAgent,int yAgent){ int zone; xAgent=xAgent/BLOCK_SIZE; yAgent=yAgent/BLOCK_SIZE; - if(xAgent<23 && yAgent<=8){zone=0;} - else if(xAgent<31 && yAgent<=8){zone=1;} - else if(xAgent<23 && yAgent<=18){zone=2;} - else if(xAgent< 31&& yAgent<18){zone=3;} + if(xAgent<23 && yAgent<=4){zone=0;} + else if(xAgent<31 && yAgent<=4){zone=1;} + else if(xAgent<23 && yAgent<9){zone=2;} + else if(xAgent< 31&& yAgent<9){zone=3;} + + return zone ; +} +int convertIntoZoneCanon(int xCanon,int yCanon){ + int zone; + xCanon=xCanon/BLOCK_SIZE; + yCanon=yCanon/BLOCK_SIZE; + if(xCanon<9 && yCanon<=4){zone=0;} + else if(xCanon<15 && yCanon<=4){zone=1;} + else if(xCanon<9 && yCanon<9){zone=2;} + else if(xCanon<15&& yCanon<9){zone=3;} return zone ; } @@ -214,28 +225,6 @@ int takeAction(int xAgent, int yAgent, float ***** Q, int canonZone, int angleHZ return action; } -void insertPointToLine(ptline_t head, int receiverZone, int canonZone, int angleHZone, int angleFZone){ - ptline_t newPoint = (ptline_t )malloc(sizeof(line_t)); - if (newPoint==NULL){ - printf("error malloc\n "); - exit(1); - } - newPoint->receiverZone = receiverZone; - newPoint->shooterZone = canonZone; - newPoint-> angleHZone= angleHZone; - newPoint->angleFZone=angleFZone; - newPoint->next =head;// problème pt - head = newPoint; -} - -void freeLine ( ptline_t headLine){ - ptline_t current= headLine; - while ( current !=NULL){ - ptline_t temporary = current; - current = current ->next; - free(temporary); - } -} int setReward(int xAgent, int yAgent, int dropZone){ int zoneAgent; @@ -248,27 +237,98 @@ int setReward(int xAgent, int yAgent, int dropZone){ return (reward); } -float defineAngle (int xCanon, int yCanon , int xDropPoint, int yDropPoint){ - float distance; - float angleSin; - distance= sqrtf( powf((float)(xDropPoint-xCanon),2)+powf((float)(yDropPoint-yCanon),2)); - angleSin = asinf(distance/(xDropPoint-xCanon)); - return angleSin; +stack_t* initStack(int numberelt){ + stack_t *stack=NULL; + + stack=(stack_t *)malloc(sizeof(stack_t)); // allocation du ptr de tête. + + if (stack==NULL){ // vérification de l'allocation. + printf("problème d'allocation\n"); + exit(1); + } + + stack->base = (line_t *) malloc(numberelt*sizeof(line_t));// allocation de la stack de longueur numberelt. + + if (stack->base==NULL){ + printf("problème d'allocation\n"); + exit(1); + } + + stack->numberelt=numberelt; //ajout du nombre d'élément insérable dans la file. + stack->top=-1; //initialisation de l'indice du dernier élément. + + return (stack); } -void traningAgent ( int numberRun, int numberStep, float *****Q) { - int i ; - int action; - point_t canon ; - point_t dropPoint ; + +int emptyStack(stack_t *stack){ - while (numberRun>0){ - canon=initPoint(0); - dropPoint= initPoint(1); - for (i=0; i< numberStep;i++){ + int result =0; + + if (stack->top==-1){ // test de l'indice du top. + + result=1; // la stack est vide. + } + + return (result); +} + +int fullStack(stack_t *stack) +{ + int result = 0; + + if (stack->numberelt == (stack->top)+1) //si le top est l'indice du dernier élément élément de la stack + { + result = 1; + } + + return result; +} + +void actionStack(stack_t *stack, line_t element) +{ + if(!fullStack(stack)) //si la stack n'est pas pleine + { + stack->base[(stack->top)+1] = element; //on ajoute l'élément à l'indice top+1 + stack->top = stack->top+1; //on incrémente l'indice du top + } + else + { + printf("Pile pleine\n" ); + } +} + +line_t unStack(stack_t *stack) +{ + line_t top; + + if(!emptyStack(stack)) //si la stack n'est pas vide + { + top = stack->base[stack->top]; //on récupère le top + stack->top = stack->top-1; //on décrémente l'indice du top + } + else + { + printf("Pile vide"); + } + + return top; +} + +void freeStack(stack_t *stack) +{ + if(stack != NULL) + { + free(stack->base); //on libère le tableau dynamique + free(stack); //on libère la tête de la stack + printf("Pile libérée\n"); + } + else + { + printf("Libération impossible, stack == NULL\n"); + } +} + - } - } -} diff --git a/travail_de_groupe/jeu_appren_par_renfo/src/qlearn.h b/travail_de_groupe/jeu_appren_par_renfo/src/qlearn.h index 48b0f6e..875f314 100644 --- a/travail_de_groupe/jeu_appren_par_renfo/src/qlearn.h +++ b/travail_de_groupe/jeu_appren_par_renfo/src/qlearn.h @@ -4,12 +4,14 @@ #include <stdio.h> #include <stdlib.h> #include <time.h> -//#include "ball.h" +#include "ball.h" #include "math.h" #include "map.h" #define M_PI 3.14159265358979323846 +#define LEARN_RATE 0.9 + #define NUMBER_ZONE_SHOOTER 4 #define NUMBER_ZONE_RECEIVER 4 #define NUMBER_ACTION 5 @@ -20,6 +22,7 @@ #define DOWN 3 #define WAIT 4 + typedef struct agent { int x; int y; @@ -33,14 +36,18 @@ typedef struct line { int shooterZone; int angleHZone; int angleFZone; - struct line *next ; + int action; + int reward; +}line_t; -}line_t,*ptline_t; +typedef struct stack +{ + line_t *base; + int numberelt; + int top; + +} stack_t; -typedef struct point{ - int x; - int y; -} point_t ; agent_t* initAgent ( ); @@ -49,10 +56,16 @@ float ***** allocateAndInitiateQ(); void writeQ(float *****); int argmax(float * ); int convertIntoZone(int ,int y); +int convertIntoZoneCanon(int xCanon,int yCanon); int converterIntoAngleF(float); int converterIntoAngleH(float); int takeAction(int ,int , float ***** , int , int, int, float ); -void insertPointToLine(ptline_t head, int, int , int , int); -void freeLine ( ptline_t makeheadLine); int setReward(int , int , int ); +stack_t* initStack (int nbelt); +int emptyStack (stack_t *stack); +int fullStack(stack_t *stack); +void actionStack(stack_t *stack, line_t line); +line_t unStack(stack_t *stack); +void freeStack(stack_t *stack); + #endif \ No newline at end of file -- GitLab