CEBL  2.1
SimpleMax.cpp
Go to the documentation of this file.
1 #include "SimpleMax.hpp"
2 #include "cppR/cppR.hpp"
3 
4 using namespace cppR;
5 
6 namespace CEBL {
7 
8  SimpleMax::SimpleMax()
9  {
10  this->growth_rate = .001;
11  this->shrink_rate = 0.0;
12  this->plugin_name = "SimpleMax";
13  }
14 
15  SimpleMax::~SimpleMax()
16  {
17  }
18 
19  void SimpleMax::init(int num_classes)
20  {
21  this->num_classes = num_classes;
22  this->proportions = cppR::asStdVector(cppR::rep(0.0, num_classes));
23  }
24 
26  std::map<std::string, CEBL::Param> SimpleMax::getParamsList()
27  {
28  std::map<std::string, CEBL::Param> params;
29 
30  CEBL::Param g("Growth Rate",
31  "How much to grow classes on each update based on their probability.",
32  this->growth_rate);
33  g.setStep(0.0001);
34  g.setMax(0.5);
35  g.setMin(0.0);
36  params["g"] = g;
37 
38  CEBL::Param s("Shrink",
39  "How much to shrink all classes on each update.",
40  this->shrink_rate);
41  s.setStep(0.0001);
42  s.setMax(0.5);
43  s.setMin(0.0);
44  params["s"] = s;
45 
46  return params;
47  }
48 
50  void SimpleMax::setParamsList( std::map<std::string, CEBL::Param> &p)
51  {
52  this->growth_rate = p["g"].getDouble();
53  this->shrink_rate = p["s"].getDouble();
54  }
55 
56 
57  void SimpleMax::updateWithProbabilities(std::vector<double> probs)
58  {
59  if(probs.size() != unsigned(num_classes))
60  {
61  this->init(probs.size());
62  }
63 
64  using namespace cppR;
65  this->proportions.resize(num_classes);
66  for(int i=0;i<num_classes;i++)
67  {
68  double growth = (growth_rate + shrink_rate) * probs[i]
69  - shrink_rate;
70  this->proportions[i] += growth;
71  if(this->proportions[i] < 0)
72  this->proportions[i] = 0;
73 
74 
75  }
76  }
77 
78  std::vector<double> SimpleMax::decideClasses()
79  {
80  std::vector<double> ret = this->proportions;
81  ublas::vector<double> props = asUblasVector(this->proportions);
82  //check if one has reached 1.0
83  if(max(props) >= 1.0)
84  {
85  props = rep(0.0, num_classes);
86  }
87  this->proportions = asStdVector(props);
88 
89  return ret;
90  }
91 
92  //----------------------------------------------------------------------
93  //SAVING and LOADING
94 
95 
96 
98  map<string, SerializedObject> SimpleMax::save() const
99  {
100  map<string, SerializedObject> ret;
101  ret["growth_rate"] = serialize(growth_rate);
102  ret["shrink_rate"] = serialize(shrink_rate);
103  return ret;
104  }
105 
107  void SimpleMax::load(map<string, SerializedObject> objects)
108  {
109  deserialize(objects["growth_rate"],growth_rate);
110  deserialize(objects["shrink_rate"],shrink_rate);
111  }
112 }
113 
114 
115 
116 /*************************************************************/
117 //DYNAMIC LOADING
118 
120 {
121  return new CEBL::SimpleMax;
122 }
123 
124 extern "C" void ObjectDestroy(CEBL::Decision* p)
125 {
126  delete p;
127 }
128