RapidLib  v2.2.0
A simple library for interactive machine learning
baseModel.h
Go to the documentation of this file.
1 
10 #ifndef BASEMODEL_H
11 #define BASEMODEL_H
12 
13 #include <vector>
14 #include "trainingExample.h"
15 
16 #ifndef EMSCRIPTEN
17 #include "../dependencies/json/json.h"
18 #endif
19 
20 #ifdef __ANDROID__
21 #include <ostream>
22 namespace std
23 { // Workaround for android
24  template <typename T>
25  std::string to_string(T Value)
26  {
27  std::ostringstream TempStream;
28  TempStream << Value;
29  return TempStream.str();
30  }
31 
32  inline long double strtold(const char* str, char** str_end)
33  {
34  return strtod(str, str_end);
35  }
36 }
37 #endif
38 
40 template<typename T>
41 class baseModel
42 {
43 public:
44  virtual ~baseModel() {};
45  virtual T run(const std::vector<T>& inputVector) = 0; //TODO: I'd like this to be const
46  virtual void train(const std::vector<trainingExampleTemplate<T> >& trainingSet) = 0;
47  virtual void train(const std::vector<trainingExampleTemplate<T> >& trainingSet, const std::size_t whichOutput) = 0;
48  virtual void reset() = 0;;
49  virtual size_t getNumInputs() const = 0;
50  virtual std::vector<size_t> getWhichInputs() const = 0;
51 
52 #ifndef EMSCRIPTEN
53  virtual void getJSONDescription(Json::Value& currentModel) = 0;
54 
55 protected:
56 
57  template<typename TT, class Dummy=int>
58  Json::Value vector2json(TT vec)
59  {
60  Json::Value toReturn;
61  for (auto value : vec)
62  {
63  toReturn.append( (Json::Value)value );
64  }
65  return toReturn;
66  }
67 
68  //FIXME: This is a temporary hack because Json::Value doesn't know what to do with unsinged longs, and XCode cares
69  template<class Dummy=int>
70  Json::Value vector2json(std::vector<unsigned long> vec)
71  {
72  Json::Value toReturn;
73  for (auto value : vec)
74  {
75  toReturn.append( (double)value ); //I chose double here because that's close to what JS uses
76  }
77  return toReturn;
78  }
79 #endif
80 
81 };
82 
83 #endif
Definition: baseModel.h:42
virtual std::vector< size_t > getWhichInputs() const =0
virtual T run(const std::vector< T > &inputVector)=0
virtual void getJSONDescription(Json::Value &currentModel)=0
virtual void reset()=0
virtual size_t getNumInputs() const =0
virtual void train(const std::vector< trainingExampleTemplate< T > > &trainingSet, const std::size_t whichOutput)=0
Json::Value vector2json(TT vec)
Definition: baseModel.h:58
virtual ~baseModel()
Definition: baseModel.h:44
Json::Value vector2json(std::vector< unsigned long > vec)
Definition: baseModel.h:70
virtual void train(const std::vector< trainingExampleTemplate< T > > &trainingSet)=0
Definition: trainingExample.h:19