47 #include <Bpp/App/ApplicationTools.h> 48 #include <Bpp/Numeric/ParameterList.h> 49 #include <Bpp/Numeric/Function/BfgsMultiDimensions.h> 50 #include <Bpp/Numeric/Function/ReparametrizationFunctionWrapper.h> 51 #include <Bpp/Numeric/Function/ThreePointsNumericalDerivative.h> 52 #include <Bpp/Numeric/Function/ConjugateGradientMultiDimensions.h> 53 #include <Bpp/Numeric/Function/TwoPointsNumericalDerivative.h> 54 #include <Bpp/Numeric/Function/DownhillSimplexMethod.h> 57 #include <Bpp/Seq/Io/Fasta.h> 84 if (
brLen_.hasParameter(
"RootPosition"))
85 brLen_.deleteParameter(
"RootPosition");
86 lambda_.addParameter(Parameter(
"scale factor", 0));
92 throw (ParameterNotFoundException, ConstraintException)
94 if (lambda.size() != 1)
95 throw Exception(
"OptimizationTools::ScaleFunction::f(). This is a one parameter function!");
96 lambda_.setParametersValues(lambda);
100 throw (ParameterException)
103 ParameterList brLen = brLen_;
104 double s = exp(lambda_[0].getValue());
105 for (
unsigned int i = 0; i < brLen.size(); i++)
109 brLen[i].setValue(brLen[i].getValue() * s);
111 catch (ConstraintException& cex)
116 return tl_->f(brLen);
124 unsigned int tlEvalMax,
125 OutputStream* messageHandler,
126 OutputStream* profiler,
127 unsigned int verbose)
131 BrentOneDimension bod(&sf);
132 bod.setMessageHandler(messageHandler);
133 bod.setProfiler(profiler);
134 ParameterList singleParameter;
135 singleParameter.addParameter(Parameter(
"scale factor", 0));
136 bod.setInitialInterval(-0.5, 0.5);
137 bod.init(singleParameter);
138 ParametersStopCondition PS(&bod, tolerance);
139 bod.setStopCondition(PS);
140 bod.setMaximumNumberOfEvaluations(tlEvalMax);
142 ApplicationTools::displayTaskDone();
144 ApplicationTools::displayResult(
"Tree scaled by", exp(sf.
getParameters()[0].getValue()));
145 return bod.getNumberOfEvaluations();
152 const ParameterList& parameters,
153 OptimizationListener* listener,
156 unsigned int tlEvalMax,
157 OutputStream* messageHandler,
158 OutputStream* profiler,
159 bool reparametrization,
160 unsigned int verbose,
161 const std::string& optMethodDeriv,
162 const std::string& optMethodModel)
165 DerivableSecondOrder* f = tl;
166 ParameterList pl = parameters;
169 auto_ptr<DerivableSecondOrder> frep;
170 if (reparametrization)
172 frep.reset(
new ReparametrizationDerivableSecondOrderWrapper(f, parameters));
176 pl = f->getParameters().subList(parameters.getParameterNames());
184 MetaOptimizerInfos* desc =
new MetaOptimizerInfos();
185 MetaOptimizer* poptimizer = 0;
186 AbstractNumericalDerivative* fnum =
new ThreePointsNumericalDerivative(f);
189 desc->addOptimizer(
"Branch length parameters",
new ConjugateGradientMultiDimensions(f), tl->getBranchLengthsParameters().getParameterNames(), 2, MetaOptimizerInfos::IT_TYPE_FULL);
191 desc->addOptimizer(
"Branch length parameters",
new PseudoNewtonOptimizer(f), tl->getBranchLengthsParameters().getParameterNames(), 2, MetaOptimizerInfos::IT_TYPE_FULL);
193 desc->addOptimizer(
"Branch length parameters",
new BfgsMultiDimensions(f), tl->getBranchLengthsParameters().getParameterNames(), 2, MetaOptimizerInfos::IT_TYPE_FULL);
195 throw Exception(
"OptimizationTools::optimizeNumericalParameters. Unknown optimization method: " + optMethodDeriv);
201 ParameterList plsm = parameters.getCommonParametersWith(tl->getSubstitutionModelParameters());
202 desc->addOptimizer(
"Substitution model parameter",
new SimpleMultiDimensions(f), plsm.getParameterNames(), 0, MetaOptimizerInfos::IT_TYPE_STEP);
205 ParameterList plrd = parameters.getCommonParametersWith(tl->getRateDistributionParameters());
206 desc->addOptimizer(
"Rate distribution parameter",
new SimpleMultiDimensions(f), plrd.getParameterNames(), 0, MetaOptimizerInfos::IT_TYPE_STEP);
207 poptimizer =
new MetaOptimizer(f, desc, nstep);
211 vector<string> vNameDer;
213 ParameterList plsm = parameters.getCommonParametersWith(tl->getSubstitutionModelParameters());
214 vNameDer = plsm.getParameterNames();
216 ParameterList plrd = parameters.getCommonParametersWith(tl->getRateDistributionParameters());
218 vector<string> vNameDer2 = plrd.getParameterNames();
220 vNameDer.insert(vNameDer.begin(), vNameDer2.begin(), vNameDer2.end());
221 fnum->setParametersToDerivate(vNameDer);
223 desc->addOptimizer(
"Rate & model distribution parameters",
new BfgsMultiDimensions(fnum), vNameDer, 1, MetaOptimizerInfos::IT_TYPE_FULL);
224 poptimizer =
new MetaOptimizer(fnum, desc, nstep);
227 throw Exception(
"OptimizationTools::optimizeNumericalParameters. Unknown optimization method: " + optMethodModel);
229 poptimizer->setVerbose(verbose);
230 poptimizer->setProfiler(profiler);
231 poptimizer->setMessageHandler(messageHandler);
232 poptimizer->setMaximumNumberOfEvaluations(tlEvalMax);
233 poptimizer->getStopCondition()->setTolerance(tolerance);
236 poptimizer->setConstraintPolicy(AutoParameter::CONSTRAINTS_AUTO);
238 poptimizer->addOptimizationListener(nanListener);
240 poptimizer->addOptimizationListener(listener);
241 poptimizer->init(pl);
242 poptimizer->optimize();
245 ApplicationTools::displayMessage(
"\n");
248 unsigned int nb = poptimizer->getNumberOfEvaluations();
257 const ParameterList& parameters,
258 OptimizationListener* listener,
260 unsigned int tlEvalMax,
261 OutputStream* messageHandler,
262 OutputStream* profiler,
263 bool reparametrization,
265 unsigned int verbose,
266 const std::string& optMethodDeriv)
269 DerivableSecondOrder* f = tl;
270 ParameterList pl = parameters;
272 auto_ptr<GlobalClockTreeLikelihoodFunctionWrapper> fclock;
278 ApplicationTools::displayResult(
"Log-likelihood after adding clock", -tl->getLogLikelihood());
281 pl = fclock->getParameters().getCommonParametersWith(parameters);
285 auto_ptr<DerivableSecondOrder> frep;
286 if (reparametrization)
288 frep.reset(
new ReparametrizationDerivableSecondOrderWrapper(f, pl));
292 pl = f->getParameters().subList(pl.getParameterNames());
295 auto_ptr<AbstractNumericalDerivative> fnum;
297 auto_ptr<Optimizer> optimizer;
300 fnum.reset(
new TwoPointsNumericalDerivative(f));
301 fnum->setInterval(0.0000001);
302 optimizer.reset(
new ConjugateGradientMultiDimensions(fnum.get()));
306 fnum.reset(
new ThreePointsNumericalDerivative(f));
307 fnum->setInterval(0.0001);
312 fnum.reset(
new TwoPointsNumericalDerivative(f));
313 fnum->setInterval(0.0001);
314 optimizer.reset(
new BfgsMultiDimensions(fnum.get()));
317 throw Exception(
"OptimizationTools::optimizeNumericalParameters2. Unknown optimization method: " + optMethodDeriv);
320 ParameterList tmp = tl->getNonDerivableParameters();
323 fnum->setParametersToDerivate(tmp.getParameterNames());
324 optimizer->setVerbose(verbose);
325 optimizer->setProfiler(profiler);
326 optimizer->setMessageHandler(messageHandler);
327 optimizer->setMaximumNumberOfEvaluations(tlEvalMax);
328 optimizer->getStopCondition()->setTolerance(tolerance);
331 optimizer->setConstraintPolicy(AutoParameter::CONSTRAINTS_AUTO);
333 optimizer->addOptimizationListener(nanListener);
335 optimizer->addOptimizationListener(listener);
337 optimizer->optimize();
340 ApplicationTools::displayMessage(
"\n");
343 return optimizer->getNumberOfEvaluations();
350 const ParameterList& parameters,
351 OptimizationListener* listener,
353 unsigned int tlEvalMax,
354 OutputStream* messageHandler,
355 OutputStream* profiler,
356 unsigned int verbose,
357 const std::string& optMethodDeriv)
361 Optimizer* optimizer = 0;
364 tl->enableFirstOrderDerivatives(
true);
365 tl->enableSecondOrderDerivatives(
false);
366 optimizer =
new ConjugateGradientMultiDimensions(tl);
370 tl->enableFirstOrderDerivatives(
true);
371 tl->enableSecondOrderDerivatives(
true);
376 tl->enableFirstOrderDerivatives(
true);
377 tl->enableSecondOrderDerivatives(
false);
378 optimizer =
new BfgsMultiDimensions(tl);
381 throw Exception(
"OptimizationTools::optimizeBranchLengthsParameters. Unknown optimization method: " + optMethodDeriv);
382 optimizer->setVerbose(verbose);
383 optimizer->setProfiler(profiler);
384 optimizer->setMessageHandler(messageHandler);
385 optimizer->setMaximumNumberOfEvaluations(tlEvalMax);
386 optimizer->getStopCondition()->setTolerance(tolerance);
389 ParameterList pl = parameters.getCommonParametersWith(tl->getBranchLengthsParameters());
390 optimizer->setConstraintPolicy(AutoParameter::CONSTRAINTS_AUTO);
392 optimizer->addOptimizationListener(listener);
394 optimizer->optimize();
396 ApplicationTools::displayMessage(
"\n");
399 unsigned int n = optimizer->getNumberOfEvaluations();
408 const ParameterList& parameters,
409 OptimizationListener* listener,
412 unsigned int tlEvalMax,
413 OutputStream* messageHandler,
414 OutputStream* profiler,
415 unsigned int verbose,
416 const std::string& optMethodDeriv)
419 AbstractNumericalDerivative* fun = 0;
422 MetaOptimizerInfos* desc =
new MetaOptimizerInfos();
425 fun =
new TwoPointsNumericalDerivative(cl);
426 fun->setInterval(0.0000001);
427 desc->addOptimizer(
"Branch length parameters",
new ConjugateGradientMultiDimensions(fun), cl->getBranchLengthsParameters().getParameterNames(), 2, MetaOptimizerInfos::IT_TYPE_FULL);
431 fun =
new ThreePointsNumericalDerivative(cl);
432 fun->setInterval(0.0001);
433 desc->addOptimizer(
"Branch length parameters",
new PseudoNewtonOptimizer(fun), cl->getBranchLengthsParameters().getParameterNames(), 2, MetaOptimizerInfos::IT_TYPE_FULL);
436 throw Exception(
"OptimizationTools::optimizeNumericalParametersWithGlobalClock. Unknown optimization method: " + optMethodDeriv);
439 ParameterList tmp = parameters.getCommonParametersWith(cl->getBranchLengthsParameters());
440 fun->setParametersToDerivate(tmp.getParameterNames());
442 ParameterList plsm = parameters.getCommonParametersWith(cl->getSubstitutionModelParameters());
443 if (plsm.size() < 10)
444 desc->addOptimizer(
"Substitution model parameter",
new SimpleMultiDimensions(cl), plsm.getParameterNames(), 0, MetaOptimizerInfos::IT_TYPE_STEP);
446 desc->addOptimizer(
"Substitution model parameters",
new DownhillSimplexMethod(cl), plsm.getParameterNames(), 0, MetaOptimizerInfos::IT_TYPE_FULL);
448 ParameterList plrd = parameters.getCommonParametersWith(cl->getRateDistributionParameters());
449 if (plrd.size() < 10)
450 desc->addOptimizer(
"Rate distribution parameter",
new SimpleMultiDimensions(cl), plrd.getParameterNames(), 0, MetaOptimizerInfos::IT_TYPE_STEP);
452 desc->addOptimizer(
"Rate dsitribution parameters",
new DownhillSimplexMethod(cl), plrd.getParameterNames(), 0, MetaOptimizerInfos::IT_TYPE_FULL);
454 MetaOptimizer optimizer(fun, desc, nstep);
455 optimizer.setVerbose(verbose);
456 optimizer.setProfiler(profiler);
457 optimizer.setMessageHandler(messageHandler);
458 optimizer.setMaximumNumberOfEvaluations(tlEvalMax);
459 optimizer.getStopCondition()->setTolerance(tolerance);
462 optimizer.setConstraintPolicy(AutoParameter::CONSTRAINTS_AUTO);
464 optimizer.addOptimizationListener(listener);
465 optimizer.init(parameters);
466 optimizer.optimize();
468 ApplicationTools::displayMessage(
"\n");
471 return optimizer.getNumberOfEvaluations();
478 const ParameterList& parameters,
479 OptimizationListener* listener,
481 unsigned int tlEvalMax,
482 OutputStream* messageHandler,
483 OutputStream* profiler,
484 unsigned int verbose,
485 const std::string& optMethodDeriv)
488 AbstractNumericalDerivative* fun = 0;
491 Optimizer* optimizer = 0;
494 fun =
new TwoPointsNumericalDerivative(cl);
495 fun->setInterval(0.0000001);
496 optimizer =
new ConjugateGradientMultiDimensions(fun);
500 fun =
new ThreePointsNumericalDerivative(cl);
501 fun->setInterval(0.0001);
505 throw Exception(
"OptimizationTools::optimizeBranchLengthsParameters. Unknown optimization method: " + optMethodDeriv);
508 ParameterList tmp = parameters.getCommonParametersWith(cl->getParameters());
509 fun->setParametersToDerivate(tmp.getParameterNames());
511 optimizer->setVerbose(verbose);
512 optimizer->setProfiler(profiler);
513 optimizer->setMessageHandler(messageHandler);
514 optimizer->setMaximumNumberOfEvaluations(tlEvalMax);
515 optimizer->getStopCondition()->setTolerance(tolerance);
518 optimizer->setConstraintPolicy(AutoParameter::CONSTRAINTS_AUTO);
520 optimizer->addOptimizationListener(listener);
521 optimizer->init(parameters);
522 optimizer->optimize();
524 ApplicationTools::displayMessage(
"\n");
527 unsigned int n = optimizer->getNumberOfEvaluations();
539 if (optimizeCounter_ == optimizeNumerical_)
542 parameters_.matchParametersValues(likelihood->getParameters());
543 OptimizationTools::optimizeNumericalParameters(likelihood, parameters_, 0, nStep_, tolerance_, 1000000, messenger_, profiler_, reparametrization_, verbose_, optMethod_);
544 optimizeCounter_ = 0;
553 if (optimizeCounter_ == optimizeNumerical_)
556 parameters_.matchParametersValues(likelihood->getParameters());
557 OptimizationTools::optimizeNumericalParameters2(likelihood, parameters_, 0, tolerance_, 1000000, messenger_, profiler_, reparametrization_,
false, verbose_, optMethod_);
558 optimizeCounter_ = 0;
566 const ParameterList& parameters,
567 bool optimizeNumFirst,
570 unsigned int tlEvalMax,
571 unsigned int numStep,
572 OutputStream* messageHandler,
573 OutputStream* profiler,
574 bool reparametrization,
575 unsigned int verbose,
576 const std::string& optMethodDeriv,
578 const std::string& nniMethod)
582 if (optimizeNumFirst)
584 OptimizationTools::optimizeNumericalParameters(tl, parameters, NULL, nStep, tolBefore, 1000000, messageHandler, profiler, reparametrization, verbose, optMethodDeriv);
599 const ParameterList& parameters,
600 bool optimizeNumFirst,
603 unsigned int tlEvalMax,
604 unsigned int numStep,
605 OutputStream* messageHandler,
606 OutputStream* profiler,
607 bool reparametrization,
608 unsigned int verbose,
609 const std::string& optMethodDeriv,
610 const std::string& nniMethod)
614 if (optimizeNumFirst)
616 OptimizationTools::optimizeNumericalParameters2(tl, parameters, NULL, tolBefore, 1000000, messageHandler, profiler, reparametrization,
false, verbose, optMethodDeriv);
631 unsigned int verbose)
649 const ParameterList& parametersToIgnore,
650 const std::string& param,
651 unsigned int verbose)
throw (Exception)
654 throw Exception(
"OptimizationTools::estimateDistanceMatrix. Invalid option param=" + param +
".");
655 estimationMethod.resetAdditionalParameters();
656 estimationMethod.setVerbose(verbose);
659 ParameterList tmp = estimationMethod.getSubstitutionModel().getIndependentParameters();
660 tmp.addParameters(estimationMethod.getRateDistribution().getIndependentParameters());
661 tmp.deleteParameters(parametersToIgnore.getParameterNames());
662 estimationMethod.setAdditionalParameters(tmp);
666 ApplicationTools::displayTask(
"Estimating distance matrix",
true);
667 estimationMethod.computeMatrix();
668 auto_ptr<DistanceMatrix> matrix(estimationMethod.getMatrix());
670 ApplicationTools::displayTaskDone();
672 return matrix.release();
680 const ParameterList& parametersToIgnore,
682 const std::string& param,
684 unsigned int tlEvalMax,
685 OutputStream* profiler,
686 OutputStream* messenger,
687 unsigned int verbose)
throw (Exception)
689 estimationMethod.resetAdditionalParameters();
690 estimationMethod.setVerbose(verbose);
693 ParameterList tmp = estimationMethod.getSubstitutionModel().getIndependentParameters();
694 tmp.addParameters(estimationMethod.getRateDistribution().getIndependentParameters());
695 tmp.deleteParameters(parametersToIgnore.getParameterNames());
696 estimationMethod.setAdditionalParameters(tmp);
705 ApplicationTools::displayTask(
"Estimating distance matrix",
true);
706 estimationMethod.computeMatrix();
707 DistanceMatrix* matrix = estimationMethod.getMatrix();
709 ApplicationTools::displayTaskDone();
713 ApplicationTools::displayTask(
"Building tree");
714 reconstructionMethod.setDistanceMatrix(*matrix);
715 reconstructionMethod.computeTree();
720 ApplicationTools::displayTaskDone();
721 if (previousTree && verbose > 0)
724 ApplicationTools::displayResult(
"Topo. distance with previous iteration", TextTools::toString(rf));
732 auto_ptr<SubstitutionModel> model(estimationMethod.getSubstitutionModel().clone());
733 auto_ptr<DiscreteDistribution> rdist(estimationMethod.getRateDistribution().clone());
735 *estimationMethod.getData(),
740 ParameterList parameters = tl.getParameters();
743 vector<string> vs = tl.getBranchLengthsParameters().getParameterNames();
744 parameters.deleteParameters(vs);
746 parameters.deleteParameters(parametersToIgnore.getParameterNames());
750 ParameterList tmp = tl.getSubstitutionModelParameters();
751 for (
unsigned int i = 0; i < tmp.size(); i++)
753 ApplicationTools::displayResult(tmp[i].getName(), TextTools::toString(tmp[i].getValue()));
755 tmp = tl.getRateDistributionParameters();
756 for (
unsigned int i = 0; i < tmp.size(); i++)
758 ApplicationTools::displayResult(tmp[i].getName(), TextTools::toString(tmp[i].getValue()));
void setNumericalOptimizationCounter(unsigned int c)
Listener used internally by the optimizeTreeNNI2 method.
void search()
Performs the search.
virtual ParameterList getBranchLengthsParameters() const =0
Get the branch lengths parameters.
The TreeLikelihood interface.
A listener which capture NaN function values and throw an exception in case this happens.
Double recursive implementation of interface TreeParsimonyScore.
ParameterList getHeightParameters() const
Interface for Nearest Neighbor Interchanges algorithms.
The phylogenetic tree class.
This class adds support for NNI topology estimation to the DRHomogeneousTreeLikelihood class...
Interface for agglomerative distance methods.
void initialize()
Init the likelihood object.
void addTopologyListener(TopologyListener *listener)
Add a listener to the list.
Interface for rate across sites (RAS) implementation.
This class implements the likelihood computation for a tree using the double-recursive algorithm...
void setNumericalOptimizationCounter(unsigned int c)
Interface for likelihood computation with a global clock and rate across sites variation.
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.
NNISearchable * getSearchableObject()
void topologyChangeSuccessful(const TopologyChangeEvent &event)
Tell that a topology change is definitive.
static const std::string PHYML
void topologyChangeSuccessful(const TopologyChangeEvent &event)
Tell that a topology change is definitive.
Estimate a distance matrix from sequence data, according to a given model.
Listener used internally by the optimizeTreeNNI method.
NNI topology search method.