/* =======================================================
	Neural Network Classes for the NeXT Computer
	Written by: Ralph Zazula
					University of Arizona
					zazula@bonehead.tucson.az.us (NeXT Mail)
==========================================================*/
//
// This feed-forward, back-prop network is setup to learn
// the identity function (output=input).  The number N is the
// number of inputs (outputs) and H is the number of hidden
// nodes.  The case (H < N) is most interesting - this is a form
// of data compression.
// The number P determines the number of random patterns to learn.
//

#import "BackPropEngine.h"
#import <stdio.h>
#define N 8
#define H 4
#define P 10

void main()
{
   id    bp = [[BackPropEngine alloc]  initWithInputs:N
                                       hidden:H
                                       outputs:N];
   id		random = [[Random alloc] init];
	
   double inputs[P][N];
   double target[P][N];
   
   int   i,j;
   
   [bp setEta:0.9];
   
	for(j=0; j<P; j++)
		for(i=0; i<N; i++)
			inputs[j][i] = target[j][i] = [random percent]*0.8;
			
   for(j=0; j<100000; j++) {
		for(i=0; i<20; i++) {
   		[bp applyInput:inputs[j%P]];
      	[bp correctWithTarget:target[j%P]];
		}
      if(!(j % P))									// BPE doesn't average the errors
         printf("%u %e\n",j,[bp getError]);  // should do that here...
   }
   
   for(j=0; j<[[bp inputs] count]; j++) {
      for(i=0; i<[[bp hidden] count]; i++)
         printf("%10e  ",[[[bp hidden] objectAt:i]
            getWeightFor:[[bp inputs] objectAt:j]]);
      printf("\n");
   }
	printf("-----\n");
   for(j=0; j<[[bp hidden] count]; j++) {
      for(i=0; i<[[bp outputs] count]; i++)
         printf("%10e  ",[[[bp outputs] objectAt:i]
            getWeightFor:[[bp hidden] objectAt:j]]);
      printf("\n");
   }
}
