aboutsummaryrefslogtreecommitdiffstats
path: root/core/freeze_param.hpp
blob: 6a4f3093cb646fc564ed1ad6b7c586556fe18130 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
/**
   \file freeze_param.hpp
*/

#ifndef FREEZE_PARAM_HPP
#define FREEZE_PARAM_HPP
#define OPT_HEADER
#include "fitter.hpp"
#include <vector>
#include <set>

namespace opt_utilities
{
  /**
     freeze a set of parameter in model fitting
     \tparam Ty the return type of a model
     \tparam Tx the type of the self-var
     \tparam Tp the type of the model parameter
     \tparam Tstr the type of string used
   */
  template <typename Ty,typename Tx,typename Tp,typename Tstr=std::string>
  class freeze_param
    :public param_modifier<Ty,Tx,Tp,Tstr>
  {
  private:
    std::set<Tstr> param_names;
    std::vector<size_t> param_num;
    size_t num_free;
    
  public:
    /**
       the default construct function
     */
    freeze_param()
    {
    }
    
    /**
       construct function
       \param name the name of the parameter to be frozen
     */
    freeze_param(const Tstr& name)
    {
      param_names.insert(name);
    }
    
  private:
    freeze_param<Ty,Tx,Tp,Tstr>* do_clone()const
    {
      return new freeze_param<Ty,Tx,Tp,Tstr>(*this);
    }
    
    
    /**
       update the parameter information
       should be called by the library, rather than the user
     */
    void update()
    {
      param_num.clear();
      for(typename std::set<Tstr>::const_iterator  i=param_names.begin();
	  i!=param_names.end();++i)
	{
	  try
	    {
	      param_num.push_back(this->get_model().get_param_order(*i));
	    }
	  catch(opt_exception& e)
	    {
	      param_names.erase(*i);
	      throw;
	    }
	  
	}
    }

    size_t do_get_num_free_params()const
    {
      return this->get_model().get_num_params()-param_num.size();
    }

    bool is_frozen(size_t i)const
    {
      if(find(param_num.begin(),param_num.end(),i)==param_num.end())
	{
	  return false;
	}
      return true;
    }


    Tp do_reform(const Tp& p)const
    {
      size_t nparams=(this->get_model().get_num_params());
      Tp reformed_p(nparams);
      size_t i=0,j=0;
      for(i=0;i<nparams;++i)
	{
	  if(this->is_frozen(i))
	    {
	      const param_info<Tp,Tstr>& pinf=this->get_model().get_param_info(i);
	      //std::cout<<"frozen:"<<pinf.get_name()
	      //	       <<i<<"\t"<<j
	      //       <<std::endl;
	      //opt_eq(get_element(reformed_p,i),pinf.get_value());
	      set_element(reformed_p,i,pinf.get_value());
	    }
	  else
	    {
	      //opt_eq(get_element(reformed_p,i),get_element(p,j));
	      set_element(reformed_p,i,get_element(p,j));
	      j++;
	    }
	}
      /*
      for(int i=0;i<reformed_p.size();++i)
      {
      std::cout<<get_element(reformed_p,i)<<",";
      }
      */
      //std::cout<<"\n";
      return reformed_p;
      // return p;
    }

    Tp do_deform(const Tp& p)const
    {
      Tp deformed_p(do_get_num_free_params());
      size_t i(0),j(0);
      assert(get_size(p)==this->get_model().get_num_params());
      for(;i<get_size(p);++i)
	{
	  //std::cout<<is_frozen(j)<<"\n";
	  if(!this->is_frozen(i))
	    {
	      //opt_eq(get_element(deformed_p,j),get_element(p,i));
	      set_element(deformed_p,j,get_element(p,i));
	      j++;
	    }
	}
      
      assert(j==do_get_num_free_params());
      return deformed_p;
    }


    Tstr do_report_param_status(const Tstr& name)const
    {
      if(param_names.find(name)==param_names.end())
	{
	  return "thawed";
	}
      return "frozen";
    }

  public:
    
    /**
       plus operator, used to combine a list of freeze_param objects
       \param fp another freeze_param object
       \return the combined freeze_param object
     */
    freeze_param operator+(const freeze_param& fp)const
    {
      freeze_param result(*this);
      for(typename std::set<Tstr>::const_iterator i=fp.param_names.begin();
	  i!=fp.param_names.end();
	  ++i)
	{
	  result.param_names.insert(*i);
	}
      return result;
    }

    /**
       += operator
       like the plus operator
     */
    freeze_param& operator+=(const freeze_param& fp)
    {
      //param_names.insert(param_names.end(),
      //fp.param_names.begin(),
      //fp.param_names.end());
      for(typename std::set<Tstr>::const_iterator i=fp.param_names.begin();
	  i!=fp.param_names.end();
	  ++i)
	{
	  param_names.insert(*i);
	}
      try
	{
	  update();
	}
      catch(opt_exception& e)
	{
	  throw;
	}
      return *this;
    }


    /**
       un-freeze a parameter to a list of pre-frozen parameter list
       \param fp the parameter to be un-frozen
       \return the reference to the operated freeze_param object
     */
    freeze_param& operator-=(const freeze_param& fp)
    {
      //param_names.insert(param_names.end(),
      //fp.param_names.begin(),
      //fp.param_names.end());
      for(typename std::set<Tstr>::const_iterator i=fp.param_names.begin();
	  i!=fp.param_names.end();
	  ++i)
	{
	  param_names.erase(*i);
	}
      try
	{
	  update();
	}
      catch(opt_exception& e)
	{
	  throw;
	}
      return *this;
    }

  };

  /**
     help function to create a freeze_param object
     \param name the name to be frozen
     \return the created freeze_param object
   */
  template <typename Ty,typename Tx,typename Tp,typename Tstr>
  freeze_param<Ty,Tx,Tp,Tstr> freeze(const Tstr& name)
  {
    return freeze_param<Ty,Tx,Tp,Tstr>(name);
  }

}


#endif
//EOF