diff --git a/src/classification/LPWrapper.cpp b/src/classification/LPWrapper.cpp index e3d4c2a9f73da7e0638c8a82417689582546ca7d..dbe7bf5793f25dac3fe8e1d12170dd5c85dc8799 100644 --- a/src/classification/LPWrapper.cpp +++ b/src/classification/LPWrapper.cpp @@ -1,5 +1,5 @@ #include <classification/LPWrapper.hpp> -LPWrapper::LPWrapper(std::vector<int> samp_per_class, int task_num, int n_class, int n_dim, int n_samp, double tol) : +LPWrapper::LPWrapper(std::vector<int> samp_per_class, const int task_num, const int n_class, const int n_dim, const int n_samp, const double tol) : _elements((n_dim + 1) * n_samp, 0.0), _row_lower((n_dim + 1) * n_samp, 1.0), _row_upper((n_dim + 1) * n_samp, 1.0), @@ -40,7 +40,7 @@ void LPWrapper::setup_constraints() } } -void LPWrapper::copy_data(int cls, std::vector<int> inds) +void LPWrapper::copy_data(const int cls, const std::vector<int> inds) { if(inds.size() > _n_dim) { @@ -82,7 +82,7 @@ void LPWrapper::copy_data(int cls, std::vector<int> inds) ); } -void LPWrapper::copy_data(int cls, std::vector<double*> val_ptrs) +void LPWrapper::copy_data(const int cls, const std::vector<double*> val_ptrs) { if(val_ptrs.size() > _n_dim) { @@ -126,7 +126,7 @@ void LPWrapper::copy_data(int cls, std::vector<double*> val_ptrs) } -int LPWrapper::get_n_overlap(std::vector<int> inds) +int LPWrapper::get_n_overlap(const std::vector<int> inds) { _n_overlap = 0; @@ -146,7 +146,7 @@ int LPWrapper::get_n_overlap(std::vector<int> inds) return _n_overlap; } -int LPWrapper::get_n_overlap(std::vector<double*> val_ptrs) +int LPWrapper::get_n_overlap(const std::vector<double*> val_ptrs) { _n_overlap = 0; diff --git a/src/classification/LPWrapper.hpp b/src/classification/LPWrapper.hpp index 7b75e34e01a5b541f116aac7a69423c9310568c2..f3157242c5eeead9e300d541a734b9b31610beb5 100644 --- a/src/classification/LPWrapper.hpp +++ b/src/classification/LPWrapper.hpp @@ -35,16 +35,16 @@ protected: std::vector<int> _n_col_per_class; //!< Number of columns per class - double _tol; //!< tolerance value (\epsilon) for the final row constraint + const double _tol; //!< tolerance value (\epsilon) for the final row constraint - int _n_dim; //!< The number of dimensions for the SVM problem - int _n_samp; //!< The number of samples per feature - int _n_class; //!< The number of classes in the data set + const int _n_dim; //!< The number of dimensions for the SVM problem + const int _n_samp; //!< The number of samples per feature + const int _n_class; //!< The number of classes in the data set + const int _n_row; //!< The number of rows in the A matrix + const int _task_num; //!< The task number the LPWrapper is used for int _n_col; //!< The number of columns in the A matrix - int _n_row; //!< The number of rows in the A matrix int _n_overlap; //!< The number of misclassified data points in the training set int _n_pts_check; //!< The number of points to check - int _task_num; //!< The task number the LPWrapper is used for public: /** @@ -57,7 +57,7 @@ public: * @param n_samp Number of samples in the dataset * @param tol The tolerance used to have a fuzzy border around the convex hull */ - LPWrapper(std::vector<int> samp_per_class, int task_num, int n_class, int n_dim, int n_samp, double tol); + LPWrapper(std::vector<int> samp_per_class, const int task_num, const int n_class, const int n_dim, const int n_samp, const double tol); /** * @brief Set up the constraint matrix for the LP problem @@ -70,7 +70,7 @@ public: * @param cls class the number of the class to copy over * @param inds list of indexes to pull data for */ - void copy_data(int cls, std::vector<int> inds); + void copy_data(const int cls, const std::vector<int> inds); /** * @brief Copies the data from a set data pointers @@ -78,45 +78,45 @@ public: * @param cls class the number of the class to copy over * @param val_ptrs The pointers to the feature's data */ - void copy_data(int cls, std::vector<double*> val_ptrs); + void copy_data(const int cls, const std::vector<double*> val_ptrs); /** * @brief Copy the data from a set of feature indexes (sorted_dmatrix) into the x_space and train the SVM model * * @param inds list of indexes to pull data for */ - int get_n_overlap(std::vector<int> inds); + int get_n_overlap(const std::vector<int> inds); /** * @brief Copy tthe data from a set data pointers and train the SVM model * * @param val_ptrs The pointers to the feature's data */ - int get_n_overlap(std::vector<double*> val_ptrs); + int get_n_overlap(const std::vector<double*> val_ptrs); /** * @brief The number of classes in the training set * @return The number of classes in the training set */ - inline int n_class(){return _n_class;} + inline int n_class() const {return _n_class;} /** * @brief The task id number * @return The task id number */ - inline int task_num(){return _task_num;} + inline int task_num() const {return _task_num;} /** * @brief The number of dimensions of the Convex Hulls * @return The number of dimensions of the Convex Hulls */ - inline int n_dim(){return _n_dim;} + inline int n_dim() const {return _n_dim;} /** * @brief The number of samples in the training set * @return The number of samples in the training set */ - inline int n_samp(){return _n_samp;} + inline int n_samp() const {return _n_samp;} /** * @brief The actual class for each sample in the data set @@ -128,7 +128,7 @@ public: * @brief Number of misclassified samples in the data set * @return Number of misclassified samples in the data set */ - inline int n_overlap(){return _n_overlap;} + inline int n_overlap() const {return _n_overlap;} }; #endif diff --git a/src/classification/SVMWrapper.cpp b/src/classification/SVMWrapper.cpp index 2ac66f6a252fc0771fdd12855ec982fc92fbfea9..53048ab167a7690ef44cc8d069843016b070a424 100644 --- a/src/classification/SVMWrapper.cpp +++ b/src/classification/SVMWrapper.cpp @@ -1,8 +1,8 @@ #include <classification/SVMWrapper.hpp> -SVMWrapper::SVMWrapper(int n_class, int n_dim, int n_samp, double* prop) : +SVMWrapper::SVMWrapper(const int n_class, const int n_dim, const int n_samp, const double* prop) : _model(nullptr), - _y(n_samp), + _y(prop, prop + n_samp), _y_est(n_samp), _x_space(n_samp * (n_dim + 1)), _x(n_samp), @@ -15,7 +15,7 @@ SVMWrapper::SVMWrapper(int n_class, int n_dim, int n_samp, double* prop) : _n_samp(n_samp), _n_class(n_class) { - std::copy_n(prop, _n_samp, _y.data()); + // std::copy_n(prop, _n_samp, _y.data()); setup_parameter_obj(_C); setup_x_space(); @@ -24,7 +24,7 @@ SVMWrapper::SVMWrapper(int n_class, int n_dim, int n_samp, double* prop) : _prob.x = _x.data(); } -SVMWrapper::SVMWrapper(int n_class, int n_dim, std::vector<double> prop) : +SVMWrapper::SVMWrapper(const int n_class, const int n_dim, const std::vector<double> prop) : _model(nullptr), _y(prop), _y_est(prop.size()), @@ -47,9 +47,9 @@ SVMWrapper::SVMWrapper(int n_class, int n_dim, std::vector<double> prop) : _prob.x = _x.data(); } -SVMWrapper::SVMWrapper(double C, int n_class, int n_dim, int n_samp, double* prop) : +SVMWrapper::SVMWrapper(const double C, const int n_class, const int n_dim, const int n_samp, const double* prop) : _model(nullptr), - _y(n_samp), + _y(prop, prop + n_samp), _y_est(n_samp), _x_space(n_samp * (n_dim + 1)), _x(n_samp), @@ -62,7 +62,7 @@ SVMWrapper::SVMWrapper(double C, int n_class, int n_dim, int n_samp, double* pro _n_samp(n_samp), _n_class(n_class) { - std::copy_n(prop, _n_samp, _y.data()); + // std::copy_n(prop, _n_samp, _y.data()); setup_parameter_obj(_C); setup_x_space(); @@ -71,7 +71,7 @@ SVMWrapper::SVMWrapper(double C, int n_class, int n_dim, int n_samp, double* pro _prob.x = _x.data(); } -SVMWrapper::SVMWrapper(double C, int n_class, int n_dim, std::vector<double> prop) : +SVMWrapper::SVMWrapper(const double C, const int n_class, const int n_dim, const std::vector<double> prop) : _model(nullptr), _y(prop), _y_est(prop.size()), @@ -104,7 +104,7 @@ SVMWrapper::~SVMWrapper() } -void SVMWrapper::setup_parameter_obj(double C) +void SVMWrapper::setup_parameter_obj(const double C) { _param.svm_type = C_SVC; _param.kernel_type = LINEAR; @@ -138,7 +138,7 @@ void SVMWrapper::setup_x_space() } -void SVMWrapper::copy_data(std::vector<int> inds, int task) +void SVMWrapper::copy_data(const std::vector<int> inds, const int task) { if(inds.size() > _n_dim) { @@ -161,7 +161,7 @@ void SVMWrapper::copy_data(std::vector<int> inds, int task) } } -void SVMWrapper::copy_data(std::vector<double*> val_ptrs) +void SVMWrapper::copy_data(const std::vector<double*> val_ptrs) { if(val_ptrs.size() > _n_dim) { @@ -184,7 +184,7 @@ void SVMWrapper::copy_data(std::vector<double*> val_ptrs) } } -void SVMWrapper::train(bool remap_coefs) +void SVMWrapper::train(const bool remap_coefs) { _model = svm_train(&_prob, &_param); std::copy_n(_model->rho, _intercept.size(), _intercept.data()); @@ -227,19 +227,19 @@ void SVMWrapper::train(bool remap_coefs) _n_misclassified += _y[ss] != _y_est[ss]; } -void SVMWrapper::train(std::vector<int> inds, int task, bool remap_coefs) +void SVMWrapper::train(const std::vector<int> inds, const int task, const bool remap_coefs) { copy_data(inds, task); train(remap_coefs); } -void SVMWrapper::train(std::vector<double*> val_ptrs, bool remap_coefs) +void SVMWrapper::train(const std::vector<double*> val_ptrs, const bool remap_coefs) { copy_data(val_ptrs); train(remap_coefs); } -std::vector<double> SVMWrapper::predict(int n_test_samp, std::vector<double*> val_ptrs) +std::vector<double> SVMWrapper::predict(const int n_test_samp, const std::vector<double*> val_ptrs) { if(val_ptrs.size() > _n_dim) { diff --git a/src/classification/SVMWrapper.hpp b/src/classification/SVMWrapper.hpp index 243ab85c7b0d1c70bbc78871f765e385a1145726..3fbf758b5956e9f9d70796f7f8195486b005b053 100644 --- a/src/classification/SVMWrapper.hpp +++ b/src/classification/SVMWrapper.hpp @@ -32,11 +32,11 @@ protected: std::vector<double> _w_remap; //!< Prefactors to convert the data to/from the preferred SVM range (0 to 1) std::vector<double> _b_remap; //!< Prefactors to map the minimum of the features to 0.0 - double _C; //!< The C parameter for the SVM calculation + const double _C; //!< The C parameter for the SVM calculation - int _n_dim; //!< The number of dimensions for the SVM problem - int _n_samp; //!< The number of samples per feature - int _n_class; //!< The number of classes in the data set + const int _n_dim; //!< The number of dimensions for the SVM problem + const int _n_samp; //!< The number of samples per feature + const int _n_class; //!< The number of classes in the data set int _n_misclassified; //!< The number of misclassified data points in the training set public: @@ -48,7 +48,7 @@ public: * @param n_samp Number of samples in the dataset * @param prop pointer to the start of the property vector */ - SVMWrapper(int n_class, int n_dim, int n_samp, double* prop); + SVMWrapper(const int n_class, const int n_dim, const int n_samp, const double* prop); /** * @brief The constructor for the SVMWrapper @@ -57,7 +57,7 @@ public: * @param n_dim Number of dimensions of the problem * @param prop The property vector */ - SVMWrapper(int n_class, int n_dim, std::vector<double> prop); + SVMWrapper(const int n_class, const int n_dim, const std::vector<double> prop); /** * @brief The constructor for the SVMWrapper @@ -68,7 +68,7 @@ public: * @param n_samp Number of samples in the dataset * @param prop pointer to the start of the property vector */ - SVMWrapper(double C, int n_class, int n_dim, int n_samp, double* prop); + SVMWrapper(const double C, const int n_class, const int n_dim, const int n_samp, const double* prop); /** * @brief The constructor for the SVMWrapper @@ -78,7 +78,7 @@ public: * @param n_dim Number of dimensions of the problem * @param prop The property vector */ - SVMWrapper(double C, int n_class, int n_dim, std::vector<double> prop); + SVMWrapper(const double C, const int n_class, const int n_dim, const std::vector<double> prop); /** * @brief The copy constructor @@ -105,7 +105,7 @@ public: * * @param C The C value for the SVM calculation */ - void setup_parameter_obj(double C); + void setup_parameter_obj(const double C); /** * @brief Set up the x_space and objects for libsvm @@ -119,21 +119,21 @@ public: * @param inds list of indexes to pull data for * @param task the task number for the calculation */ - void copy_data(std::vector<int> inds, int task); + void copy_data(const std::vector<int> inds, const int task); /** * @brief Copies the data from a set data pointers * * @param val_ptrs The pointers to the feature's data */ - void copy_data(std::vector<double*> val_ptrs); + void copy_data(const std::vector<double*> val_ptrs); /** * @brief Train the SVM model * * @param remap_coefs If true remap the final coefficients back to the unscaled feature space */ - void train(bool remap_coefs=true); + void train(const bool remap_coefs=true); /** * @brief Copy the data from a set of feature indexes (sorted_dmatrix) into the x_space and train the SVM model @@ -142,7 +142,7 @@ public: * @param task the task number for the calculation * @param remap_coefs If true remap the final coefficients back to the unscaled feature space */ - void train(std::vector<int> inds, int task, bool remap_coefs=true); + void train(const std::vector<int> inds, const int task, const bool remap_coefs=true); /** * @brief Copy tthe data from a set data pointers and train the SVM model @@ -150,7 +150,7 @@ public: * @param val_ptrs The pointers to the feature's data * @param remap_coefs If true remap the final coefficients back to the unscaled feature space */ - void train(std::vector<double*> val_ptrs, bool remap_coefs=true); + void train(const std::vector<double*> val_ptrs, const bool remap_coefs=true); /** * @brief Predict the class of a set of data from the SVM model @@ -160,7 +160,7 @@ public: * * @return The predicted class of the test samples */ - std::vector<double> predict(int n_test_samp, std::vector<double*> val_ptrs); + std::vector<double> predict(const int n_test_samp, const std::vector<double*> val_ptrs); /** * @brief the coefficients of the decision planes for each of the one against one SVM models @@ -172,43 +172,43 @@ public: * @brief the list of the bias terms in all the Svm models * @return The bias term for each SVM generated decision function */ - inline std::vector<double> intercept(){return _intercept;} + inline std::vector<double> intercept() const {return _intercept;} /** * @brief The number of classes in the training set * @return The number of classes in the training set */ - inline int n_class(){return _n_class;} + inline int n_class() const {return _n_class;} /** * @brief The number of dimensions of the SVM model * @return The number of dimensions of the SVM model */ - inline int n_dim(){return _n_dim;} + inline int n_dim() const {return _n_dim;} /** * @brief The number of samples in the training set * @return The number of samples in the training set */ - inline int n_samp(){return _n_samp;} + inline int n_samp() const {return _n_samp;} /** * @brief The predicted class for each sample in the data set * @return The predicted class for each sample in the data set */ - inline std::vector<double> y_estimate(){return _y_est;} + inline std::vector<double> y_estimate() const {return _y_est;} /** * @brief The actual class for each sample in the data set * @return The actual class for each sample in the data set */ - inline std::vector<double> y_actual(){return _y;} + inline std::vector<double> y_actual() const {return _y;} /** * @brief Number of misclassified samples in the data set * @return Number of misclassified samples in the data set */ - inline int n_misclassified(){return _n_misclassified;} + inline int n_misclassified() const {return _n_misclassified;} };