#include "game.h" #include "carlearn.h" Game::Game() { shmCreate(); data->read=0; data->Res[2]=1; R.rebuild(CAMVECT,NOMBRE_NEURONES_INTERMEDIAIRES,TAILLERESULTAT); neurError=1; data->Neuron=0; data->action=0; data->APon=1; data->viewNetwork=0; showOutput=1; #ifdef __SHOW_NETWORK rSDL.setReseau(&R); #endif } void Game::shmCreate() { if ((shmid = shmget(SHMKEY, sizeof(struct shmdata), IPC_CREAT | 0666)) < 0) { perror("shmget"); exit(1); } if ((data = (struct shmdata *) shmat(shmid, NULL, 0)) == (struct shmdata *) -1) { perror("shmat"); exit(1); } } void Game::pilot(struct shmdata *data) { int i,z1=0,z2=0,center,np=0; for(i=0;iV[i]+1)%2; if(data->V[i]==1 && z1==0) { z1=i; while(data->V[i]==1) i++; z1+=i; z1/=2; } else if(data->V[i]==1) { z2=i; while(data->V[i]==1) i++; z2+=i; z2/=2; } } /* printf("zones : %d , %d \n",z1,z2); */ if(z1&&z2) center=(z1+z2)/2; else if(z1) center=z1; else if(z2) center=z2; else center=VSIZE/2; if(np>(VSIZE/2)) { data->Res[2]=1; } else { data->Res[2]=0; } if(center < (VSIZE/2)-2 ) data->Res[0]=1; else if(center > (VSIZE/2)+2 ) data->Res[1]=1; } void Game::learnThis() { if(showOutput) { cout << " LEARN : " ; for(int i=0;iRes[i] << "]"; } neurError+=R.learnOne(data->V,data->Res); neurError/=2; } void Game::neuronThis() { Utils u; std::vector V; V=R.forward(data->V); if(showOutput) cout << " neuron reply : "; for(unsigned i=0;iRes[i]=u.accept(V[i]); if(showOutput) cout << " " << data->Res[i]; } if(showOutput) cout << " bool :"; for(unsigned i=0;iread!=666;cpt++) { #ifdef __SHOW_NETWORK rSDL.setState(data->viewNetwork); if(!(cpt%16)) rSDL.refresh(); #endif while(data->read==0) usleep(data->delay/2); // on attends que la voiture ait bougé if(data->read==666) break; if(showOutput) { cout << "V = "; for(i=0;iV[i]) cout << " "; else cout << "#"; } } if(data->action==SAVE_STATE) { cout << "Enregistrement de l'etat du reseau dans le fichier : " << STATE_FILE << endl; data->action=0; R.saveState(STATE_FILE); } else if(data->action==RELOAD_STATE) { cout << "Restauration de l'etat du reseau dans le fichier : " << STATE_FILE << endl; data->action=0; R.loadState(STATE_FILE); } if(data->APon==1) { pilot(data); // mode pilot automatique if(showOutput) { cout << " AP= "; for(i=0;iRes[i]); } } if(data->Neuron==1) { if(data->APon) // mode apprentissage (neurone + pilote) learnThis(); else // neurone seul { neuronThis(); } } if(showOutput) printf("\n"); data->read=0; } cout << " EXIT AutoPilot ... " << endl; } void Game::preLearn() { // double learnAll(std::vector inputs, std::vector targets); std::vector in; std::vector r; int cpt=0; double erreur=0; for(int i=0;i V; for(int i=0;i