/*
  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.
*/

#include <stdio.h>
#include "svm.h"
#include "dataset.h"

#ifndef  INIT_NO_MODELS
#define  INIT_NO_MODELS 10
#endif

struct pcl_svm_parameters
{
  int    kernel_type; /* LINEAR, POLY, RBF, SIGMOID */
  double C; 
  double degree;      /* for polynomial kernel */
  double gamma;       /* for polynomial/RBG/sigmoid kernels */
  double coef0;       /* for polynomial/sigmoid kernels */
};

/*
  Load SVM models from 'fname', potentially in Adaboost format. Return
  number of models in 'nmodels' and their respective weights in
  'weights'.

  In case of error, return NULL and set errno. Possible errors are
  malloc() and file access errors.
*/
struct svm_model **pcl_svm_load(char *svm_fname, int *nmodels, float **weights);

/*
  Save 'models' in 'svm_fname'. 

  Return 0 in case of success, -1 in case of error and set errno. The
  possible errors are file access errors or EINVAL. errno is set to
  EINVAL if svm_fname is NULL, svm_model is NULL, or nmodels <= 0.
*/
int pcl_svm_save(char *svm_fname, struct svm_model **models, int nmodels);

/*
  Train a committee of 'nmodels' Support Vector Machines using 'dset'
  and 'parameters'.

  Return the combined model represented by an array of (struct
  svm_model *) structures. The array can be used for prediction with
  pcl_svm_predict().

  In case of error, return NULL.
*/
struct svm_model **pcl_svm_learn(struct dataset *dset, int nmodels, 

				 struct pcl_svm_parameters *parameters, int *errc);

/*
  Check Support Vector Machine 'parameters' for 'dset'.

  Return NULL in case of success; otherwise return string containing
  the error message.
*/
char *pcl_svm_check(struct dataset *dset, struct pcl_svm_parameters *parameters);

/*
  Classify 'dset' using a committee of 'nmodels' Support Vector
  Machine classifiers stored in 'models'. Predicted classes are stored
  in dset->prediction.

  The input models can be built using pcl_svm_learn().
*/
void pcl_svm_predict(struct dataset *dset, struct svm_model **models, int nmodels,
		     float *weights, int *errc);

/*
  Perform cross-validation results for Support Vector Machine on
  'dset'.  The function calculates FN (false negative) counts per
  class/experiment.

  If 'normalize' is 1, normalize the data sets before applying the
  learning method.

  Optionally reduce dimension of the training/testing data sets to
  'idr' using 'dr_method'.

  'nmodels' is the number of models for bagging/Adaboost methods.

  In case of success, the function returns the array of false negative
  counts per experiment and class and experiment. In case of failure,
  it returns -1 and sets 'errc'.
*/
int **pcl_svm_xlearn(struct dataset *dset, int dr_method, int idr, int normalize,
		     int nexp, unsigned int seed, int nmodels, 
		     struct pcl_svm_parameters *parameters, int *errc);

/*
  Create LIBSVM representation of feature vector 'x' of length 'd'.
*/
struct svm_node *create_svm_vector(float *x, int d);

/*
  Convert 'dset' to LIBSVM-compatible structure. Don't use the
  'sparse' representation. See LIBSVM README file for more information
  on the 'svm_node' structure.
*/
struct svm_problem *create_problem(struct dataset *dset);

/*
  Convert 'dset' to LIBSVM-compatible two-class structure. The first
  class is `cx', the second class is everything else. `cx' is 0-based
  index.

  Don't use the 'sparse' representation. See LIBSVM README file for
  more information on the 'svm_node' structure.
*/
struct svm_problem *create_binary_problem(struct dataset *dset, int cx);

/*
  Free 'problem'. 
*/
void free_problem(struct svm_problem *problem);

/*
  This function is used to append an individual SVM model to a file
  containing a SVM bagging (committee) classifier. Each model in the
  file has an index and weight. The function appends 'model', whose
  model index number is 'mdx' and weight 'weight', to 'fptr'.

  In case of success, return 0. In case of failure, return -1 and set
  errno.
*/
int save_svm(FILE *fptr, void *model, int mdx, float weight);


syntax highlighted by Code2HTML, v. 0.9.1