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

commit pour changement de branche

parent 247f646f
No related branches found
No related tags found
No related merge requests found
...@@ -6,14 +6,14 @@ int game_state; ...@@ -6,14 +6,14 @@ int game_state;
int main(){ int main(){
float ***** Q = allocateAndInitiateQ(); float ***** Q = allocateAndInitiateQ();
int i = 10000; int i= 10;
srand ( time(NULL)); srand ( time(NULL));
while (i>0){ while (i>0){
traningAgent(10000000, 40, Q); traningAgent(10000,5000, Q);
i--;} i--;}
writeQ(Q); writeQ(Q);
// running = 1; // running = 1;
// game_state = GAME; // game_state = GAME;
// readMapFromFile("map.txt"); // readMapFromFile("map.txt");
......
...@@ -128,18 +128,18 @@ int argmax(float * arr){ ...@@ -128,18 +128,18 @@ int argmax(float * arr){
} }
int convertIntoZone(int xAgent,int yAgent){ int convertIntoZone(int xAgent,int yAgent){
int zone; int zone=0;
xAgent=xAgent/BLOCK_SIZE; xAgent=xAgent/BLOCK_SIZE;
yAgent=yAgent/BLOCK_SIZE; yAgent=yAgent/BLOCK_SIZE;
if(xAgent<23 && yAgent<=4){zone=0;} if(xAgent> 15 && xAgent<23 && yAgent<=4){zone=0;}
else if(xAgent<31 && yAgent<=4){zone=1;} else if(xAgent>22 && xAgent<31 && yAgent<=4){zone=1;}
else if(xAgent<23 && yAgent<9){zone=2;} else if(xAgent> 15 && xAgent<23 && yAgent<=9){zone=2;}
else if(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; int zone=0;
xCanon=xCanon/BLOCK_SIZE; xCanon=xCanon/BLOCK_SIZE;
yCanon=yCanon/BLOCK_SIZE; yCanon=yCanon/BLOCK_SIZE;
if(xCanon<9 && yCanon<=4){zone=0;} if(xCanon<9 && yCanon<=4){zone=0;}
...@@ -174,9 +174,9 @@ int converterIntoAngleH(float angleH){ ...@@ -174,9 +174,9 @@ int converterIntoAngleH(float angleH){
int takeAction(int xAgent, int yAgent, float ***** Q, int canonZone, int angleHZone, int angleFZone, float eps){ int takeAction(int xAgent, int yAgent, float ***** Q, int canonZone, int angleHZone, int angleFZone, float eps){
int action; int action;
int proba = rand() % 100; int proba = rand() % 10000;
int receiverZone=0; int receiverZone=0;
if (proba < eps * 100){ 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/BLOCK_SIZE > (MAP_WIDTH-1)/2+1 && xAgent/BLOCK_SIZE < MAP_WIDTH- 2 && yAgent/BLOCK_SIZE > 1 && yAgent/BLOCK_SIZE < MAP_HEIGHT - 2){
action = rand() % 5;// OK cas au centre action = rand() % 5;// OK cas au centre
} }
...@@ -321,7 +321,7 @@ void freeStack(stack_t *stack) ...@@ -321,7 +321,7 @@ void freeStack(stack_t *stack)
{ {
free(stack->base); //on libère le tableau dynamique free(stack->base); //on libère le tableau dynamique
free(stack); //on libère la tête de la stack free(stack); //on libère la tête de la stack
printf("Pile libérée\n"); //printf("Pile libérée\n");
} }
else else
{ {
...@@ -349,7 +349,7 @@ void traningAgent ( int numberRun, int numberStep, float *****Q) {// pour avoir ...@@ -349,7 +349,7 @@ void traningAgent ( int numberRun, int numberStep, float *****Q) {// pour avoir
line_t line; line_t line;
float greedy=1; float greedy=1;
int maxAction; int maxAction;
stack= initStack(1000); stack= initStack(6000);
while (numberRun>0){ while (numberRun>0){
agent=initAgent(); agent=initAgent();
...@@ -362,7 +362,7 @@ void traningAgent ( int numberRun, int numberStep, float *****Q) {// pour avoir ...@@ -362,7 +362,7 @@ void traningAgent ( int numberRun, int numberStep, float *****Q) {// pour avoir
dropZone=convertIntoZone(dropPoint.x,dropPoint.y); dropZone=convertIntoZone(dropPoint.x,dropPoint.y);
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);
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);
...@@ -372,28 +372,30 @@ void traningAgent ( int numberRun, int numberStep, float *****Q) {// pour avoir ...@@ -372,28 +372,30 @@ void traningAgent ( int numberRun, int numberStep, float *****Q) {// pour avoir
line.angleHZone= zoneAngleH; line.angleHZone= zoneAngleH;
line.angleFZone= zoneAngleF; line.angleFZone= zoneAngleF;
line.action= action; line.action= action;
line.reward=0; line.reward= reward ;
actionStack(stack,line); actionStack(stack,line);
moveAgent(agent, action); moveAgent(agent, action);
} }
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;
if (agentZone==dropZone){ // actionStack(stack,line);
line.reward=1;
}
else{line.reward= 0;}
actionStack(stack,line);
moveAgent(agent, action); moveAgent(agent, action);
Q[line.receiverZone][line.shooterZone][line.angleHZone][line.angleFZone][line.action] += Q[line.receiverZone][line.shooterZone][line.angleHZone][line.angleFZone][line.action] +=
+ LEARN_RATE* ( line.reward - Q[line.receiverZone][line.shooterZone][line.angleHZone][line.angleFZone][line.action] ); + LEARN_RATE* ( reward - Q[line.receiverZone][line.shooterZone][line.angleHZone][line.angleFZone][line.action] );
while (!emptyStack(stack)){ while (!emptyStack(stack)){
maxAction= argmax(Q[line.receiverZone][line.shooterZone][line.angleHZone][line.angleFZone]); maxAction= argmax(Q[line.receiverZone][line.shooterZone][line.angleHZone][line.angleFZone]);
...@@ -404,10 +406,10 @@ void traningAgent ( int numberRun, int numberStep, float *****Q) {// pour avoir ...@@ -404,10 +406,10 @@ void traningAgent ( int numberRun, int numberStep, float *****Q) {// pour avoir
+ LEARN_RATE* ( reward + DISCOUNT*Q[line.receiverZone][line.shooterZone][line.angleHZone][line.angleFZone][maxAction] + LEARN_RATE* ( reward + DISCOUNT*Q[line.receiverZone][line.shooterZone][line.angleHZone][line.angleFZone][maxAction]
- Q[line.receiverZone][line.shooterZone][line.angleHZone][line.angleFZone][line.action] ); - Q[line.receiverZone][line.shooterZone][line.angleHZone][line.angleFZone][line.action] );
} }
//printf("is empty : %d\n ", emptyStack(stack));
numberRun--; numberRun--;
greedy=greedy-1/((float)numberRun); greedy=greedy-1/((float)numberRun);
if ( numberRun%1000000==1){printf (" %d \n ", numberRun);} if ( numberRun%1000000==1){printf (" %d \n ", numberRun);}
} }
freeStack(stack);
} }
\ No newline at end of file
...@@ -11,7 +11,7 @@ ...@@ -11,7 +11,7 @@
#define M_PI 3.14159265358979323846 #define M_PI 3.14159265358979323846
#define LEARN_RATE 0.8 #define LEARN_RATE 0.8
#define DISCOUNT 0.5 #define DISCOUNT 0.2
......
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