KSquare Utilities
ModelParamKnn.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 
11 #include "GlobalGoalKeeper.h"
12 #include "KKBaseTypes.h"
13 using namespace KKB;
14 
15 
16 #include "ModelParamKnn.h"
17 #include "FileDesc.h"
18 #include "MLClass.h"
19 #include "KKMLLTypes.h"
20 #include "OSservices.h"
21 #include "RunLog.h"
22 using namespace KKMLL;
23 
24 
25 
26 
27 
29  ModelParam (),
30  k(1)
31 {
32 }
33 
34 
35 
37  ModelParam (_param),
38  k (_param.k)
39 {
40 }
41 
42 
43 
45 {
46 }
47 
48 
49 
50 
51 ModelParamKnnPtr ModelParamKnn::Duplicate () const
52 {
53  return new ModelParamKnn (*this);
54 }
55 
56 
57 
58 
60 {
61  return ModelParam::ToCmdLineStr () + " -K " + StrFormatInt (k, "###0");
62 }
63 
64 
65 
66 
67 
68 void ModelParamKnn::ParseCmdLineParameter (const KKStr& parameter,
69  const KKStr& value,
70  bool& parameterUsed,
71  RunLog& log
72  )
73 {
74  parameterUsed = true;
75 
76  if (parameter.EqualIgnoreCase ("-K"))
77  {
78  k = value.ToInt ();
79  if ((k < 1) || (k > 1000))
80  {
81  log.Level (-1) << "ModelParamKnn::ParseCmdLineParameter ***ERROR*** Invalid -K parameter[" << value << "]" << endl;
82  validParam = false;
83  }
84  }
85  else
86  {
87  parameterUsed = false;
88  }
89 } /* ParseCmdLineParameter*/
90 
91 
92 
93 
94 
95 void ModelParamKnn::WriteXML (const KKStr& varName,
96  ostream& o
97  ) const
98 {
99  XmlTag startTag ("ModelParamDual", XmlTag::TagTypes::tagStart);
100  if (!varName.Empty ())
101  startTag.AddAtribute ("VarName", varName);
102  startTag.WriteXML (o);
103  o << endl;
104 
106 
107 
108 
109 
110  XmlTag endTag ("ModelParamDual", XmlTag::TagTypes::tagEnd);
111  endTag.WriteXML (o);
112  o << endl;
113 } /* WriteXML */
114 
115 
116 
117 
118 
120  XmlTagConstPtr tag,
121  VolConstBool& cancelFlag,
122  RunLog& log
123  )
124 {
125  XmlTokenPtr t = s.GetNextToken (cancelFlag, log);
126  while (t && (!cancelFlag))
127  {
129  if (t)
130  {
131  const KKStr& varName = t->VarName ();
132 
133  if (varName.EqualIgnoreCase ("k"))
134  k = dynamic_cast<XmlElementInt32Ptr> (t)->Value ();
135 
136  else if (varName.EqualIgnoreCase ("fileName"))
137  fileName = *(dynamic_cast<XmlElementKKStrPtr> (t)->Value ());
138  }
139 
140  delete t;
141  t = s.GetNextToken (cancelFlag, log);
142  }
143  delete t;
144  t = NULL;
145 } /* ReadXML */
146 
147 XmlFactoryMacro(ModelParamKnn)
XmlTag(const KKStr &_name, TagTypes _tagType)
Definition: XmlStream.cpp:586
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 ModelParamKnnPtr Duplicate() const
kkint32 ToInt() const
Definition: KKStr.cpp:3565
KKStr operator+(const char *right) const
Definition: KKStr.cpp:3986
XmlToken * XmlTokenPtr
Definition: XmlStream.h:18
virtual KKStr ToCmdLineStr(RunLog &log) const
Creates a Command Line String that represents these parameters.
KKTHread * KKTHreadPtr
void AddAtribute(const KKStr &attributeName, const KKStr &attributeValue)
Definition: XmlStream.cpp:602
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
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
static KKStr Concat(const std::vector< std::string > &values)
Concatenates the list of &#39;std::string&#39; strings.
Definition: KKStr.cpp:1082
KKStr StrFormatInt(kkint32 val, const char *mask)
Definition: KKStr.cpp:5004
KKStr operator+(const KKStr &right) const
Definition: KKStr.cpp:3998
ModelParam(const ModelParam &_param)
Definition: ModelParam.cpp:61
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)
ModelParamKnn(const ModelParamKnn &_param)
virtual void ReadXML(XmlStream &s, XmlTagConstPtr tag, VolConstBool &cancelFlag, RunLog &log)
virtual XmlTokenPtr GetNextToken(VolConstBool &cancelFlag, RunLog &log)
Definition: XmlStream.cpp:116
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
volatile const bool VolConstBool
Definition: KKBaseTypes.h:163