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