KSquare Utilities
ModelKnn.cpp
Go to the documentation of this file.
1 #include "FirstIncludes.h"
2 
3 #include <stdio.h>
4 #include <string>
5 #include <iostream>
6 #include <fstream>
7 #include <math.h>
8 #include <vector>
9 #include <sstream>
10 #include <iomanip>
11 #include <set>
12 #include <vector>
13 #include "MemoryDebug.h"
14 using namespace std;
15 
16 
17 #include "GlobalGoalKeeper.h"
18 #include "KKBaseTypes.h"
19 #include "KKException.h"
20 #include "OSservices.h"
21 #include "RunLog.h"
22 #include "KKStr.h"
23 using namespace KKB;
24 
25 
26 #include "Model.h"
27 #include "ClassProb.h"
28 #include "FeatureNumList.h"
29 #include "FeatureVector.h"
30 #include "ModelKnn.h"
31 using namespace KKMLL;
32 
33 
34 
36  Model (),
37  param (NULL)
38 {
39 }
40 
41 
42 
43 ModelKnn::ModelKnn (FactoryFVProducerPtr _factoryFVProducer):
44  Model (_factoryFVProducer),
45  param (NULL)
46 {
47 }
48 
49 
50 
51 
52 ModelKnn::ModelKnn (const KKStr& _name,
53  const ModelParamKnn& _param, // Create new model from
54  FactoryFVProducerPtr _factoryFVProducer
55  ):
56  Model (_name, _param, _factoryFVProducer),
57  param (NULL)
58 {
59  param = dynamic_cast<ModelParamKnnPtr> (Model::param);
60 }
61 
62 
63 
64 
65 ModelKnn::ModelKnn (const ModelKnn& _model):
66  Model (_model),
67  param (NULL)
68 {
69  param = dynamic_cast<ModelParamKnnPtr> (Model::param);
70 }
71 
72 
73 
75 {
76 
77 }
78 
79 
80 
81 
82 ModelKnnPtr ModelKnn::Duplicate () const
83 {
84  return new ModelKnn (*this);
85 }
86 
87 
88 ModelParamKnnPtr ModelKnn::Param ()
89 {
90  return param;
91 }
92 
93 
94 MLClassPtr ModelKnn::Predict (FeatureVectorPtr example,
95  RunLog& log
96  )
97 {
98  return NULL;
99 }
100 
101 
102 
104  MLClassPtr knownClass,
105  MLClassPtr& predClass1,
106  MLClassPtr& predClass2,
107  kkint32& predClass1Votes,
108  kkint32& predClass2Votes,
109  double& probOfKnownClass,
110  double& predClass1Prob,
111  double& predClass2Prob,
112  kkint32& numOfWinners,
113  bool& knownClassOneOfTheWinners,
114  double& breakTie,
115  RunLog& log
116  )
117 {
118 } /* Predict */
119 
120 
121 
122 
124  RunLog& log
125  )
126 {
127  if (!classesIndex)
128  {
129  KKStr errMsg = "ModelKnn::ProbabilitiesByClass ***ERROR*** (classLabelAssigments == NULL)";
130  log.Level (-1) << endl << endl << errMsg << endl << endl;
131  throw KKException (errMsg);
132  }
133 
134  bool newExampleCreated = false;
135  FeatureVectorPtr encodedExample = PrepExampleForPrediction (example, newExampleCreated);
136 
137  // double y = SVM289::svm_predict_probability (svmModel, *encodedExample, classProbs, votes);
138 
139  if (newExampleCreated)
140  {
141  delete encodedExample;
142  encodedExample = NULL;
143  }
144 
145  ClassProbListPtr results = new ClassProbList ();
146  kkint32 idx;
147  for (idx = 0; idx < numOfClasses; idx++)
148  {
149  MLClassPtr ic = classesIndex->GetMLClass (idx);
150  results->PushOnBack (new ClassProb (ic, 0.0, 0.0f));
151  }
152 
153  return results;
154 } /* ProbabilitiesByClass */
155 
156 
157 
158 
160  const MLClassList& _mlClasses,
161  kkint32* _votes,
162  double* _probabilities,
163  RunLog& log
164  )
165 {
166 }
167 
168 
170  const MLClassList& _mlClasses,
171  double* _probabilities,
172  RunLog& log
173  )
174 {
175 } /* ProbabilitiesByClass */
176 
177 
178 
179 
181  bool _alreadyNormalized,
182  bool _takeOwnership, /**< Model will take ownership of these examples */
183  VolConstBool& _cancelFlag,
184  RunLog& _log
185  )
186 {
187  _log.Level (20) << "ModelKnn::TrainModel alreadyNormalized[" << _alreadyNormalized << "] _takeOwnership[" << _takeOwnership << "]." << endl;
188 
189  try
190  {
191  Model::TrainModel (_trainExamples, _alreadyNormalized, _takeOwnership, _cancelFlag, _log);
192  }
193  catch (const KKException& e)
194  {
195  validModel = false;
196  throw e;
197  }
198 } /* TrainModel */
199 
200 
201 
202 void ModelKnn::WriteXML (const KKStr& varName,
203  ostream& o
204  ) const
205 {
206  XmlTag startTag ("ModelParamDual", XmlTag::TagTypes::tagStart);
207  if (!varName.Empty ())
208  startTag.AddAtribute ("VarName", varName);
209  startTag.WriteXML (o);
210  o << endl;
211 
212  WriteModelXMLFields (o); // Write the base class data fields 1st.
213  if (param)
214  param->WriteXML ("Param", o);
215 
216  XmlTag endTag ("ModelParamDual", XmlTag::TagTypes::tagEnd);
217  endTag.WriteXML (o);
218  o << endl;
219 } /* WriteXML */
220 
221 
222 
223 
224 
226  XmlTagConstPtr tag,
227  VolConstBool& cancelFlag,
228  RunLog& log
229  )
230 {
231  XmlTokenPtr t = s.GetNextToken (cancelFlag, log);
232  while (t && (!cancelFlag))
233  {
234  t = ReadXMLModelToken (t, log);
235  if (t)
236  {
237  const KKStr& varName = t->VarName ();
238 
239  if ((varName.EqualIgnoreCase ("Param")) && (typeid (*t) == typeid (XmlElementModelParamKnn)))
240  {
241  delete param;
242  param = dynamic_cast<XmlElementModelParamKnnPtr> (t)->TakeOwnership ();
243  }
244  else
245  {
246  KKStr errMsg (256);
247  errMsg << "Invalid XmlElement Encountered: Section: " << t->SectionName () << " VarName: " << varName;
248  log.Level (-1) << endl << errMsg << endl << endl;
249  AddErrorMsg (errMsg, 0);
250  }
251  }
252  delete t;
253  t = s.GetNextToken (cancelFlag, log);
254  }
255 
256  delete t;
257  t = NULL;
258 
259  if (!param)
260  param = dynamic_cast<ModelParamKnnPtr> (Model::param);
261 
262  if (Model::param == NULL)
263  {
264  KKStr errMsg (128);
265  errMsg << "ModelKnn::ReadXML ***ERROR*** Base class 'Model' does not have 'param' defined.";
266  AddErrorMsg (errMsg, 0);
267  log.Level (-1) << endl << errMsg << endl << endl;
268  }
269 
270  else if (typeid (*Model::param) != typeid(ModelParamKnn))
271  {
272  KKStr errMsg (128);
273  errMsg << "ModelKnn::ReadXML ***ERROR*** Base class 'Model' param parameter is of the wrong type; found: " << Model::param->ModelParamTypeStr ();
274  AddErrorMsg (errMsg, 0);
275  log.Level (-1) << endl << errMsg << endl << endl;
276  }
277 
278  else
279  {
280  param = dynamic_cast<ModelParamKnnPtr> (Model::param);
281  }
282 
283  ReadXMLModelPost (log);
284 } /* ReadXML */
285 
286 
287 
288 XmlFactoryMacro(ModelKnn)
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
ModelKnn(const ModelKnn &_madel)
Definition: ModelKnn.cpp:65
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
virtual void PushOnBack(ClassProbPtr cp)
Definition: ClassProb.cpp:212
void AddErrorMsg(const KKStr &errMsg, kkint32 lineNum)
Definition: Model.cpp:239
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: ModelKnn.cpp:180
virtual ClassProbListPtr ProbabilitiesByClass(FeatureVectorPtr example, RunLog &log)
Definition: ModelKnn.cpp:123
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
void WriteXML(const KKStr &varName, ostream &o) const
Definition: ModelKnn.cpp:202
XmlToken * XmlTokenPtr
Definition: XmlStream.h:18
kkint32 numOfClasses
Definition: Model.h:410
MLClassPtr GetMLClass(kkint16 classIndex)
Locates the MLClass that was assigned classIndex.
Definition: MLClass.cpp:1636
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
KKTHread * KKTHreadPtr
virtual ModelKnnPtr Duplicate() const
Definition: ModelKnn.cpp:82
ModelParamKnnPtr Param()
Definition: ModelKnn.cpp:88
void AddAtribute(const KKStr &attributeName, const KKStr &attributeValue)
Definition: XmlStream.cpp:602
Used to record probability for a specified class; and a list of classes.
Definition: ClassProb.h:25
virtual ~ModelKnn()
Definition: ModelKnn.cpp:74
bool Empty() const
Definition: KKStr.h:241
XmlTag const * XmlTagConstPtr
Definition: KKStr.h:45
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
virtual void ProbabilitiesByClass(FeatureVectorPtr _example, const MLClassList &_mlClasses, double *_probabilities, RunLog &_log)
Derives predicted probabilities by class.
Definition: ModelKnn.cpp:169
ModelKnn(const KKStr &_name, const ModelParamKnn &_param, FactoryFVProducerPtr _factoryFVProducer)
Definition: ModelKnn.cpp:52
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
virtual MLClassPtr Predict(FeatureVectorPtr example, RunLog &log)
Definition: ModelKnn.cpp:94
ModelKnn(FactoryFVProducerPtr _factoryFVProducer)
Definition: ModelKnn.cpp:43
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
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 ...
Definition: ModelKnn.cpp:225
virtual void WriteXML(const KKStr &varName, ostream &o) const
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 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)
Definition: ModelKnn.cpp:103
void ReadXMLModelPost(RunLog &log)
Definition: Model.cpp:987
virtual KKStr ModelParamTypeStr() const
Definition: ModelParam.h:108
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
ClassProb(MLClassPtr _classLabel, double _probability, float _votes)
Definition: ClassProb.cpp:22
virtual void ProbabilitiesByClass(FeatureVectorPtr example, const MLClassList &_mlClasses, kkint32 *_votes, double *_probabilities, RunLog &log)
Definition: ModelKnn.cpp:159
Maintains a list of MLClass instances.
Definition: MLClass.h:233
FeatureVectorList * FeatureVectorListPtr
Definition: Model.h:46
virtual const KKStr & VarName() const
Definition: XmlStream.h:269
#define XmlFactoryMacro(NameOfClass)
Definition: XmlStream.h:688
MLClassIndexListPtr classesIndex
Definition: Model.h:392
FactoryFVProducer * FactoryFVProducerPtr
Definition: Model.h:75
Model(FactoryFVProducerPtr _factoryFVProducer)
Use this when you are planning on creating a empty model without parameters.
Definition: Model.cpp:117
volatile const bool VolConstBool
Definition: KKBaseTypes.h:163