/* *************************************************************

        Final Project                                 MMU 1996

        ######################################################
        #                                                    #
        #               A Modular Neural Network             #   
        #                                                    #
        #              A TWO-LAYER MODULAR NETWORK           #   
        #                                                    #
        ######################################################

        Albrecht Schmidt                              09.08.96

        FILE: CMul2L.h                             Version 1.0

   ************************************************************* */
#ifndef _CMul2L_h
#define _CMul2L_h


#include <stdlib.h>
#include "neuro.h"
#include "CMLayer.h"

/* In this class a library for a two-layer modular neural network
   is implemented. It uses one instance of a single modular Layer
   and an additional decision network.                           */
class CMul2L : CSupport
{
  private:
	int no_user_inps;     // number of required inputs 
                              // (determined by the problem)
	int no_classes;       // number of required classes
                              // (determined by the problem)
	int no_nets_L1;       // number of modules in the input layer
	int no_out_net_L1;    // number of outputs per net in the input layer
                              // dependent on the representaion 
	int no_main_inps;     // number of inputs to the decision net
                              // = no_nets_L1 * no_out_net_L1
	int no_main_outs;     // number of outputs of the decision net
			      // in this case the number of classes
	TNetDef *def_nets_L1; // the struct with the definition of the 
                              // networks in the first layer (definition
 	                      // in CMLayer.h)
	TNetDef *def_nets_m;  // the struct with the definition of the 
                              // decision networks (definition 
                              // in CMLayer.h)
	CMulLayer *layer1;    // The modular input layer
	CBackPro *main_net;   // the decision network
	FILE *logfd;          // the log-file Pointer
	int  logStatus;       // is either ON or OFF


  public:
	// the constructor with parameters
	CMul2L::CMul2L( int no_u_inps,     // number of user inputs
			int no_cl,         // number of classes
			TNetDef *netL1,    // definition of the
					   // networks in the input layer
			TNetDef *net_m,    // definition of the decision
					   // networks
			char *logfile = "multi.log",// the log file
			int mapping = RND_MAPPING,  // mapping of inputs onto
						    // the input modules
			TOutRep out_rep = small);   // the representation of the
						    // classes in the first layer
	// the constructor from file
	CMul2L::CMul2L(	char *filename,    // the name of the desciption file
			char *dirname = "net_dir",
			char *logfile = "multi.log"); // the log file

	// the destructor
	CMul2L::~CMul2L();

	// function to store the network
	int CMul2L::StoreNet( char *filename , char *dirname = "net_dir");

	// calculate the response for an input vector (the vector) 
	TVector CMul2L::Apply( TVector resV,  // allocated result vector
                               TVector inV ); // input vector
		// returns the calculated output vector

	// reset the weights in the network
	void CMul2L::ResetWeights(double min = MIN_RND, double max = MAX_RND);

	// calculate the respone for an input vector (the class number)
	int CMul2L::Detect(TVector inV, // input vector
			   float diff = 0.01); // the wanted difference 
		// Returns the class number of -1 if the deifference
		// between the winner and the runner-up is smaller than diff

	// train the first modular layer on a file
	int CMul2L::TrainL1( char *datafile,    // the data file 
			     int maxStep,       // the number of steps (maximal)
                             double maxError,   // the aceptable error
		             char *err_file = "err.1", //name of the error file
			     double eta=1,      // lerning constant
	                     double alpha = 0); // momentum	
		// Returns the number of training steps performed
                // or -1 if the datafile does not exist

	// train the decision network on a file
	int CMul2L::Train_m( char *datafile,    // the data file     
			     int maxStep,       // the number of steps (maximal)
			     double maxError,   // the aceptable error
                             char *err_file = "err.m", //name of the error file
                             double eta=1,      // lerning constant
                             double alpha = 0); // momentum 
		// Returns the number of training steps performed
                // or -1 if the datafile does not exist

	// train the whole network
	void CMul2L::Train( char *datafile, // the data file
			int maxStep_1, double maxError_1, // input layer
			int maxStep_m, double maxError_m, // decision network
			char *err_file_1 = "err.1" , char *err_file_m = "err.m",
			double eta=1, double alpha = 0);

	// test the performance of the network
	double CMul2L::Test(char *filename, // the data file
			    double diff =0.01,
				    // the required difference between
				    // winner and runner-up
			    int displ = 0); // if displ >= 1 each result is
					    // printed on the screen
		//Returns the performance in percent (0..100%)

	// calculate the output for given inputs
	void CMul2L::Work(char *infile,  // the input data
			  char *outfile, // the output file
			  int datefiletype =1,
			     // the type of the data file
			     // 0 = with    ( 1 0 1 0 : 1 ;)
			     // 1 = without ( 1 0 1 0 ;)
			  int displ = 0,
			     // if displ > 0 each result is
			     // printed on the screen
			  double diff = 0.01,
			     // the required difference between
			     // winner and runner-up
			  int o_mode = 1);    // output mode 0= class only
					      //             1= vector

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

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

};

#endif

