/*
  File name: p_mlp.c
  Created by: Ljubomir Buturovic
  Created: 08/30/2005
  Purpose: menu functions for Multi-Layer Perceptron learning.
*/

/*
  Copyright 2005 Ljubomir J. Buturovic

  Permission is hereby granted, free of charge, to any person
  obtaining a copy of this software and associated documentation files
  (the "Software"), to deal in the Software without restriction,
  including without limitation the rights to use, copy, modify, merge,
  publish, distribute, sublicense, and/or sell copies of the Software,
  and to permit persons to whom the Software is furnished to do so,
  subject to the following conditions:

  The above copyright notice and this permission notice shall be
  included in all copies or substantial portions of the Software.

  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  SOFTWARE.
*/

static char rcsid[] = "$Id: p_mlp.c,v 1.9 2006/01/23 02:21:30 ljubomir Exp $";

#include <stdlib.h>
#include <stdio.h>
#include <float.h>
#include <errno.h>
#include <math.h>
#include <unistd.h>
#include "lerr.h"
#include "xpar.h"
#include "dataset.h"
#include "xlearn.h"
#include "lau.h"
#include "pcp.h"
#include "pau.h"
#include "mlp.h"

/*
  Multi-Layer Perceptron model selection. The function chooses optimal
  number of hidden nodes and number of iterations. It is assumed that
  the MLP has one hidden layer.
*/
void pcp_mlp_xpar(int *errc, int dbg, char **xname)
{
  int   status;
  int   i;
  int   j;
  int   nhl;
  int   nhh;
  int   hstep;
  int   nit;
  int   nitl;
  int   nith;
  int   itstep;
  int   ndim;
  int   iter;
  int   opt_method;
  float range;
  float eta;
  float mu;
  float fval;
  float grid_point[2];
  struct xpar_crit_parameters *xpar_parameters;
  struct mlp_options *mlp_optional;
  FILE  *fdbg;

  if (dbg > 0)
    fdbg = fopen(PCP_DBG, "a");
  else
    fdbg = (FILE *) 0;
  status = 0;
  clear_screen();
  cursor_on();
  xpar_parameters = init_spar(PALG_MLP, tds, fdbg);
  input_mlp(stdout, tds->d, tds->c, tds->nv, (int *) 0, 
	    (int *) 0, (int **) 0, &nit, &range, &opt_method,
	    &eta, &mu, (int *) 0, 0, (int *) 0,
	    (unsigned int *) 0, (char **) 0);
  mlp_optional = calloc(1, sizeof(struct mlp_options));
  mlp_optional->nlayers = 2;
  mlp_optional->npl = (int *) malloc(mlp_optional->nlayers*sizeof(int));
  mlp_optional->range = range;
  mlp_optional->eta = eta;
  mlp_optional->mu = mu;
  mlp_optional->opt_method = opt_method;
  xpar_parameters->options = mlp_optional;
  input_xpar(tds, xpar_parameters);
  input_xpar_mlp(&nhl, &nhh, &hstep, (int *) 0, (int *) 0, (int *) 0);
  /*
    Compute optimal feature transformations/subsets once, reuse them
    later.
  */
  if (xpar_parameters->dr_method != PDR_NONE)
    status = compute_dr(xpar_parameters, xpar_parameters->dr_method, 
			xpar_parameters->idr, xpar_parameters->fscrit, fdbg, errc);
  iter = 1;
  /*
    On 09/07/2005, decided not to optimize number of iterations. Use
    the fixed value provided by user.
  */
  ndim = 1; /* optimizing on number of hidden nodes */
  nitl = nit;
  nith = nit;
  itstep = 1;
  unlink(PCP_MSL);
  for (i = nhl; i <= nhh && !*errc && !status; i += hstep)
    {
      grid_point[0] = i;
      for (j = nitl; j <= nith && !*errc; j += itstep)
	{
	  grid_point[1] = j;
	  ((struct mlp_options *) xpar_parameters->options)->npl[0] = i;
	  ((struct mlp_options *) xpar_parameters->options)->npl[1] = tds->c;
	  ((struct mlp_options *) xpar_parameters->options)->itmax = j;
	  fval = xpar_func(grid_point, ndim, iter, xpar_parameters, errc);
	  if (*errc == LERR_LNSEARCH) /* ignore LERR_LNSEARCH; what else to do?  */
	    *errc = 0;
	  if (!*errc)
	    {
	      viprint_line(5, 1, "Current number of hidden nodes: %10d; error rate: %7.2f%%.", 
			   (int) grid_point[0], fval);
	      viprint_line(5, 1, "Optimal number of hidden nodes: %10d; error rate: %7.2f%%.", 
			   (int) xpar_parameters->x1, xpar_parameters->eval);
	      printf("\n");
	      iter++;
	    }
	}
    }
  /*
    We have the optimal parameters - now apply to the test set, if
    defined.
  */
  if (!*errc && !status)
    {
      if (teds)
	{
	  viprint_line(2, 1, "Applying the optimal classifier to the test dataset...");
	  xtest_optimal(xpar_parameters, errc, xname);
	}
      else
	pwait();
    }
  xpar_free(xpar_parameters);
}



syntax highlighted by Code2HTML, v. 0.9.1