KSquare Utilities
ModelParam.h
Go to the documentation of this file.
1 #ifndef _MODELPARAM_
2 #define _MODELPARAM_
3 
4 #include "RunLog.h"
5 #include "KKStr.h"
6 #include "FeatureNumList.h"
7 
8 namespace KKMLL
9 {
10  #ifndef _FileDesc_Defined_
11  class FileDesc;
12  typedef FileDesc* FileDescPtr;
13  #endif
14 
15 
16  // All the classes sub-classed from 'ModelParam' will need this.
17  #if !defined(_MLCLASS_)
18  class MLClass;
19  typedef MLClass* MLClassPtr;
20  class MLClassList;
21  typedef MLClassList* MLClassListPtr;
22  #endif
23 
24 
25 /**
26  *@brief Abstract Base class for Machine Learning parameters.
27  *@author Kurt Kramer
28  *@details
29  *For each Machine Learning algorithm implemented you would create a specialization of this class
30  *to manage the parameters required by the algorithm. Specifically for each new class that you
31  *create that is derived from 'Model' you will create a class derived from 'ModelParam'.
32  *This class encapsulates general parameters that are common to all Machine Learning Models.
33  *@see Model
34  */
35  class ModelParam
36  {
37  public:
39 
40  enum class ModelParamTypes
41  {Null,
42  Dual,
43  KNN,
44  OldSVM,
45  SvmBase,
46  UsfCasCor
47  };
48 
49  static KKStr ModelParamTypeToStr (ModelParamTypes _modelParamType);
50  static ModelParamTypes ModelParamTypeFromStr (const KKStr& _modelParamTypeStr);
51 
52 
53  /**
54  * Needs to be synchronized with SVMparam::SVM_EncodingMethod
55  */
56  enum class EncodingMethodType
57  {
58  Null,
59  NoEncoding,
60  Binary,
61  Scaled
62  };
63 
64  ModelParam ();
65 
66 
67  ModelParam (const ModelParam& _param);
68 
69  virtual
70  ~ModelParam ();
71 
72 
73 
74  virtual
75  ModelParamPtr Duplicate () const = 0;
76 
77 
78  virtual
80 
81 
82  virtual
83  void ParseCmdLine (KKStr _cmdLineStr,
84  bool& _validFormat,
85  RunLog& _log
86  );
87 
88 
89  virtual
90  void ParseCmdLinePost (RunLog& log);
91 
92 
93 
94 
95  /**
96  *@brief Creates a a Command Line String that represents these parameters.
97  *@details All derived classes should implement this method. They should first call this method and
98  * then append there own parameters that are specific to their implementation.
99  */
100  virtual
101  KKStr ToCmdLineStr () const;
102 
103 
104 
105  // Member access methods
106 
107  virtual ModelParamTypes ModelParamType () const = 0;
109 
110  virtual float AvgMumOfFeatures () const;
111  virtual EncodingMethodType EncodingMethod () const {return encodingMethod;}
112  virtual KKStr EncodingMethodStr () const {return EncodingMethodToStr (encodingMethod);}
113  virtual kkint32 ExamplesPerClass () const {return examplesPerClass;}
114  virtual const KKStr& FileName () const {return fileName;}
115  virtual bool NormalizeNominalFeatures () const {return normalizeNominalFeatures;}
116  virtual FeatureNumListConstPtr SelectedFeatures () const {return selectedFeatures;}
117  virtual bool ValidParam () const {return validParam;}
118 
119  // Access members that were originally put here for 'ModelSVMBase' and 'ModelOldSVM'
120  virtual float A_Param () const;
121  virtual double C_Param () const; // Same as 'Cost'
122  virtual double Cost () const;
123  virtual double Gamma () const;
124  virtual float Prob () const;
125 
126 
127  // Member update methods
128  virtual void EncodingMethod (EncodingMethodType _encodingMethod) {encodingMethod = _encodingMethod;}
129  virtual void ExamplesPerClass (kkint32 _examplesPerClass) {examplesPerClass = _examplesPerClass;}
130  virtual void FileName (const KKStr& _fileName) {fileName = _fileName;}
131  virtual void SelectedFeatures (FeatureNumListConst& _selectedFeatures);
132  virtual void ValidParam (bool _validParam) {validParam = _validParam;}
133 
134 
135  virtual void A_Param (float _prob);
136  virtual void C_Param (double _cost); // Same as 'Cost'
137  virtual void Cost (double _cost);
138  virtual void Gamma (double _gamma);
139  virtual void Prob (float _prob);
140 
142  RunLog& log
143  ) const;
144 
145 
146  static KKStr EncodingMethodToStr (EncodingMethodType encodingMethod);
147 
148  static EncodingMethodType EncodingMethodFromStr (const KKStr& encodingMethodStr);
149 
150 
151  virtual void ReadXML (XmlStream& s,
152  XmlTagConstPtr tag,
153  VolConstBool& cancelFlag,
154  RunLog& log
155  ) = 0;
156 
157 
158  virtual void WriteXML (const KKStr& varName,
159  std::ostream& o
160  ) const = 0;
161 
162 
163  /** @brief Will process any tokens that belong to 'ModelParam' and return NULL ones that are not will be passed back. */
165 
166  void WriteXMLFields (std::ostream& o) const;
167 
168 
169  private:
170 
171  virtual
172  void ParseCmdLineParameter (const KKStr& parameter,
173  const KKStr& value,
174  bool& parameterUsed,
175  RunLog& log
176  ) = 0;
177 
178 
179  EncodingMethodType encodingMethod;
180 
181  kkint32 examplesPerClass;
182 
183  KKStr fileName;
184 
185  bool normalizeNominalFeatures;
186 
187  mutable
188  FeatureNumListPtr selectedFeatures; /**< Feature Number to use. */
189 
190  bool validParam;
191 
192  // The following parameters are placed hear to support SVM based algorithms.
193  // to help transition from old(CRAPPY) design to newer less crappy design.
194  double cost;
195  double gamma;
196  float prob;
197 
198 
199  }; /* ModelParam */
200 
201  typedef ModelParam::ModelParamPtr ModelParamPtr;
202 
203 
204 
205 
206 
207  /**
208  * @brief Base class to be used by all ModelParam derived objects.
209  * @details Each derived class will need to implement their own XmlElement derived helper class. They will do it by utilizing the
210  * [[XmlElementModelParamTemplate]] template which inherits this base class "XmlElementModelParam".
211  */
213  {
214  public:
216  XmlStream& s,
217  VolConstBool& cancelFlag,
218  RunLog& log
219  ):
220  XmlElement (tag, s, log),
221  value (NULL)
222  {
223  }
224 
225 
227  {
228  delete value;
229  value = NULL;
230  }
231 
232 
233  virtual
234  ModelParam* const Value () const {return value;}
235 
236  virtual
238  {
239  ModelParam* v = value;
240  value = NULL;
241  return v;
242  }
243 
244  protected:
246  }; /* XmlElementModelParam */
247 
249 
250 
251 
252 
253 
254 
255  /**
256  *@brief ModelParam derived classes will implement their "XmlElement" helper class via this template.
257  *@see XmlElementModelParamKnn, XmlElementModelParamUsfCasCor, XmlElementModelOldSVM
258  */
259  template<class T>
261  {
262  public:
264  XmlStream& s,
265  VolConstBool& cancelFlag,
266  RunLog& log
267  ):
269  {
270  value = new T();
272  }
273 
274 
276  {
277  }
278 
279  T* const Value () const {return dynamic_cast<T*> (value);}
280 
282  {
283  T* v = dynamic_cast<T*> (value);
284  value = NULL;
285  return v;
286  }
287 
288  static
289  void WriteXML (const T& t,
290  const KKStr& varName,
291  std::ostream& o
292  )
293  {
294  dynamic_cast<T>(t).WriteXML (varName, o);
295  }
296  private:
297  }; /* XmlElementModelParamTemplate */
298 
299 
300 } /* namespace KKMLL */
301 
302 
303 
304 #endif
XmlTag * XmlTagPtr
Definition: Atom.h:31
virtual double Gamma() const
Definition: ModelParam.cpp:187
Provides a detailed description of the attributes of a dataset.
Definition: FileDesc.h:72
virtual KKStr ToCmdLineStr() const
Creates a a Command Line String that represents these parameters.
Definition: ModelParam.cpp:367
void WriteXMLFields(std::ostream &o) const
Definition: ModelParam.cpp:464
__int32 kkint32
Definition: KKBaseTypes.h:88
virtual void Cost(double _cost)
Definition: ModelParam.cpp:213
XmlElement(XmlTagPtr _nameTag, XmlStream &s, RunLog &log)
Definition: XmlStream.cpp:758
ModelParam derived classes will implement their "XmlElement" helper class via this template...
Definition: ModelParam.h:260
virtual double C_Param() const
Definition: ModelParam.cpp:177
virtual ModelParam *const Value() const
Definition: ModelParam.h:234
virtual void ExamplesPerClass(kkint32 _examplesPerClass)
Definition: ModelParam.h:129
Represents a "Class" in the Machine Learning Sense.
Definition: MLClass.h:52
FeatureNumList const FeatureNumListConst
virtual bool NormalizeNominalFeatures() const
Definition: ModelParam.h:115
virtual void EncodingMethod(EncodingMethodType _encodingMethod)
Definition: ModelParam.h:128
virtual void ParseCmdLinePost(RunLog &log)
Called after &#39;ParseCmdLine&#39; is completed. Classed derived from &#39;ModelParam&#39; can implement this method...
Definition: ModelParam.cpp:358
virtual EncodingMethodType EncodingMethod() const
Definition: ModelParam.h:111
virtual void WriteXML(const KKStr &varName, std::ostream &o) const =0
static void WriteXML(const T &t, const KKStr &varName, std::ostream &o)
Definition: ModelParam.h:289
XmlToken * XmlTokenPtr
Definition: XmlStream.h:18
virtual ModelParamPtr Duplicate() const =0
virtual void ValidParam(bool _validParam)
Definition: ModelParam.h:132
virtual void Prob(float _prob)
Definition: ModelParam.cpp:225
static KKStr ModelParamTypeToStr(ModelParamTypes _modelParamType)
Definition: ModelParam.cpp:122
static ModelParamTypes ModelParamTypeFromStr(const KKStr &_modelParamTypeStr)
Definition: ModelParam.cpp:148
virtual ModelParam * TakeOwnership()
Definition: ModelParam.h:237
virtual void Gamma(double _gamma)
Definition: ModelParam.cpp:219
FeatureNumList * FeatureNumListPtr
virtual kkint32 MemoryConsumedEstimated() const
Definition: ModelParam.cpp:87
static EncodingMethodType EncodingMethodFromStr(const KKStr &encodingMethodStr)
Definition: ModelParam.cpp:444
virtual float A_Param() const
Definition: ModelParam.cpp:172
virtual void FileName(const KKStr &_fileName)
Definition: ModelParam.h:130
XmlElementModelParam(XmlTagPtr tag, XmlStream &s, VolConstBool &cancelFlag, RunLog &log)
Definition: ModelParam.h:215
XmlTokenPtr ReadXMLModelParamToken(XmlTokenPtr t)
Will process any tokens that belong to &#39;ModelParam&#39; and return NULL ones that are not will be passed ...
Definition: ModelParam.cpp:487
virtual FeatureNumListConstPtr SelectedFeatures() const
Definition: ModelParam.h:116
XmlTag const * XmlTagConstPtr
Definition: KKStr.h:45
virtual ModelParamTypes ModelParamType() const =0
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 float AvgMumOfFeatures() const
Definition: ModelParam.cpp:99
virtual const KKStr & FileName() const
Definition: ModelParam.h:114
static KKStr Concat(const std::vector< std::string > &values)
Concatenates the list of &#39;std::string&#39; strings.
Definition: KKStr.cpp:1082
XmlElementModelParamTemplate(XmlTagPtr tag, XmlStream &s, VolConstBool &cancelFlag, RunLog &log)
Definition: ModelParam.h:263
FileDesc * FileDescPtr
virtual KKStr EncodingMethodStr() const
Definition: ModelParam.h:112
virtual void SelectedFeatures(FeatureNumListConst &_selectedFeatures)
Definition: ModelParam.cpp:109
virtual float Prob() const
Definition: ModelParam.cpp:192
ModelParam(const ModelParam &_param)
Definition: ModelParam.cpp:61
virtual kkint32 NumOfFeaturesAfterEncoding(FileDescPtr fileDesc, RunLog &log) const
Definition: ModelParam.cpp:387
FeatureNumListConst * FeatureNumListConstPtr
KKStr & operator=(const KKStr &src)
Definition: KKStr.cpp:1390
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)
static KKStr EncodingMethodToStr(EncodingMethodType encodingMethod)
Definition: ModelParam.cpp:428
virtual KKStr ModelParamTypeStr() const
Definition: ModelParam.h:108
XmlElementModelParam * XmlElementModelParamPtr
Definition: ModelParam.h:248
virtual void A_Param(float _prob)
Definition: ModelParam.cpp:201
Maintains a list of MLClass instances.
Definition: MLClass.h:233
virtual void ReadXML(XmlStream &s, XmlTagConstPtr tag, VolConstBool &cancelFlag, RunLog &log)=0
virtual void C_Param(double _cost)
Definition: ModelParam.cpp:207
Base class to be used by all ModelParam derived objects.
Definition: ModelParam.h:212
virtual double Cost() const
Definition: ModelParam.cpp:182
Abstract Base class for Machine Learning parameters.
Definition: ModelParam.h:35
virtual ~ModelParam()
Definition: ModelParam.cpp:80
virtual kkint32 ExamplesPerClass() const
Definition: ModelParam.h:113
ModelParam * ModelParamPtr
Definition: ModelParam.h:38
virtual bool ValidParam() const
Definition: ModelParam.h:117
virtual void ParseCmdLine(KKStr _cmdLineStr, bool &_validFormat, RunLog &_log)
Definition: ModelParam.cpp:234
volatile const bool VolConstBool
Definition: KKBaseTypes.h:163