/*
  File name: bagging.c
  Created by: Ljubomir Buturovic
  Created: 07/31/2002
  Purpose: implement bagging (committee) variants of common pattern
  clssification algorithms.
*/

/*
  Copyright 2004 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: bagging.c,v 1.71 2006/04/04 18:18:34 ljubomir Exp $";

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#include "dataset.h"
#include "bagging.h"
#include "mlp.h"
#include "svm.h"
#include "pcl_svm.h"
#include "lau.h"
#include "lin.h"
#include "lind.h"
#include "parametric.h"
#include "adaboost.h"

/*
  Train a combined classifier using 'bagging' algorithm described in:
  Amanda J. C. Sharkey (Ed.), Combining Artificial Neural Nets,
  Chapter 2, Sections 2.2.2 and 3.2. Springer, London, 1999. 'nmodels'
  is the number of classifiers used for bagging; 'bag_size' is the
  size of each sampling dataset used to build an individual
  classifier. The sampling datasets are obtained by resampling the
  input 'dset'.

  Return array of 'nmodels' classifier structs (one per sampling
  dataset), representing the bagging classifier. The function stores
  the classifier in file 'fname'. In case of error, return NULL and
  set 'errc'.

  The 'problems' argument is specific to SVM bagging. It returns the
  set of SVM svm_problems structures used to build the bagging
  classifier, for subsequent free()-ing. The reason that the
  structures are not free()-ed here is that the returned models
  reference them, so the free() can only be done by the caller once
  the models are no longer needed.

  This function has been reimplemented as pcl_svm_learn() for SVM
  classifiers, and put into libpcl.a library, for external
  use. Consider reimplementing bagging for all classifiers in such
  manner.
*/
void **bagging(struct dataset *dset, FILE *outdev, int method, char *fname,
	       unsigned int seed, int nmodels, int bag_size, void *options,
	       void ***problems, int *errc, FILE *fdbg)
{
  int    i;
  int    d;
  int    nvec;
  int    status;
  int    mode;
  int    *bnd;
  char   *tname; /* temporary MLP file */
  float  **bag = (float **) 0;
  float  **target;
  void   *model; /* individual model */
  void   **models = (void **) 0; /* array of individual models, comprising the bagging classifier */
  struct dataset *bag_set;
  struct mlp_options *mlp_optional;
  struct svm_parameter *parameters;
  struct svm_problem *problem;
  struct svm_problem **probs;
  FILE   *fptr;

  model = (void *) 0;
  mlp_optional = (struct mlp_options *) 0;
  parameters = (struct svm_parameter *) 0;
  probs = (struct svm_problem **) 0;
  fptr = (FILE *) 0;
  d = dset->d;
  if (method == PALG_BAG_MLP)
    mlp_optional = (struct mlp_options *) options;
  else if (method == PALG_BAG_SVM)
    {
      parameters = (struct svm_parameter *) options;
      probs = malloc(nmodels*sizeof(struct svm_problem *));
    }
  /*
    For each bagging subset, train a classifier and append the model
    to the output file.
  */
  bag = malloc(bag_size*sizeof(float *));
  bnd = calloc(dset->c, sizeof(int));
  status = 0;
  tname = tempfile();
  if (tname == (char *) 0)
    status = -1;
  if (status == 0)
    models = calloc(nmodels+1, sizeof((void **) 0));
  if (fname && *fname)
    {
      fptr = fopen(fname, "w");
      if (!fptr)
	status = -1;
    }
  for (i = 0; (i < nmodels) && (status == 0); i++)
    {
      nvec = resample(i, dset, bag_size, bag, bnd, fdbg);
      if (method == PALG_BAG_MLP)
	{
	  target = mlp_target(dset->c, bnd);
	  /*
	    Train, then append the model to 'fname'.
	  */
	  model = mlp_learn(mlp_optional->opt_method, bag, nvec, bnd, d, target,
			    mlp_optional->nlayers, mlp_optional->npl, 
			    mlp_optional->itmax, mlp_optional->range, mlp_optional->eta,
			    mlp_optional->mu, outdev, 0, tname, seed, errc, fdbg);
	  mx_free((void **) target, nvec);
	  if (model)
	    {
	      free(((struct mlp *) model)->fname);
	      ((struct mlp *) model)->fname = strdup(fname);
	      status = mlp_save(model, MLP_MODE_APPEND, i+1, 1.0);
	    }
	  else
	    status = -1;
	}
      else if (method == PALG_BAG_SVM)
	{
	  bag_set = dataset_lt(d, dset->c, bnd, nvec, (char **) 0, bag);
	  problem = create_problem(bag_set); 
	  free(bag_set);
	  probs[i] = problem;
	  model = svm_train(problem, parameters);
	  if (model)
	    {
	      status = save_svm(fptr, model, i+1, 1.0);
	      if (status == 0)
		status = fflush(fptr);
	      else
		{
		  *errc = errno;
		  fptr_close(fptr);
		}
	    }
	  else
	    {
	      status = -1;
	      *errc = errno;
	    }
	}
      else if ((method == PALG_BAG_LIN) || (method == PALG_BAG_PLC))
	{
	  bag_set = dataset_lt(d, dset->c, bnd, nvec, (char **) 0, bag);
	  if (method == PALG_BAG_LIN)
	    model = lind_learn(bag_set, errc, fdbg);
	  else
	    {
	      /*
		TBD: make mode a parameter to bagging().
	      */
	      mode = WEIGHTED_COV;
	      model = lin_learn(mode, bag_set, errc);
	    }
	  if (model)
	    lin_write(fptr, nmodels, model, dset->c, d+1, i+1, 1.0);
	  else
	    status = -1;
	  free(bag_set);
	}
      else if (method == PALG_BAG_PQC)
	{
	  bag_set = dataset_lt(d, dset->c, bnd, nvec, (char **) 0, bag);
	  model = pqc_learn(bag_set, errc);
	  if (model)
	    status = pqc_write(fptr, nmodels, model, d, dset->c, i+1, 1.0);
	  else
	    status = -1;
	  mx_free((void **) bag_set->sigma, dset->c);
	  free(bag_set->det);
	  free(bag_set);
	}
      models[i] = model;
    }
  if (status == -1)
    {
      free(models);
      models = (void **) 0;
    }
  unlink(tname);
  free(tname);
  free(bag);
  free(bnd);
  fptr_close(fptr);
  if (problems)
    *problems = (void **) probs;
  return models;
}



syntax highlighted by Code2HTML, v. 0.9.1