STK++ 0.9.13
STK::FullStrategy Class Reference

A FullStrategy is based on the following paradigm: More...

#include <STK_FullStrategy.h>

Inheritance diagram for STK::FullStrategy:
Inheritance graph

Public Member Functions

 FullStrategy (IMixtureComposer *&p_model)
 default constructor.
 
 FullStrategy (FullStrategy const &strategy)
 copy constructor.
 
virtual ~FullStrategy ()
 destructor
 
virtual FullStrategyclone () const
 clone pattern
 
void setParam (FullStrategyParam *p_param)
 set the parameters of the strategy
 
virtual bool run ()
 run the strategy
 
 FullStrategy (IMixtureComposer *&p_model)
 default constructor.
 
 FullStrategy (FullStrategy const &strategy)
 copy constructor.
 
virtual ~FullStrategy ()
 destructor
 
virtual FullStrategyclone () const
 clone pattern
 
void setParam (FullStrategyParam *p_param)
 set the parameters of the strategy
 
virtual bool run ()
 run the strategy
 
- Public Member Functions inherited from STK::IMixtureStrategy
 IMixtureStrategy (IMixtureComposer *&p_model)
 default constructor.
 
 IMixtureStrategy (IMixtureStrategy const &strategy)
 copy constructor
 
virtual ~IMixtureStrategy ()
 destructor
 
int nbTry () const
 
void setNbTry (int nbTry)
 set the number of tries of each strategies.
 
void setMixtureInit (IMixtureInit *p_init)
 set the initialization method to use
 
- Public Member Functions inherited from STK::IRunnerBase
String consterror () const
 get the last error message.
 

Protected Member Functions

bool initStep (IMixtureComposer *&p_bestModel)
 Perform the Initialization step Initialize nbInitRun_ (should be > 0) model and select the best model among them.
 
bool initStep (IMixtureComposer *&p_bestModel)
 Perform the Initialization step Initialize nbInitRun_ (should be > 0) model and select the best model among them.
 
- Protected Member Functions inherited from STK::IMixtureStrategy
void storeModel (IMixtureComposer *&p_otherModel)
 store a model in p_model_ if it is better.
 
- Protected Member Functions inherited from STK::IRunnerBase
 IRunnerBase ()
 default constructor
 
 IRunnerBase (IRunnerBase const &runner)
 copy constructor
 
virtual ~IRunnerBase ()
 destructor
 
virtual void update ()
 update the runner.
 

Protected Attributes

FullStrategyParamp_param_
 
- Protected Attributes inherited from STK::IMixtureStrategy
int nbTry_
 number of tries of each strategies (1 by default)
 
IMixtureComposer *& p_model_
 reference on the main model
 
IMixtureInitp_init_
 initialization method
 
- Protected Attributes inherited from STK::IRunnerBase
String msg_error_
 String with the last error message.
 
bool hasRun_
 true if run has been used, false otherwise
 

Detailed Description

A FullStrategy is based on the following paradigm:

  • perform nbInitRun_ of the p_init_ initialization method, select the best initialization
  • perform nbShortRun of the shortAlgo with a small number of iterations and a high tolerance,
  • pick the best model obtained,
  • on this best model perform a long run. This strategy is used in Rmixmod R package.

Definition at line 75 of file STK_FullStrategy.h.

Constructor & Destructor Documentation

◆ FullStrategy() [1/4]

STK::FullStrategy::FullStrategy ( IMixtureComposer *&  p_model)
inline

default constructor.

Parameters
p_modela reference pointer on the model to estimate

Definition at line 81 of file STK_FullStrategy.h.

81 : IMixtureStrategy(p_model), p_param_()
82 {}
FullStrategyParam * p_param_
IMixtureStrategy(IMixtureComposer *&p_model)
default constructor.

◆ FullStrategy() [2/4]

STK::FullStrategy::FullStrategy ( FullStrategy const strategy)
inline

copy constructor.

Parameters
strategythe strategy to copy

Definition at line 86 of file STK_FullStrategy.h.

86 : IMixtureStrategy(strategy), p_param_(0)
87 {}

◆ ~FullStrategy() [1/2]

virtual STK::FullStrategy::~FullStrategy ( )
inlinevirtual

destructor

Definition at line 89 of file STK_FullStrategy.h.

89{ if (p_param_) delete p_param_;}

References p_param_.

◆ FullStrategy() [3/4]

STK::FullStrategy::FullStrategy ( IMixtureComposer *&  p_model)
inline

default constructor.

Parameters
p_modela reference pointer on the model to estimate

Definition at line 181 of file STK_MixtureStrategy.h.

181 : IMixtureStrategy(p_model), p_param_()
182 {}

◆ FullStrategy() [4/4]

STK::FullStrategy::FullStrategy ( FullStrategy const strategy)
inline

copy constructor.

Parameters
strategythe strategy to copy

Definition at line 186 of file STK_MixtureStrategy.h.

186 : IMixtureStrategy(strategy), p_param_(0)
187 {}

◆ ~FullStrategy() [2/2]

virtual STK::FullStrategy::~FullStrategy ( )
inlinevirtual

destructor

Definition at line 189 of file STK_MixtureStrategy.h.

189{ if (p_param_) delete p_param_;}

References p_param_.

Member Function Documentation

◆ clone() [1/2]

virtual FullStrategy * STK::FullStrategy::clone ( ) const
inlinevirtual

clone pattern

Definition at line 91 of file STK_FullStrategy.h.

91{ return new FullStrategy(*this);}
FullStrategy(IMixtureComposer *&p_model)
default constructor.

◆ clone() [2/2]

virtual FullStrategy * STK::FullStrategy::clone ( ) const
inlinevirtual

clone pattern

Definition at line 191 of file STK_MixtureStrategy.h.

191{ return new FullStrategy(*this);}

◆ initStep() [1/2]

bool STK::FullStrategy::initStep ( IMixtureComposer *&  p_bestModel)
protected

Perform the Initialization step Initialize nbInitRun_ (should be > 0) model and select the best model among them.

Parameters
p_bestModela pointer initialized to

Definition at line 196 of file STK_FullStrategy.cpp.

197{
198#ifdef STK_MIXTURE_VERBOSE
199 stk_cout << _T("<+++++\n");
200 stk_cout << _T("Entering FullStrategy::initStep\n");
201 stk_cout << _T("nbInitRun = ") << p_param_->nbInitRun_ << _T("\n");
202#endif
203 bool flag = true;
204 IMixtureComposer* p_initModel = 0;
205 try
206 {
207 Real valueBest = -Arithmetic<Real>::infinity();
208 for (int iInitRun=0; iInitRun < p_param_->nbInitRun_; iInitRun++)
209 {
210 // Initialize a new model if necessary
211 if (!p_initModel)
212 { p_initModel = p_model_->create();}
213 p_init_->setModel(p_initModel);
214 if (!p_init_->run())
215 {
216#ifdef STK_MIXTURE_VERBOSE
217 stk_cout << _T("FullStrategy::initStep, iInitRun=") << iInitRun
218 << _T(", initialization failed:\n");
219 stk_cout << p_init_->error() << _T("\n");
220#endif
222 }
223 else
224 {
225 // if we get a better result, swap initModel with bestModel
226 Real value = p_initModel->lnLikelihood();
227 if( (valueBest < value) && isFinite(value))
228 {
229 std::swap(p_initModel, p_bestModel);
230 valueBest = value;
231#ifdef STK_MIXTURE_VERY_VERBOSE
232 stk_cout << _T("FullStrategy::initStep, iInitRun =") << iInitRun
233 << _T(", get a better model with value =") << valueBest << _T("\n");
234#endif
235 }
236 }
237 } // iInitRun
238 // In case we never get a better model, clone current model
239 // and perform short run with the current model
240 if (!p_bestModel) { p_bestModel = p_model_->clone();}
241 p_param_->p_shortAlgo_->setModel(p_bestModel);
242 if (!p_param_->p_shortAlgo_->run())
243 {
246#ifdef STK_MIXTURE_VERBOSE
247 stk_cout << _T("In FullStrategy::initStep() shortAlgo failed:\n");
248 stk_cout << msg_error_ << _T("\n");
249#endif
250 }
251 }
252 catch (Exception const& e)
253 {
254 msg_error_ = e.error();
255 flag = false;
256 }
257 // in case all initialization failed or nbInitRun_ <= 0
258 if (!p_bestModel) { p_bestModel = p_model_->clone();}
259 if (p_initModel)
260 {
261#ifdef STK_MIXTURE_DEBUG_CREATE
262 stk_cout << _T("FullStrategy::initStep terminated. Deleting p_initModel.\n");
263#endif
264 delete p_initModel; p_initModel = 0;
265#ifdef STK_MIXTURE_DEBUG_CREATE
266 stk_cout << _T("FullStrategy::initStep p_initModel deleted.\n");
267 stk_cout << _T("p_bestModel->writeParameters\n");
268 p_bestModel->writeParameters(stk_cout);
269#endif
270 }
271#ifdef STK_MIXTURE_VERBOSE
272 stk_cout << _T("FullStrategy::initStep done\n");
273 stk_cout << _T("p_bestModel->lnLikelihood() = ") << p_bestModel->lnLikelihood() << _T("\n");
274 stk_cout << _T("+++++>\n");
275#endif
276 return flag;
277}
#define STKERROR_NO_ARG(Where, Error)
Definition STK_Macros.h:49
#define stk_cout
Standard stk output stream.
#define _T(x)
Let x unmodified.
bool initStep(IMixtureComposer *&p_bestModel)
Perform the Initialization step Initialize nbInitRun_ (should be > 0) model and select the best model...
void setModel(IMixtureComposer *p_model)
set model
virtual IMixtureComposer * clone() const =0
clone pattern
virtual IMixtureComposer * create() const =0
create pattern
void setModel(IMixtureComposer *p_model)
set a new model
IMixtureInit * p_init_
initialization method
IMixtureComposer *& p_model_
reference on the main model
String msg_error_
String with the last error message.
Definition STK_IRunner.h:96
virtual bool run()=0
run the computations.
String const & error() const
get the last error message.
Definition STK_IRunner.h:82
bool isFinite(Type const &x)
utility method allowing to know if a value is a finite value
double Real
STK fundamental type of Real values.
IMixtureAlgo * p_shortAlgo_
algorithm to use in short runs
int nbInitRun_
number of initialization run to perform

References _T, STK::IMixtureComposer::clone(), STK::IMixtureComposer::create(), STK::IRunnerBase::error(), initStep(), STK::isFinite(), STK::IRunnerBase::msg_error_, STK::FullStrategyParam::nbInitRun_, STK::IMixtureStrategy::p_init_, STK::IMixtureStrategy::p_model_, p_param_, STK::FullStrategyParam::p_shortAlgo_, STK::IRunnerBase::run(), STK::IMixtureAlgo::setModel(), STK::IMixtureInit::setModel(), stk_cout, and STKERROR_NO_ARG.

Referenced by initStep(), and run().

◆ initStep() [2/2]

bool STK::FullStrategy::initStep ( IMixtureComposer *&  p_bestModel)
protected

Perform the Initialization step Initialize nbInitRun_ (should be > 0) model and select the best model among them.

Parameters
p_bestModela pointer initialized to

◆ run() [1/2]

bool STK::FullStrategy::run ( )
virtual

run the strategy

Implements STK::IRunnerBase.

Definition at line 54 of file STK_FullStrategy.cpp.

55{
56 // add some perturbation to the tik and compute the ln-likelihood
58#ifdef STK_MIXTURE_DEBUG
60#endif
62#ifdef STK_MIXTURE_DEBUG
64#endif
65 Real initialValue = p_model_->lnLikelihood();
66#ifdef STK_MIXTURE_VERBOSE
67 stk_cout << _T("<+++\n");
68 stk_cout << _T("Entering FullStrategy::run() with nbTry_ = ") << nbTry_
69 << _T(", nbShortRun_ = ") << p_param_->nbShortRun_
70 << _T(", p_model_->lnLikelihood() = ") << initialValue
71 << _T("\n");
72#endif
73 IMixtureComposer *p_bestModel =0, *p_bestShortModel =0;
74 // start estimation
75 try
76 {
77 // Main loop. If the Full strategy success in estimating a model, the
78 // iterations are stopped and the best model find is stored in p_model_
79 for (int iTry = 0; iTry < nbTry_; ++iTry)
80 {
81 // in case nbShortRun_==0: initialize directly p_bestShortModel
82 if (p_param_->nbShortRun_ <= 0)
83 {
84 if (!initStep(p_bestShortModel))
85 {
86 msg_error_ += STKERROR_NO_ARG(FullStrategy::run,init step failed\n);
88#ifdef STK_MIXTURE_VERBOSE
89 stk_cout << _T("In FullStrategy::run()") << _T(", iTyry =") << iTry
90 << _T(", init step failed\n");
91 stk_cout << msg_error_ << _T("\n");
92#endif
93 }
94 }
95 else
96 {
97#ifdef STK_MIXTURE_VERY_VERBOSE
98 stk_cout << _T("In FullStrategy::run(), entering short run steps\n")
99 << _T("iTyry =") << iTry << _T("\n");
100#endif
101 Real valueBest = -Arithmetic<Real>::infinity();
102 for (int iShort=0; iShort < p_param_->nbShortRun_; ++iShort)
103 {
104#ifdef STK_MIXTURE_VERY_VERBOSE
105 stk_cout << _T("In FullStrategy::run(), iShort =") << iShort << _T("\n");
106#endif
107 // perform nbInitRun_ initialization step and get the best result in p_bestModel
108 if (!initStep(p_bestModel))
109 {
110 msg_error_ += STKERROR_NO_ARG(FullStrategy::run,init step failed\n);
112#ifdef STK_MIXTURE_VERBOSE
113 stk_cout << _T("In FullStrategy::run()") << _T(", iTyry =") << iTry << _T(", iShort =") << iShort
114 << _T(", init step failed\n");
115 stk_cout << msg_error_ << _T("\n");
116#endif
117 }
118 // if we get a better result, store it in p_bestShortModel
119 Real value = p_bestModel->lnLikelihood();
120 if( valueBest<value)
121 {
122 std::swap(p_bestShortModel, p_bestModel);
123 valueBest = value;
124#ifdef STK_MIXTURE_VERY_VERBOSE
125 stk_cout << _T("In FullStrategy::run()")
126 << _T(", iTyry =") << iTry << _T(", iShort =") << iShort
127 << _T(", get better value in short run. valueBest =") << valueBest << _T("\n");
128#endif
129 }
130 } // ishort
131 // release memory
132 if (p_bestModel) { delete p_bestModel; p_bestModel = 0;}
133 }
134 // in case all initialization failed
135 if (!p_bestShortModel) { p_bestShortModel = p_model_->clone();}
136#ifdef STK_MIXTURE_VERY_VERBOSE
137 stk_cout << _T("In FullStrategy::run() all short run done") << _T(", iTyry =") << iTry << _T(" terminated.\n")
138 << _T("p_bestShortModel->lnLikelihood() = ") << p_bestShortModel->lnLikelihood()
139 << _T("\n");
140#endif
141 // start a long run with p_bestShortModel. If success, save model
142 // and exit the iTry loop
143 p_param_->p_longAlgo_->setModel(p_bestShortModel);
144 if (!p_param_->p_longAlgo_->run())
145 {
146 msg_error_ += STKERROR_NO_ARG(FullStrategy::run,long algo failed\n);
148#ifdef STK_MIXTURE_VERBOSE
149 stk_cout << _T("In FullStrategy::run(): Long Algo failed\n");
150#endif
151 }
152#ifdef STK_MIXTURE_VERY_VERBOSE
153 stk_cout << _T("In FullStrategy::run() long run") << _T(", iTyry =") << iTry << _T(" terminated.\n")
154 << _T("p_bestShortModel->lnLikelihood() = ") << p_bestShortModel->lnLikelihood()
155 << _T("\n");
156#endif
157 // if we get a better result, store it in p_model_ and stop to try
158 if( p_model_->lnLikelihood()<p_bestShortModel->lnLikelihood())
159 {
160 std::swap(p_model_, p_bestShortModel);
161 break;
162 }
163#ifdef STK_MIXTURE_VERBOSE
164 stk_cout << _T("In FullStrategy::run(), iTry =") << iTry << _T(" failed\n");
165#endif
166 // release memory before next try
167 if (p_bestModel) delete p_bestModel;
168 p_bestModel = 0;
169 if (p_bestShortModel) delete p_bestShortModel;
170 p_bestShortModel = 0;
171 } // end iTry
172 }
173 catch (Exception const& e)
174 {
175 if (p_bestModel) delete p_bestModel;
176 if (p_bestShortModel) delete p_bestShortModel;
177 msg_error_ += e.error();
178 return false;
179 }
180#ifdef STK_MIXTURE_VERBOSE
181 stk_cout << "FullStrategy::run() terminated. \n";
182 stk_cout << _T("+++>\n");
183#endif
184 // normally not needed
185 if (p_bestModel) delete p_bestModel;
186 if (p_bestShortModel) delete p_bestShortModel;
187 if (p_model_->lnLikelihood() <= initialValue)
188 {
190 return false;
191 }
192 return true;
193}
virtual bool run()
run the strategy
void setState(Clust::modelState state)
set the state of the model : should be used by any strategy
void randomFuzzyInit()
Initialize randomly the posterior probabilities tik of the model, then compute the zi values with map...
virtual void writeParameters(ostream &os) const
write the parameters of the model in the stream os.
int nbTry_
number of tries of each strategies (1 by default)
@ modelInitialized_
the model is initialized and its parameters are initialized to default values
IMixtureAlgo * p_longAlgo_
algorithm to use in long run
int nbShortRun_
number of short run to perform

References _T, STK::IMixtureComposer::clone(), STK::IRunnerBase::error(), initStep(), STK::IStatModelBase::lnLikelihood(), STK::Clust::modelInitialized_, STK::IRunnerBase::msg_error_, STK::FullStrategyParam::nbShortRun_, STK::IMixtureStrategy::nbTry_, STK::FullStrategyParam::p_longAlgo_, STK::IMixtureStrategy::p_model_, p_param_, STK::FullStrategyParam::p_shortAlgo_, STK::IMixtureComposer::randomFuzzyInit(), run(), STK::IRunnerBase::run(), STK::IMixtureAlgo::setModel(), STK::IMixtureComposer::setState(), stk_cout, STKERROR_NO_ARG, and STK::IMixtureStatModel::writeParameters().

Referenced by run().

◆ run() [2/2]

virtual bool STK::FullStrategy::run ( )
virtual

run the strategy

Implements STK::IRunnerBase.

◆ setParam() [1/2]

void STK::FullStrategy::setParam ( FullStrategyParam p_param)
inline

set the parameters of the strategy

Parameters
p_paramthe parameters of the Xem strategy

Definition at line 95 of file STK_FullStrategy.h.

95{ p_param_ = p_param;}

References p_param_.

◆ setParam() [2/2]

void STK::FullStrategy::setParam ( FullStrategyParam p_param)
inline

set the parameters of the strategy

Parameters
p_paramthe parameters of the Xem strategy

Definition at line 195 of file STK_MixtureStrategy.h.

195{ p_param_ = p_param;}

References p_param_.

Member Data Documentation

◆ p_param_

FullStrategyParam * STK::FullStrategy::p_param_
protected

Definition at line 101 of file STK_FullStrategy.h.

Referenced by initStep(), run(), setParam(), and ~FullStrategy().


The documentation for this class was generated from the following files: