contrib/mul/clsfy/clsfy_smo_base.h
Go to the documentation of this file.
00001 #ifndef clsfy_smo_base_h_
00002 #define clsfy_smo_base_h_
00003 
00004 //:
00005 // \file
00006 // \author Ian Scott
00007 // \date 26-Nov-2001
00008 // \brief Sequential Minimum Optimisation algorithm
00009 
00010 #include <vcl_vector.h>
00011 #include <vnl/vnl_vector.h>
00012 #include <vnl/vnl_random.h>
00013 #include <mbl/mbl_data_wrapper.h>
00014 
00015 // Edit these definitions to suit your taste, but
00016 // don't commit the changes.
00017 
00018 #define CLSFY_SMO_BASE_PRINT_PROGRESS   0
00019 
00020 //: A base class for sequential minimal optimisation.
00021 // This finds the optimal point on a quadratic function constrained by
00022 // inequality bounds on each parameter, and a single equality constraint.
00023 // The is the class of problems associated with Support Vector Machines
00024 class clsfy_smo_base
00025 {
00026  protected:
00027 
00028   //: Error rate on the training data
00029   double error_;
00030 
00031   //: An iterator on the data;
00032   mbl_data_wrapper<vnl_vector<double> > *data_;
00033 
00034   //: The parameters to be optimised
00035   vnl_vector<double> alph_;
00036 
00037   //: Amount by which a sample can violate the KKT conditions
00038   double tolerance_;
00039 
00040   //: Tolerance on several equalities.
00041   // Including testing if a Lagrange multiplier is at one of the bounds.
00042   double eps_;
00043 
00044   //: Bias
00045   // \invariant a_i unbound and optimal => KKT_i holds
00046   double b_;
00047 
00048   //: Cache KKT error values for unbound multipliers.
00049   // \invariant a_i unbound => E_i = f(x_i) - y1 
00050   vcl_vector<double> error_cache_;
00051 
00052   //: Target values y_i
00053   vcl_vector<int> target_;
00054 
00055   //: The norm of each training vector is useful to know quickly
00056   vcl_vector<double> precomputed_self_dot_product_;
00057 
00058   vnl_random rng_;
00059 
00060   //: Attempt to jointly optimise Lagrange multipliers i1, and i2.
00061   // \param i1 first Lagrange multiplier.
00062   // \param i2 second Lagrange multiplier.
00063   // \param E1 The amount by which i1 violates KKT conditions.
00064   virtual int take_step(int i1, int i2, double E1) =0;
00065 
00066   //: Attempt to optimise sample i1.
00067   // This attempts to find another value i2,
00068   // in order to jointly optimise both.
00069   virtual int examine_example(int i1) =0;
00070 
00071   //: Access the data points
00072   const vnl_vector<double> & data_point(unsigned long l);
00073 
00074   //: Calculate the kernel for data items i1 and i2;
00075   virtual double kernel(int i1, int i2) =0;
00076 
00077   //: Calculate the classifier function learnt so far for data item k.
00078   virtual double learned_func(int k) ;
00079 
00080  public:
00081 
00082   //: Get the optimised parameters
00083   const vnl_vector<double>& lagrange_mults() const;
00084 
00085   //: Set the initial values of the parameters to be optimised.
00086   // The caller is responsible for ensuring that the initial values
00087   // fulfill the constraints;
00088   void set_lagrange_mults(const vnl_vector<double>& lagrange_mults);
00089 
00090   //: Bias term in function evaluation.
00091   // For SVMs this would be the value to be subtracted from sum of kernel
00092   // functions to get 0 as class boundary.
00093   double bias();
00094 
00095   //: Reseeds the internal random number generator.
00096   // To achieve quasi-random initialisation use;
00097   // \code
00098   // #include <vcl_ctime.h>
00099   // ..
00100   // sampler.reseed(vcl_time(0));
00101   // \endcode
00102   virtual void reseed(unsigned long seed);
00103 
00104   //: amount by which a sample can violate the KKT conditions
00105   const double& tolerance() const;
00106 
00107   //: Set the amount by which a sample can violate the KKT conditions.
00108   // Default value is 0.001
00109   void set_tolerance(double tolerance);
00110 
00111   //: tolerance on several equalities.
00112   // Including testing if a Lagrange multiplier is at one of the bounds.
00113   double eps() const;
00114 
00115   //: Set the tolerance on several equalities.
00116   // Including testing if a Lagrange multiplier is at one of the bounds.
00117   // Default value is 0.001
00118   void set_eps(double eps);
00119 
00120   clsfy_smo_base();
00121   virtual ~clsfy_smo_base();
00122 
00123   virtual double error_rate();
00124 
00125   //: Run the optimisation
00126   virtual int calc()=0;
00127 
00128   //: error rate on the training data
00129   double error();
00130 };
00131 
00132 #endif