KSquare Utilities
ModelParamSvmBase.cpp
Go to the documentation of this file.
1 #include "FirstIncludes.h"
2 #include <stdio.h>
3 #include <fstream>
4 #include <string>
5 #include <iostream>
6 #include <vector>
7 #include "MemoryDebug.h"
8 using namespace std;
9 
10 #include "GlobalGoalKeeper.h"
11 #include "KKBaseTypes.h"
12 #include "OSservices.h"
13 #include "RunLog.h"
14 using namespace KKB;
15 
16 
17 #include "ModelParamSvmBase.h"
18 #include "FileDesc.h"
19 #include "MLClass.h"
20 #include "KKMLLTypes.h"
21 using namespace KKMLL;
22 
23 #include "svm2.h"
24 using namespace SVM289_MFS;
25 
26 
27 
29 
30  ModelParam (),
31  svmParam ()
32 {
33 }
34 
35 
36 
37 
39  Kernel_Type _kernelType,
40  double _cost,
41  double _gamma
42  ):
43 
44  ModelParam (),
45  svmParam ()
46 {
47  svmParam.SvmType (_svm_type);
48  svmParam.KernalType (_kernelType);
49  svmParam.Cost (_cost);
50  svmParam.Gamma (_gamma);
51 }
52 
53 
54 
55 
56 
58  ModelParam (_param),
59  svmParam (_param.svmParam)
60 {
61 }
62 
63 
64 
66 {
67 }
68 
69 
70 ModelParamSvmBasePtr ModelParamSvmBase::Duplicate () const
71 {
72  return new ModelParamSvmBase (*this);
73 } /* Duplicate */
74 
75 
76 
77 void ModelParamSvmBase::Cost (double _cost)
78 {
79  svmParam.Cost (_cost);
80  ModelParam::Cost (_cost);
81 }
82 
83 
84 void ModelParamSvmBase::Gamma (double _gamma)
85 {
86  svmParam.Gamma (_gamma);
87  ModelParam::Gamma (_gamma);
88 }
89 
90 
91 double ModelParamSvmBase::Cost () const
92 {
93  return svmParam.Cost ();
94 }
95 
96 
97 double ModelParamSvmBase::Gamma () const
98 {
99  return svmParam.Gamma ();
100 }
101 
102 
103 
104 void ModelParamSvmBase::ParseCmdLineParameter (const KKStr& parameter,
105  const KKStr& value,
106  bool& parameterUsed,
107  RunLog& log
108  )
109 {
110  svmParam.ProcessSvmParameter (parameter, value, parameterUsed);
111 } /* ParseCmdLineParameter */
112 
113 
114 
115 
116 /**
117  @brief // Will get called after the entire parameter string has been processed.
118  */
119 void ModelParamSvmBase::ParseCmdLinePost ()
120 {
121  svmParam.Gamma (Gamma ());
122  svmParam.Cost (Cost ());
123 }
124 
125 
126 
127 
128 /**
129  * @brief Convert all parameters to a command line string.
130 */
132 {
133  KKStr cmdStr (300);
134  cmdStr = ModelParam::ToCmdLineStr () + " " + svmParam.ToCmdLineStr ();
135  return cmdStr;
136 } /* ToCmdLineStr */
137 
138 
139 
140 
141 
142 void ModelParamSvmBase::WriteXML (const KKStr& varName,
143  ostream& o
144  ) const
145 {
146  XmlTag startTag ("ModelParamOldSVM", XmlTag::TagTypes::tagStart);
147  if (!varName.Empty ())
148  startTag.AddAtribute ("VarName", varName);
149  startTag.WriteXML (o);
150  o << endl;
151 
153 
154 
155  svmParam.ToTabDelStr ().WriteXML ("SvmParam", o);
156 
157  XmlTag endTag ("ModelParamOldSVM", XmlTag::TagTypes::tagEnd);
158  endTag.WriteXML (o);
159  o << endl;
160 } /* WriteXML */
161 
162 
163 
164 
165 
167  XmlTagConstPtr tag,
168  VolConstBool& cancelFlag,
169  RunLog& log
170  )
171 {
172  XmlTokenPtr t = s.GetNextToken (cancelFlag, log);
173  while (t && (!cancelFlag))
174  {
176  if (t)
177  {
178  if (t->VarName ().EqualIgnoreCase ("SvmParam"))
179  {
180  svmParam.ParseTabDelStr (*(dynamic_cast<XmlElementKKStrPtr> (t)->Value ()));
181  }
182  }
183  delete t;
184  t = s.GetNextToken (cancelFlag, log);
185  }
186  delete t;
187  t = NULL;
188 } /* ReadXML */
189 
190 
191 XmlFactoryMacro(ModelParamSvmBase)
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
void Gamma(double _gamma)
Definition: svm2.h:110
bool EqualIgnoreCase(const char *s2) const
Definition: KKStr.cpp:1257
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
virtual void Cost(double _cost)
Definition: ModelParam.cpp:213
double Cost() const
Definition: svm2.h:114
virtual void ReadXML(XmlStream &s, XmlTagConstPtr tag, VolConstBool &cancelFlag, RunLog &log)
SVM_Type
Definition: svm2.h:72
virtual KKStr ToCmdLineStr() const
Creates a Command Line String that represents these parameters.
virtual double Gamma() const
KKStr operator+(const char *right) const
Definition: KKStr.cpp:3986
XmlToken * XmlTokenPtr
Definition: XmlStream.h:18
KKStr & operator=(KKStr &&src)
Definition: KKStr.cpp:1369
virtual double Cost() const
KKTHread * KKTHreadPtr
virtual void Gamma(double _gamma)
Definition: ModelParam.cpp:219
svm_parameter(const svm_parameter &_param)
Definition: svm2.cpp:198
void AddAtribute(const KKStr &attributeName, const KKStr &attributeValue)
Definition: XmlStream.cpp:602
ModelParamSvmBase(const ModelParamSvmBase &_param)
double Gamma() const
Definition: svm2.h:115
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
KKStr ToCmdLineStr() const
Definition: svm2.cpp:302
bool Empty() const
Definition: KKStr.h:241
XmlTag const * XmlTagConstPtr
Definition: KKStr.h:45
Manages the reading and writing of objects in a simple XML format. For a class to be supported by Xml...
Definition: XmlStream.h:46
void ProcessSvmParameter(const KKStr &cmd, const KKStr &value, bool &parmUsed)
Definition: svm2.cpp:330
static KKStr Concat(const std::vector< std::string > &values)
Concatenates the list of &#39;std::string&#39; strings.
Definition: KKStr.cpp:1082
KKStr ToTabDelStr() const
Definition: svm2.cpp:406
void WriteXML(const KKStr &varName, std::ostream &o) const
Definition: KKStr.cpp:4420
KKStr operator+(const KKStr &right) const
Definition: KKStr.cpp:3998
ModelParam(const ModelParam &_param)
Definition: ModelParam.cpp:61
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)
void Cost(double _cost)
Definition: svm2.h:109
virtual void Gamma(double _gamma)
virtual void Cost(double _cost)
virtual XmlTokenPtr GetNextToken(VolConstBool &cancelFlag, RunLog &log)
Definition: XmlStream.cpp:116
virtual void WriteXML(const KKStr &varName, ostream &o) const
void KernalType(Kernel_Type _kernalType)
Definition: svm2.h:111
Abstract Base class for Machine Learning parameters.
Definition: ModelParam.h:35
virtual const KKStr & VarName() const
Definition: XmlStream.h:269
#define XmlFactoryMacro(NameOfClass)
Definition: XmlStream.h:688
Kernel_Type
Definition: svm2.h:83
virtual ModelParamSvmBasePtr Duplicate() const
ModelParamSvmBase(SVM_Type _svm_type, Kernel_Type _kernelType, double _cost, double _gamma)
void SvmType(SVM_Type _svm_type)
Definition: svm2.h:112
volatile const bool VolConstBool
Definition: KKBaseTypes.h:163