SHOGUN  4.1.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
MinimizerContext.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) The Shogun Machine Learning Toolbox
3  * Written (w) 2015 Wu Lin
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright notice, this
10  * list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright notice,
12  * this list of conditions and the following disclaimer in the documentation
13  * and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
19  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
22  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  *
26  * The views and conclusions contained in the software and documentation are those
27  * of the authors and should not be interpreted as representing official policies,
28  * either expressed or implied, of the Shogun Development Team.
29  *
30  */
31 
32 #ifndef MINIMIZERCONTEXT_H
33 #define MINIMIZERCONTEXT_H
34 #include <shogun/lib/config.h>
35 #include <shogun/lib/SGVector.h>
36 #include <shogun/base/Parameter.h>
37 #include <shogun/base/SGObject.h>
38 #include <shogun/lib/StringMap.h>
39 
40 namespace shogun
41 {
46 {
47 public:
48  /* Constructor */
50  :CSGObject()
51  {
52  init();
53  }
54 
55  /* Destructor */
57  {
61  }
62 
67  virtual const char* get_name() const {return "MinimizerContext";}
68 
69 
74  virtual void save_data(const std::string& key, SGVector<float64_t> value)
75  {
76  REQUIRE(!m_sgvector_float64_map->contains(key),
77  "Failed to save data due to duplicate key:%s\n", key.c_str());
78  m_sgvector_float64_map->add(key, value);
79  REQUIRE(m_sgvector_float64_map->contains(key),
80  "Failed to save data for key:%s\n", key.c_str());
81  }
82 
87  virtual void save_data(const std::string& key, float64_t value)
88  {
90  "Failed to save data due to duplicate key:%s\n", key.c_str());
91  m_float64_map->add(key, value);
93  "Failed to save data for key:%s\n", key.c_str());
94  }
95 
100  virtual void save_data(const std::string& key, int32_t value)
101  {
103  "Failed to save data due to duplicate key:%s\n", key.c_str());
104  m_int32_map->add(key, value);
106  "Failed to save data for key:%s\n", key.c_str());
107  }
108 
113  virtual SGVector<float64_t> get_data_sgvector_float64(const std::string& key)
114  {
115  REQUIRE(m_sgvector_float64_map->contains(key),
116  "Failed to load data because key:%s does not exist\n", key.c_str());
117  return m_sgvector_float64_map->get_element(key);
118  }
119 
124  virtual float64_t get_data_float64(const std::string& key)
125  {
127  "Failed to load data because key:%s does not exist\n", key.c_str());
128  return m_float64_map->get_element(key);
129  }
130 
135  virtual int32_t get_data_int32(const std::string& key)
136  {
138  "Failed to load data because key:%s does not exist\n", key.c_str());
139  return m_int32_map->get_element(key);
140  }
141 
142 protected:
149 private:
151  void init()
152  {
153  m_float64_map=new CStringMap<float64_t>();
154  m_int32_map=new CStringMap<int32_t>();
155  m_sgvector_float64_map=new CStringMap< SGVector<float64_t> >();
156  SG_REF(m_sgvector_float64_map);
157  SG_REF(m_int32_map);
158  SG_REF(m_float64_map);
159  //TODO: uncomment the following lines when CMap (including CStringMap) supports load_serializable() and save_serializable()
160  //SG_ADD((CSGObject **)&m_int32_map, "int32_map", "int32_map", MS_NOT_AVAILABLE);
161  //SG_ADD((CSGObject **)&m_float64_map, "float64_map", "float64_map", MS_NOT_AVAILABLE);
162  //SG_ADD((CSGObject **)&m_sgvector_float64_map, "sgvector_float64_map", "sgvector_float64_map", MS_NOT_AVAILABLE);
163  }
164 
165 };
166 }
167 
168 #endif
virtual void save_data(const std::string &key, SGVector< float64_t > value)
virtual void save_data(const std::string &key, int32_t value)
CStringMap< SGVector< float64_t > > * m_sgvector_float64_map
The class is used to serialize and deserialize variables for the optimization framework.
T get_element(const K &key)
Definition: Map.h:171
#define REQUIRE(x,...)
Definition: SGIO.h:206
CStringMap< int32_t > * m_int32_map
virtual SGVector< float64_t > get_data_sgvector_float64(const std::string &key)
#define SG_REF(x)
Definition: SGObject.h:51
bool contains(const K &key)
Definition: Map.h:122
int32_t add(const K &key, const T &data)
Definition: Map.h:101
Class SGObject is the base class of all shogun objects.
Definition: SGObject.h:112
double float64_t
Definition: common.h:50
virtual void save_data(const std::string &key, float64_t value)
virtual float64_t get_data_float64(const std::string &key)
#define SG_UNREF(x)
Definition: SGObject.h:52
all of classes and functions are contained in the shogun namespace
Definition: class_list.h:18
virtual const char * get_name() const
CStringMap< float64_t > * m_float64_map
virtual int32_t get_data_int32(const std::string &key)

SHOGUN Machine Learning Toolbox - Documentation