#include "recall.h"

//Function Prototype Flashes


NSRecallNetwork::NSRecallNetwork()
{
	m_usiWeightFileVersion = 100;
	m_dpInputNormAmp = NULL;
	m_dpInputNormOffset = NULL;
	m_dpOutputNormAmp = NULL;
	m_dpOutputNormOffset = NULL;
	m_bNormalizeInput = false;
	m_bNormalizeOutput = false;
	m_ipInputDataMap = NULL;

}

int NSRecallNetwork::createNetwork()
{
	srand((unsigned) time(NULL));
	createComponents();
	setInitializations();
	setInterconnections();
	postInterconnections();
	return 0;
}

int NSRecallNetwork::createComponents()
{
    inputAxon = new Axon;
    hidden1Synapse = new FullSynapse;
    hidden1Axon = new TanhAxon;
    hidden2Synapse = new FullSynapse;
    hidden2Axon = new TanhAxon;
    hidden3Synapse = new FullSynapse;
    hidden3Axon = new TanhAxon;
    hidden4Synapse = new FullSynapse;
    hidden4Axon = new TanhAxon;
    hidden5Synapse = new FullSynapse;
    hidden5Axon = new TanhAxon;
    outputSynapse = new FullSynapse;
    outputAxon = new TanhAxon;
    control = new DynamicControl;
    recurrentSynapse = new FullSynapse;
    forwardSynapse = new FullSynapse;

	return 0;
}

int NSRecallNetwork::setInitializations()
{
inputAxon->setTemporalDimension(40);
inputAxon->setRows(36);
hidden1Axon->setTemporalDimension(40);
hidden1Axon->setRows(72);
hidden2Synapse->setTemporalDimension(1);
hidden2Axon->setTemporalDimension(40);
hidden2Axon->setRows(54);
hidden3Axon->setTemporalDimension(40);
hidden3Axon->setRows(36);
hidden4Axon->setTemporalDimension(40);
hidden4Axon->setRows(36);
hidden5Axon->setTemporalDimension(40);
hidden5Axon->setRows(18);
outputAxon->setTemporalDimension(40);
outputAxon->setRows(2);
control->setPhases(1);
control->setCostOfBestWeights(1.0000000000000000e+009);
control->setEpochOfBestWeights(0);
control->setLearningType((NLearningType)0);
control->allocateNewLearningGlobals();
control->setWeightsRandomized(FALSE);
control->setOptimizeInitialWeights(FALSE);
control->setInitialLambda(1.0000000000000000e-002);
recurrentSynapse->setTemporalDimension(1);

	m_iNumberOfInputs = inputAxon->U();
	m_iNumberOfOutputs = outputAxon->V();

	m_iNumberOfInputsAfterMask = m_iNumberOfInputs;

	return 0;
}

int NSRecallNetwork::setInterconnections()
{
inputAxon->setNext(hidden1Synapse);
inputAxon->setNext(forwardSynapse);
hidden1Synapse->setLast(inputAxon);
hidden1Synapse->setNext(hidden1Axon);
hidden1Axon->setLast(hidden1Synapse);
hidden1Axon->setLast(recurrentSynapse);
hidden1Axon->setNext(hidden2Synapse);
hidden1Axon->setNext(recurrentSynapse);
hidden2Synapse->setLast(hidden1Axon);
hidden2Synapse->setNext(hidden2Axon);
hidden2Axon->setLast(hidden2Synapse);
hidden2Axon->setLast(forwardSynapse);
hidden2Axon->setNext(hidden3Synapse);
hidden3Synapse->setLast(hidden2Axon);
hidden3Synapse->setNext(hidden3Axon);
hidden3Axon->setLast(hidden3Synapse);
hidden3Axon->setNext(hidden4Synapse);
hidden4Synapse->setLast(hidden3Axon);
hidden4Synapse->setNext(hidden4Axon);
hidden4Axon->setLast(hidden4Synapse);
hidden4Axon->setNext(hidden5Synapse);
hidden5Synapse->setLast(hidden4Axon);
hidden5Synapse->setNext(hidden5Axon);
hidden5Axon->setLast(hidden5Synapse);
hidden5Axon->setNext(outputSynapse);
outputSynapse->setLast(hidden5Axon);
outputSynapse->setNext(outputAxon);
outputAxon->setLast(outputSynapse);

recurrentSynapse->setLast(hidden1Axon);
recurrentSynapse->setNext(hidden1Axon);
forwardSynapse->setLast(inputAxon);
forwardSynapse->setNext(hidden2Axon);


	return 0;
}

int NSRecallNetwork::postInterconnections()
{
control->prepareToFire(NULL);

	preRunGetReady();
control->preFireGetReady(FALSE);

	postFireGetReady();

	return 0;
}

int NSRecallNetwork::preRunGetReady()
{
inputAxon->setStaticControl(control);
inputAxon->preFireGetReady();
hidden1Synapse->setStaticControl(control);
hidden1Synapse->preFireGetReady();
hidden1Axon->setStaticControl(control);
hidden1Axon->preFireGetReady();
hidden2Synapse->setStaticControl(control);
hidden2Synapse->preFireGetReady();
hidden2Axon->setStaticControl(control);
hidden2Axon->preFireGetReady();
hidden3Synapse->setStaticControl(control);
hidden3Synapse->preFireGetReady();
hidden3Axon->setStaticControl(control);
hidden3Axon->preFireGetReady();
hidden4Synapse->setStaticControl(control);
hidden4Synapse->preFireGetReady();
hidden4Axon->setStaticControl(control);
hidden4Axon->preFireGetReady();
hidden5Synapse->setStaticControl(control);
hidden5Synapse->preFireGetReady();
hidden5Axon->setStaticControl(control);
hidden5Axon->preFireGetReady();
outputSynapse->setStaticControl(control);
outputSynapse->preFireGetReady();
outputAxon->setStaticControl(control);
outputAxon->preFireGetReady();
recurrentSynapse->setStaticControl(control);
recurrentSynapse->preFireGetReady();
forwardSynapse->setStaticControl(control);
forwardSynapse->preFireGetReady();


	return 0;
}

int NSRecallNetwork::postFireGetReady()
{
control->setCrossValidationEnabled(TRUE);
control->clearEnginesToFire();
control->addToEnginesToFireList(recurrentSynapse, TRUE);
control->addToEnginesToFireList(hidden2Synapse, TRUE);
control->addToEnginesToFireList(inputAxon, FALSE);
control->determineFiringSequence();
control->fireConfirmReady();


	return 0;
}





int NSRecallNetwork::getInputs(int &inputs)
{
	inputs = m_iNumberOfInputs;
	return 0;
}

int NSRecallNetwork::getOutputs(int &outputs)
{
	outputs = m_iNumberOfOutputs;
	return 0;
}

int NSRecallNetwork::loadWeights(char *weightsPathName)
{
	FILE *weightsFile;
	weightsFile = fopen(weightsPathName,"r");
	if (!weightsFile)
		return 1;
	
	if (loadComponentWeights(weightsFile))
		return 2;
	
	if (loadNormalizationCoefficients(weightsFile))
		return 3;
	
	fclose(weightsFile);
	return 0;
}

int NSRecallNetwork::loadComponentWeights(FILE *&weightsFile)
{
	int lcw_errorcode = 0;
	m_usiWeightFileVersion = getWeightFileVersion(weightsFile);

	if (!seekComponentReturnNull(weightsFile, inputAxon->className(), "inputAxon"))
		lcw_errorcode = 1;
	else
		inputAxon->loadWeights(weightsFile, m_usiWeightFileVersion);

	hidden1Synapse->retryConnections();
	if (!seekComponentReturnNull(weightsFile, "FullSynapse", "hidden1Synapse"))
		lcw_errorcode = 1;
	else
		hidden1Synapse->loadWeights(weightsFile, m_usiWeightFileVersion);

	if (!seekComponentReturnNull(weightsFile, hidden1Axon->className(), "hidden1Axon"))
		lcw_errorcode = 1;
	else
		hidden1Axon->loadWeights(weightsFile, m_usiWeightFileVersion);

	hidden2Synapse->retryConnections();
	if (!seekComponentReturnNull(weightsFile, "FullSynapse", "hidden2Synapse"))
		lcw_errorcode = 1;
	else
		hidden2Synapse->loadWeights(weightsFile, m_usiWeightFileVersion);

	if (!seekComponentReturnNull(weightsFile, hidden2Axon->className(), "hidden2Axon"))
		lcw_errorcode = 1;
	else
		hidden2Axon->loadWeights(weightsFile, m_usiWeightFileVersion);

	hidden3Synapse->retryConnections();
	if (!seekComponentReturnNull(weightsFile, "FullSynapse", "hidden3Synapse"))
		lcw_errorcode = 1;
	else
		hidden3Synapse->loadWeights(weightsFile, m_usiWeightFileVersion);

	if (!seekComponentReturnNull(weightsFile, hidden3Axon->className(), "hidden3Axon"))
		lcw_errorcode = 1;
	else
		hidden3Axon->loadWeights(weightsFile, m_usiWeightFileVersion);

	hidden4Synapse->retryConnections();
	if (!seekComponentReturnNull(weightsFile, "FullSynapse", "hidden4Synapse"))
		lcw_errorcode = 1;
	else
		hidden4Synapse->loadWeights(weightsFile, m_usiWeightFileVersion);

	if (!seekComponentReturnNull(weightsFile, hidden4Axon->className(), "hidden4Axon"))
		lcw_errorcode = 1;
	else
		hidden4Axon->loadWeights(weightsFile, m_usiWeightFileVersion);

	hidden5Synapse->retryConnections();
	if (!seekComponentReturnNull(weightsFile, "FullSynapse", "hidden5Synapse"))
		lcw_errorcode = 1;
	else
		hidden5Synapse->loadWeights(weightsFile, m_usiWeightFileVersion);

	if (!seekComponentReturnNull(weightsFile, hidden5Axon->className(), "hidden5Axon"))
		lcw_errorcode = 1;
	else
		hidden5Axon->loadWeights(weightsFile, m_usiWeightFileVersion);

	outputSynapse->retryConnections();
	if (!seekComponentReturnNull(weightsFile, "FullSynapse", "outputSynapse"))
		lcw_errorcode = 1;
	else
		outputSynapse->loadWeights(weightsFile, m_usiWeightFileVersion);

	if (!seekComponentReturnNull(weightsFile, outputAxon->className(), "outputAxon"))
		lcw_errorcode = 1;
	else
		outputAxon->loadWeights(weightsFile, m_usiWeightFileVersion);

	if (!seekComponentReturnNull(weightsFile, control->className(), "control"))
		lcw_errorcode = 1;
	else
		control->loadWeights(weightsFile, m_usiWeightFileVersion);

	recurrentSynapse->retryConnections();
	if (!seekComponentReturnNull(weightsFile, "FullSynapse", "recurrentSynapse"))
		lcw_errorcode = 1;
	else
		recurrentSynapse->loadWeights(weightsFile, m_usiWeightFileVersion);

	forwardSynapse->retryConnections();
	if (!seekComponentReturnNull(weightsFile, "FullSynapse", "forwardSynapse"))
		lcw_errorcode = 1;
	else
		forwardSynapse->loadWeights(weightsFile, m_usiWeightFileVersion);


	if (inputAxon->U() != getNumberOfInputs() || outputAxon->V() != m_iNumberOfOutputs)
		return 2;

	return lcw_errorcode;
}

int NSRecallNetwork::getNumberOfInputs()
{
	// Returns the actual number of inputs used by the neural network.  If an input
	// data mask is being used, this may be less than m_iNumberOfInputs.
	if (m_ipInputDataMap)
		return m_iNumberOfInputsAfterMask;
	else
		return m_iNumberOfInputs;
}

int NSRecallNetwork::loadNormalizationCoefficients(FILE *&weightsFile)
{
    int numberOfCoefficients, i;

    if (seekComponentReturnNull(weightsFile, "File", "inputFile"))
    {    
         fscanf(weightsFile,"%d",&numberOfCoefficients);
         if (numberOfCoefficients != getNumberOfInputs())
             return 1;

         m_bNormalizeInput = true;

         if (m_dpInputNormAmp)
             delete m_dpInputNormAmp;
         m_dpInputNormAmp = new double[numberOfCoefficients];

         if (m_dpInputNormOffset)
             delete m_dpInputNormOffset;
         m_dpInputNormOffset = new double[numberOfCoefficients];

         for (i = 0; i < numberOfCoefficients; i++)
         {
             fscanf(weightsFile,"%lf", m_dpInputNormAmp + i);
             fscanf(weightsFile,"%lf", m_dpInputNormOffset + i);
         }
    }
    else
         m_bNormalizeInput = false;

    if (seekComponentReturnNull(weightsFile, "File", "desiredFile"))
    {    
         fscanf(weightsFile,"%d",&numberOfCoefficients);
         if (numberOfCoefficients != m_iNumberOfOutputs)
             return 1;

         m_bNormalizeOutput = true;

         if (m_dpOutputNormAmp)
             delete m_dpOutputNormAmp;
         m_dpOutputNormAmp = new double[m_iNumberOfOutputs];

         if (m_dpOutputNormOffset)
             delete m_dpOutputNormOffset;
         m_dpOutputNormOffset = new double[m_iNumberOfOutputs];

         for (i = 0; i < m_iNumberOfOutputs; i++)
         {
             fscanf(weightsFile,"%lf", m_dpOutputNormAmp + i);
             fscanf(weightsFile,"%lf", m_dpOutputNormOffset + i);
         }
    }
    else
         m_bNormalizeOutput = false;

    return 0;
}

int NSRecallNetwork::saveWeights(char *weightsPathName, char *note)
{
	FILE *weightsFile;
	weightsFile = fopen(weightsPathName,"w");
	if (!weightsFile)
		return 1;

	writeWeightFileVersion(weightsFile);
	if (note)
		fprintf(weightsFile, note);
	fprintf(weightsFile, "\n\n");
	saveComponentWeights(weightsFile);
	saveNormalizationCoefficients(weightsFile);
	fclose(weightsFile);
	return 0;
}

int NSRecallNetwork::saveComponentWeights(FILE *&weightsFile)
{
	inputAxon->saveWeights(putComponent(weightsFile, inputAxon->className(), "inputAxon"));
	hidden1Synapse->saveWeights(putComponent(weightsFile, "FullSynapse", "hidden1Synapse"));
	hidden1Axon->saveWeights(putComponent(weightsFile, hidden1Axon->className(), "hidden1Axon"));
	hidden2Synapse->saveWeights(putComponent(weightsFile, "FullSynapse", "hidden2Synapse"));
	hidden2Axon->saveWeights(putComponent(weightsFile, hidden2Axon->className(), "hidden2Axon"));
	hidden3Synapse->saveWeights(putComponent(weightsFile, "FullSynapse", "hidden3Synapse"));
	hidden3Axon->saveWeights(putComponent(weightsFile, hidden3Axon->className(), "hidden3Axon"));
	hidden4Synapse->saveWeights(putComponent(weightsFile, "FullSynapse", "hidden4Synapse"));
	hidden4Axon->saveWeights(putComponent(weightsFile, hidden4Axon->className(), "hidden4Axon"));
	hidden5Synapse->saveWeights(putComponent(weightsFile, "FullSynapse", "hidden5Synapse"));
	hidden5Axon->saveWeights(putComponent(weightsFile, hidden5Axon->className(), "hidden5Axon"));
	outputSynapse->saveWeights(putComponent(weightsFile, "FullSynapse", "outputSynapse"));
	outputAxon->saveWeights(putComponent(weightsFile, outputAxon->className(), "outputAxon"));
	control->saveWeights(putComponent(weightsFile, control->className(), "control"));
	recurrentSynapse->saveWeights(putComponent(weightsFile, "FullSynapse", "recurrentSynapse"));
	forwardSynapse->saveWeights(putComponent(weightsFile, "FullSynapse", "forwardSynapse"));


     return 0;
}

int NSRecallNetwork::saveNormalizationCoefficients(FILE *&weightsFile)
{
    if (m_bNormalizeInput)
    {
        fprintf(weightsFile, "#inputFile File\n");
        fprintf(weightsFile, "%d\n", getNumberOfInputs());
        for (int i = 0; i < getNumberOfInputs(); i++)
            fprintf(weightsFile, "%21.15e %21.15e\n", m_dpInputNormAmp[i], m_dpInputNormOffset[i]);
        fprintf(weightsFile, "0");
        fprintf(weightsFile, "\n");
    }
    if (m_bNormalizeOutput)
    {
        fprintf(weightsFile, "#desiredFile File\n");
        fprintf(weightsFile, "%d\n", m_iNumberOfOutputs);
        for (int i = 0; i < m_iNumberOfOutputs; i++)
            fprintf(weightsFile, "%21.15e %21.15e\n", m_dpOutputNormAmp[i], m_dpOutputNormOffset[i]);
        fprintf(weightsFile, "0");
        fprintf(weightsFile, "\n");
    }
    return 0;
}

int NSRecallNetwork::getResponse(int exemplars, double *inputData, double *outputData)
{
	// Normalization coeff are not computed for efficiency.  It is assumed that
	// either the weights (holding appropriate norm coeff) will be loaded or the
	// network will be trained before calling getResponse.

control->prepareToFire(NULL);

	preRunGetReady();
control->preFireGetReady(FALSE);

	postFireGetReady();



control->setEpochs(1);
control->setExemplars(exemplars);
control->setActiveNumberOfExemplars(exemplars);
control->setEpochCounter(0);
control->setBackpropLearningOn(FALSE);



	for (; control->epochCounter()<control->epochs(); control->incrementEpochCounter())
	{
		control->setExemplarCounter(0);
		for (; control->exemplarCounter()<control->exemplars(); control->incrementExemplarCounter())
		{
			setInput(control->exemplarCounter(), inputData);
			control->fireEngines();

			outputAxon->decrement();
			getOutput(control->exemplarCounter(), outputData); 
			outputAxon->increment();

		}

	}
	fireConclude();
	if (control->epochCounter() == 0)
		return 1;
	return 0;
}

int NSRecallNetwork::setInput(int exemplar, double *inputData)
{
	if (m_bNormalizeInput)
	{
		if (m_ipInputDataMap)
		{
			for (int pe = 0; pe < m_iNumberOfInputsAfterMask; pe++)
			{
				inputAxon->V(pe) = *(inputData + exemplar * m_iNumberOfInputs + m_ipInputDataMap[pe]);
				inputAxon->V(pe) = inputAxon->V(pe) * m_dpInputNormAmp[pe] + m_dpInputNormOffset[pe];
			}
		}
		else
		{
			for (int pe = 0; pe < m_iNumberOfInputs; pe++)
			{
				inputAxon->V(pe) = *(inputData + exemplar * m_iNumberOfInputs + pe);
				inputAxon->V(pe) = inputAxon->V(pe) * m_dpInputNormAmp[pe] + m_dpInputNormOffset[pe];
			}
		}
	}
	else
	{
		if (m_ipInputDataMap)
		{
			for (int pe = 0; pe < m_iNumberOfInputsAfterMask; pe++)
			{
				inputAxon->V(pe) = *(inputData + exemplar * m_iNumberOfInputs + m_ipInputDataMap[pe]);
			}
		}
		else
		{
			for (int pe = 0; pe < m_iNumberOfInputs; pe++)
			{
				inputAxon->V(pe) = *(inputData + exemplar * m_iNumberOfInputs + pe);
			}
		}
	}
	return 0;
}

int NSRecallNetwork::getOutput(int exemplar, double *outputData)
{
    if (m_bNormalizeOutput)
    {
        for (int pe = 0; pe < m_iNumberOfOutputs; pe++)
            outputData[pe + exemplar * m_iNumberOfOutputs] = (outputAxon->V(pe) - m_dpOutputNormOffset[pe]) / m_dpOutputNormAmp[pe];
    }
    else
    {
        for (int pe = 0; pe < m_iNumberOfOutputs; pe++)
            outputData[pe + exemplar * m_iNumberOfOutputs] = outputAxon->V(pe);
    }
    return 0;
}

int NSRecallNetwork::getSensitivity(int exemplars, double *inputData, double *&sensitivityData, double dither)
{
    // Normalization coeff are not computed for efficiency.  It is assumed that
    // either the weights (holding appropriate norm coeff) will be loaded or the
    // network will be trained before calling getSensitivity.

control->prepareToFire(NULL);

	preRunGetReady();
control->preFireGetReady(FALSE);

	postFireGetReady();

    double *nonDitheredOutputData;
    double *ditheredOutputData;
    nonDitheredOutputData = new double[m_iNumberOfOutputs];
    ditheredOutputData = new double[m_iNumberOfOutputs];

    for (int i = 0; i < m_iNumberOfOutputs * m_iNumberOfInputs; i++)
        sensitivityData[i] = 0;



control->setEpochs(1);
control->setExemplars(exemplars);
control->setActiveNumberOfExemplars(exemplars);
control->setEpochCounter(0);
control->setBackpropLearningOn(FALSE);



	for (; control->epochCounter()<control->epochs(); control->incrementEpochCounter())
	{
		control->setExemplarCounter(0);
		for (; control->exemplarCounter()<control->exemplars(); control->incrementExemplarCounter())
		{
			setInput(control->exemplarCounter(), inputData);
			control->fireEngines();

			outputAxon->decrement();
			getOutputWithoutDenormalization(0, nonDitheredOutputData);
			outputAxon->increment();
			for (int inputPE = 0; inputPE < getNumberOfInputs(); inputPE++)
			{
				setInput(control->exemplarCounter(), inputData);
				inputAxon->V(inputPE) = inputAxon->V(inputPE) + dither;
				control->fireEngines();

				outputAxon->decrement();
				getOutputWithoutDenormalization(0, ditheredOutputData);

				int outputPE;
				if (m_ipInputDataMap)
				{
					for (outputPE = 0; outputPE < m_iNumberOfOutputs; outputPE++)
						sensitivityData[outputPE + m_ipInputDataMap[inputPE] * m_iNumberOfOutputs] += (double) fabs(ditheredOutputData[outputPE] - nonDitheredOutputData[outputPE]);
						//sensitivityData[outputPE + m_ipInputDataMap[inputPE] * m_iNumberOfOutputs] = ditheredOutputData[outputPE] - nonDitheredOutputData[outputPE];
				}
				else
				{
					for (outputPE = 0; outputPE < m_iNumberOfOutputs; outputPE++)
						sensitivityData[outputPE + inputPE * m_iNumberOfOutputs] += (double) fabs(ditheredOutputData[outputPE] - nonDitheredOutputData[outputPE]);
						//sensitivityData[outputPE + inputPE * m_iNumberOfOutputs] = ditheredOutputData[outputPE] - nonDitheredOutputData[outputPE];
				}
				outputAxon->increment();
			}

		}

	}
	fireConclude();
	if (control->epochCounter() == 0)
		return 1;
	return 0;
}

int NSRecallNetwork::fireConclude()
{

	return 0;
}

int NSRecallNetwork::getOutputWithoutDenormalization(int exemplar, double *outputData)
{
   for (int pe = 0; pe < m_iNumberOfOutputs; pe++)
       outputData[pe + exemplar * m_iNumberOfOutputs] = outputAxon->V(pe);
   return 0;
}

int NSRecallNetwork::randomizeWeights()
{
	inputAxon->networkRandomize();
	hidden1Synapse->networkRandomize();
	hidden1Axon->networkRandomize();
	hidden2Synapse->networkRandomize();
	hidden2Axon->networkRandomize();
	hidden3Synapse->networkRandomize();
	hidden3Axon->networkRandomize();
	hidden4Synapse->networkRandomize();
	hidden4Axon->networkRandomize();
	hidden5Synapse->networkRandomize();
	hidden5Axon->networkRandomize();
	outputSynapse->networkRandomize();
	outputAxon->networkRandomize();
	recurrentSynapse->networkRandomize();
	forwardSynapse->networkRandomize();

     return 0;
}

int NSRecallNetwork::resetNetwork()
{
	inputAxon->networkReset();
	hidden1Synapse->networkReset();
	hidden1Axon->networkReset();
	hidden2Synapse->networkReset();
	hidden2Axon->networkReset();
	hidden3Synapse->networkReset();
	hidden3Axon->networkReset();
	hidden4Synapse->networkReset();
	hidden4Axon->networkReset();
	hidden5Synapse->networkReset();
	hidden5Axon->networkReset();
	outputSynapse->networkReset();
	outputAxon->networkReset();
	recurrentSynapse->networkReset();
	forwardSynapse->networkReset();

	inputAxon->networkRandomize();
	hidden1Synapse->networkRandomize();
	hidden1Axon->networkRandomize();
	hidden2Synapse->networkRandomize();
	hidden2Axon->networkRandomize();
	hidden3Synapse->networkRandomize();
	hidden3Axon->networkRandomize();
	hidden4Synapse->networkRandomize();
	hidden4Axon->networkRandomize();
	hidden5Synapse->networkRandomize();
	hidden5Axon->networkRandomize();
	outputSynapse->networkRandomize();
	outputAxon->networkRandomize();
	recurrentSynapse->networkRandomize();
	forwardSynapse->networkRandomize();

     return 0;
}

int NSRecallNetwork::destroyNetwork()
{
inputAxon->setStaticControl(NULL);
hidden1Synapse->setStaticControl(NULL);
hidden1Axon->setStaticControl(NULL);
hidden2Synapse->setStaticControl(NULL);
hidden2Axon->setStaticControl(NULL);
hidden3Synapse->setStaticControl(NULL);
hidden3Axon->setStaticControl(NULL);
hidden4Synapse->setStaticControl(NULL);
hidden4Axon->setStaticControl(NULL);
hidden5Synapse->setStaticControl(NULL);
hidden5Axon->setStaticControl(NULL);
outputSynapse->setStaticControl(NULL);
outputAxon->setStaticControl(NULL);
control->clearComponentLists();
control->setStaticControl(NULL);
recurrentSynapse->setStaticControl(NULL);
forwardSynapse->setStaticControl(NULL);

	inputAxon->freeObject();
	delete inputAxon;
	hidden1Synapse->freeObject();
	delete hidden1Synapse;
	hidden1Axon->freeObject();
	delete hidden1Axon;
	hidden2Synapse->freeObject();
	delete hidden2Synapse;
	hidden2Axon->freeObject();
	delete hidden2Axon;
	hidden3Synapse->freeObject();
	delete hidden3Synapse;
	hidden3Axon->freeObject();
	delete hidden3Axon;
	hidden4Synapse->freeObject();
	delete hidden4Synapse;
	hidden4Axon->freeObject();
	delete hidden4Axon;
	hidden5Synapse->freeObject();
	delete hidden5Synapse;
	hidden5Axon->freeObject();
	delete hidden5Axon;
	outputSynapse->freeObject();
	delete outputSynapse;
	outputAxon->freeObject();
	delete outputAxon;
	control->freeObject();
	delete control;
	recurrentSynapse->freeObject();
	delete recurrentSynapse;
	forwardSynapse->freeObject();
	delete forwardSynapse;

	if (m_dpInputNormAmp)
		delete m_dpInputNormAmp;
	if (m_dpInputNormOffset)
		delete m_dpInputNormOffset;
	if (m_dpOutputNormAmp)
		delete m_dpOutputNormAmp;
	if (m_dpOutputNormOffset)
		delete m_dpOutputNormOffset;
	if (m_ipInputDataMap)
		delete m_ipInputDataMap;
	return 0;
}
