diff options
-rw-r--r-- | core/fitter.hpp | 9 | ||||
-rw-r--r-- | core/optimizer.hpp | 25 | ||||
-rw-r--r-- | methods/aga/aga.hpp | 24 | ||||
-rw-r--r-- | methods/powell/powell_method.hpp | 16 |
4 files changed, 63 insertions, 11 deletions
diff --git a/core/fitter.hpp b/core/fitter.hpp index e07469e..2a81d70 100644 --- a/core/fitter.hpp +++ b/core/fitter.hpp @@ -1628,10 +1628,17 @@ namespace opt_utilities return p_model->get_all_params();
}
+ /**
+ stop the fitting
+ */
+ void stop()
+ {
+ optengine.stop();
+ }
+
};
-
/**
\brief virtual class representing a statistic
\tparam Ty the type of the model return type
diff --git a/core/optimizer.hpp b/core/optimizer.hpp index 6380d66..bb26bfb 100644 --- a/core/optimizer.hpp +++ b/core/optimizer.hpp @@ -198,6 +198,13 @@ namespace opt_utilities { return typeid(*this).name(); } + + /** + interrupting the optimization + */ + virtual void do_stop() + { + } public: /** Interface function for seting optimizer @@ -300,6 +307,13 @@ namespace opt_utilities return do_clone(); } + /** + stop the optimization + */ + void stop() + { + do_stop(); + } /** destroy the cloned object. @@ -621,6 +635,17 @@ namespace opt_utilities } return p_opt_method->optimize(); } + + /** + stop the optimize + */ + void stop() + { + if(p_opt_method) + { + p_opt_method->stop(); + } + } /** \return the pointer to the inner object function diff --git a/methods/aga/aga.hpp b/methods/aga/aga.hpp index cf0b4a6..77c5700 100644 --- a/methods/aga/aga.hpp +++ b/methods/aga/aga.hpp @@ -69,7 +69,7 @@ namespace opt_utilities pT reproduction_box; std::vector<vp_pair<rT,pT> > samples; std::vector<pT> buffer; - + mutable bool bstop; private: typename element_type_trait<pT>::element_type uni_rand (typename element_type_trait<pT>::element_type x1, @@ -216,7 +216,7 @@ namespace opt_utilities } pT lb(get_size(samples[0].p)); pT ub(get_size(samples[0].p)); - for(int i=0;i<n2;++i) + for(int i=0;i<n2&&!bstop;++i) { pT p(samples[i].p); for(size_t j=0;j<get_size(p);++j) @@ -244,9 +244,13 @@ namespace opt_utilities } buffer[i]=p; } - for(int i=0;i<n1;++i) + if(bstop) { - for(int j=0;j<n2;++j) + return false; + } + for(int i=0;i<n1&&!bstop;++i) + { + for(int j=0;j<n2&&!bstop;++j) { pT p(samples[i].p); for(size_t k=0;k<get_size(p);++k) @@ -261,6 +265,10 @@ namespace opt_utilities } } } + if(bstop) + { + return false; + } double n_per_dim=pow((double)n0,1./get_size(lower_bound)); for(size_t i=0;i<get_size(reproduction_box);++i) { @@ -276,6 +284,7 @@ namespace opt_utilities pT do_optimize() { + bstop=false; srand(time(0)); buffer.resize(n2); double n_per_dim=pow((double)n0,1./get_size(lower_bound)); @@ -289,10 +298,15 @@ namespace opt_utilities get_element(lower_bound,i))/n_per_dim); } - while(iter()){} + while(iter()&&!bstop){} return samples.begin()->p; } + + void do_stop() + { + bstop=true; + } }; diff --git a/methods/powell/powell_method.hpp b/methods/powell/powell_method.hpp index 0ec095c..ee5adeb 100644 --- a/methods/powell/powell_method.hpp +++ b/methods/powell/powell_method.hpp @@ -31,7 +31,7 @@ namespace opt_utilities private: func_obj<rT,pT>* p_fo; optimizer<rT,pT>* p_optimizer; - + mutable bool bstop; //typedef blitz::Array<rT,2> array2d_type; @@ -99,13 +99,13 @@ namespace opt_utilities array1d_type ptt(n); array1d_type xit(n); fret=p_fo->eval(p); - + for(j=0;j<n;++j) { //get_element(pt,j)=get_element(p,j); set_element(pt,j,get_element(p,j)); } - for(iter=0;;++iter) + for(iter=0;!bstop;++iter) { fp=fret; ibig=0; @@ -238,7 +238,7 @@ namespace opt_utilities pT do_optimize() { - + bstop=false; init_xi((int)get_size(start_point)); @@ -255,7 +255,13 @@ namespace opt_utilities rT fret; powell(end_point,threshold,iter,fret); return end_point; - } + } + + void do_stop() + { + bstop=true; + } + }; } |