/* FFNet_Pattern_Categories.c */
/* David Weenink, 20010531 */

#include "FFNet_Activation_Categories.h"
#include "FFNet_Pattern_Categories.h"
#include "FFNet_Pattern_Activation.h"

static _FFNet_Pattern_Categories_learn (FFNet me, Pattern p, Categories c, 
	long maxNumOfEpochs, double tolerance, Any parameters, int costFunctionType,
	int (*learn) (FFNet, Pattern, Activation, long, double, Any, int))
{
    Activation activation; 
	int status; 
	double min, max;
    
    if (my nInputs != p->nx) return Melder_error 
		("_FFNet_Pattern_Categories_learn:\n"
    	"#colums in Pattern must equal #inputs of neural net ");
    if (p->ny != c->size) return Melder_error 
		("_FFNet_Pattern_Categories_learn:\n"
    	"#rows in Categories must equal #rows in Pattern");
    if (! (activation = FFNet_Categories_to_Activation (me, c))) return 0;
	Matrix_getWindowExtrema (p, 0, 0, 0, 0, &min, &max);
    status = learn (me, p, activation, maxNumOfEpochs, tolerance, parameters,
		costFunctionType);
	forget (activation);
	Matrix_getWindowExtrema (p, 0, 0, 0, 0, &min, &max);
	if (min < 0 || max > 1) Melder_warning ("_FFNet_Pattern_Categories_learn: "
		"Not all values in the Pattern are in the interval [0,1].\n"
		"Succesfull training is not guaranteed.");
	return status;
}

int FFNet_Pattern_Categories_learnPR (FFNet me, Pattern p, Categories c, 
	long maxNumOfEpochs, double tolerance, Any parameters, int costFunctionType)
{
	return _FFNet_Pattern_Categories_learn (me, p, c, maxNumOfEpochs,
    	tolerance, parameters, costFunctionType,
		FFNet_Pattern_Activation_learnPR);
}

int FFNet_Pattern_Categories_learnFP (FFNet me, Pattern p, Categories c, 
	long maxNumOfEpochs, double tolerance, Any parameters, int costFunctionType)
{
	return _FFNet_Pattern_Categories_learn (me, p, c, maxNumOfEpochs,
    	tolerance, parameters, costFunctionType,
		FFNet_Pattern_Activation_learnFP);
}

int FFNet_Pattern_Categories_learnSM (FFNet me, Pattern p, Categories c, 
	long maxNumOfEpochs, double tolerance, Any parameters, int costFunctionType)
{
	return _FFNet_Pattern_Categories_learn (me, p, c, maxNumOfEpochs,
    	tolerance, parameters, costFunctionType,
		FFNet_Pattern_Activation_learnSM);
}

int FFNet_Pattern_Categories_learnSD (FFNet me, Pattern p, Categories c, 
	long maxNumOfEpochs, double tolerance, Any parameters, int costFunctionType)
{
	return _FFNet_Pattern_Categories_learn (me, p, c, maxNumOfEpochs,
    	tolerance, parameters, costFunctionType, 
		FFNet_Pattern_Activation_learnSD);
}

Categories FFNet_Pattern_to_Categories (FFNet me, Pattern thee, int labeling)
{
    Categories him = NULL; 
	long k, index;
	Data item;
    if (! my outputCategories) return Melder_errorp 
		("FFNet_Pattern_to_Categories: FFNet has no output categories.");
    if (my nInputs != thy nx) return Melder_errorp 
		("FFNet_Pattern_to_Categories: "
    	"Dimension of FFNet inputs and #cols in Pattern do not agree.");
		
	him = Categories_create ();	
	if (him == NULL) return NULL;
	
    for (k = 1; k <= thy ny; k++)
    {
     	FFNet_propagate (me, thy z[k], NULL);
    	index = FFNet_getWinningUnit (me, labeling);
		item = Data_copy (my outputCategories->item[index]);
    	if (item == NULL || ! Collection_addItem (him, item))
		{
			forget (him); return NULL;
		}
	}
    return him;
}

/* End of file FFNet_Pattern_Categories.c */
