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

qlearn function pour taha

parent 57bc429e
No related branches found
No related tags found
No related merge requests found
...@@ -19,14 +19,14 @@ void initBall() ...@@ -19,14 +19,14 @@ void initBall()
int trajectoireAntoine[NUMBERPOINT_TRAJEC][2]; int trajectoireAntoine[NUMBERPOINT_TRAJEC][2];
point_t initCanon (point_t canon){ point_t initCanon (point_t canon){
canon.x=(1+rand()%14)*BLOCK_SIZE; canon.x=(1+rand()%14);
canon.y=(1+rand()%8)*BLOCK_SIZE; canon.y=(1+rand()%8);
return canon; return canon;
} }
point_t initDropPoint (point_t dropPoint){ point_t initDropPoint (point_t dropPoint){
dropPoint.x=(16+rand()%14)*BLOCK_SIZE; dropPoint.x=(16+rand()%14);
dropPoint.y=(1+rand()%8)*BLOCK_SIZE; dropPoint.y=(1+rand()%8);
return dropPoint; return dropPoint;
} }
...@@ -34,10 +34,10 @@ float defineAngleF(int canonX, int canonY, int xDropPoint, int yDropPoint) ...@@ -34,10 +34,10 @@ float defineAngleF(int canonX, int canonY, int xDropPoint, int yDropPoint)
{ {
float distance; float distance;
float angleSin; float angleSin;
canonX=canonX/BLOCK_SIZE; canonX=canonX;
canonY=canonY/BLOCK_SIZE; canonY=canonY;
xDropPoint= xDropPoint/BLOCK_SIZE; xDropPoint= xDropPoint;
yDropPoint= yDropPoint/BLOCK_SIZE; yDropPoint= yDropPoint;
distance = sqrtf(powf((float)(xDropPoint - canonX), 2) + powf((float)(yDropPoint - canonY), 2)); distance = sqrtf(powf((float)(xDropPoint - canonX), 2) + powf((float)(yDropPoint - canonY), 2));
angleSin = asinf(((float) (yDropPoint - canonY))/distance); angleSin = asinf(((float) (yDropPoint - canonY))/distance);
...@@ -49,8 +49,8 @@ float defineAngleH(int xCanon, int xDropPoint){ ...@@ -49,8 +49,8 @@ float defineAngleH(int xCanon, int xDropPoint){
int yNet= 4; int yNet= 4;
float angle ; float angle ;
float yTemp; float yTemp;
xCanon= xCanon/BLOCK_SIZE; xCanon= xCanon;
xDropPoint=xDropPoint/BLOCK_SIZE; xDropPoint=xDropPoint;
yTemp = lagrangeInterpolation((float)(xCanon+(xDropPoint-xCanon)/4), xCanon , 2,xNet , yNet , xDropPoint, 0 ); yTemp = lagrangeInterpolation((float)(xCanon+(xDropPoint-xCanon)/4), xCanon , 2,xNet , yNet , xDropPoint, 0 );
angle= atanf( (float)(yTemp/ (xCanon+(xDropPoint-xCanon)/4) )) ; // possible faute ? angle= atanf( (float)(yTemp/ (xCanon+(xDropPoint-xCanon)/4) )) ; // possible faute ?
......
...@@ -6,11 +6,11 @@ int game_state; ...@@ -6,11 +6,11 @@ int game_state;
int main(){ int main(){
float ***** Q = allocateAndInitiateQ(); float ***** Q = allocateAndInitiateQ();
int i= 10; int i= 5;
srand ( time(NULL)); srand ( time(NULL));
while (i>0){ while (i>0){
traningAgent(10000,5000, Q); traningAgent(10,1, Q);
i--;} i--;}
writeQ(Q); writeQ(Q);
......
...@@ -5,11 +5,7 @@ ...@@ -5,11 +5,7 @@
#define MAP_WIDTH 31 #define MAP_WIDTH 31
<<<<<<< HEAD
#define MAP_HEIGHT 10
=======
#define MAP_HEIGHT 11 #define MAP_HEIGHT 11
>>>>>>> qlearn
extern int map[MAP_HEIGHT][MAP_WIDTH]; extern int map[MAP_HEIGHT][MAP_WIDTH];
......
...@@ -6,10 +6,10 @@ agent_t * initAgent (){ ...@@ -6,10 +6,10 @@ agent_t * initAgent (){
printf("erreur alloc\n "); printf("erreur alloc\n ");
exit (1); exit (1);
} }
agent->x=(16+rand()%14)*BLOCK_SIZE; agent->x=(16+rand()%14);
agent->y=(1+rand()%8)*BLOCK_SIZE; agent->y=(1+rand()%8);
agent->high=2*BLOCK_SIZE; agent->high=2;
agent->weight=2*BLOCK_SIZE; agent->weight=2;
agent->speed = 1; agent->speed = 1;
// si changement de speed => changement de collisiosn dans le takeaction // si changement de speed => changement de collisiosn dans le takeaction
return(agent); return(agent);
...@@ -129,19 +129,19 @@ int argmax(float * arr){ ...@@ -129,19 +129,19 @@ int argmax(float * arr){
int convertIntoZone(int xAgent,int yAgent){ int convertIntoZone(int xAgent,int yAgent){
int zone=0; int zone=0;
xAgent=xAgent/BLOCK_SIZE; xAgent=xAgent;
yAgent=yAgent/BLOCK_SIZE; yAgent=yAgent;
if(xAgent> 15 && xAgent<23 && yAgent<=4){zone=0;} if(xAgent> 15 && xAgent<23 && yAgent<=4){zone=0;}
else if(xAgent>22 && xAgent<31 && yAgent<=4){zone=1;} else if(xAgent>22 && xAgent<31 && yAgent<=4){zone=1;}
else if(xAgent> 15 && xAgent<23 && yAgent<=9){zone=2;} else if(xAgent> 15 && xAgent<23 && yAgent<9){zone=2;}
else if(xAgent> 22 && xAgent<= 31&& yAgent<=9){zone=3;} else if(xAgent> 22 && xAgent<= 31&& yAgent<9){zone=3;}
return zone ; return zone ;
} }
int convertIntoZoneCanon(int xCanon,int yCanon){ int convertIntoZoneCanon(int xCanon,int yCanon){
int zone=0; int zone=0;
xCanon=xCanon/BLOCK_SIZE; xCanon=xCanon;
yCanon=yCanon/BLOCK_SIZE; yCanon=yCanon;
if(xCanon<9 && yCanon<=4){zone=0;} if(xCanon<9 && yCanon<=4){zone=0;}
else if(xCanon<15 && yCanon<=4){zone=1;} else if(xCanon<15 && yCanon<=4){zone=1;}
else if(xCanon<9 && yCanon<9){zone=2;} else if(xCanon<9 && yCanon<9){zone=2;}
...@@ -177,38 +177,38 @@ int takeAction(int xAgent, int yAgent, float ***** Q, int canonZone, int angleHZ ...@@ -177,38 +177,38 @@ int takeAction(int xAgent, int yAgent, float ***** Q, int canonZone, int angleHZ
int proba = rand() % 10000; int proba = rand() % 10000;
int receiverZone=0; int receiverZone=0;
if (proba < eps * 10000){ if (proba < eps * 10000){
if (xAgent/BLOCK_SIZE > (MAP_WIDTH-1)/2+1 && xAgent/BLOCK_SIZE < MAP_WIDTH- 2 && yAgent/BLOCK_SIZE > 1 && yAgent/BLOCK_SIZE < MAP_HEIGHT - 2){ if (xAgent > (MAP_WIDTH-1)/2+1 && xAgent < MAP_WIDTH- 2 && yAgent > 1 && yAgent < MAP_HEIGHT - 2){
action = rand() % 5;// OK cas au centre action = rand() % 5;// OK cas au centre
} }
else if (xAgent/BLOCK_SIZE == (MAP_WIDTH-1)/2+1 && yAgent/BLOCK_SIZE > 1 && yAgent/BLOCK_SIZE < MAP_HEIGHT - 2){ else if (xAgent == (MAP_WIDTH-1)/2+1 && yAgent > 1 && yAgent < MAP_HEIGHT - 2){
int possibleActions[4] = {1, 2, 3,4}; int possibleActions[4] = {1, 2, 3,4};
action = possibleActions[rand() % 4];// OK cas filet action = possibleActions[rand() % 4];// OK cas filet
} }
else if (xAgent/BLOCK_SIZE == (MAP_WIDTH-1)/2+1 && yAgent/BLOCK_SIZE== 1){ else if (xAgent == (MAP_WIDTH-1)/2+1 && yAgent== 1){
int possibleActions[3] = {1, 3, 4}; int possibleActions[3] = {1, 3, 4};
action = possibleActions[rand() % 3];// cas en haut a gauche action = possibleActions[rand() % 3];// cas en haut a gauche
} }
else if (xAgent/BLOCK_SIZE == (MAP_WIDTH-1)/2+1 && yAgent/BLOCK_SIZE==MAP_HEIGHT - 2){ else if (xAgent == (MAP_WIDTH-1)/2+1 && yAgent==MAP_HEIGHT - 2){
int possibleActions[3] = {1, 2, 4}; int possibleActions[3] = {1, 2, 4};
action = possibleActions[rand() % 3];// cas en bas a gauche action = possibleActions[rand() % 3];// cas en bas a gauche
} }
else if (yAgent/BLOCK_SIZE ==1 && xAgent/BLOCK_SIZE > (MAP_WIDTH-1)/2+1 && xAgent/BLOCK_SIZE < MAP_WIDTH- 2){ else if (yAgent ==1 && xAgent > (MAP_WIDTH-1)/2+1 && xAgent < MAP_WIDTH- 2){
int possibleActions[4] = {0, 1,3,4}; int possibleActions[4] = {0, 1,3,4};
action = possibleActions[rand() % 4];// cas en haut au milieu action = possibleActions[rand() % 4];// cas en haut au milieu
} }
else if (xAgent/BLOCK_SIZE == MAP_WIDTH- 2 && yAgent/BLOCK_SIZE == 1){ else if (xAgent == MAP_WIDTH- 2 && yAgent == 1){
int possibleActions[3] = {0, 3,4}; int possibleActions[3] = {0, 3,4};
action = possibleActions[rand() % 3];// cas en haut a droite action = possibleActions[rand() % 3];// cas en haut a droite
} }
else if (xAgent/BLOCK_SIZE == MAP_WIDTH-2 && yAgent/BLOCK_SIZE <MAP_HEIGHT-2 && yAgent/BLOCK_SIZE>1){ else if (xAgent == MAP_WIDTH-2 && yAgent <MAP_HEIGHT-2 && yAgent>1){
int possibleActions[4] = {0,2,3,4}; int possibleActions[4] = {0,2,3,4};
action = possibleActions[rand() % 4];// cas a droite au milieu action = possibleActions[rand() % 4];// cas a droite au milieu
} }
else if (xAgent/BLOCK_SIZE== MAP_WIDTH-2 && yAgent/BLOCK_SIZE == MAP_HEIGHT-2){ else if (xAgent== MAP_WIDTH-2 && yAgent == MAP_HEIGHT-2){
int possibleActions[3] = {0, 2,4}; int possibleActions[3] = {0, 2,4};
action = possibleActions[rand() % 3];// cas en bas a droite action = possibleActions[rand() % 3];// cas en bas a droite
} }
else if (xAgent/BLOCK_SIZE > (MAP_WIDTH-1)/2+1 && xAgent/BLOCK_SIZE < MAP_WIDTH- 2 && yAgent/BLOCK_SIZE == MAP_HEIGHT-2){ else if (xAgent > (MAP_WIDTH-1)/2+1 && xAgent < MAP_WIDTH- 2 && yAgent == MAP_HEIGHT-2){
int possibleActions[4] = {0,1,2,4}; int possibleActions[4] = {0,1,2,4};
action = possibleActions[rand() % 4]; action = possibleActions[rand() % 4];
} }
...@@ -217,7 +217,7 @@ int takeAction(int xAgent, int yAgent, float ***** Q, int canonZone, int angleHZ ...@@ -217,7 +217,7 @@ int takeAction(int xAgent, int yAgent, float ***** Q, int canonZone, int angleHZ
} }
} }
else{ else{
receiverZone= convertIntoZone(xAgent/BLOCK_SIZE,yAgent/BLOCK_SIZE); receiverZone= convertIntoZone(xAgent,yAgent);
action = argmax(Q[receiverZone][canonZone][angleHZone][angleFZone]); action = argmax(Q[receiverZone][canonZone][angleHZone][angleFZone]);
//printf("wtf"); //printf("wtf");
} }
...@@ -363,6 +363,7 @@ void traningAgent ( int numberRun, int numberStep, float *****Q) {// pour avoir ...@@ -363,6 +363,7 @@ void traningAgent ( int numberRun, int numberStep, float *****Q) {// pour avoir
canonZone= convertIntoZoneCanon(canon.x,canon.y); canonZone= convertIntoZoneCanon(canon.x,canon.y);
reward=0; reward=0;
printf("%d %d %d %d \n",dropZone, canonZone,zoneAngleH,zoneAngleF); printf("%d %d %d %d \n",dropZone, canonZone,zoneAngleH,zoneAngleF);
printf("%d %d \n",agent->x, agent->y);
for (i=0; i<numberStep-1;i++){ for (i=0; i<numberStep-1;i++){
action = takeAction(agent->x,agent->y,Q,canonZone,zoneAngleH,zoneAngleF,greedy); action = takeAction(agent->x,agent->y,Q,canonZone,zoneAngleH,zoneAngleF,greedy);
...@@ -379,19 +380,19 @@ void traningAgent ( int numberRun, int numberStep, float *****Q) {// pour avoir ...@@ -379,19 +380,19 @@ void traningAgent ( int numberRun, int numberStep, float *****Q) {// pour avoir
} }
action = takeAction(agent->x, agent->y,Q,canonZone,zoneAngleH,zoneAngleF,greedy); action = takeAction(agent->x, agent->y,Q,canonZone,zoneAngleH,zoneAngleF,greedy);
agentZone = convertIntoZone(agent->x, agent->y); agentZone = convertIntoZone(agent->x, agent->y);
if (agentZone==dropZone){
reward=1;
}
else{reward= 0;}
line.receiverZone=agentZone; line.receiverZone=agentZone;
line.shooterZone =canonZone; line.shooterZone =canonZone;
line.angleHZone= zoneAngleH; line.angleHZone= zoneAngleH;
line.angleFZone= zoneAngleF; line.angleFZone= zoneAngleF;
line.action= action; line.action= action;
line.reward = reward; line.reward = 0;
// actionStack(stack,line); // actionStack(stack,line);
moveAgent(agent, action); moveAgent(agent, action);
if (agentZone==dropZone){
reward=1;
}
else{reward= 0;}
Q[line.receiverZone][line.shooterZone][line.angleHZone][line.angleFZone][line.action] += Q[line.receiverZone][line.shooterZone][line.angleHZone][line.angleFZone][line.action] +=
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment