Machine Learning Library
CSVCandidate.h
Go to the documentation of this file.
1 /*
2  COPYRIGHT (C) 2003 APPLIED NEUROINFORMATIC GROUP - UNIVERSITY OF BIELEFELD.
3 
4  ALL RIGHTS RESERVED.
5 
6  REDISTRIBUTION AND USE IN SOURCE AND BINARY FORM, WITH OR WITHOUT
7  MODIFICATION, REQUIRE THE PERMISSION OF THE COPYRIGHT HOLDERS.
8 
9  COMMERCIAL USE WITHOUT THE EXPLICIT PERMISSION OF THE COPYRIGHT HOLDERS
10  IS FORBIDDEN
11 
12  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS''
13  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
14  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
15  ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE FOR
16  ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
17  DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
18  SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
19  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
20  OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
21  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
22 */
23 
24 
25 
26 #ifndef CSVCANDIDATE_H
27 #define CSVCANDIDATE_H
28 
29 #include <assert.h>
30 #include <Macros.h>
31 #include <CArchiv.h>
32 #include "CDatasetItem.h"
33 
43 enum SVTYPE { ACTIVE = 0,
44  BOUNDED = 1,
46 };
47 
48 enum SORTING{ OUTERROR = 0,
49  LAGRANGE = 1,
50  CLASS = 2,
52  ID = 4
53 };
54 
55 
56 
57 
58 template<class Type>
59 class CSVCandidate : public CDatasetItem<Type>
60 {
61 public:
63 protected:
64  double dLagrange;
65  double dGradient;
67  Type tError;
68  double dOutput; /* used for one-class svm */
72 
73 
74 public:
75 
76 
77 
78 
79 
80 
81 
82 
83 
84  CSVCandidate() : CDatasetItem<Type>(0,1)
85  {
86  this->dLagrange = 0;
87  this->tError = 0;
88  this->dGradient = 0;
89  this->iLastChange = 0;
90  this->iIdxTestSet = 0;
91  this->iId = 0;
92  this->tSorting = LAGRANGE;
93  this->tType = NONSUPPORT;
94  this->tTargetValue= 0;
95  this->dOutput = 0;
96  };
97  CSVCandidate(int iInputDim) : CDatasetItem<Type>(iInputDim,1)
98  {
99  this->dLagrange = 0;
100  this->tError = 0;
101  this->dGradient = 0;
102  this->iLastChange = 0;
103  this->iIdxTestSet = 0;
104  this->iId = 0;
105  this->tSorting = LAGRANGE;
106  this->tType = NONSUPPORT;
107  this->tTargetValue= 0;
108  this->dOutput = 0;
109  };
110 
111  CSVCandidate(const CVector<Type>& tInput, Type tClass) : CDatasetItem<Type>(tInput,CDenseVector<Type>(1,&tClass))
112  {
113  this->dLagrange = 0;
114  this->tError = 0;
115  this->dGradient = 0;
116  this->iLastChange = 0;
117  this->iIdxTestSet = 0;
118  this->iId = 0;
119  this->tSorting = LAGRANGE;
120  this->tType = NONSUPPORT;
121  this->tTargetValue= tClass;
122  this->dOutput = 0;
123  };
124 
125 
126 
127  CSVCandidate(const CSVCandidate<Type>& rtItem) : CDatasetItem<Type>(rtItem)
128  {
129 
130  this->tSorting = rtItem.tSorting;
131 
132  this->dLagrange = rtItem.dLagrange;
133  this->tError = rtItem.tError;
134  this->tType = rtItem.tType;
135  this->iId = rtItem.iId;
136  this->dGradient = rtItem.dGradient;
137  this->iIdxTestSet = rtItem.iIdxTestSet;
138  this->dOutput = rtItem.dOutput;
139  this->iLastChange = rtItem.iLastChange;
140  this->tTargetValue= rtItem.tTargetValue;
141  this->tTargetValue= rtItem.tTargetValue;
142 
143  };
144 
145 
146  CSVCandidate(const CDatasetItem<Type>& rtItem) : CDatasetItem<Type>(rtItem)
147  {
148  this->tSorting = LAGRANGE;
149 
150  this->dLagrange = 0;
151  this->tError = 0;
152  this->iId = 0;
153  this->tType = NONSUPPORT;
154  this->iIdxTestSet = 0;
155  this->dGradient = 0;
156  this->iLastChange = 0;
157  this->dOutput = 0;
158  this->tTargetValue= rtItem.getOutputComponent(0);
159 
160  };
161 
163  {
164 
165  if(this == &rtItem) return *this;
166  this->tSorting = rtItem.tSorting;
167  this->dLagrange = rtItem.dLagrange;
168  this->tError = rtItem.tError;
169  this->tType = rtItem.tType;
170  this->iId = rtItem.iId;
171  this->dGradient = rtItem.dGradient;
172  this->dOutput = rtItem.dOutput;
173  this->iIdxTestSet = rtItem.iIdxTestSet;
174  this->iLastChange = rtItem.iLastChange;
175  this->tTargetValue= rtItem.tTargetValue;
176  *((CDatasetItem<Type>*) this)=rtItem;
177 
178  return *this;
179  };
180 
182 
183 
184  bool operator== (const CSVCandidate<Type>& right) const {
185  if(tSorting == OUTERROR) {
186  if(this->tError == right.tError)
187  return true;
188  else
189  return false;
190  } else if(tSorting == LAGRANGE) {
191  if(this->dLagrange == right.dLagrange)
192  return 1;
193  else
194  return 0;
195  } else if(tSorting == CLASS) {
196  if(tTargetValue == right.tTargetValue)
197  return 1;
198  else
199  return 0;
200  } else if(tSorting == ID) {
201  if(this->iId == right.iId)
202  return 1;
203  else
204  return 0;
205  }
206  return 0;
207  };
208 
209  virtual string className() const { return string("CSVCandidate");};
210  virtual bool isA(const char* acClass) const
211  {
212  if(string(acClass) == this->className())
213  return true;
214  else return CDatasetItem<Type>::isA(acClass);
215  };
216 
217  bool operator!=(const CSVCandidate<Type>& right) const {
218  if(tSorting == CLASS) {
219  if(this->tTargetValue != right.tTargetValue)
220  return 1;
221  else
222  return 0;
223  } else if(tSorting == LAGRANGE) {
224  if(this->dLagrange !=right.dLagrange)
225  return 1;
226  else
227  return 0;
228  } else if(tSorting == OUTERROR) {
229  if(this->tError != right.tError)
230  return 1;
231  else
232  return 0;
233  } else if(tSorting == ID) {
234  if(this->iId != right.iId)
235  return 1;
236  else
237  return 0;
238  }
239  return 0;
240  }
241 
242  bool operator>(const CSVCandidate<Type>& right) const {
243  if(tSorting == CLASS) {
244  if(this->tTargetValue < right.tTargetValue)
245  return 1;
246  else
247  return 0;
248  } else if(tSorting == LAGRANGE) {
249  if(this->dLagrange < right.dLagrange)
250  return 1;
251  else
252  return 0;
253  } else if(tSorting == OUTERROR) {
254  if(this->tError < right.tError)
255  return 1;
256  else
257  return 0;
258  } else if(tSorting == ID) {
259  if((int)this->iId < (int)right.iId)
260  return 0;
261  else
262  return 1;
263  }
264  return 0;
265  }
266 
267  bool operator<(const CSVCandidate<Type>& right) const {
268  if(tSorting == CLASS) {
269  if(this->tTargetValue > right.tTargetValue)
270  return 1;
271  else
272  return 0;
273  } else if(tSorting == LAGRANGE) {
274  if(this->dLagrange > right.dLagrange)
275  return 1;
276  else
277  return 0;
278  } else if(tSorting == OUTERROR) {
279  if(this->tError > right.tError)
280  return 1;
281  else
282  return 0;
283  } else if(tSorting == ID) {
284  if((int)this->iId > (int)right.iId)
285  return 0;
286  else
287  return 1;
288  }
289  return 0;
290  }
291 
292  inline double& lagrange() {
293  return dLagrange;
294  };
295 
296  inline Type& error() {
297  return tError;
298  };
299 
300  inline int testSetIndex() {
301  return iIdxTestSet;
302  };
303 
304  inline SVTYPE& type() {
305  return tType;
306  };
307 
308  inline Type& targetValue() {
309  return tTargetValue;
310  };
311 
312  inline int& lastChange() {
313  return iLastChange;
314  };
315 
316  inline double& gradient() {
317  return dGradient;
318  };
319 
320 
321 
322  inline double& output() {
323  return dOutput;
324  };
325 
326  inline int& id() {
327  return this->iId;
328  };
329 
330 
331 
332 
333  inline const SVTYPE& getType() const {
334  return tType;
335  };
336 
337  inline const Type& getTargetValue() const {
338  return tTargetValue;
339  };
340 
341  inline const double& getLagrange() const {
342  return dLagrange;
343  };
344 
345  inline const int& getLastChange() const {
346  return iLastChange;
347  };
348 
349  inline const double& getGradient() const {
350  return dGradient;
351  };
352 
353 
354 
355  inline const double& getOutput() const {
356  return dOutput;
357  };
358 
359  inline const int& getId() const {
360  return this->iId;
361  };
362 
363 
364  bool serialize(fstream& stream, IO_MODE mode) {
365  CDatasetItem<Type>::serialize(stream, mode);
366  if(mode == WRITE) {
367  int iType=this->dataType();
368  stream.write((char*) &iType, sizeof(int));
369  stream.write((char*) &dLagrange, sizeof(double));
370  stream.write((char*) &dGradient, sizeof(double));
371  stream.write((char*) &tTargetValue, sizeof(Type));
372  stream.write((char*) &tError, sizeof(Type));
373  stream.write((char*) &dOutput, sizeof(double));
374  stream.write((char*) &iIdxTestSet, sizeof(int));
375  stream.write((char*) &iLastChange, sizeof(int));
376  stream.write((char*) &tType, sizeof(int));
377 
378  return true;
379  } else {
380  DATATYPE tType;
381  stream.read((char*) &tType, sizeof(int));
382  stream.read((char*) &dLagrange, sizeof(double));
383  stream.read((char*) &dGradient, sizeof(double));
384  readValueOfType(tTargetValue, stream, tType);
385  readValueOfType(tError, stream, tType);
386  stream.read((char*) &dOutput, sizeof(double));
387  stream.read((char*) &iIdxTestSet, sizeof(int));
388  stream.read((char*) &iLastChange, sizeof(int));
389  stream.read((char*) &tType, sizeof(int));
390 
391  return true;
392  }
393  return false;
394  }
395 
396 
397 
398 
399  bool serialize2(CArchiv& tA) {
401  return false;
402 
403  if( tA.isReading()) {
404  int iTmp;
405  tA >> dLagrange;
406  tA >> dGradient;
407  tA >> tTargetValue;
408  tA >> tError;
409  tA >> dOutput;
410  tA >> iIdxTestSet;
411  tA >> iLastChange;
412  tA >> iTmp; tType = (SVTYPE) iTmp;
413  tA >> iTmp; tSorting = (SORTING) iTmp;
414  return true;
415  } else {
416  tA << dLagrange; tA.flush();
417  tA << dGradient; tA.flush();
418  tA << tTargetValue; tA.flush();
419  tA << tError; tA.flush();
420  tA << dOutput; tA.flush();
421  tA << iIdxTestSet; tA.flush();
422  tA << iLastChange; tA.flush();
423  tA << (int)tType; tA.flush();
424  tA << (int)tSorting; tA.flush();
425  return true;
426  }
427  return false;
428  }
429 };
430 
431 
432 
433 #endif
virtual bool isReading() const
Definition: CArchiv.h:25
CSVCandidate(const CSVCandidate< Type > &rtItem)
Definition: CSVCandidate.h:127
Type & targetValue()
Definition: CSVCandidate.h:308
SVTYPE & type()
Definition: CSVCandidate.h:304
const SVTYPE & getType() const
Definition: CSVCandidate.h:333
CSVCandidate(int iInputDim)
Definition: CSVCandidate.h:97
DATATYPE
Definition: CObject.h:39
Definition: CObject.h:38
bool operator==(const CSVCandidate< Type > &right) const
Definition: CSVCandidate.h:184
IO_MODE
Definition: CObject.h:38
virtual bool isA(const char *acClass) const
Check if the object is an instance of the class with given name.
Definition: CSVCandidate.h:210
Definition: CSVCandidate.h:52
CSVCandidate(const CDatasetItem< Type > &rtItem)
Definition: CSVCandidate.h:146
CSVCandidate()
Definition: CSVCandidate.h:84
double dLagrange
Definition: CSVCandidate.h:64
Type tTargetValue
Definition: CSVCandidate.h:66
Definition: CSVCandidate.h:48
Definition: CSVCandidate.h:44
double & output()
Definition: CSVCandidate.h:322
SVTYPE
Definition: CSVCandidate.h:43
Type tError
Definition: CSVCandidate.h:67
int iLastChange
Definition: CSVCandidate.h:70
const int & getLastChange() const
Definition: CSVCandidate.h:345
const double & getOutput() const
Definition: CSVCandidate.h:355
const double & getGradient() const
Definition: CSVCandidate.h:349
const Type & getTargetValue() const
Definition: CSVCandidate.h:337
void readValueOfType(Type &rtValue, fstream &stream, DATATYPE tType)
Definition: Macros.h:60
double & gradient()
Definition: CSVCandidate.h:316
Definition: CSVCandidate.h:49
bool serialize(fstream &stream, IO_MODE mode)
Definition: CDatasetItem.h:308
int iIdxTestSet
Definition: CSVCandidate.h:69
bool serialize(fstream &stream, IO_MODE mode)
Definition: CSVCandidate.h:364
bool operator!=(const CSVCandidate< Type > &right) const
Definition: CSVCandidate.h:217
Definition: CSVCandidate.h:45
SVTYPE tType
Definition: CSVCandidate.h:71
Single item of a dataset consisting of a pair of input and out vectors.
Definition: CDatasetItem.h:43
int testSetIndex()
Definition: CSVCandidate.h:300
virtual string className() const
Returns the class name.
Definition: CSVCandidate.h:209
double dGradient
Definition: CSVCandidate.h:65
Definition: CSVCandidate.h:51
const double & getLagrange() const
Definition: CSVCandidate.h:341
~CSVCandidate()
Definition: CSVCandidate.h:181
Definition: CSVCandidate.h:59
Base class of object serialization.
Definition: CArchiv.h:19
double dOutput
Definition: CSVCandidate.h:68
const int & getId() const
Definition: CSVCandidate.h:359
Type getOutputComponent(int iIndex) const
Definition: CDatasetItem.h:233
Template object for vectors of single and double precision and integer.
Definition: CDenseVector.h:37
int iId
Definition: CDatasetItem.h:47
CSVCandidate< Type > & operator=(const CSVCandidate< Type > &rtItem)
Definition: CSVCandidate.h:162
templatized vector for numerical applications
Definition: CMatrix.h:39
int & id()
Definition: CSVCandidate.h:326
Definition: CSVCandidate.h:50
double & lagrange()
Definition: CSVCandidate.h:292
int & lastChange()
Definition: CSVCandidate.h:312
SORTING
Definition: CSVCandidate.h:48
DATATYPE dataType() const
Returns the template data type.
virtual bool isA(const char *acClass) const
Check if the object is an instance of the class with given name.
Definition: CObject.h:93
CSVCandidate(const CVector< Type > &tInput, Type tClass)
Definition: CSVCandidate.h:111
SORTING tSorting
Definition: CSVCandidate.h:62
Type & error()
Definition: CSVCandidate.h:296
virtual void flush()
Definition: CArchiv.h:26
bool operator>(const CSVCandidate< Type > &right) const
Definition: CSVCandidate.h:242
Definition: CSVCandidate.h:43
bool serialize2(CArchiv &tA)
Definition: CSVCandidate.h:399