/* *************************************************************  
 
        Final Project                                 MMU 1996

        ######################################################
        #                                                    #
	#               A Modular Neural Network             #	
        #                                                    #
	#               THE BACKPROPAGATION CLASS            #
        #                                                    #
        ######################################################
   
        Albrecht Schmidt                              09.08.96  
   
        FILE: CBackPro.h                           Version 1.0
 
   ************************************************************* */
#ifndef _CBackPro_h
#define _CBackPro_h

#include <stdio.h>
#include <stdlib.h>

#include "global.h"
#include "CSupport.h"
#include "CLayer.h"

class CBackPro : CSupport
{
  private:
        int        no_inputs, no_layers;
        TFunction  function;

	double     lambda;

	FILE       *logfd;

	int 	   logStatus;    // is either ON or OFF
	int 	   screenStatus; // is either ON or OFF
	int        no_neurons[MAX_LAYER];
	CLayer 	   *layer[MAX_LAYER];


  public:

/* This is a Constructor:
   It is used to set up a network with the known parameters.
   The number of neurons in the last used layer is equal to
   the number of outputs.                                        */
CBackPro::CBackPro(int no_i,      // no of inputs
                   int no_l,      // no of layers
                   double la,     // lambda value 
                   TFunction f,   // tranfer function
                   char *logfile, // name of the log file
                                  // use NO_LOG for skipping the log
                   int neu0,      // no of neurons in layer 0
                   int neu1,      // no of neurons in layer 1 
                   int neu2=0, 
		   int neu3=0,	  // no of neurons in the following 
                   int neu4=0, 	  // layers (default = 0)
                   int neu5=0);

/* This is a Constructor:
   The first parameter is the name of file which contains a description
   of a network (with or without weights). The net description files
   might be type by using any editor or generated by the method
   CBackPro::StoreNet(...) . The second parameter is the 
   name of the logfile (NO_LOG makes no logfile).             */ 
CBackPro::CBackPro(char *filename,      // net description file
                   char *logfile);      // name of the logfile


// The destructor 
CBackPro::~CBackPro();

// Reset the weighst to a random value in a Range between min and max 
void CBackPro::ResetWeights(double min=MIN_RND, double max=MAX_RND);

// Store the network in a file 
void CBackPro::StoreNet(char *filename);

 
// train the network on a file
double CBackPro::Train(double eta,        // the learning constant
                      double maxError,   // the accepted maximal error
                      char * filename,  // the file to learn
                      int steps,        // the maximal number of steps
                      char *errfile = "error.dat",
                                        // the name of the error file
                      double alpha =0) ; // the momentum
 
// calculate for an input file the results
void CBackPro::Work(char * infile,    // input file with input data
                    char *outfile);    // the result file
 
 
// Test the performance on a data file (with input and desired
// values)
double CBackPro::Test( char *testfile,              // the data file
                      double max_difference = 0.5); // the difference between
                                                   // desired and output
 
// Learn one vector for which the result is known 
double CBackPro::Learn(TVector inpV,       // the vector to learn
		      TVector tarV,        // the desired result
		      double eta,          // the learning constant
		      double alpha);       // the momentum

// calculate the output for a given input
TVector CBackPro::Apply(TVector outV, TVector inpV);

// functions to activate the writing of the Logfile
void CBackPro::LogWriteOn();

// functions to deactivate the writing of the Logfile
void CBackPro::LogWriteOff();

// functions to activate the writing on the screen
void CBackPro::ScreenOn();

// functions to deactivate the writing on the screen
void CBackPro::ScreenOff();
};
#endif
