00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #ifndef __plLearn_h__
00019 #define __plLearn_h__
00020
00021
00022 #include <plDataValues.h>
00023 #include <plValues.h>
00024 #include <plVariablesConjunction.h>
00025 #include <plKernelTable.h>
00026 #include <plKernelMap.h>
00027
00028 class kplVariable;
00029
00031 class plLearnObject : public plObject
00032 {
00033 public:
00035 plLearnObject(const plVariablesConjunction &variables)
00036 :_nsamples(0),
00037 _total_weight(0.0),
00038 _all_vars(variables.get_variable_list()),
00039 _values(_all_vars){};
00040
00042 plLearnObject()
00043 :_nsamples(0),_total_weight(0.0){};
00044
00046 virtual ~plLearnObject(){};
00047
00050 unsigned int get_nsamples() const {return _nsamples;}
00051
00054 double get_total_weight() const {return _total_weight;}
00055
00058 virtual void reset() = 0;
00059
00063 virtual void internal_addPoint(const plDataValues &point, double weight = 1.0) = 0;
00064
00065 public:
00068 void addNewPoint(const plValues &point, double weight = 1.0)
00069 {
00070 internal_addPoint(point.to_data_values(_all_vars), weight);
00071 }
00072
00075 template <class arrayT>
00076 void addNewPoint(arrayT* point, double weight = 1.0)
00077 {
00078 for(unsigned int i = 0; i < _values.size(); i++){
00079 _values[i] = point[i];
00080 }
00081 internal_addPoint(_values, weight);
00082 }
00083
00086 template <class arrayT>
00087 void addNewPoint(const vector <arrayT> &point, double weight = 1.0)
00088 {
00089 for(unsigned int i = 0; i < _values.size(); i++){
00090 _values[i] = point[i];
00091 }
00092 internal_addPoint(_values, weight);
00093 }
00097 template <class singleT>
00098 void addNewPoint(singleT point, double weight = 1.0)
00099 {
00100 _values[0] = point;
00101 internal_addPoint(_values, weight);
00102 }
00103
00105 const list <kplVariable*> &get_vars()const{return _all_vars;}
00106
00108 void set_known_range(const vector <bool> known_range){ _known_range = known_range;}
00109
00110 virtual plKernelMap get_kernel_map( const void *parameters = NULL )const {return plKernelMap();}
00111
00112
00113 virtual void internal_addPointWithDef(const plDataValues &point, const vector <bool> &left_defined, double weight = 1.0)
00114 {
00115 internal_addPointWithDef(point, left_defined, weight);
00116 }
00117
00120 void addNewPointWithDef(const plValues &point, const vector <bool> &left_defined, double weight = 1.0)
00121 {
00122 internal_addPointWithDef(point.to_data_values(_all_vars), left_defined, weight);
00123 }
00124
00127 template <class arrayT>
00128 void addNewPointWithDef(arrayT* point, const vector <bool> &left_defined, double weight = 1.0)
00129 {
00130 for(unsigned int i = 0; i < _values.size(); i++){
00131 _values[i] = point[i];
00132 }
00133 internal_addPointWithDef(_values, left_defined, weight);
00134 }
00135
00138 template <class arrayT>
00139 void addNewPointWithDef(const vector <arrayT> &point, const vector <bool> &left_defined, double weight = 1.0)
00140 {
00141 for(unsigned int i = 0; i < _values.size(); i++){
00142 _values[i] = point[i];
00143 }
00144 internal_addPointWithDef(_values, left_defined, weight);
00145 }
00146
00147
00148 protected:
00149 unsigned int _nsamples;
00150 double _total_weight;
00151
00152
00153 list <kplVariable*> _all_vars;
00154 vector <bool> _known_range;
00155 plDataValues _values;
00156 };
00157
00158
00161 class plNonCndLearnObject :public plLearnObject
00162 {
00163 public:
00165 plNonCndLearnObject()
00166 :plLearnObject(){}
00167
00169 plNonCndLearnObject(const plVariablesConjunction &variables)
00170 :plLearnObject(variables),_left_vars(variables){}
00171
00172
00174 virtual ~plNonCndLearnObject(){}
00175
00178 virtual void get_params(plValues ¶ms) const = 0;
00179
00181 virtual plKernel get_distribution(const void *parameters = NULL ) const = 0;
00182
00183 protected:
00184 plVariablesConjunction _left_vars;
00185 };
00186
00187
00194 template <class T>
00195 class plCndLearnObject :public plLearnObject
00196 {
00197 public:
00201 plCndLearnObject(const plVariablesConjunction &left_vars, const plVariablesConjunction &right_vars)
00202 :plLearnObject(left_vars^right_vars),
00203 _left_value(left_vars.get_variable_list()),
00204 _right_value(right_vars.get_variable_list()),
00205 _left_vars(left_vars), _right_vars(right_vars), _initialized(false)
00206 {
00207 }
00208
00212 plCndLearnObject(const plVariablesConjunction &left_vars, const plVariablesConjunction &right_vars, const T&init_object)
00213 :plLearnObject(left_vars^right_vars),
00214 _left_value(left_vars.get_variable_list()),
00215 _right_value(right_vars.get_variable_list()),
00216 _left_vars(left_vars), _right_vars(right_vars), _initialized(true), _init_object(init_object)
00217 {
00218 }
00219
00221 virtual ~plCndLearnObject(){}
00222
00224 void reset()
00225 {
00226 _nsamples = 0;
00227 _total_weight = 0.0;
00228 _NCndLObjects_map.clear();
00229 }
00230
00233 void internal_addPoint(const plDataValues &point, double weight = 1.0)
00234 {
00235 _nsamples++;
00236 _total_weight += weight;
00237 point.extract(_left_value, 0, _left_value.size());
00238 point.extract(_right_value, _left_value.size(), _right_value.size());
00239 typename map <plDataValues, T>::iterator mapIt = _NCndLObjects_map.find(_right_value);
00240 if(mapIt != _NCndLObjects_map.end() )
00241 (mapIt->second).internal_addPoint(_left_value, weight);
00242 else{
00243 if(_initialized){
00244 T nlo(_init_object);
00245 nlo.internal_addPoint(_left_value, weight);
00246 pair<plDataValues,T> p(_right_value, nlo);
00247 _NCndLObjects_map.insert(p);
00248 }
00249 else{
00250 T nlo(_left_vars);
00251 nlo.internal_addPoint(_left_value, weight);
00252 pair<plDataValues,T> p(_right_value, nlo);
00253 _NCndLObjects_map.insert(p);
00254 }
00255
00256 }
00257
00258 }
00259
00260
00261 void internal_addPointWithDef(const plDataValues &point, const vector <bool> &left_defined, double weight = 1.0)
00262 {
00263 _nsamples++;
00264 _total_weight += weight;
00265 point.extract(_left_value, 0, _left_value.size());
00266 point.extract(_right_value, _left_value.size(), _right_value.size());
00267 typename map <plDataValues, T>::iterator mapIt = _NCndLObjects_map.find(_right_value);
00268 if(mapIt != _NCndLObjects_map.end() )
00269 (mapIt->second).internal_addPointWithDef(_left_value, left_defined, weight);
00270 else{
00271 if(_initialized){
00272 T nlo(_init_object);
00273 nlo.internal_addPointWithDef(_left_value, left_defined, weight);
00274 pair<plDataValues,T> p(_right_value, nlo);
00275 _NCndLObjects_map.insert(p);
00276 }
00277 else{
00278 T nlo(_left_vars);
00279 nlo.internal_addPointWithDef(_left_value, left_defined, weight);
00280 pair<plDataValues,T> p(_right_value, nlo);
00281 _NCndLObjects_map.insert(p);
00282 }
00283
00284 }
00285 }
00286
00287
00288
00289
00290
00291 public:
00293 const map <plDataValues, T> &get_map()const
00294 {
00295 return _NCndLObjects_map;
00296 }
00297
00300 void get_params(plValues ¶ms, const plValues &right_value)
00301 {
00302 typename map <plDataValues, T>::iterator mIt =
00303 _NCndLObjects_map.find(right_value.to_data_values(_right_vars.get_variable_list()));
00304
00305 if(mIt != _NCndLObjects_map.end())
00306 mIt->second.get_params(params);
00307 else
00308 plWarning(51);
00309 }
00310
00312 plKernelMap get_kernel_map( const void *parameters = NULL )const
00313 {
00314 plKernelMap kenMap(_left_vars, _right_vars);
00315 plValues rightVals(_right_vars);
00316
00317 typename map< plDataValues, T >::const_iterator mapIt;
00318 for(mapIt = _NCndLObjects_map.begin(); mapIt != _NCndLObjects_map.end(); ++mapIt)
00319 {
00320 rightVals.set_values_values(mapIt->first);
00321 kenMap.push(rightVals, mapIt->second.get_distribution( parameters ) );
00322 }
00323 return kenMap;
00324 }
00325
00326 void get_total_weights(vector <double> &w)const
00327 {
00328 w.resize(_NCndLObjects_map.size());
00329
00330 typename map< plDataValues, T >::const_iterator mapIt;
00331
00332 unsigned int index;
00333 for(index = 0, mapIt = _NCndLObjects_map.begin(); mapIt != _NCndLObjects_map.end(); ++mapIt, ++index){
00334 w[index] = mapIt->second.get_total_weight();
00335 }
00336
00337 }
00338
00339
00340 protected:
00341
00342 map <plDataValues, T> _NCndLObjects_map;
00343 plDataValues _left_value;
00344 plDataValues _right_value;
00345
00346 plVariablesConjunction _left_vars;
00347 plVariablesConjunction _right_vars;
00348
00349 bool _initialized;
00350 const T _init_object;
00351
00352 };
00353
00354
00357 class plLearnDistribVector :public plLearnObject
00358 {
00359 public:
00365 plLearnDistribVector(const vector <plLearnObject*> &learn_objects,
00366 const plVariablesConjunction &vars,
00367 const vector <bool> &known_range);
00368
00371 plLearnDistribVector(const vector <plLearnObject*> &learn_objects,
00372 const plVariablesConjunction &vars);
00373
00374
00376 virtual ~plLearnDistribVector(){}
00377
00379 void reset();
00380
00383 void internal_addPoint(const plDataValues &point, double weight = 1.0);
00384
00385
00390 template <class filterArrayT>
00391 void addFilterNewPoint(const plValues &point, filterArrayT filter, double weight = 1.0)
00392 {
00393 addFilterPoint(point.to_data_values(_all_vars), filter, weight);
00394 }
00395
00397 template <class arrayT, class filterArrayT>
00398 void addFilterNewPoint(arrayT* point, filterArrayT filter, double weight = 1.0)
00399 {
00400 for(unsigned int i = 0; i < _values.size(); i++){
00401 _values[i] = point[i];
00402 }
00403 addFilterPoint(_values, filter, weight);
00404 }
00405
00407 template <class arrayT, class filterArrayT>
00408 void addFilterNewPoint(const vector <arrayT> &point, filterArrayT filter, double weight = 1.0)
00409 {
00410 for(unsigned int i = 0; i < _values.size(); i++){
00411 _values[i] = point[i];
00412 }
00413 addFilterPoint(_values, filter, weight);
00414 }
00415
00418 template <class singleT, class filterArrayT>
00419 void addFilterNewPoint(singleT point, filterArrayT filter, double weight = 1.0)
00420 {
00421 _values[0] = point;
00422 addFilterPoint(_values, filter, weight);
00423 }
00424
00426 template <class filterArrayT>
00427 void addFilterPoint(const plDataValues &point, filterArrayT distrib_to_update, double weight = 1.0)
00428 {
00429 unsigned int i, j;
00430
00431 for( i = 0; i < _learn_objects.size(); i++){
00432
00433 if(distrib_to_update[i]){
00434 for(j = 0; j < _point_to_children_points[i].size(); j++){
00435 _children_points[i][j] = point[ _point_to_children_points[i][j]];
00436 }
00437 _learn_objects[i]->internal_addPoint(_children_points[i], weight);
00438 }
00439 }
00440 }
00441
00442
00443 protected:
00444 void Init(const vector <plLearnObject*> &learn_objects, const vector <bool> &known_range);
00445 protected:
00446 vector <plLearnObject*> _learn_objects;
00447 vector <plDataValues> _children_points;
00448
00449 vector < vector < unsigned int> > _point_to_children_points;
00450
00451 };
00452
00453
00454 #endif