Skip to content
Snippets Groups Projects
Commit 71b802df authored by maberet's avatar maberet
Browse files

ajout des découpage des zones

parent 36ec91c9
No related branches found
No related tags found
No related merge requests found
......@@ -14,4 +14,5 @@
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
......@@ -5,13 +5,11 @@ int game_state;
int main(){
float ***** Q = allocateAndInitiateQ();
writeQ(Q);
running = 1;
game_state = GAME;
readMapFromFile("map.txt");
//printMap();
initPlayer();
initKeys();
......
......@@ -5,7 +5,7 @@
#define MAP_WIDTH 31
#define MAP_HEIGHT 17
#define MAP_HEIGHT 18
extern int map[MAP_HEIGHT][MAP_WIDTH];
......
......@@ -77,7 +77,7 @@ float ***** allocateAndInitiateQ(){
return q;
}
// attention il manque 3 lignes
void writeQ(float *****Q){
int i, j, k, l, m ;
FILE * fp = fopen("q.txt", "w+");
......@@ -87,13 +87,113 @@ void writeQ(float *****Q){
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");
}
fprintf(fp, "\n");
}
}fprintf(fp, "\n");
fflush(fp);
fclose(fp);
}
int argmax(float * arr){
int i;
float max = arr[0];
int maxIndex = 0;
printf("argmax: %f ", arr[0]);
for(i = 1; i < NUMBER_ACTION; i++){
printf("%f ", arr[i]);
if (arr[i] > max){
max = arr[i];
maxIndex = i;
}
}
printf("\n");
return maxIndex;
}
int convertIntoZone(int xAgent,int yAgent){
int zone;
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;}
return zone ;
}
int converterIntoAngleF(float angleF){
int angleZone=0;
long angleFd=(long)(angleF*1000000);
printf ("%ld \n ", angleFd);
if( ((long)(-M_PI/2*1000000)<=angleFd)&&(angleFd<=(long)((-M_PI/2+M_PI/5)*1000000))){angleZone=4;}
else if( ((long)((-(M_PI/2)+(M_PI/5))*1000000)<angleFd)&&(angleFd<=(long)((-M_PI/2+2*M_PI/5)*1000000))){angleZone=3;}
else if( (angleFd>(long)((-M_PI/2+2*M_PI/5)*1000000))&&(angleFd<=(long)((-M_PI/2+3*M_PI/5)*1000000))){angleZone=2;}
else if( (angleFd>(long)((-M_PI/2+3*M_PI/5)*1000000))&&(angleFd<=(long)((-M_PI/2+4*M_PI/5)*1000000))){angleZone=1;}
else if( (angleFd>(long)((-M_PI/2+4*M_PI/5)*1000000))&&(angleFd<=(long)(M_PI/2*1000000))){angleZone=0;}
return(angleZone);
}
int converterIntoAngleH(float angleH){
int angleZone=0;
long angleHd=(long)(angleH*1000000);
if( (0<=angleHd)&&(angleHd<=(long)((M_PI/6)*1000000))){angleZone=0;}
else if(( ((long)((M_PI/6))*1000000)<angleHd)&&(angleHd<=(long)((M_PI/3)*1000000))){angleZone=1;}
else if( (angleHd>(long)((M_PI/3)*1000000))&&(angleHd<=(long)((M_PI/2)*1000000))){angleZone=2;}
return(angleZone);
}
int takeAction(int xagent, int yagent, float ***** Q, int canonZone, int angleHZone, int angleFZone, float eps){
int action;
int proba = rand() % 100;
int receiverZone=0;
if (proba < eps * 100){
if (xagent > (MAP_WIDTH-1)/2+1 && xagent < MAP_WIDTH- 2 && yagent > 1 && yagent < MAP_HEIGHT - 2){
action = rand() % 5;// OK cas au centre
}
else if (xagent == (MAP_WIDTH-1)/2+1 && yagent > 1 && yagent < MAP_HEIGHT - 2){
int possibleActions[4] = {1, 2, 3,4};
action = possibleActions[rand() % 4];// OK cas filet
}
else if (xagent == (MAP_WIDTH-1)/2+1 && yagent == 1){
int possibleActions[3] = {1, 3, 4};
action = possibleActions[rand() % 3];// cas en haut a gauche
}
else if (xagent == (MAP_WIDTH-1)/2+1 && yagent==MAP_HEIGHT - 2){
int possibleActions[3] = {1, 2, 4};
action = possibleActions[rand() % 3];// cas en bas a gauche
}
else if (yagent ==1 && xagent > (MAP_WIDTH-1)/2+1 && xagent < MAP_WIDTH- 2){
int possibleActions[4] = {0, 1,3,4};
action = possibleActions[rand() % 4];// cas en haut au milieu
}
else if (xagent == MAP_WIDTH- 2 && yagent == 1){
int possibleActions[3] = {0, 3,4};
action = possibleActions[rand() % 3];// cas en haut a droite
}
else if (xagent == MAP_WIDTH-2 && yagent <MAP_HEIGHT-2 && yagent>1){
int possibleActions[4] = {0,2,3,4};
action = possibleActions[rand() % 4];// cas a droite au milieu
}
else if (xagent == MAP_WIDTH-2 && yagent == MAP_HEIGHT-2){
int possibleActions[3] = {0, 2,4};
action = possibleActions[rand() % 3];// cas en bas a droite
}
else if (xagent > (MAP_WIDTH-1)/2+1 && xagent < MAP_WIDTH- 2 && yagent == MAP_HEIGHT-2){
int possibleActions[4] = {0,1,2,4};
action = possibleActions[rand() % 4];
}
else{
action = rand() % 5;
}
}
else{
receiverZone= convertIntoZone(xagent,yagent);
action = argmax(Q[receiverZone][canonZone][angleHZone][angleFZone]);
//printf("wtf");
}
return action;
}
\ No newline at end of file
......@@ -8,8 +8,11 @@
#include "math.h"
#include "map.h"
#define M_PI 3.14159265358979323846
#define NUMBER_ZONE_SHOOTER 4
#define NUMBER_ZONE_RECEIVER 4
#define NUMBER_ACTION 5
#define FOWARD 0//<--
#define BACK 1 //-->
......@@ -28,5 +31,10 @@ typedef struct agent {
void moveAgent(agent_t * agent, int choice);
float ***** allocateAndInitiateQ();
void writeQ(float *****Q);
void writeQ(float *****);
int argmax(float * );
int convertIntoZone(int ,int y);
int converterIntoAngleF(float);
int converterIntoAngleH(float);
int takeAction(int ,int , float ***** , int , int, int, float );
#endif
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment