Commit be943c14 authored by Joseph Mirabel's avatar Joseph Mirabel Committed by Joseph Mirabel
Browse files

Remove svm classifiers

parent fdd60305
......@@ -126,7 +126,6 @@ SET(${PROJECT_NAME}_HEADERS
include/hpp/fcl/collision_func_matrix.h
include/hpp/fcl/distance.h
include/hpp/fcl/continuous_collision.h
include/hpp/fcl/math/vec_nf.h
include/hpp/fcl/math/matrix_3f.h
include/hpp/fcl/math/matrix_3fx.h
include/hpp/fcl/math/vec_3f.h
......
......@@ -39,7 +39,6 @@ add_fcl_test(test_fcl_capsule_box_1 test_fcl_capsule_box_1.cpp)
add_fcl_test(test_fcl_capsule_box_2 test_fcl_capsule_box_2.cpp)
#add_fcl_test(test_fcl_obb test_fcl_obb.cpp)
#add_fcl_test(test_fcl_global_penetration test_fcl_global_penetration.cpp libsvm/svm.cpp test_fcl_utility.cpp)
add_fcl_test(test_fcl_bvh_models test_fcl_bvh_models.cpp test_fcl_utility.cpp)
if (FCL_HAVE_OCTOMAP)
......
This diff is collapsed.
#ifndef _LIBSVM_H
#define _LIBSVM_H
#include <cstdlib>
#define LIBSVM_VERSION 314
#ifdef __cplusplus
extern "C" {
#endif
extern int libsvm_version;
struct svm_node
{
int index;
double value;
};
struct svm_problem
{
int l;
double *y;
struct svm_node **x;
double *W; /* instance weight */
};
enum { C_SVC, NU_SVC, ONE_CLASS, EPSILON_SVR, NU_SVR }; /* svm_type */
enum { LINEAR, POLY, RBF, SIGMOID, PRECOMPUTED }; /* kernel_type */
struct svm_parameter
{
int svm_type;
int kernel_type;
int degree; /* for poly */
double gamma; /* for poly/rbf/sigmoid */
double coef0; /* for poly/sigmoid */
/* these are for training only */
double cache_size; /* in MB */
double eps; /* stopping criteria */
double C; /* for C_SVC, EPSILON_SVR and NU_SVR */
int nr_weight; /* for C_SVC */
int *weight_label; /* for C_SVC */
double* weight; /* for C_SVC */
double nu; /* for NU_SVC, ONE_CLASS, and NU_SVR */
double p; /* for EPSILON_SVR */
int shrinking; /* use the shrinking heuristics */
int probability; /* do probability estimates */
};
//
// svm_model
//
struct svm_model
{
struct svm_parameter param; /* parameter */
int nr_class; /* number of classes, = 2 in regression/one class svm */
int l; /* total #SV */
struct svm_node **SV; /* SVs (SV[l]) */
double **sv_coef; /* coefficients for SVs in decision functions (sv_coef[k-1][l]) */
double *rho; /* constants in decision functions (rho[k*(k-1)/2]) */
double *probA; /* pairwise probability information */
double *probB;
int *sv_indices; /* sv_indices[0,...,nSV-1] are values in [1,...,num_traning_data] to indicate SVs in the training set */
/* for classification only */
int *label; /* label of each class (label[k]) */
int *nSV; /* number of SVs for each class (nSV[k]) */
/* nSV[0] + nSV[1] + ... + nSV[k-1] = l */
/* XXX */
int free_sv; /* 1 if svm_model is created by svm_load_model*/
/* 0 if svm_model is created by svm_train */
};
struct svm_model *svm_train(const struct svm_problem *prob, const struct svm_parameter *param);
void svm_cross_validation(const struct svm_problem *prob, const struct svm_parameter *param, int nr_fold, double *target);
int svm_save_model(const char *model_file_name, const struct svm_model *model);
struct svm_model *svm_load_model(const char *model_file_name);
int svm_get_svm_type(const struct svm_model *model);
int svm_get_nr_class(const struct svm_model *model);
void svm_get_labels(const struct svm_model *model, int *label);
void svm_get_sv_indices(const struct svm_model *model, int *sv_indices);
int svm_get_nr_sv(const struct svm_model *model);
double svm_get_svr_probability(const struct svm_model *model);
double svm_predict_values(const struct svm_model *model, const struct svm_node *x, double* dec_values);
double svm_predict(const struct svm_model *model, const struct svm_node *x);
double svm_predict_probability(const struct svm_model *model, const struct svm_node *x, double* prob_estimates);
double svm_predict_values_twoclass(const struct svm_model* model, const struct svm_node* x);
double svm_hyper_w_normsqr_twoclass(const struct svm_model* model);
double k_function(const svm_node* x, const svm_node* y, const svm_parameter& param);
void svm_free_model_content(struct svm_model *model_ptr);
void svm_free_and_destroy_model(struct svm_model **model_ptr_ptr);
void svm_destroy_param(struct svm_parameter *param);
const char *svm_check_parameter(const struct svm_problem *prob, const struct svm_parameter *param);
int svm_check_probability_model(const struct svm_model *model);
void svm_set_print_string_function(void (*print_func)(const char *));
#ifdef __cplusplus
}
#endif
#endif /* _LIBSVM_H */
#ifndef FCL_TEST_LIBSVM_CLASSIFIER_H
#define FCL_TEST_LIBSVM_CLASSIFIER_H
#include <hpp/fcl/learning/classifier.h>
#include <libsvm/svm.h>
namespace fcl
{
template<std::size_t N>
class LibSVMClassifier : public SVMClassifier<N>
{
public:
LibSVMClassifier()
{
param.svm_type = C_SVC;
param.kernel_type = RBF;
param.degree = 3;
param.gamma = 0; // 1/num_features
param.coef0 = 0;
param.nu = 0.5;
param.cache_size = 100; // can change
param.C = 1;
param.eps = 1e-3;
param.p = 0.1;
param.shrinking = 1; // use shrinking
param.probability = 0;
param.nr_weight = 0;
param.weight_label = NULL;
param.weight = NULL;
param.nr_weight = 2;
param.weight_label = (int *)realloc(param.weight_label, sizeof(int) * param.nr_weight);
param.weight = (double *)realloc(param.weight, sizeof(double) * param.nr_weight);
param.weight_label[0] = -1;
param.weight_label[1] = 1;
param.weight[0] = 1;
param.weight[1] = 1;
model = NULL;
x_space = NULL;
problem.x = NULL;
problem.y = NULL;
problem.W = NULL;
}
void setCSVM() { param.svm_type = C_SVC; }
void setNuSVM() { param.svm_type = NU_SVC; }
void setC(FCL_REAL C) { param.C = C; }
void setNu(FCL_REAL nu) { param.nu = nu; }
void setLinearClassifier() { param.kernel_type = LINEAR; }
void setNonLinearClassifier() { param.kernel_type = RBF; }
void setProbability(bool use_probability) { param.probability = use_probability; }
virtual void setScaler(const Scaler<N>& scaler_)
{
scaler = scaler_;
}
void setNegativeWeight(FCL_REAL c)
{
param.weight[0] = c;
}
void setPositiveWeight(FCL_REAL c)
{
param.weight[1] = c;
}
void setEPS(FCL_REAL e)
{
param.eps = e;
}
void setGamma(FCL_REAL gamma)
{
param.gamma = gamma;
}
~LibSVMClassifier()
{
svm_destroy_param(&param);
svm_free_and_destroy_model(&model);
delete [] x_space;
delete [] problem.x;
delete [] problem.y;
delete [] problem.W;
}
virtual void learn(const std::vector<Item<N> >& data)
{
if(data.size() == 0) return;
if(model) svm_free_and_destroy_model(&model);
if(param.gamma == 0) param.gamma = 1.0 / N;
problem.l = data.size();
if(problem.y) delete [] problem.y;
problem.y = new double [problem.l];
if(problem.x) delete [] problem.x;
problem.x = new svm_node* [problem.l];
if(problem.W) delete [] problem.W;
problem.W = new double [problem.l];
if(x_space) delete [] x_space;
x_space = new svm_node [(N + 1) * problem.l];
for(std::size_t i = 0; i < data.size(); ++i)
{
svm_node* cur_x_space = x_space + (N + 1) * i;
Vecnf<N> q_scaled = scaler.scale(data[i].q);
for(std::size_t j = 0; j < N; ++j)
{
cur_x_space[j].index = j + 1;
cur_x_space[j].value = q_scaled[j];
}
cur_x_space[N].index = -1;
problem.x[i] = cur_x_space;
problem.y[i] = (data[i].label ? 1 : -1);
problem.W[i] = data[i].w;
}
model = svm_train(&problem, &param);
hyperw_normsqr = svm_hyper_w_normsqr_twoclass(model);
}
virtual std::vector<PredictResult> predict(const std::vector<Vecnf<N> >& qs) const
{
std::vector<PredictResult> predict_results;
int nr_class = svm_get_nr_class(model);
double* prob_estimates = NULL;
svm_node* x = (svm_node*)malloc((N + 1) * sizeof(svm_node));
if(param.probability)
prob_estimates = (double*)malloc(nr_class * sizeof(double));
Vecnf<N> v;
for(std::size_t i = 0; i < qs.size(); ++i)
{
v = scaler.scale(qs[i]);
for(std::size_t j = 0; j < N; ++j)
{
x[j].index = j + 1;
x[j].value = v[j];
}
x[N].index = -1;
double predict_label;
if(param.probability)
{
predict_label = svm_predict_probability(model, x, prob_estimates);
predict_label = (predict_label > 0) ? 1 : 0;
predict_results.push_back(PredictResult(predict_label, *prob_estimates));
}
else
{
predict_label = svm_predict(model, x);
predict_label = (predict_label > 0) ? 1 : 0;
predict_results.push_back(PredictResult(predict_label));
}
}
if(param.probability) free(prob_estimates);
free(x);
return predict_results;
}
virtual PredictResult predict(const Vecnf<N>& q) const
{
return (predict(std::vector<Vecnf<N> >(1, q)))[0];
}
void save(const std::string& filename) const
{
if(model)
svm_save_model(filename.c_str(), model);
}
virtual std::vector<Item<N> > getSupportVectors() const
{
std::vector<Item<N> > results;
Item<N> item;
for(std::size_t i = 0; i < (std::size_t)model->l; ++i)
{
for(std::size_t j = 0; j < N; ++j)
item.q[j] = model->SV[i][j].value;
item.q = scaler.unscale(item.q);
int id = model->sv_indices[i] - 1;
item.label = (problem.y[id] > 0);
results.push_back(item);
}
return results;
}
svm_parameter param;
svm_problem problem;
svm_node* x_space;
svm_model* model;
double hyperw_normsqr;
Scaler<N> scaler;
};
}
#endif
......@@ -19,18 +19,6 @@ static bool approx(FCL_REAL x, FCL_REAL y)
return std::abs(x - y) < epsilon;
}
template<std::size_t N>
double distance_Vecnf(const Vecnf<N>& a, const Vecnf<N>& b)
{
double d = 0;
for(std::size_t i = 0; i < N; ++i)
d += (a[i] - b[i]) * (a[i] - b[i]);
return d;
}
BOOST_AUTO_TEST_CASE(projection_test_line)
{
Vec3f v1(0, 0, 0);
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment