14 #include "../Io/Newick.h"
15 #include "../PseudoNewtonOptimizer.h"
16 #include "../Tree/NNISearchable.h"
36 std::shared_ptr<TreeLikelihoodInterface> tl) :
42 brLen_ = tl->getBranchLengthsParameters();
52 if (lambda.
size() != 1)
53 throw Exception(
"LegacyOptimizationTools::ScaleFunction::f(). This is a one parameter function!");
54 lambda_.setParametersValues(lambda);
61 double s =
exp(lambda_[0].getValue());
62 for (
unsigned int i = 0; i < brLen.
size(); i++)
66 brLen[i].setValue(brLen[i].getValue() * s);
79 std::shared_ptr<TreeLikelihoodInterface> tl,
81 unsigned int tlEvalMax,
82 std::shared_ptr<OutputStream> messageHandler,
83 std::shared_ptr<OutputStream> profiler,
86 auto sf = make_shared<ScaleFunction>(tl);
93 bod.
init(singleParameter);
94 auto PS = make_shared<ParametersStopCondition>(&bod, tolerance);
107 std::shared_ptr<DiscreteRatesAcrossSitesTreeLikelihoodInterface> tl,
109 std::shared_ptr<OptimizationListener> listener,
112 unsigned int tlEvalMax,
113 std::shared_ptr<OutputStream> messageHandler,
114 std::shared_ptr<OutputStream> profiler,
115 bool reparametrization,
116 unsigned int verbose,
117 const string& optMethodDeriv,
118 const string& optMethodModel)
120 shared_ptr<SecondOrderDerivable> f = tl;
124 if (reparametrization)
126 f = make_shared<ReparametrizationDerivableSecondOrderWrapper>(f, parameters);
137 auto desc = make_unique<MetaOptimizerInfos>();
138 unique_ptr<MetaOptimizer> poptimizer;
142 desc->addOptimizer(
"Branch length parameters", make_shared<ConjugateGradientMultiDimensions>(f), tl->getBranchLengthsParameters().getParameterNames(), 2,
MetaOptimizerInfos::IT_TYPE_FULL);
144 desc->addOptimizer(
"Branch length parameters", make_shared<PseudoNewtonOptimizer>(f), tl->getBranchLengthsParameters().getParameterNames(), 2,
MetaOptimizerInfos::IT_TYPE_FULL);
146 desc->addOptimizer(
"Branch length parameters", make_shared<BfgsMultiDimensions>(f), tl->getBranchLengthsParameters().getParameterNames(), 2,
MetaOptimizerInfos::IT_TYPE_FULL);
148 throw Exception(
"LegacyOptimizationTools::optimizeNumericalParameters. Unknown optimization method: " + optMethodDeriv);
164 vector<string> vNameDer;
173 vNameDer.insert(vNameDer.begin(), vNameDer2.begin(), vNameDer2.end());
174 fnum->setParametersToDerivate(vNameDer);
177 poptimizer.reset(
new MetaOptimizer(fnum, std::move(desc), nstep));
180 throw Exception(
"LegacyOptimizationTools::optimizeNumericalParameters. Unknown optimization method: " + optMethodModel);
182 poptimizer->setVerbose(verbose);
183 poptimizer->setProfiler(profiler);
184 poptimizer->setMessageHandler(messageHandler);
185 poptimizer->setMaximumNumberOfEvaluations(tlEvalMax);
186 poptimizer->getStopCondition()->setTolerance(tolerance);
190 auto nanListener = make_shared<NaNListener>(poptimizer.get(), tl.get());
191 poptimizer->addOptimizationListener(nanListener);
193 poptimizer->addOptimizationListener(listener);
194 poptimizer->init(pl);
195 poptimizer->optimize();
201 unsigned int nb = poptimizer->getNumberOfEvaluations();
209 std::shared_ptr<DiscreteRatesAcrossSitesTreeLikelihoodInterface> tl,
211 std::shared_ptr<OptimizationListener> listener,
213 unsigned int tlEvalMax,
214 std::shared_ptr<OutputStream> messageHandler,
215 std::shared_ptr<OutputStream> profiler,
216 bool reparametrization,
218 unsigned int verbose,
219 const std::string& optMethodDeriv)
221 shared_ptr<SecondOrderDerivable> f = tl;
222 shared_ptr<GlobalClockTreeLikelihoodFunctionWrapper> fclock;
238 if (reparametrization)
240 f = make_shared<ReparametrizationDerivableSecondOrderWrapper>(f, pl);
246 shared_ptr<AbstractNumericalDerivative> fnum;
249 unique_ptr<OptimizerInterface> optimizer;
253 fnum->setInterval(0.0000001);
259 fnum->setInterval(0.0001);
265 fnum->setInterval(0.0001);
269 throw Exception(
"LegacyOptimizationTools::optimizeNumericalParameters2. Unknown optimization method: " + optMethodDeriv);
277 optimizer->setVerbose(verbose);
278 optimizer->setProfiler(profiler);
279 optimizer->setMessageHandler(messageHandler);
280 optimizer->setMaximumNumberOfEvaluations(tlEvalMax);
281 optimizer->getStopCondition()->setTolerance(tolerance);
285 auto nanListener = make_shared<NaNListener>(optimizer.get(), tl.get());
286 optimizer->addOptimizationListener(nanListener);
288 optimizer->addOptimizationListener(listener);
290 optimizer->optimize();
296 return optimizer->getNumberOfEvaluations();
303 std::shared_ptr<DiscreteRatesAcrossSitesTreeLikelihoodInterface> tl,
305 std::shared_ptr<OptimizationListener> listener,
307 unsigned int tlEvalMax,
308 std::shared_ptr<OutputStream> messageHandler,
309 std::shared_ptr<OutputStream> profiler,
310 unsigned int verbose,
311 const string& optMethodDeriv)
314 unique_ptr<OptimizerInterface> optimizer;
317 tl->enableFirstOrderDerivatives(
true);
318 tl->enableSecondOrderDerivatives(
false);
323 tl->enableFirstOrderDerivatives(
true);
324 tl->enableSecondOrderDerivatives(
true);
329 tl->enableFirstOrderDerivatives(
true);
330 tl->enableSecondOrderDerivatives(
false);
334 throw Exception(
"LegacyOptimizationTools::optimizeBranchLengthsParameters. Unknown optimization method: " + optMethodDeriv);
335 optimizer->setVerbose(verbose);
336 optimizer->setProfiler(profiler);
337 optimizer->setMessageHandler(messageHandler);
338 optimizer->setMaximumNumberOfEvaluations(tlEvalMax);
339 optimizer->getStopCondition()->setTolerance(tolerance);
345 optimizer->addOptimizationListener(listener);
347 optimizer->optimize();
352 unsigned int n = optimizer->getNumberOfEvaluations();
361 if (optimizeCounter_ == optimizeNumerical_)
363 auto likelihood = dynamic_pointer_cast<DiscreteRatesAcrossSitesTreeLikelihoodInterface>(topoSearch_->getSearchableObject());
364 parameters_.matchParametersValues(likelihood->getParameters());
365 LegacyOptimizationTools::optimizeNumericalParameters(likelihood, parameters_, 0, nStep_, tolerance_, 1000000, messenger_, profiler_, reparametrization_, verbose_, optMethod_);
366 optimizeCounter_ = 0;
375 if (optimizeCounter_ == optimizeNumerical_)
377 auto likelihood = dynamic_pointer_cast<DiscreteRatesAcrossSitesTreeLikelihoodInterface>(topoSearch_->getSearchableObject());
378 parameters_.matchParametersValues(likelihood->getParameters());
379 LegacyOptimizationTools::optimizeNumericalParameters2(likelihood, parameters_, 0, tolerance_, 1000000, messenger_, profiler_, reparametrization_,
false, verbose_, optMethod_);
380 optimizeCounter_ = 0;
387 std::shared_ptr<NNIHomogeneousTreeLikelihood> tl,
389 bool optimizeNumFirst,
392 unsigned int tlEvalMax,
393 unsigned int numStep,
394 std::shared_ptr<OutputStream> messageHandler,
395 std::shared_ptr<OutputStream> profiler,
396 bool reparametrization,
397 unsigned int verbose,
398 const string& optMethodDeriv,
400 const string& nniMethod)
403 if (optimizeNumFirst)
405 LegacyOptimizationTools::optimizeNumericalParameters(tl, parameters, NULL, nStep, tolBefore, 1000000, messageHandler, profiler, reparametrization, verbose, optMethodDeriv);
408 auto topoSearch = make_shared<NNITopologySearch>(tl, nniMethod, verbose > 2 ? verbose - 2 : 0);
409 auto topoListener = make_shared<NNITopologyListener>(topoSearch, parameters, tolDuring, messageHandler, profiler, verbose, optMethodDeriv, nStep, reparametrization);
410 topoListener->setNumericalOptimizationCounter(numStep);
411 topoSearch->addTopologyListener(topoListener);
412 topoSearch->search();
413 return dynamic_pointer_cast<NNIHomogeneousTreeLikelihood>(topoSearch->getSearchableObject());
419 std::shared_ptr<NNIHomogeneousTreeLikelihood> tl,
421 bool optimizeNumFirst,
424 unsigned int tlEvalMax,
425 unsigned int numStep,
426 std::shared_ptr<OutputStream> messageHandler,
427 std::shared_ptr<OutputStream> profiler,
428 bool reparametrization,
429 unsigned int verbose,
430 const string& optMethodDeriv,
431 const string& nniMethod)
434 if (optimizeNumFirst)
436 LegacyOptimizationTools::optimizeNumericalParameters2(tl, parameters,
nullptr, tolBefore, 1000000, messageHandler, profiler, reparametrization,
false, verbose, optMethodDeriv);
439 auto topoSearch = make_shared<NNITopologySearch>(tl, nniMethod, verbose > 2 ? verbose - 2 : 0);
440 auto topoListener = make_shared<NNITopologyListener2>(topoSearch, parameters, tolDuring, messageHandler, profiler, verbose, optMethodDeriv, reparametrization);
441 topoListener->setNumericalOptimizationCounter(numStep);
442 topoSearch->addTopologyListener(topoListener);
443 topoSearch->search();
444 return dynamic_pointer_cast<NNIHomogeneousTreeLikelihood>(topoSearch->getSearchableObject());
450 std::shared_ptr<DRTreeParsimonyScore> tp,
451 unsigned int verbose)
453 auto topo = dynamic_pointer_cast<NNISearchable>(tp);
unsigned int getNumberOfEvaluations() const override
void setProfiler(std::shared_ptr< OutputStream > profiler) override
void init(const ParameterList ¶ms) override
void setMessageHandler(std::shared_ptr< OutputStream > mh) override
void setStopCondition(std::shared_ptr< OptimizationStopCondition > stopCondition) override
void setMaximumNumberOfEvaluations(unsigned int max) override
static std::string CONSTRAINTS_AUTO
void setInitialInterval(double inf, double sup)
void topologyChangeSuccessful(const TopologyChangeEvent &event)
Tell that a topology change is definitive.
void topologyChangeSuccessful(const TopologyChangeEvent &event)
Tell that a topology change is definitive.
NNI topology search method.
static const std::string PHYML
std::shared_ptr< NNISearchable > getSearchableObject()
void search()
Performs the search.
virtual bool hasParameter(const std::string &name) const
virtual void addParameters(const ParameterList ¶ms)
virtual ParameterList getCommonParametersWith(const ParameterList ¶ms) const
virtual std::vector< std::string > getParameterNames() const
virtual void addParameter(const Parameter ¶m)
virtual void deleteParameter(const std::string &name)
virtual ParameterList createSubList(const std::vector< std::string > &names) const
This Optimizer implements Newton's algorithm for finding a minimum of a function. This is in fact a m...
Class for notifying new toplogy change events.
Defines the basic types of data flow nodes.
ExtendedFloat exp(const ExtendedFloat &ef)