aboutsummaryrefslogtreecommitdiffstats
path: root/core/pre_estimater.hpp
blob: 2a0c455f0ea7aa5cf944246a99a58aca2e358d84 (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
#ifndef PRE_ESTIMATER_HPP
#define PRE_ESTIMATER_HPP

#include <core/fitter.hpp>


namespace opt_utilities
{
  

  template <typename  Ty,typename Tx,typename Tp,typename Tstr=std::string>
  class pre_estimater
  {
  private:
    std::string model_id;
  private:
    virtual void do_estimate(const data_set<Ty,Tx>& d,model<Ty,Tx,Tp,Tstr>& m)const=0;
    virtual pre_estimater* do_clone()const=0;
    virtual void do_destroy()
    {
      delete this;
    }
  public:
    void estimate(const data_set<Ty,Tx>& d,model<Ty,Tx,Tp,Tstr>& m)const
    {
      do_estimate(d,m);
    }

    pre_estimater* clone()const
    {
      return this->do_clone();
    }

    void destroy()
    {
      do_destroy();
    }
  public:
    std::string get_model_id()const
    {
      return model_id;
    }
    
    void set_model_id(const std::string& s)
    {
      model_id=s;
    }
  };

  template <typename  Ty,typename Tx,typename Tp,typename Tstr=std::string>
  class pre_estimatable
  {
  private:
    pre_estimater<Ty,Tx,Tp,Tstr>* ppe;
  public:
    pre_estimatable()
      :ppe(0)
    {}

    pre_estimatable(const pre_estimatable<Ty,Tx,Tp,Tstr>& rhs)
      :ppe(0)
    {
      if(rhs.ppe)
	{
	  ppe=rhs.ppe->clone();
	}
    }

    pre_estimatable& operator=(const pre_estimatable<Ty,Tx,Tp,Tstr>& rhs)
    {
      if(this==&rhs)
	{
	  return *this;
	}
      if(ppe)
	{
	  ppe->destroy();
	}
      ppe=rhs.ppe->clone();
    }
    
    void set_pre_estimater(const pre_estimater<Ty,Tx,Tp,Tstr>& pe)
    {
      if(dynamic_cast<model<Ty,Tx,Tp,Tstr>&>(*this).get_type_name()!=pe.get_model_id())
	{
	  return;
	}
      if(ppe)
	{
	  ppe->destroy();
	}
      ppe=pe.clone();
    }

    virtual ~pre_estimatable()
    {
      if(ppe)
	{
	  ppe->destroy();
	}
    }

  public:
    void estimate(const data_set<Ty,Tx>& d)
    {
      if(ppe)
	{
	  ppe->estimate(d,dynamic_cast<model<Ty,Tx,Tp,Tstr>&>(*this));
	}
    }
  };
  
  template <typename Ty,typename Tx,typename Tp,typename Ts,typename Tstr>
  void pre_estimate(fitter<Ty,Tx,Tp,Ts,Tstr>& fit)
  {
    dynamic_cast<pre_estimatable<Ty,Tx,Tp,Tstr>&>(fit.get_model()).estimate(fit.get_data_set());
  }
}


#endif