KSquare Utilities
BinaryClassParms.h
Go to the documentation of this file.
1 #ifndef _BINARYCLASSPARMS_
2 #define _BINARYCLASSPARMS_
3 
4 /**
5  @class KKMLL::BinaryClassParms
6  @brief Machine Learning parameters to be used by a pair of classes.
7  @author Kurt Kramer
8  @details
9  @code
10  **********************************************************************
11  ** Written by: Kurt Kramer *
12  ** For: Research Work, Plankton recognition System *
13  **-------------------------------------------------------------------*
14  ** History *
15  ** *
16  ** date Programmer Description *
17  ** *
18  ** May-2004 Kurt Kramer Original Development. *
19  **-------------------------------------------------------------------*
20  ** Was developed to support Feature Selection for each binary class *
21  ** combination. This way each two class combination can have SVM *
22  ** and feature parameters that are specific to them. *
23  **********************************************************************
24  @endcode
25  @see KKMLL::SVMparam, KKMLL::ModelParamOldSVM, KKMLL::SVM233
26  */
27 #include "KKQueue.h"
28 
29 #include "svm.h"
30 #include "FeatureNumList.h"
31 #include "FileDesc.h"
32 
33 namespace KKMLL
34 {
35  #if !defined (_MLCLASS_)
36  class MLClass;
37  typedef MLClass* MLClassPtr;
38  #endif
39 
40 
41  /**
42  *@brief Similar to SVMparam except it is specialized for two classes.
43  * @see SVMparam
44  * @see SVM233
45  */
47  {
48  public:
50 
51  typedef SVM233::svm_parameter svm_parameter;
52 
53  /**
54  *@brief Constructor for 'BinaryClassParms' where caller supplies the two classes and parameters for that specific class pair.
55  *@param[in] _class1
56  *@param[in] _class2
57  *@param[in] _param
58  *@param[in] _selectedFeatures Will create a duplicate instance; caller will still have same ownership status.
59  *@param[in] _weight
60  */
61 
63  MLClassPtr _class2,
64  const svm_parameter& _param,
65  FeatureNumListConstPtr _selectedFeatures,
66  float _weight
67  );
68 
69  BinaryClassParms (const BinaryClassParms& binaryClassParms);
70 
71  ~BinaryClassParms ();
72 
73  static
75  RunLog& _log
76  );
77 
78  // Member Access Methods
79  double AParam () const {return param.A;}
80  MLClassPtr Class1 () const {return class1;}
81  MLClassPtr Class2 () const {return class2;}
82  double C () const {return param.C;}
83  const svm_parameter& Param () const {return param;}
84  FeatureNumListConstPtr SelectedFeatures () const {return selectedFeatures;}
85  float Weight () const {return weight;}
86 
87  kkuint16 NumOfFeatures (FileDescPtr fileDesc) const;
89 
90 
91  // member Update methods
92  void AParam (float _A) {param.A = _A;}
93  void C (double _c) {param.C = _c;}
94  void Gamma (double _gamma) {param.gamma = _gamma;}
95  void Param (const svm_parameter& _param) {param = _param;}
96  void SelectedFeatures (const FeatureNumListConst& _selectedFeatures);
97  void SelectedFeatures (FeatureNumListConstPtr _selectedFeatures); /**< Will make copy of instance; caller will retain ownership status. */
98  void Weight (float _weight) {weight = _weight;}
99 
100  KKStr Class1Name () const;
101  KKStr Class2Name () const;
103  KKStr ToTabDelString () const;
104 
105 
106  private:
107  MLClassPtr class1;
108  MLClassPtr class2;
109 
110  svm_parameter param; /**< From SVMlib */
111 
112  mutable
113  FeatureNumListPtr selectedFeatures; /**< Feature Numbers to use. */
114  float weight;
115  }; /* BinaryClassParms */
116 
117 
118 
119 
120  typedef BinaryClassParms::BinaryClassParmsPtr BinaryClassParmsPtr;
121 
122 
123 
125  {
126  public:
128 
130 
131  BinaryClassParmsList (bool _owner);
132 
133 
134  private:
135  BinaryClassParmsList (const BinaryClassParmsList& binaryClassList);
136 
137  public:
138  BinaryClassParmsList (const BinaryClassParmsList& binaryClassList,
139  bool _owner
140  );
141 
142 
143  static
144  BinaryClassParmsListPtr CreateFromXML (std::istream& i,
145  FileDescPtr fileDesc,
146  RunLog& log
147  );
148 
149  static
151  FileDescPtr fileDesc,
152  RunLog& log
153  );
154 
156 
157  float FeatureCountNet (FileDescPtr fileDesc) const;
158 
159  BinaryClassParmsPtr LookUp (MLClassPtr _class1,
160  MLClassPtr _class2
161  ) const;
162 
164 
166 
167  virtual
168  void PushOnBack (BinaryClassParmsPtr binaryParms);
169 
170  virtual
171  void PushOnFront (BinaryClassParmsPtr binaryParms);
172 
173  void ReadXML (FILE* i,
174  FileDescPtr fileDesc,
175  RunLog& log
176  );
177 
178  void ReadXML (std::istream& i,
179  FileDescPtr fileDesc,
180  RunLog& log
181  );
182 
183  void WriteXML (std::ostream& o) const;
184 
185 
186 
187  virtual void ReadXML (XmlStream& s,
188  XmlTagConstPtr tag,
189  VolConstBool& cancelFlag,
190  RunLog& log
191  );
192 
193 
194  virtual void WriteXML (const KKStr& varName,
195  std::ostream& o
196  ) const;
197 
198 
199 
200 
201  private:
202  struct KeyField
203  {
204  KeyField (MLClassPtr _class1,
205  MLClassPtr _class2
206  );
207 
208  bool operator< (const KeyField& p2) const;
209 
210  MLClassPtr class1;
211  MLClassPtr class2;
212  };
213 
214  typedef std::map<KeyField,BinaryClassParmsPtr> ClassIndexType;
215  typedef std::pair<KeyField, BinaryClassParmsPtr> ClassIndexPair;
216  ClassIndexType classIndex;
217  };
218 
219 
220  typedef BinaryClassParmsList::BinaryClassParmsListPtr BinaryClassParmsListPtr;
221 
224 
225 
226 } /* namespace KKMLL */
227 
228 #endif
MLClass * MLClassPtr
Definition: MLClass.h:46
void Gamma(double _gamma)
BinaryClassParms(MLClassPtr _class1, MLClassPtr _class2, const svm_parameter &_param, FeatureNumListConstPtr _selectedFeatures, float _weight)
Constructor for &#39;BinaryClassParms&#39; where caller supplies the two classes and parameters for that spec...
__int32 kkint32
Definition: KKBaseTypes.h:88
void SelectedFeatures(const FeatureNumListConst &_selectedFeatures)
Similar to SVMparam except it is specialized for two classes.
decision_function svm_train_one(const svm_problem *prob, const svm_parameter *param, double Cp, double Cn, std::set< kkint32 > &BSVIndex)
Definition: svm.cpp:3154
BinaryClassParmsPtr LookUp(MLClassPtr _class1, MLClassPtr _class2) const
virtual void ReadXML(XmlStream &s, XmlTagConstPtr tag, VolConstBool &cancelFlag, RunLog &log)
static BinaryClassParmsListPtr CreateFromXML(std::istream &i, FileDescPtr fileDesc, RunLog &log)
void Weight(float _weight)
unsigned __int16 kkuint16
16 bit unsigned integer.
Definition: KKBaseTypes.h:86
kkuint16 NumOfFeatures(FileDescPtr fileDesc) const
FeatureNumList const FeatureNumListConst
void ReadXML(std::istream &i, FileDescPtr fileDesc, RunLog &log)
static BinaryClassParmsListPtr CreateFromXML(std::FILE *i, FileDescPtr fileDesc, RunLog &log)
void Param(const svm_parameter &_param)
float FeatureCountNet(FileDescPtr fileDesc) const
Returns the Average number of selected features.
XmlElementBinaryClassParmsList * XmlElementBinaryClassParmsListPtr
BinaryClassParms(const BinaryClassParms &binaryClassParms)
void WriteXML(std::ostream &o) const
FeatureNumList * FeatureNumListPtr
void SelectedFeatures(FeatureNumListConstPtr _selectedFeatures)
XmlTag const * XmlTagConstPtr
Definition: KKStr.h:45
BinaryClassParmsList * BinaryClassParmsListPtr
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 WriteXML(const KKStr &varName, std::ostream &o) const
BinaryClassParmsList(const BinaryClassParmsList &binaryClassList, bool _owner)
static KKStr Concat(const std::vector< std::string > &values)
Concatenates the list of &#39;std::string&#39; strings.
Definition: KKStr.cpp:1082
virtual void PushOnBack(BinaryClassParmsPtr binaryParms)
FileDesc * FileDescPtr
MLClassPtr Class2() const
void ReadXML(FILE *i, FileDescPtr fileDesc, RunLog &log)
const svm_parameter & Param() const
kkint32 MemoryConsumedEstimated() const
static BinaryClassParmsPtr CreateFromTabDelStr(const KKStr &_str, RunLog &_log)
FeatureNumListConst * FeatureNumListConstPtr
Used for logging messages.
Definition: RunLog.h:49
BinaryClassParmsList * DuplicateListAndContents() const
void EncodeProblem(const struct svm_paramater &param, struct svm_problem &prob_in, struct svm_problem &prob_out)
FeatureNumListConstPtr SelectedFeatures() const
virtual void PushOnFront(BinaryClassParmsPtr binaryParms)
XmlElementTemplate< BinaryClassParmsList > XmlElementBinaryClassParmsList
double gamma
Definition: svm.h:79
svm_parameter & operator=(const svm_parameter &right)
Definition: svm.cpp:828
BinaryClassParms * BinaryClassParmsPtr
kkint32 MemoryConsumedEstimated() const
FeatureNumListConstPtr SelectedFeaturesFD(FileDescPtr fileDesc) const
MLClassPtr Class1() const
volatile const bool VolConstBool
Definition: KKBaseTypes.h:163