remove temp file

This commit is contained in:
2024-11-13 11:44:46 +01:00
parent b86f62ccf2
commit 05c43c2c36
12 changed files with 0 additions and 8064 deletions

View File

@@ -1,312 +0,0 @@
#include <iostream>
#include <SDL.h>
#include <math.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include "sdlcommon.h"
#include "car.h"
#include "shmdata.h"
using namespace std;
Car::Car()
{
initShm();
reset();
showbar=1;
angstep=5;
for(int i=0;i<CAMVECT;i++) camera[i]=0;
carimage=0;
for(int i=0;i<TRAINEESIZE;i++) { trainee[i][0]=0; trainee[i][1]=0; }
traineepos=0;
traineesize=1;
}
void Car::loadImage(char *imagefile)
{
SDL_Surface *voitsurf;
Uint32 cur=SDL_GetTicks();
voitsurf=SDL_LoadBMP(imagefile);
printf("Precaching Surfaces ..... \n");
for(int i=0;i<360;i++)
{
Surface_car[i]=SDL_CreateRGBSurface(SDL_HWSURFACE, voitsurf->w, voitsurf->h, 32, 0,0,0,0);
if(SDL_SetColorKey(Surface_car[i], SDL_SRCCOLORKEY | SDL_RLEACCEL, SDL_MapRGB(Surface_car[i]->format, 255,0,255)) == -1)
fprintf(stderr, "Warning: colorkey will not be used, reason: %s\n", SDL_GetError());
rotatesurface(voitsurf,Surface_car[i],i);
}
carimage=1;
printf("Done ... (%d ms)\n",SDL_GetTicks()-cur);
}
void Car::updateCar(SDL_Surface* screen)
{
int i;
for(i=0;(SData->read!=0) && i<200;i++) usleep(5); // on attends 1 seconde
if(i==200)
{
cout << "AutoPilot Timed Out ...";
exit(0);
}
if(SData->Res[0]) decAngle();
if(SData->Res[1]) incAngle();
SData->Res[0]=0;
SData->Res[1]=0;
SData->pos_x+=getSpeed()*(cos(SData->angle*PI/180));
SData->pos_y+=getSpeed()*(sin(SData->angle*PI/180));
showTrainee(screen);
showCamera(screen); /* obligé pour avoir les pixels :-) */
// CAMERASIZE // CAMERASTEP
if(carimage) /* on blit la voiture */
{
SDL_Rect r;
r.w=Surface_car[SData->angle]->w;
r.h=Surface_car[SData->angle]->h;
r.x=(int)SData->pos_x-(r.w/2);
r.y=(int)SData->pos_y-(r.h/2);
SDL_BlitSurface( Surface_car[SData->angle], NULL, screen, &r);
}
else for(int i=0;i<20;i++) /* affichage de la voiture simplifiée (ligne rouge) */
{
setpixel(screen,(int)(SData->pos_x-(cos(SData->angle*PI/180)*i)),(int)( SData->pos_y-(sin(SData->angle*PI/180)*i)),255,0,0);
}
for(int i=0;i<CAMVECT;i++)
{
// cout << camera[i];
SData->V[i]=camera[i];
}
SData->read=1; /* on a lu :D */
//cout << endl;
}
void Car::showTrainee(SDL_Surface* screen)
{
int k;
trainee[traineepos][0]=(int)SData->pos_x;
trainee[traineepos][1]=(int)SData->pos_y;
traineepos=(traineepos+1)%traineesize;
k=traineepos;
k=(k+1)%traineesize;
for(int i=0; /*i < traineesize &&*/ k!=traineepos;i++) /* affichage de la trainée */
{
//setpixel(screen,trainee[k][0],trainee[k][1],0,(int)(i*(256.0/traineesize))%256,255);
int x0,y0,x1,y1;
x0=trainee[(k+traineesize-1)%traineesize][0];
y0=trainee[(k+traineesize-1)%traineesize][1];
x1=trainee[k][0];
y1=trainee[k][1];
if(x0==0) x0=x1;
if(y0==0) y0=y1;
if(x1==0) x1=x0;
if(y1==0) y1=y0;
DrawLine(screen,x0,y0,x1,y1,0,0,255);
k=(k+1)%traineesize;
}
}
void Car::showCamera(SDL_Surface* screen)
{
double x,y;
int k,ang;
x=SData->pos_x + ( cos(SData->angle*PI/180) * CAMERADISTANCE );
y=SData->pos_y + ( sin(SData->angle*PI/180) * CAMERADISTANCE );
ang=(SData->angle+90)%360;
x-=CAMERASIZE/2*cos(ang*PI/180);
y-=CAMERASIZE/2*sin(ang*PI/180);
k=0;
for(int i=0;i<CAMERASIZE;i++)
{
x+=cos(ang*PI/180);
y+=sin(ang*PI/180);
if(!(i%CAMERASTEP))
{
camera[k]=1;
if(inCarArea(screen,(int)x,(int)y)) //if(x>0 && x<screen->w && y>0 && y<screen->h)
camera[k]=(((getpixel(screen,(int)x,(int)y))%2)+1)%2;
else camera[k]=0;
if(camera[k] && showbar)
{
setpixel(screen, (int)x, (int)y,255,0,0);
//setpixel(screen, (int)x, (int)y-1,255,0,0);
//setpixel(screen, (int)x-1, (int)y,255,0,0);
//setpixel(screen, (int)x-1, (int)y-1,255,0,0);
}
else if(showbar)
{
setpixel(screen, (int)x, (int)y,0,255,0);
//setpixel(screen, (int)x, (int)y-1,0,255,0);
//setpixel(screen, (int)x-1, (int)y,0,255,0);
//setpixel(screen, (int)x-1, (int)y-1,0,255,0);
}
k++;
}
else
{
if(showbar) setpixel(screen, (int)x, (int)y,0,0,255);
}
}
}
void Car::incAngle()
{
SData->angle=(SData->angle+angstep)%360;
}
void Car::decAngle()
{
SData->angle=(SData->angle+360-angstep)%360;
}
int Car::incAngleStep(int i)
{
angstep+=i;
return angstep;
}
void Car::incSpeed()
{
if(SData->Res[2]==0)
{
SData->Res[0]=0;
SData->Res[1]=0;
SData->Res[2]=1;
if(SData->speed==0) SData->speed=1;
}
SData->speed++;
}
void Car::decSpeed()
{
SData->speed--;
}
bool Car::APToggle()
{
SData->APon=(SData->APon+1)%2;
return SData->APon;
}
bool Car::APState() const
{
return SData->APon;
}
bool Car::neurToggle()
{
SData->Neuron=(SData->Neuron+1)%2;
return SData->Neuron;
}
bool Car::neurState() const
{
return SData->Neuron;
}
void Car::initShm()
{
if ((shmid = shmget(SHMKEY, sizeof(struct shmdata), 0666)) < 0)
{
perror("shmget");
exit(1);
}
if (( SData = (struct shmdata *)shmat(shmid, NULL, 0)) == (struct shmdata *) -1)
{
perror("shmat");
exit(1);
}
}
int Car::getSpeed() const
{
if(SData->Res[2])
return SData->speed;
return 0;
}
int Car::getAngle() const
{
return SData->angle;
}
bool Car::showBar(int i)
{
showbar=(showbar+i)%2;
return showbar;
}
bool* Car::getVect()
{
return SData->Res;
}
bool* Car::getView()
{
return SData->V;
}
void Car::reset()
{
SData->pos_x=200;
SData->pos_y=200;
SData->speed=1;
SData->angle=0;
SData->Res[2]=1;
}
void Car::turn180()
{
SData->angle=(SData->angle+180)%360;
}
int Car::incTraineeSize(int s)
{
int i,k;
if(s==0) return traineesize;
if(traineesize==1 && s<0) return traineesize;
if(traineesize+s > TRAINEESIZE) s=TRAINEESIZE-traineesize-1;
if(traineesize+s < 0) s=1-TRAINEESIZE;
int tmp[traineesize+s][2];
k=traineepos;
if(s<0) k=(traineesize+traineepos-s)%traineesize;
for(i=0;i<(traineesize+s);i++)
{
if(i>traineesize)
{
tmp[i][0]=0;
tmp[i][1]=0;
}
else
{
tmp[i][0]=trainee[k][0];
tmp[i][1]=trainee[k][1];
}
k=(k+1)%traineesize;
}
for(i=0;i<traineesize+s;i++)
{
trainee[i][0]=tmp[i][0];
trainee[i][1]=tmp[i][1];
}
traineepos=traineesize-1;
// if(traineesize<1) { traineesize=1; traineepos=0; }
if(s<0)traineepos=0;
traineesize+=s;
return traineesize;
}
void Car::setDelay(int del)
{
SData->delay=del;
}
void Car::harakiri()
{
cout << " EXIT INTERFACE ...\n";
SData->read=666;
usleep(100);
SData->read=666;
usleep(100);
SData->read=666;
}
void Car::saveState()
{
SData->action=SAVE_STATE;
}
void Car::loadState()
{
SData->action=RELOAD_STATE;
}
void Car::showNetwork(bool a)
{
SData->viewNetwork=a;
}
bool Car::showNetworkState()
{
return SData->viewNetwork;
}

View File

@@ -1,57 +0,0 @@
#ifndef __CAR_H
#define __CAR_H
#define PI 3.141592
#define ANGLESTEP 5
#define TRAINEESIZE 8192
#include "shmdata.h"
class Car
{
private :
SDL_Surface *Surface_car[360];
SDL_Rect rect;
bool carimage;
bool showbar;
bool camera[CAMVECT];
struct shmdata *SData;
int shmid;
int angstep;
int trainee[TRAINEESIZE][2];
int traineepos;
int traineesize;
public :
Car();
void updateCar(SDL_Surface* screen);
void loadImage(char *imagefile);
void initShm();
int incTraineeSize(int);
void incAngle();
void decAngle();
void incSpeed();
void decSpeed();
int incAngleStep(int);
bool APToggle();
bool APState() const;
bool neurToggle();
bool neurState() const;
int getSpeed() const;
int getAngle() const;
bool showBar(int i);
bool* getVect();
bool* getView();
void reset();
void turn180();
void showCamera(SDL_Surface* screen);
void showTrainee(SDL_Surface* screen);
void loadState();
void saveState();
void harakiri();
void setDelay(int del);
bool showNetwork(bool a);
bool showNetworkState();
};
#endif

View File

@@ -1,376 +0,0 @@
#include <SDL.h>
#include <SDL_image.h>
#include <time.h>
#include <iostream>
#include "sdlcommon.h"
#include "fenetre.h"
#include "shmdata.h"
using namespace std;
Fenetre::Fenetre ()
{
obstacle=0;
SHOWcamera=1;
SHOWpilot=1;
fps=0;
font=SFont_InitFont(IMG_Load(FONT_PATH));
if( SDL_Init(SDL_INIT_VIDEO) < 0 )
{
fprintf(stderr, "Could not initialize SDL: %s\n", SDL_GetError());
return ;
}
atexit(SDL_Quit);
screen = SDL_SetVideoMode(800,600, 32, SDL_HWSURFACE);
/* SDL_HWSURFACE / SDL_SWSURFACE */
/*|SDL_ANYFORMAT*/
if( screen==NULL )
{
fprintf(stderr, "Could not create surface: %s\n", SDL_GetError());
return ;
}
return ;
Voiture.loadImage("Cars/voiture4.bmp");
circuit=1;
}
Fenetre::Fenetre (int largeur = 800, int hauteur = 600, int nb_couleurs = 16, char *titre = "Fenetre SDL", char *background=NULL)
{
obstacle=0;
SHOWcamera=1;
SHOWpilot=1;
fps=0;
font=SFont_InitFont(IMG_Load(FONT_PATH));
if (SDL_Init (SDL_INIT_VIDEO) < 0)
{
cerr << "Erreur dans l'initialisation de la librairie" << endl;
}
screen = SDL_SetVideoMode (largeur, hauteur, nb_couleurs, SDL_HWSURFACE | SDL_DOUBLEBUF);
//SDL_CreateRGBSurface(SDL_HWSURFACE, largeur,hauteur,32,0,0,0,0);
if (screen == NULL)
{
cerr << "Erreur dans l'initialisation des modes vidéos:" <<
SDL_GetError ();
SDL_Quit ();
}
SDL_WM_SetCaption (titre, NULL);
fond = SDL_LoadBMP (background);
SDL_BlitSurface (fond, NULL, screen, NULL);
SDL_Flip (screen);
HardBG=SDL_CreateRGBSurface(SDL_HWSURFACE, largeur, hauteur, 32, 0,0,0,0);
SDL_BlitSurface( fond, NULL, HardBG, NULL);
Voiture.loadImage("Cars/voiture4.bmp");
circuit=1;
}
void Fenetre::loadBG(char *background)
{
fond = SDL_LoadBMP (background);
SDL_BlitSurface (fond, NULL, /*screen*/HardBG, NULL);
SDL_BlitSurface( fond, NULL, HardBG, NULL);
SDL_Flip (screen);
}
Fenetre::~Fenetre ()
{
/*
if(fond) SDL_FreeSurface (fond);
if(screen) SDL_FreeSurface (screen);
*/
SDL_Quit ();
}
void Fenetre::showView(int x,int y)
{
bool* v=Voiture.getView();
SDL_Rect r;
r.x=x; r.y=y-1; r.h=1; r.w=4*CAMVECT;
SDL_FillRect(screen,&r, SDL_MapRGB(screen->format, 255,0,0));
r.x=x; r.y=y+4; r.h=1; r.w=4*CAMVECT;
SDL_FillRect(screen,&r, SDL_MapRGB(screen->format, 255,0,0));
for(int i=0;i<CAMVECT;i++)
{
if(v[i])
{
r.x=x+(4*i);
r.y=y;
r.w=4;
r.h=4;
SDL_FillRect(screen,&r, SDL_MapRGB(screen->format, 0,0,0));
}
}
}
void Fenetre::showObstacle()
{
SDL_Rect r;
r.x=400;
r.y=20;
r.w=40;
r.h=screen->h-40;
SDL_FillRect(screen,&r, SDL_MapRGB(screen->format, 255,0,0));
}
void Fenetre::showPilot(int x,int y)
{
if(Voiture.getVect()[0])
for(int i=0;i<16;i++)
{
for(int j=y-i;j<y+i;j++)
putPixel(x+i,j,0,0,0);
}
if(Voiture.getVect()[1])
for(int i=0;i<16;i++)
{
for(int j=y-i;j<y+i;j++)
putPixel(x+64-i,j,0,0,0);
}
if(Voiture.getVect()[2]==1)
for(int i=0;i<16;i++)
{
for(int j=x-i+32;j<x+i+32;j++)
putPixel(j,y+i-32,0,0,0);
}
if(Voiture.getVect()[2]==0)
for(int i=0;i<16;i++)
{
for(int j=x-i+32;j<x+i+32;j++)
putPixel(j,y-i+32,0,0,0);
}
}
void Fenetre::drawText()
{
char *bf=(char*)malloc(256);
sprintf(bf,": %d",Voiture.APState());
SFont_Write(screen,font,10,10,"AutoPilot (a)");
SFont_Write(screen,font,120,10,bf);
sprintf(bf,": %d",Voiture.neurState());
SFont_Write(screen,font,10,25,"NeuronPilot (n)");
SFont_Write(screen,font,120,25,bf);
sprintf(bf,": %d",Voiture.getSpeed());
SFont_Write(screen,font,10,40,"Speed");
SFont_Write(screen,font,120,40,bf);
sprintf(bf,": %d",Voiture.getAngle());
SFont_Write(screen,font,10,55,"Angle");
SFont_Write(screen,font,120,55,bf);
sprintf(bf,": %d",fps);
SFont_Write(screen,font,10,70,"Framerate");
SFont_Write(screen,font,120,70,bf);
sprintf(bf,": %d",Voiture.incTraineeSize(0));
SFont_Write(screen,font,10,85,"trainee(F8,F9)");
SFont_Write(screen,font,120,85,bf);
sprintf(bf,": %d",Voiture.showBar(0));
SFont_Write(screen,font,200,10,"Showbar (b)");
SFont_Write(screen,font,300,10,bf);
sprintf(bf,": %d",SHOWcamera);
SFont_Write(screen,font,200,25,"Showcam (c)");
SFont_Write(screen,font,300,25,bf);
sprintf(bf,": %d",SHOWpilot);
SFont_Write(screen,font,200,40,"Showpilot (p)");
SFont_Write(screen,font,300,40,bf);
sprintf(bf,": %d",obstacle);
SFont_Write(screen,font,200,55,"Obstacle (o)");
SFont_Write(screen,font,300,55,bf);
sprintf(bf,": %d",Voiture.incAngleStep(0));
SFont_Write(screen,font,200,70,"AStep (pgU-D)");
SFont_Write(screen,font,300,70,bf);
sprintf(bf,": %d",Voiture.showNetworkState());
SFont_Write(screen,font,200,85,"NeuronIface(i)");
SFont_Write(screen,font,300,85,bf);
DrawLine(screen,0,99,319,99,0,0,128);
}
void Fenetre::redraw()
{
#ifdef __DEBUG_TIME
Uint32 deltatime;
#endif
if(sec!=time(NULL)) { sec=time(NULL); fps=frame; frame=0; }
frame++;
#ifdef __DEBUG_TIME
deltatime=SDL_GetTicks();
#endif
/* SDL_BlitSurface( fond, NULL, screen, NULL); Software Mode */
SDL_BlitSurface( HardBG, NULL, screen , NULL); /* Hardware Mode */
#ifdef __DEBUG_TIME
printf("Background Blit : %d ms, ",SDL_GetTicks()-deltatime);
deltatime=SDL_GetTicks();
#endif
drawText();
if(SHOWpilot) showPilot(440,50);
if(SHOWcamera) showView(350,10);
if(obstacle) showObstacle();
Voiture.updateCar(screen);
#ifdef __DEBUG_TIME
printf("Car Update : %d ms, ",SDL_GetTicks()-deltatime);
deltatime=SDL_GetTicks();
#endif
SDL_Flip (screen);
#ifdef __DEBUG_TIME
printf("Screen Refresh : %d ms\n",SDL_GetTicks()-deltatime);
#endif
}
void Fenetre::putPixel(int x, int y, char r, char g, char b)
{
setpixel(screen,x,y,r,g,b);
}
void Fenetre::handle()
{
int exitkey = 0;
bool shownet= 0;
int waitdel = 20;
SDL_Event event;
while (!exitkey)
{
SDL_Delay (waitdel);
redraw();
while (SDL_PollEvent (&event))
{
switch (event.type)
{
case SDL_QUIT:
exitkey = 1;
printf ("Quit!\n");
break;
case SDL_KEYDOWN:
switch (event.key.keysym.sym)
{
case SDLK_ESCAPE:
Voiture.harakiri();
exitkey = 1;
break;
case SDLK_LEFT:
Voiture.decAngle();
break;
case SDLK_RIGHT:
Voiture.incAngle();
break;
case SDLK_DOWN:
Voiture.decSpeed();
break;
case SDLK_UP:
Voiture.incSpeed();
break;
case SDLK_a:
Voiture.APToggle();
break;
case SDLK_b:
Voiture.showBar(1);
break;
case SDLK_c:
SHOWcamera=(SHOWcamera+1)%2;
break;
case SDLK_i:
shownet=(shownet+1)%2;
Voiture.showNetwork(shownet);
break;
case SDLK_n:
Voiture.neurToggle();
break;
case SDLK_o:
obstacle=(obstacle+1)%2;
break;
case SDLK_p:
SHOWpilot=(SHOWpilot+1)%2;
break;
case SDLK_r:
Voiture.reset();
break;
case SDLK_t:
Voiture.turn180();
break;
case SDLK_w:
Voiture.saveState();
//SData->action=SAVE_STATE;
break;
case SDLK_x:
Voiture.loadState();
//SData->action=RELOAD_STATE;
break;
case SDLK_F1:
printf("Chargement Circuit 1\n");
circuit=1;
loadBG("Back/bg1.bmp");
break;
case SDLK_F2:
printf("Chargement Circuit 2\n");
circuit=2;
loadBG("Back/bg2.bmp");
break;
case SDLK_F3:
printf("Chargement Circuit 3\n");
circuit=3;
loadBG("Back/bg3.bmp");
break;
case SDLK_F4:
printf("Chargement Circuit 4\n");
circuit=4;
loadBG("Back/bg4.bmp");
break;
case SDLK_F5:
printf("Chargement Circuit 5\n");
circuit=5;
loadBG("Back/bg5.bmp");
break;
case SDLK_F6:
printf("Chargement Circuit 6\n");
circuit=6;
loadBG("Back/bg6.bmp");
break;
case SDLK_F7:
Voiture.incTraineeSize(-256);
break;
case SDLK_F8:
Voiture.incTraineeSize(256);
break;
case SDLK_F9:
printf("Chargement Voiture 1\n");
Voiture.loadImage("Cars/voiture1.bmp");
break;
case SDLK_F10:
printf("Chargement Voiture 2\n");
Voiture.loadImage("Cars/voiture2.bmp");
break;
case SDLK_F11:
printf("Chargement Voiture 3\n");
Voiture.loadImage("Cars/voiture3.bmp");
break;
case SDLK_F12:
printf("Chargement Voiture 4\n");
Voiture.loadImage("Cars/voiture4.bmp");
break;
case SDLK_PAGEUP:
Voiture.incAngleStep(1);
break;
case SDLK_PAGEDOWN:
Voiture.incAngleStep(-1);
break;
case SDLK_HOME:
waitdel+=5;
break;
case SDLK_END:
waitdel-=5;
if(waitdel<0) waitdel=0;
break;
default :
break;
}
break;
}
break;
}
}
}

View File

@@ -1,34 +0,0 @@
#ifndef __FENETRE_H
#define __FENETRE_H
#include "car.h"
#include "SFont.h"
#define FONT_PATH "font.png"
class Fenetre
{
private:
SDL_Surface* screen;
SDL_Surface* fond;
SDL_Surface* HardBG;
SFont_Font* font;
car Voiture;
int frame,sec,fps,circuit;
bool SHOWpilot,SHOWcamera,obstacle;
public:
Fenetre();
Fenetre (int , int , int , char *, char *);
~Fenetre();
void loadBG(char *);
void redraw();
void handle();
void putPixel(int , int , char, char, char);
void showView(int ,int);
void showPilot(int,int);
void showObstacle();
void drawText();
};
#endif

View File

@@ -1,26 +0,0 @@
#ifndef __SDLCOMMON_H
#define __SDLCOMMON_H
#ifndef PI
#define PI 3.141592
#endif
#ifndef NULLCOL
#define NULLCOL 0x00FF00FF
#define NULLCOLA 0xFF
#define NULLCOLR 0xFF
#define NULLCOLG 0x00
#define NULLCOLB 0xFF
#endif
void setpixel(SDL_Surface*, int, int, Uint8, Uint8, Uint8);
int getpixel(SDL_Surface*, int, int);
void DrawLine(SDL_Surface*, int , int , int , int , char , char, char);
void rotatesurface(SDL_Surface*, SDL_Surface*, int);
void interpole(SDL_Surface* , SDL_Surface* , Uint32 );
bool inCarArea(SDL_Surface* , int , int );
#endif

View File

@@ -1,33 +0,0 @@
#ifndef __SHMDATA_H
#define __SHMDATA_H
#define SHMKEY 5678
#define CAMERASIZE 128
#define CAMERASTEP 2
#define CAMERADISTANCE 50
#define CAMVECT (CAMERASIZE/CAMERASTEP)
#define NOMBRE_NEURONES_INTERMEDIAIRES 8
#define TAILLERESULTAT 3
#define SAVE_STATE 1
#define RELOAD_STATE 2
#define STATE_FILE "State.network"
struct shmdata
{
bool V[CAMVECT]; /* vecteur camera */
bool Res[3]; /* 0 gauche 1 droite 2 avancer 3 stop*/
int read; /* 1 = info a ete traitée */
int angle; /* angle de la voiture 0->359 */
int speed; /* vitesse */
int APon; /* pilote automatique (mode apprentissage) (0/1) */
int Neuron; /* activer le reseau de neurone (0/1) */
double pos_x; /* position x de la voiture */
double pos_y; /* position y de la voiture */
int action; /* action a executer (genre sauvegarde) */
int delay; /* delai entre les frames */
};
#endif

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@@ -1,11 +0,0 @@
#ifndef _GLOBAL_H
#define _GLOBAL_H
#define SYNAPSE_WEIGHT_MAX 2.0
#define SYNAPSE_WEIGHT_MIN -2.0
#define LEARNING_RATE 0.01
#define MOMENTUM 0.1
#define ACCEPT 0.65 /* Was 0.95 */
#define LEARNACCEPT 0.15 /* Was 0.05 */
#endif

View File

@@ -1,184 +0,0 @@
#include "reseau.h"
Reseau::Reseau()
{
}
Reseau::Reseau(int In, int Hid, int Out)
{
In++; // pour le noeud de bias
Ocouche=OutputCouche(Out);
Hcouche=HiddenCouche(Hid,&Ocouche);
Icouche=InputCouche(In,&Hcouche);
Ocouche.setPrevCouche(&Hcouche);
//Icouche.printSynapseMatrix();
//Hcouche.printSynapseMatrix();
}
void Reseau::rebuild(int In, int Hid, int Out) // METHODE BOURRAIN SPOTTED
{
In++;
Ocouche=OutputCouche(Out);
Hcouche=HiddenCouche(Hid,&Ocouche);
Icouche=InputCouche(In,&Hcouche);
Ocouche.setPrevCouche(&Hcouche);
Icouche.printSynapseMatrix();
Hcouche.printSynapseMatrix();
}
Reseau::~Reseau(){
}
void Reseau::saveState(const char* filename) throw(std::string){
unsigned sizeinput = Icouche.getNumber();
unsigned sizehidden = Hcouche.getNumber();
unsigned sizeoutput = Ocouche.getNumber();
std::ofstream outFile(filename);
if(outFile.fail()){
throw(std::string("Couldn't open output file"));
}
outFile << sizeinput << " " << sizehidden << " " << sizeoutput << endl;
for( unsigned int i=0 ; i < sizeinput ; ++i )
for( unsigned int j=0 ; j < sizehidden ; ++j )
outFile << Icouche.getSynapse(i,j).getWeight() << " " << endl;
for( unsigned int i=0 ; i < sizehidden ; ++i )
for( unsigned int j=0 ; j < sizeoutput ; ++j )
outFile << Hcouche.getSynapse(i,j).getWeight() << " " << endl;
outFile.close();
}
void Reseau::loadState(const char* filename) throw(std::string){
std::ifstream inpFile(filename);
unsigned int sizeinput,sizehidden,sizeoutput;
if(inpFile.fail()){
cout << "Can't open input file" << endl;
throw(std::string("Couldn't open output file"));
}
if(!(inpFile >> sizeinput >> sizehidden >> sizeoutput))
cout<<"Super error!!!" << endl;
if(sizeinput!=Icouche.getNumber() || sizehidden!=Hcouche.getNumber() || sizeoutput != Ocouche.getNumber()){
throw(std::string("Wrong size of Couche in file"));
}
double tmpDouble;
//for( unsigned int i=0 ; i < sizeinput ; ++i )
for( unsigned int i=0 ; i < sizeinput ; ++i ){
for( unsigned int j=0 ; j < sizehidden ; ++j ){
if(!(inpFile >> tmpDouble)){
cout << "Input ERROR" << endl;
}
Icouche.getSynapse(i,j).setWeight(tmpDouble);
}
}
for( unsigned int i=0 ; i < sizehidden ; ++i ){
for( unsigned int j=0 ; j < sizeoutput ; ++j ){
if(!(inpFile >> tmpDouble)){
cout << "Input ERROR" << endl;
}
Hcouche.getSynapse(i,j).setWeight(tmpDouble);
}
}
//cout << "==========After=========" << endl;
Icouche.printSynapseMatrix();
inpFile.close();
//cout << sizeinput << " " << sizehidden << " " << sizeoutput << endl;
}
std::vector<double> Reseau::forward(bool input[])
{
std::vector<bool> tmp;
/* on active les couches */
for(unsigned i=0;i<(Icouche.getNumber()-1);i++) /* faut passer n-1 brol dans le vecteur d'activation car il y a le neurone de bias ... */
{
tmp.push_back(input[i]);
}
Icouche.activate(tmp);
Hcouche.activate(Icouche);
Ocouche.activate();
/* on place le resultat dans la shm */
//for(unsigned i=0;i<Ocouche.getNumber();i++) target[i]=util.accept(Ocouche[i].getWeight());
std::vector<double> resultat;
for(unsigned i=0;i<Ocouche.getNumber();i++) resultat.push_back(Ocouche[i].getWeight());
return resultat;
}
void Reseau::backward(bool input[], bool target[])
{
std::vector<double> hidDelta;
std::vector<double> outDelta;
double error;
hidDelta.clear();
outDelta.clear();
/* Calcul des delta pour la couche OUPUT */
for(unsigned i=0;i<Ocouche.getNumber();i++)
{
error = (double)target[i] - Ocouche[i].getWeight();
outDelta.push_back(util.dsigmoid(Ocouche[i].getWeight()) * error);
}
/* Calcul des delta pour la couche HIDDEN */
for(unsigned i=0;i<Hcouche.getNumber();i++){
error = 0.0;
for(unsigned j=0;j<Ocouche.getNumber();++j){
error += outDelta[j] * Hcouche.getSynapse(i,j).getWeight();
}
hidDelta.push_back(util.dsigmoid(Hcouche[i].getWeight()) * error);
}
Ocouche.backPropagate(outDelta);
Icouche.backPropagate(hidDelta);
}
void Reseau::initshm()
{
if ((shmid = shmget(SHMKEY, sizeof(struct shmdata), 0666)) < 0)
{
perror("Unable to get shm id \n");
exit(1);
}
if ((SData = (struct shmdata *)shmat(shmid, NULL, 0)) == (struct shmdata *) -1)
{
perror("Unable to attach shm segment\n");
exit(1);
}
}
double Reseau::getError(bool target[])
{
double error=0.0;
for(unsigned i=0;i<Ocouche.getNumber();i++)
{
error += pow(((double)target[i] - Ocouche[i].getWeight()),2);
}
return sqrt(error);
}
double Reseau::learnOne(bool input[], bool target[])
{
double error=0;
forward(input);
backward(input,target);
error=getError(target);
return error;
}
double Reseau::learnAll(std::vector<bool *> inputs, std::vector<bool *> targets)
{
double error=0.0;
unsigned i;
for(i=0;i<inputs.size();i++)
{
error+=learnOne(inputs[i],targets[i]);
}
return (double)(error/i);
}
Reseau& Reseau::operator=(const Reseau& c){
if(this!=&c){
Icouche=c.Icouche;
Hcouche=c.Hcouche;
Ocouche=c.Ocouche;
SData=c.SData;
shmid=c.shmid;
}
return *this;
}
/* END */

View File

@@ -1,46 +0,0 @@
#ifndef __RESEAU_H
#define __RESEAU_H
#include <iostream>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <vector>
#include <string>
#include <fstream>
#include "couche.h"
#include "inputCouche.h"
#include "hiddenCouche.h"
#include "outputCouche.h"
#include "../InterfaceCpp/shmdata.h"
#include "Utils.h"
#include "global.h"
class Reseau {
private:
struct shmdata *SData;
int shmid;
Utils util;
public:
InputCouche Icouche;
HiddenCouche Hcouche;
OutputCouche Ocouche;
Reseau();
Reseau(int In, int Hid, int Out);
void rebuild(int In, int Hid, int Out);
~Reseau();
// void initshm();
std::vector<double> forward(bool input[]);
void backward(bool input[], bool target[]); /* */
double getError(bool target[]);
double learnOne(bool input[], bool target[]); /* */
double learnAll(std::vector<bool *> inputs, std::vector<bool *> targets);
void saveState(const char* filename) throw(std::string);
void loadState(const char* filename) throw(std::string);
Reseau& operator=(const Reseau& c);
};
#endif