STK++ 0.9.13
STK_FullStrategy.cpp
Go to the documentation of this file.
1/*--------------------------------------------------------------------*/
2/* Copyright (C) 2004-2016 Serge Iovleff
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU Lesser General Public License as
6 published by the Free Software Foundation; either version 2 of the
7 License, or (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public
15 License along with this program; if not, write to the
16 Free Software Foundation, Inc.,
17 59 Temple Place,
18 Suite 330,
19 Boston, MA 02111-1307
20 USA
21
22 Contact : S..._DOT_I..._AT_stkpp.org (see copyright for ...)
23*/
24
25/*
26 * Project: stkpp::Clustering
27 * created on: 3 sept. 2013
28 * Author: iovleff, serge.iovleff@stkpp.org
29 **/
30
34
35#include <Sdk.h>
36
41
42namespace STK
43{
44
45/* destructor */
51
52
53/* run the full strategy */
55{
56 // add some perturbation to the tik and compute the ln-likelihood
58#ifdef STK_MIXTURE_DEBUG
59 p_model_->writeParameters(stk_cout);
60#endif
61 p_model_->randomFuzzyInit();
62#ifdef STK_MIXTURE_DEBUG
63 p_model_->writeParameters(stk_cout);
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);
87 msg_error_ += p_param_->p_shortAlgo_->error();
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);
111 msg_error_ += p_param_->p_shortAlgo_->error();
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);
147 msg_error_ += p_param_->p_longAlgo_->error();
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}
194
195/* Perform the Initialization step*/
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
221 msg_error_ += p_init_->error();
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 {
245 msg_error_ = p_param_->p_shortAlgo_->error();
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}
278
279} // namespace STK
280
281
282
In this file we define the class implementing the full strategy class.
In this file we define the abstract base class for mixture models.
#define STKERROR_NO_ARG(Where, Error)
Definition STK_Macros.h:49
In this file we define mixture algorithms.
In this file we define the interface base class for initialization methods.
#define stk_cout
Standard stk output stream.
#define _T(x)
Let x unmodified.
This file include all the other header files of the project Sdk.
Base class for all Exceptions thrown by the STK++ library.
virtual const String error() const
Returns a C-style character string describing the general cause of the current error.
virtual bool run()
run the strategy
FullStrategyParam * p_param_
bool initStep(IMixtureComposer *&p_bestModel)
Perform the Initialization step Initialize nbInitRun_ (should be > 0) model and select the best model...
Base class for Mixture (composed) model.
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)
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
bool isFinite(Type const &x)
utility method allowing to know if a value is a finite value
@ modelInitialized_
the model is initialized and its parameters are initialized to default values
double Real
STK fundamental type of Real values.
The namespace STK is the main domain space of the Statistical ToolKit project.
Arithmetic properties of STK fundamental types.
IMixtureAlgo * p_longAlgo_
algorithm to use in long run
IMixtureAlgo * p_shortAlgo_
algorithm to use in short runs
virtual ~FullStrategyParam()
destructor