KSquare Utilities
ModelUsfCasCor.cpp
Go to the documentation of this file.
1 #include "FirstIncludes.h"
2 #include <stdio.h>
3 #include <string>
4 #include <iostream>
5 #include <fstream>
6 #include <math.h>
7 #include <vector>
8 #include <sstream>
9 #include <iomanip>
10 #include <set>
11 #include <vector>
12 #include "MemoryDebug.h"
13 using namespace std;
14 
15 
16 #include "GlobalGoalKeeper.h"
17 #include "KKBaseTypes.h"
18 #include "KKException.h"
19 #include "OSservices.h"
20 #include "RunLog.h"
21 #include "KKStr.h"
22 using namespace KKB;
23 
24 
25 #include "ModelUsfCasCor.h"
26 #include "ClassProb.h"
27 #include "FeatureNumList.h"
28 #include "FeatureVector.h"
29 #include "MLClass.h"
30 using namespace KKMLL;
31 
32 
33 
35  Model (),
36  param (NULL),
37  usfCasCorClassifier (NULL)
38 {
39 }
40 
41 
42 
44  Model (_factoryFVProducer),
45  param (NULL),
46  usfCasCorClassifier (NULL)
47 {
48 }
49 
50 
52  const ModelParamUsfCasCor& _param, // Create new model from
53  FactoryFVProducerPtr _factoryFVProducer
54  ):
55  Model (_name, _param, _factoryFVProducer),
56  param (NULL),
57  usfCasCorClassifier (NULL)
58 {
59  param = dynamic_cast<ModelParamUsfCasCorPtr> (Model::param);
60 }
61 
62 
63 
64 
66  Model (_model),
67  param (NULL),
68  usfCasCorClassifier (NULL)
69 {
70  param = dynamic_cast<ModelParamUsfCasCorPtr> (Model::param);
71  if (_model.usfCasCorClassifier)
72  {
73  usfCasCorClassifier = new UsfCasCor (*_model.usfCasCorClassifier);
74  }
75 }
76 
77 
78 /**
79  *@brief Frees any memory allocated by, and owned by the ModelUsfCasCor
80  */
82 {
83  // The base class owns param, so we do not delete it.
84  // delete param;
86  {
87  delete usfCasCorClassifier;
88  usfCasCorClassifier = NULL;
89  }
90 }
91 
92 
94 {
95  kkint32 memoryConsumedEstimated = Model::MemoryConsumedEstimated () +
96  sizeof (usfCasCorClassifier);
97 
99  memoryConsumedEstimated += usfCasCorClassifier->MemoryConsumedEstimated ();
100  return memoryConsumedEstimated;
101 }
102 
103 
104 
105 
106 ModelUsfCasCorPtr ModelUsfCasCor::Duplicate () const
107 {
108  return new ModelUsfCasCor (*this);
109 }
110 
111 
112 
114 {
115  return param;
116 }
117 
118 
119 
121  bool _alreadyNormalized,
122  bool _takeOwnership, /*!< Model will take ownership of these examples */
123  VolConstBool& _cancelFlag,
124  RunLog& _log
125  )
126 {
127  _log.Level (10) << "ModelUsfCasCor::TrainModel[" << Name () << "]." << endl;
128 
129  if (param == NULL)
130  {
131  validModel = false;
132  KKStr errMsg = "ModelUsfCasCor::TrainModel (param == NULL)";
133  _log.Level (-1) << endl << endl << errMsg << endl << endl;
134  throw KKException (errMsg);
135  }
136 
138  {
139  delete usfCasCorClassifier;
140  usfCasCorClassifier = NULL;
141  }
142 
143  try
144  {
145  Model::TrainModel (_trainExamples, _alreadyNormalized, _takeOwnership, _cancelFlag, _log);
146  }
147  catch (const KKException& e)
148  {
149  validModel = false;
150  KKStr errMsg = "ModelUsfCasCor::TrainModel ***ERROR*** Exception occurred calling 'Model::TrainModel'.";
151  _log.Level (-1) << endl << errMsg << endl << e.ToString () << endl << endl;
152  throw KKException (errMsg, e);
153  }
154  catch (const exception& e2)
155  {
156  validModel = false;
157  KKStr errMsg = "ModelUsfCasCor::TrainModel ***ERROR*** Exception occurred calling 'Model::TrainModel'.";
158  _log.Level (-1) << endl << endl << errMsg << endl << e2.what () << endl << endl;
159  throw KKException (errMsg, e2);
160  }
161  catch (...)
162  {
163  validModel = false;
164  KKStr errMsg = "ModelUsfCasCor::TrainModel ***ERROR*** Exception occurred calling 'Model::TrainModel'.";
165  _log.Level (-1) << endl << endl << errMsg << endl << endl;
166  throw KKException (errMsg);
167  }
168 
169  // 'Model::TrainModel' Will have performed any BitReduction and Feature Encoding
170  // that needed to be done.
171  // Also the data structures 'classes', 'encoder', and 'fileDesc' will have been built.
172  // 'classes' will already be sorted in name order.
173  // The Prediction variables 'probabilities', 'votes', and 'crossClassProbTable' will
174  // have been built.
175 
177 
178  usfCasCorClassifier = new UsfCasCor ();
179 
180  usfCasCorClassifier->TrainNewClassifier (param->In_limit (),
188  _cancelFlag,
189  _log
190  );
191 
193 
194  _log.Level (10) << "ModelUsfCasCor::TrainModel Completed." << endl;
195 } /* TrainModel */
196 
197 
198 
199 
201  RunLog& log
202  )
203 {
204  if (!usfCasCorClassifier)
205  {
206  log.Level (-1) << endl << endl << "ModelUsfCasCor::Predict ***ERROR*** (usfCasCorClassifier == NULL)" << endl << endl;
207  return NULL;
208  }
209  bool newExampleCreated = false;
210  FeatureVectorPtr encodedExample = PrepExampleForPrediction (example, newExampleCreated);
211 
212  MLClassPtr predictedClass = usfCasCorClassifier->PredictClass (encodedExample);
213  if (newExampleCreated)
214  {
215  delete encodedExample;
216  encodedExample = NULL;
217  }
218 
219  return predictedClass;
220 } /* Predict */
221 
222 
223 
224 
226  MLClassPtr knownClass,
227  MLClassPtr& predClass1,
228  MLClassPtr& predClass2,
229  kkint32& predClass1Votes,
230  kkint32& predClass2Votes,
231  double& probOfKnownClass,
232  double& predClass1Prob,
233  double& predClass2Prob,
234  kkint32& numOfWinners,
235  bool& knownClassOneOfTheWinners,
236  double& breakTie,
237  RunLog& log
238  )
239 {
240  if (!usfCasCorClassifier)
241  {
242  KKStr errMsg = "ModelUsfCasCor::Predict ***ERROR*** (usfCasCorClassifier == NULL)";
243  log.Level (-1) << endl << endl << errMsg << endl << endl;
244  throw KKException (errMsg);
245  }
246 
247 
248  VectorFloat probabilities;
249 
250  float pc1p = 0.0f;
251  float pc2p = 0.0f;
252  float kcp = 0.0f;
253 
254  bool newExampleCreated = false;
255  FeatureVectorPtr encodedExample = PrepExampleForPrediction (example, newExampleCreated);
256  usfCasCorClassifier->PredictConfidences (encodedExample,
257  knownClass,
258  predClass1, pc1p,
259  predClass2, pc2p,
260  kcp,
261  *classes,
262  probabilities
263  );
264 
265  if (newExampleCreated)
266  {
267  delete encodedExample;
268  encodedExample = NULL;
269  }
270 
271  predClass1Prob = pc1p;
272  predClass2Prob = pc2p;
273  probOfKnownClass = kcp;
274 
275  breakTie = pc1p - pc2p;
276  numOfWinners = 1;
277 
278  knownClassOneOfTheWinners = (predClass1 == knownClass);
279 
280  return;
281 } /* Predict */
282 
283 
284 
285 
286 
288  RunLog& log
289  )
290 {
291  if (!usfCasCorClassifier)
292  {
293  KKStr errMsg = "ModelUsfCasCor::ProbabilitiesByClass ***ERROR*** (usfCasCorClassifier == NULL)";
294  log.Level (-1) << endl << endl << errMsg << endl << endl;
295  throw KKException (errMsg);
296  }
297 
298  bool newExampleCreated = false;
299  FeatureVectorPtr encodedExample = PrepExampleForPrediction (example, newExampleCreated);
300 
301  ClassProbListPtr results = usfCasCorClassifier->PredictClassConfidences (encodedExample);
302 
303  results->SortByProbability (true); // 'true' = Sort High to Low.
304 
305  if (newExampleCreated)
306  {
307  delete encodedExample;
308  encodedExample = NULL;
309  }
310 
311  return results;
312 } /* ProbabilitiesByClass */
313 
314 
315 
316 
318  const MLClassList& _mlClasses,
319  kkint32* _votes,
320  double* _probabilities,
321  RunLog& log
322  )
323 {
324  if (!usfCasCorClassifier)
325  {
326  KKStr errMsg = "ModelUsfCasCor::ProbabilitiesByClass ***ERROR*** (usfCasCorClassifier == NULL)";
327  log.Level (-1) << endl << endl << errMsg << endl << endl;
328  throw KKException (errMsg);
329  }
330 
331  VectorFloat probabilities;
332 
333  MLClassPtr pc1 = NULL;
334  MLClassPtr pc2 = NULL;
335  MLClassPtr kc = NULL;
336 
337  float pc1p = 0.0f;
338  float pc2p = 0.0f;
339  float kcp = 0.0f;
340 
341  bool newExampleCreated = false;
342  FeatureVectorPtr encodedExample = PrepExampleForPrediction (example, newExampleCreated);
343  usfCasCorClassifier->PredictConfidences (encodedExample,
344  kc,
345  pc1, pc1p,
346  pc2, pc2p,
347  kcp,
348  _mlClasses,
349  probabilities
350  );
351  if (newExampleCreated)
352  {
353  delete encodedExample;
354  encodedExample = NULL;
355  }
356 
357  if (_mlClasses.size () != probabilities.size ())
358  {
359  log.Level (-1) << endl << "ModelUsfCasCor::ProbabilitiesByClass ***ERROR***" << endl
360  << "\"_mlClasses.size () != probabilities.size ()\" This should not ever be able to happen." << endl
361  << endl;
362  for (int x = 0; x < _mlClasses.QueueSize (); ++x)
363  {
364  _votes[x] = 0;
365  _probabilities[x] = 0.0;
366  }
367  }
368  else
369  {
370  float highProb = -9999.99f;
371  int highProbIdx = -1;
372  for (kkuint32 x = 0; x < probabilities.size (); ++x)
373  {
374  float p = probabilities[x];
375  _votes[x] = 0;
376  _probabilities[x] = p;
377  if (p > highProb)
378  {
379  highProbIdx = x;
380  highProb = p;
381  }
382  }
383  if (highProbIdx >= 0)
384  votes[highProbIdx] = 1;
385  }
386 
387  return;
388 } /* ProbabilitiesByClass */
389 
390 
391 
392 
393 
395  const MLClassList& _mlClasses,
396  double* _probabilities,
397  RunLog& _log
398  )
399 {
400  if (!usfCasCorClassifier)
401  {
402  KKStr errMsg = "ModelUsfCasCor::ProbabilitiesByClass ***ERROR*** (usfCasCorClassifier == NULL)";
403  _log.Level (-1) << endl << endl << errMsg << endl << endl;
404  throw KKException (errMsg);
405  }
406 
407  VectorFloat probabilities;
408 
409  MLClassPtr pc1 = NULL;
410  MLClassPtr pc2 = NULL;
411  MLClassPtr kc = NULL;
412 
413  float pc1p = 0.0f;
414  float pc2p = 0.0f;
415  float kcp = 0.0f;
416 
417  bool newExampleCreated = false;
418  FeatureVectorPtr encodedExample = PrepExampleForPrediction (_example, newExampleCreated);
419  usfCasCorClassifier->PredictConfidences (encodedExample,
420  kc,
421  pc1, pc1p,
422  pc2, pc2p,
423  kcp,
424  _mlClasses,
425  probabilities
426  );
427  if (newExampleCreated)
428  {
429  delete encodedExample;
430  encodedExample = NULL;
431  }
432 
433  if (_mlClasses.size () != probabilities.size ())
434  {
435  _log.Level (-1) << endl << "ModelUsfCasCor::ProbabilitiesByClass ***ERROR***" << endl
436  << "\"_mlClasses.size () != probabilities.size ()\" This should not ever be able to happen." << endl
437  << endl;
438  for (int x = 0; x < _mlClasses.QueueSize (); ++x)
439  {
440  _probabilities[x] = 0.0;
441  }
442  }
443  else
444  {
445  for (kkuint32 x = 0; x < probabilities.size (); ++x)
446  _probabilities[x] = probabilities[x];
447  }
448 
449  return;
450 } /* ProbabilitiesByClass */
451 
452 
453 
454 
455 
457 {
458  return 0;
459 } /* NumOfSupportVectors */
460 
461 
462 
463 
464 
465 
466 void ModelUsfCasCor::WriteXML (const KKStr& varName,
467  ostream& o
468  ) const
469 {
470  XmlTag startTag ("ModelUsfCasCor", XmlTag::TagTypes::tagStart);
471  if (!varName.Empty ())
472  startTag.AddAtribute ("VarName", varName);
473  startTag.WriteXML (o);
474  o << endl;
475 
476  WriteModelXMLFields (o); // Write the base class data fields 1st.
477 
478  usfCasCorClassifier->WriteXML ("UsfCasCorClassifier", o);
479 
480  XmlTag endTag ("ModelUsfCasCor", XmlTag::TagTypes::tagEnd);
481  endTag.WriteXML (o);
482  o << endl;
483 } /* WriteXML */
484 
485 
486 
487 
488 
490  XmlTagConstPtr tag,
491  VolConstBool& cancelFlag,
492  RunLog& log
493  )
494 {
495  delete usfCasCorClassifier;
496  usfCasCorClassifier = NULL;
497 
498  XmlTokenPtr t = s.GetNextToken (cancelFlag, log);
499  while (t && (!cancelFlag))
500  {
501  t = ReadXMLModelToken (t, log);
502  if (t)
503  {
504  if (t->VarName ().EqualIgnoreCase ("UsfCasCorClassifier") && (typeid (*t) == typeid(XmlElementUsfCasCor)))
505  {
506  delete usfCasCorClassifier;
507  usfCasCorClassifier = dynamic_cast<XmlElementUsfCasCorPtr>(t)->TakeOwnership ();
508  }
509  else
510  {
511  KKStr errMsg (128);
512  errMsg << "ModelUsfCasCor::ReadXML ***ERROR*** Unexpected Element: Section: " << t->SectionName () << " VarName:" << t->VarName ();
513  log.Level (-1) << endl << errMsg << endl << endl;
514  AddErrorMsg (errMsg, 0);
515  }
516  }
517  delete t;
518  t = s.GetNextToken (cancelFlag, log);
519  }
520 
521  delete t;
522  t = NULL;
523 
524  if (!cancelFlag)
525  {
526  if (!param)
527  param = dynamic_cast<ModelParamUsfCasCorPtr> (Model::param);
528 
529  if (Model::param == NULL)
530  {
531  KKStr errMsg (128);
532  errMsg << "ModelUsfCasCor::ReadXML ***ERROR*** Base class 'Model' does not have 'param' defined.";
533  AddErrorMsg (errMsg, 0);
534  log.Level (-1) << endl << errMsg << endl << endl;
535  }
536 
537  else if (typeid (*Model::param) != typeid(ModelParamUsfCasCor))
538  {
539  KKStr errMsg (128);
540  errMsg << "ModelUsfCasCor::ReadXML ***ERROR*** Base class 'Model' param parameter is of the wrong type; found: " << Model::param->ModelParamTypeStr ();
541  AddErrorMsg (errMsg, 0);
542  log.Level (-1) << endl << errMsg << endl << endl;
543  }
544 
545  else
546  {
547  param = dynamic_cast<ModelParamUsfCasCorPtr> (Model::param);
548  }
549 
550  ReadXMLModelPost (log);
551  }
552 } /* ReadXML */
553 
554 
555 XmlFactoryMacro(ModelUsfCasCor)
KKStr(kkint32 size)
Creates a KKStr object that pre-allocates space for &#39;size&#39; characters.
Definition: KKStr.cpp:655
XmlTag(const KKStr &_name, TagTypes _tagType)
Definition: XmlStream.cpp:586
Base class to all Learning Algorithms.
Definition: Model.h:82
__int32 kkint32
Definition: KKBaseTypes.h:88
FeatureVector * FeatureVectorPtr
Definition: Model.h:44
ModelParamUsfCasCorPtr Param()
KKException(const KKStr &_exceptionStr, const KKException &_innerException)
Definition: KKException.cpp:83
XmlTokenPtr ReadXMLModelToken(XmlTokenPtr t, RunLog &log)
Will process any tokens that belong to &#39;ModelParam&#39; and return NULL ones that are not will be passed ...
Definition: Model.cpp:884
ModelParamPtr param
Definition: Model.h:412
FeatureVectorListPtr trainExamples
Definition: Model.h:424
void AddErrorMsg(const KKStr &errMsg, kkint32 lineNum)
Definition: Model.cpp:239
void WriteModelXMLFields(std::ostream &o) const
The "WriteXML" method in Derived classes call this method to include the parents classes fields in th...
Definition: Model.cpp:854
XmlToken * XmlTokenPtr
Definition: XmlStream.h:18
KKException(const KKStr &_exceptionStr, const std::exception &_innerException)
Definition: KKException.cpp:52
UsfCasCorPtr usfCasCorClassifier
virtual FeatureVectorPtr PrepExampleForPrediction(FeatureVectorPtr fv, bool &newExampleCreated)
Every prediction method in every class that is inherited from this class should call this method befo...
Definition: Model.cpp:574
virtual kkint32 MemoryConsumedEstimated() const
virtual void ProbabilitiesByClass(FeatureVectorPtr _example, const MLClassList &_mlClasses, double *_probabilities, RunLog &log)
Derives predicted probabilities by class.
KKTHread * KKTHreadPtr
virtual void Predict(FeatureVectorPtr example, MLClassPtr knownClass, MLClassPtr &predClass1, MLClassPtr &predClass2, kkint32 &predClass1Votes, kkint32 &predClass2Votes, double &probOfKnownClass, double &predClass1Prob, double &predClass2Prob, kkint32 &numOfWinners, bool &knownClassOneOfTheWinners, double &breakTie, RunLog &log)
void SortByProbability(bool highToLow=true)
Definition: ClassProb.cpp:143
void AddAtribute(const KKStr &attributeName, const KKStr &attributeValue)
Definition: XmlStream.cpp:602
virtual void ProbabilitiesByClass(FeatureVectorPtr example, const MLClassList &_mlClasses, kkint32 *_votes, double *_probabilities, RunLog &log)
bool Empty() const
Definition: KKStr.h:241
XmlTag const * XmlTagConstPtr
Definition: KKStr.h:45
std::vector< float > VectorFloat
Definition: KKBaseTypes.h:149
bool validModel
Definition: Model.h:426
Manages the reading and writing of objects in a simple XML format. For a class to be supported by Xml...
Definition: XmlStream.h:46
This class encapsulates are the information necessary to build a UsfCasCor class. ...
void TrainingTimeEnd()
Derived classes call this method to stop the clock for &#39;trainingTime&#39;.
Definition: Model.cpp:452
virtual kkint32 MemoryConsumedEstimated() const
Definition: Model.cpp:208
virtual ModelUsfCasCorPtr Duplicate() const
ModelUsfCasCor(const KKStr &_name, const ModelParamUsfCasCor &_param, FactoryFVProducerPtr _factoryFVProducer)
static KKStr Concat(const std::vector< std::string > &values)
Concatenates the list of &#39;std::string&#39; strings.
Definition: KKStr.cpp:1082
ClassProbList * ClassProbListPtr
Definition: Classifier2.h:30
ModelUsfCasCor(const ModelUsfCasCor &_model)
Model(const KKStr &_name, const ModelParam &_param, FactoryFVProducerPtr _factoryFVProducer)
Construct a instance of &#39;Model&#39; using the parameters specified in &#39;_param&#39;.
Definition: Model.cpp:152
virtual ~ModelUsfCasCor()
Frees any memory allocated by, and owned by the ModelUsfCasCor.
virtual ClassProbListPtr ProbabilitiesByClass(FeatureVectorPtr example, RunLog &log)
ModelParamUsfCasCor * ModelParamUsfCasCorPtr
virtual MLClassPtr Predict(FeatureVectorPtr example, RunLog &log)
void WriteXML(std::ostream &o)
Definition: XmlStream.cpp:723
Used for logging messages.
Definition: RunLog.h:49
void EncodeProblem(const struct svm_paramater &param, struct svm_problem &prob_in, struct svm_problem &prob_out)
virtual void TrainModel(FeatureVectorListPtr _trainExamples, bool _alreadyNormalized, bool _takeOwnership, VolConstBool &_cancelFlag, RunLog &_log)
Performs operations such as FeatureEncoding, and Normalization. The actual training of models occurs ...
Definition: Model.cpp:467
virtual kkint32 NumOfSupportVectors() const
void TrainingTimeStart()
Derived classes call this method to start the clock for &#39;trainingTime&#39;.
Definition: Model.cpp:445
void ReadXMLModelPost(RunLog &log)
Definition: Model.cpp:987
virtual KKStr ModelParamTypeStr() const
Definition: ModelParam.h:108
ModelParamUsfCasCorPtr param
Model(const Model &_madel)
Copy Constructor.
Definition: Model.cpp:69
virtual XmlTokenPtr GetNextToken(VolConstBool &cancelFlag, RunLog &log)
Definition: XmlStream.cpp:116
KKException(const KKStr &_exceptionStr)
Definition: KKException.cpp:45
ModelUsfCasCor(FactoryFVProducerPtr _factoryFVProducer)
Maintains a list of MLClass instances.
Definition: MLClass.h:233
virtual void ReadXML(XmlStream &s, XmlTagConstPtr tag, VolConstBool &cancelFlag, RunLog &log)
To be implemented by derived classes; the parent classes fields will be updated by the derived class ...
virtual void TrainModel(FeatureVectorListPtr _trainExamples, bool _alreadyNormalized, bool _takeOwnership, VolConstBool &_cancelFlag, RunLog &_log)
Performs operations such as FeatureEncoding, and Normalization. The actual training of models occurs ...
FeatureVectorList * FeatureVectorListPtr
Definition: Model.h:46
#define XmlFactoryMacro(NameOfClass)
Definition: XmlStream.h:688
virtual void WriteXML(const KKStr &varName, ostream &o) const
FactoryFVProducer * FactoryFVProducerPtr
Definition: Model.h:75
MLClassListPtr classes
Definition: Model.h:390
Model(FactoryFVProducerPtr _factoryFVProducer)
Use this when you are planning on creating a empty model without parameters.
Definition: Model.cpp:117
virtual FeatureNumListConstPtr SelectedFeatures() const
Definition: Model.cpp:436
volatile const bool VolConstBool
Definition: KKBaseTypes.h:163