/* FFNet_Pattern_Categories.c
 *
 * Copyright (C) 1994-2003 David Weenink
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or (at
 * your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

/*
 djmw 20020712 GPL header
 djmw 20020910 changes
 djmw 20030701 Removed non-GPL minimizations
*/

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

static int _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_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 */
