% xarxa.m nntwarn OFF % Fitxer: per mail %--------------------------------------------------------- % Amb aquest script s'entrena una xarxa neuronal per % classificar una situacio de joc: un jugador i una pilota. % Classes: ATACAR, DEFENSAR. % % 1- L'usuari entra N situacions ja ell mateix classifica % 2- S'engega el proces d'entrenament % 3- L'usuari pot comprovar el funcionament de la xarxa % % Model: feed forward, 3 capes, TANSIG % Josep Lluis de la Rosa, 4 de novembre de 1998 % (després d'una llarga, llarga nit...) %--------------------------------------------------------- %-------------Configuracions previes clear xrobot yrobot xpilota ypilota P T W b p; N=7; LLARG=48; AMPLA=36; %-------------dibuixa camp de joc... whitebg hold off plot([1 1 LLARG LLARG 1],[1 AMPLA AMPLA 1 1]) hold on; plot([LLARG/2 LLARG/2],[1 AMPLA]) title('Gol cap a la dreta') %--1-----------Entrada de posicions dels objectes (amb mouse)------------ disp('Proces d_entrenament d_una xarxa neuronal') a=['Entra l_accio a executar davant ' num2str(N) ' situacions diferents']; disp(a) disp('Les accions possibles son : ATACAR i DEFENSAR ') disp('i les situacions son les que tu vulguis.') for i=1:N, disp(['------------------' num2str(i) '----------------------']) disp('Indica on es el robot') [x y]=ginput(1); plot(x,y,'*'); xrobot(i)=fix(x); yrobot(i)=fix(y); disp('Indica on es la pilota') [x y]=ginput(1); xpilota(i)=fix(x); ypilota(i)=fix(y); plot(x,y,'o'); line([xrobot(i) x],[yrobot(i) y]); T(i)=input('Tipus d_accio? [Atacar=1/Defensar=0]'); end %--2------------Entreno la xarxa...---------------------------- P=[xrobot' yrobot' xpilota' ypilota']'; %--amb HARDLIM-- %[W,b]=initp(P,T) %[W,b,epochs,errors]=trainp(W,b,P,T,-1) %--amb LINEAR--(no convergeix prou be) %[W,b]=initlin(P,T) %tp=[50 1000 0.03]; %[W,b]=trainwh(W,b,P,T,tp) %-- amb FEED FORWARD perceptró multicapa (Santi help) [W1,B1,W2,B2,W3,B3]=initff(P,10,'tansig',10,'tansig',T,'tansig') df = 5; % Frequency of progress displays (in epochs). me = 100; % Maximum number of epochs to train. eg = 0.01; % Sum-squared error goal. lr = 2; % Learning rate. tp=[50 5000 0.01]; [W1,B1,W2,B2,W3,B3,TE,TR] = TRAINBP(W1,B1,'tansig',W2,B2,'tansig',W3,B3,'tansig',P,T,tp) % Wi - SixR weight matrix for the ith layer. % Bi - S1x1 bias vector for the ith layer. % Fi - Transfer function (string) for the ith layer. % P - RxQ matrix of input vectors. % T - SxQ matrix of target vectors. % TP - Training parameters (optional). % Returns new weights and biases and % Wi - new weights. % Bi - new biases. % TE - the actual number of epochs trained. % TR - training record: [row of errors] %--3------------i ara puc fer el test:--------------------------- a=input('Prem tecla...'); plot([1 1 LLARG LLARG 1],[1 AMPLA AMPLA 1 1]) hold on; plot([LLARG/2 LLARG/2],[1 AMPLA]) title('Gol cap a la dreta') M=input('Quantes situacions vols provar?'); for i=1:M, disp(['------------------' num2str(i) '----------------------']) disp('Indica on es el robot') [x y]=ginput(1); plot(x,y,'*'); p(1)=fix(x); p(2)=fix(y); disp('Indica on es la pilota') [x y]=ginput(1); p(3)=fix(x); p(4)=fix(y); plot(x,y,'o'); line([p(1) x],[p(2) y]); % LINEAR: % av=simulin(p',W,b) % HARDLIM: % av=simup(p',W,b) % FEED FORWARD av = simuff(p',W1,B1,'tansig',W2,B2, 'tansig', W3, B3,'purelin') if av > 0.5 disp('ATACAR') else disp('DEFENSAR') end end