KSquare Utilities
ModelParamUsfCasCor.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 
18 #include "FileDesc.h"
19 #include "MLClass.h"
20 #include "KKMLLTypes.h"
21 using namespace KKMLL;
22 
23 
25  ModelParam (),
26  in_limit (500),
27  out_limit (500),
28  number_of_rounds (-1),
29  number_of_trials (1),
30  random_seed (0),
31  useCache (false)
32 {
33 }
34 
35 
36 
37 
39  int _out_limit,
40  int _number_of_rounds,
41  int _number_of_trials,
42  kkint64 _random_seed,
43  bool _useCache
44  ):
45  ModelParam (),
46  in_limit (_in_limit),
47  out_limit (_out_limit),
48  number_of_rounds (_number_of_rounds),
49  number_of_trials (_number_of_trials),
50  random_seed (_random_seed),
51  useCache (_useCache)
52 {}
53 
54 
55 
56 
58 {
59 }
60 
61 
63 {
64  return new ModelParamUsfCasCor (*this);
65 } /* Duplicate */
66 
67 
68 
69 void ModelParamUsfCasCor::ParseCmdLineParameter (const KKStr& parameter,
70  const KKStr& value,
71  bool& parameterUsed,
72  RunLog& log
73  )
74 {
75  parameterUsed = true;
76  if (parameter.EqualIgnoreCase ("-InLimit") ||
77  parameter.EqualIgnoreCase ("-IL") ||
78  parameter.EqualIgnoreCase ("-I")
79  )
80  in_limit = value.ToInt ();
81 
82  else if (parameter.EqualIgnoreCase ("-OutLimit") ||
83  parameter.EqualIgnoreCase ("-OL") ||
84  parameter.EqualIgnoreCase ("-O")
85  )
86  out_limit = value.ToInt ();
87 
88  else if (parameter.EqualIgnoreCase ("-NumberOfRounds") ||
89  parameter.EqualIgnoreCase ("-NOR") ||
90  parameter.EqualIgnoreCase ("-Rounds") ||
91  parameter.EqualIgnoreCase ("-R")
92  )
93  number_of_rounds = value.ToInt ();
94 
95  else if (parameter.EqualIgnoreCase ("-NumberOfTrials") ||
96  parameter.EqualIgnoreCase ("-NOT") ||
97  parameter.EqualIgnoreCase ("-T")
98  )
99  number_of_trials = value.ToInt ();
100 
101 
102  else if (parameter.EqualIgnoreCase ("-RandomSeed") ||
103  parameter.EqualIgnoreCase ("-RS") ||
104  parameter.EqualIgnoreCase ("-S")
105  )
106  random_seed = value.ToInt ();
107 
108  else if (parameter.EqualIgnoreCase ("-UseCache") ||
109  parameter.EqualIgnoreCase ("-UC") ||
110  parameter.EqualIgnoreCase ("-Cache")
111  )
112  {
113  if (value.Empty ())
114  useCache = true;
115  else
116  useCache = value.ToBool ();
117  }
118 
119  else
120  parameterUsed = false;
121 } /* ParseCmdLineParameter */
122 
123 
124 
125 
126 /**
127  *@brief // Will get called after the entire parameter string has been processed.
128  */
129 void ModelParamUsfCasCor::ParseCmdLinePost ()
130 {
131 }
132 
133 
134 
135 
136 /**
137  * @brief Convert all parameters to a command line string.
138 */
140 {
141  KKStr cmdStr (300);
142 
143  cmdStr << "-InLimit " << in_limit << " "
144  << "-OutLimit " << out_limit;
145 
146  if (number_of_rounds > 0)
147  cmdStr << " -R " << number_of_rounds;
148 
149  cmdStr << " -T " << number_of_trials;
150 
151  if (random_seed > 0)
152  cmdStr << " -S " << random_seed;
153 
154  cmdStr << " -UseCache " << (useCache ? "Yes" : "No");
155 
156  return cmdStr;
157 } /* ToCmdLineStr */
158 
159 
160 
161 
162 void ModelParamUsfCasCor::WriteXML (const KKStr& varName,
163  ostream& o
164  ) const
165 {
166  XmlTag startTag ("ModelParamUsfCasCor", XmlTag::TagTypes::tagStart);
167  if (!varName.Empty ())
168  startTag.AddAtribute ("VarName", varName);
169  startTag.WriteXML (o);
170  o << endl;
171 
173 
174  XmlElementInt32::WriteXML (in_limit, "in_limit", o);
175  XmlElementInt32::WriteXML (number_of_rounds, "number_of_rounds", o);
176  XmlElementInt32::WriteXML (number_of_trials, "number_of_trials", o);
177  XmlElementInt64::WriteXML (random_seed, "random_seed", o);
178  XmlElementBool::WriteXML (useCache, "useCache", o);
179 
180  XmlTag endTag ("ModelParamUsfCasCor", XmlTag::TagTypes::tagEnd);
181  endTag.WriteXML (o);
182  o << endl;
183 } /* WriteXML */
184 
185 
186 
187 
188 
190  XmlTagConstPtr tag,
191  VolConstBool& cancelFlag,
192  RunLog& log
193  )
194 {
195  XmlTokenPtr t = s.GetNextToken (cancelFlag, log);
196  while (t && (!cancelFlag))
197  {
199  if ((t != NULL) && (t->TokenType () == XmlToken::TokenTypes::tokElement))
200  {
201  XmlElementPtr e = dynamic_cast<XmlElementPtr> (t);
202  const KKStr& varName = e->VarName ();
203 
204  if (varName.EqualIgnoreCase ("in_limit"))
205  in_limit = dynamic_cast<XmlElementInt32Ptr> (e)->Value ();
206 
207  else if (varName.EqualIgnoreCase ("number_of_rounds"))
208  number_of_rounds = dynamic_cast<XmlElementInt32Ptr> (e)->Value ();
209 
210  else if (varName.EqualIgnoreCase ("number_of_trials"))
211  number_of_trials = dynamic_cast<XmlElementInt32Ptr> (e)->Value ();
212 
213  else if (varName.EqualIgnoreCase ("random_seed"))
214  random_seed = dynamic_cast<XmlElementInt64Ptr> (e)->Value ();
215 
216  else if (varName.EqualIgnoreCase ("useCache"))
217  useCache = dynamic_cast<XmlElementBoolPtr> (e)->Value ();
218 
219  else
220  {
221  log.Level (-1) << endl
222  << "ModelParamUsfCasCor::ReadXM ***ERROR*** Unexpected Element: " << e->NameTag ()->ToString () << endl
223  << endl;
224  }
225  }
226  delete t;
227  t = s.GetNextToken (cancelFlag, log);
228  }
229  delete t;
230  t = NULL;
231 
232  bool validFormat = false;
233 } /* ReadXML */
234 
235 
236 XmlFactoryMacro(ModelParamUsfCasCor)
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
bool EqualIgnoreCase(const char *s2) const
Definition: KKStr.cpp:1257
void WriteXMLFields(std::ostream &o) const
Definition: ModelParam.cpp:464
bool Value() const
Definition: XmlStream.cpp:1028
XmlElementBool * XmlElementBoolPtr
Definition: XmlStream.h:524
kkint32 ToInt() const
Definition: KKStr.cpp:3565
static void WriteXML(const bool b, const KKStr &varName, std::ostream &o)
Definition: XmlStream.cpp:1035
XmlToken * XmlTokenPtr
Definition: XmlStream.h:18
__int64 kkint64
Definition: KKBaseTypes.h:90
KKTHread * KKTHreadPtr
XmlElement * XmlElementPtr
Definition: XmlStream.h:21
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
This class encapsulates are the information necessary to build a UsfCasCor class. ...
static KKStr Concat(const std::vector< std::string > &values)
Concatenates the list of &#39;std::string&#39; strings.
Definition: KKStr.cpp:1082
ModelParamUsfCasCor(int _in_limit, int _out_limit, int _number_of_rounds, int _number_of_trials, kkint64 _random_seed, bool _useCache)
virtual const KKStr & VarName() const
Definition: XmlStream.cpp:794
virtual ModelParamUsfCasCorPtr Duplicate() const
virtual void ReadXML(XmlStream &s, XmlTagConstPtr tag, VolConstBool &cancelFlag, RunLog &log)
virtual KKStr ToCmdLineStr() const
Creates a Command Line String that represents these parameters.
ModelParamUsfCasCor * ModelParamUsfCasCorPtr
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 WriteXML(const KKStr &varName, ostream &o) const
virtual TokenTypes TokenType()=0
virtual XmlTokenPtr GetNextToken(VolConstBool &cancelFlag, RunLog &log)
Definition: XmlStream.cpp:116
bool ToBool() const
Returns the bool equivalent of the string, ex &#39;Yes&#39; = true, &#39;No&#39; = false, &#39;True&#39; = true...
Definition: KKStr.cpp:3523
Abstract Base class for Machine Learning parameters.
Definition: ModelParam.h:35
#define XmlFactoryMacro(NameOfClass)
Definition: XmlStream.h:688
volatile const bool VolConstBool
Definition: KKBaseTypes.h:163