42 std::shared_ptr<PhyloLikelihoodInterface> lik,
43 const std::map<std::string, std::string>& params,
44 const std::string& suffix,
45 bool suffixIsOptional,
55 reparametrization(false),
58 optMethodDeriv(OPTIMIZATION_NEWTON),
59 optMethodModel(OPTIMIZATION_BRENT)
64 map<string, string> optArgs;
77 nstep = ApplicationTools::getParameter<unsigned int>(
"nstep", optArgs, 1,
"",
true, warn + 1);
82 verbose = ApplicationTools::getParameter<unsigned int>(
"optimization.verbose", params, 2, suffix, suffixIsOptional, warn + 1);
88 (mhPath ==
"none") ?
nullptr :
90 make_shared<StlOutputStream>(make_unique<ofstream>(mhPath.c_str(), ios::out));
97 (prPath ==
"none") ?
nullptr :
99 make_shared<StlOutputStream>(make_unique<ofstream>(prPath.c_str(), ios::out));
118 if (params.find(
"optimization.ignore_parameter") != params.end())
119 throw Exception(
"optimization.ignore_parameter is deprecated, use optimization.ignore_parameters instead!");
128 if (param ==
"BrLen")
130 vector<string> vs = lik->getBranchLengthParameters().getParameterNames();
135 else if (param ==
"Ancient")
137 vector<string> vs = lik->getRootFrequenciesParameters().getParameterNames();
142 else if (param ==
"Model")
144 vector<string> vs = lik->getSubstitutionModelParameters().getParameterNames();
149 else if (param ==
"*")
155 else if (param.find(
"*") != string::npos)
158 bool verbhere = verb;
193 if (params.find(
"optimization.constrain_parameter") != params.end())
194 throw Exception(
"optimization.constrain_parameter is deprecated, use optimization.constrain_parameters instead!");
198 string constraint =
"";
199 string pc, param =
"";
207 string::size_type index = pc.find(
"=");
208 if (index == string::npos)
209 throw Exception(
"PhylogeneticsApplicationTools::optimizeParamaters. Bad constrain syntax, should contain `=' symbol: " + pc);
210 param = pc.substr(0, index);
211 constraint = pc.substr(index + 1);
214 vector<string> parNames2;
216 if (param ==
"BrLen")
217 parNames2 = lik->getBranchLengthParameters().getParameterNames();
218 else if (param ==
"Ancient")
219 parNames2 = lik->getRootFrequenciesParameters().getParameterNames();
220 else if (param ==
"Model")
222 vector<string> vs = lik->getSubstitutionModelParameters().getParameterNames();
224 else if (param.find(
"*") != string::npos)
227 parNames2.push_back(param);
230 for (
size_t i = 0; i < parNames2.size(); i++)
252 throw Exception(
"Parameter '" + param +
"' does not fit the constraint " + constraint);
256 nbEvalMax = ApplicationTools::getParameter<unsigned int>(
"optimization.max_number_f_eval", params, 1000000, suffix, suffixIsOptional, warn + 1);
277 for (
size_t l = 1; l < lines.size(); ++l)
284 cerr <<
"Corrupted backup file!!!" << endl;
285 cerr <<
"at line " << l <<
": " << lines[l] << endl;
300 lik->setParameters(pl);
301 if (
convert(
abs(lik->getValue() - fval)) > 0.000001)
308 if (order ==
"Gradient")
312 else if (order ==
"Newton")
316 else if (order ==
"BFGS")
321 throw Exception(
"Unknown derivatives algorithm: '" + order +
"'.");
335 if (clock !=
"None" && clock !=
"Global")
336 throw Exception(
"Molecular clock option not recognized, should be one of 'Global' or 'None'.");
354 std::shared_ptr<PhyloLikelihoodInterface> lik,
357 shared_ptr<SecondOrderDerivable> f = lik;
363 f = make_shared<ReparametrizationDerivableSecondOrderWrapper>(f, optopt.
parameters);
374 auto desc = make_unique<MetaOptimizerInfos>();
375 unique_ptr<MetaOptimizer> poptimizer;
378 desc->addOptimizer(
"Branch length parameters", make_shared<ConjugateGradientMultiDimensions>(f), lik->getBranchLengthParameters().getParameterNames(), 2,
MetaOptimizerInfos::IT_TYPE_FULL);
380 desc->addOptimizer(
"Branch length parameters", make_shared<PseudoNewtonOptimizer>(f), lik->getBranchLengthParameters().getParameterNames(), 2,
MetaOptimizerInfos::IT_TYPE_FULL);
382 desc->addOptimizer(
"Branch length parameters", make_shared<BfgsMultiDimensions>(f), lik->getBranchLengthParameters().getParameterNames(), 2,
MetaOptimizerInfos::IT_TYPE_FULL);
384 throw Exception(
"OptimizationTools::optimizeNumericalParameters. Unknown derivative optimization method: " + optopt.
optMethodDeriv);
399 poptimizer = make_unique<MetaOptimizer>(f, std::move(desc), optopt.
nstep);
403 vector<string> vNameDer;
404 auto fnum = make_shared<ThreePointsNumericalDerivative>(f);
413 vNameDer.insert(vNameDer.begin(), vNameDer2.begin(), vNameDer2.end());
414 fnum->setParametersToDerivate(vNameDer);
417 poptimizer = make_unique<MetaOptimizer>(fnum, std::move(desc), optopt.
nstep);
420 throw Exception(
"OptimizationTools::optimizeNumericalParameters. Unknown optimization method: " + optopt.
optMethodModel);
422 poptimizer->setVerbose(optopt.
verbose);
423 poptimizer->setProfiler(optopt.
profiler);
424 poptimizer->setMessageHandler(optopt.
messenger);
425 poptimizer->setMaximumNumberOfEvaluations(optopt.
nbEvalMax);
426 poptimizer->getStopCondition()->setTolerance(optopt.
tolerance);
430 auto nanListener = make_shared<NaNListener>(poptimizer.get(), lik.get());
431 poptimizer->addOptimizationListener(nanListener);
433 poptimizer->addOptimizationListener(optopt.
listener);
435 poptimizer->init(pl);
436 poptimizer->optimize();
444 uint nb = poptimizer->getNumberOfEvaluations();
452 std::shared_ptr<PhyloLikelihoodInterface> lik,
455 shared_ptr<SecondOrderDerivable> f = lik;
473 shared_ptr<SecondOrderDerivable> frep;
484 unique_ptr<OptimizerInterface> optimizer;
485 shared_ptr<AbstractNumericalDerivative> fnum;
489 fnum = make_shared<TwoPointsNumericalDerivative>(dynamic_pointer_cast<FirstOrderDerivable>(f));
490 fnum->setInterval(0.0000001);
491 optimizer = make_unique<ConjugateGradientMultiDimensions>(fnum);
495 fnum = make_shared<ThreePointsNumericalDerivative>(f);
496 fnum->setInterval(0.0001);
497 optimizer = make_unique<PseudoNewtonOptimizer>(fnum);
501 fnum = make_shared<TwoPointsNumericalDerivative>(dynamic_pointer_cast<FirstOrderDerivable>(f));
502 fnum->setInterval(0.0001);
503 optimizer = make_unique<BfgsMultiDimensions>(fnum);
506 throw Exception(
"OptimizationTools::optimizeNumericalParameters2. Unknown optimization method: " + optopt.
optMethodDeriv);
517 optimizer->setVerbose(optopt.
verbose);
518 optimizer->setProfiler(optopt.
profiler);
519 optimizer->setMessageHandler(optopt.
messenger);
520 optimizer->setMaximumNumberOfEvaluations(optopt.
nbEvalMax);
521 optimizer->getStopCondition()->setTolerance(optopt.
tolerance);
525 auto nanListener = make_shared<NaNListener>(optimizer.get(), lik.get());
526 optimizer->addOptimizationListener(nanListener);
528 optimizer->addOptimizationListener(optopt.
listener);
531 optimizer->optimize();
537 return optimizer->getNumberOfEvaluations();
543 std::shared_ptr<SingleProcessPhyloLikelihood> lik,
546 shared_ptr<SecondOrderDerivable> f = lik;
551 f = make_shared<ReparametrizationDerivableSecondOrderWrapper>(f, optopt.
parameters);
558 shared_ptr<AbstractNumericalDerivative> fnum;
559 unique_ptr<OptimizerInterface> optimizer;
565 fnum = make_shared<TwoPointsNumericalDerivative>(dynamic_pointer_cast<FirstOrderDerivable>(f));
566 fnum->setInterval(0.0000001);
572 fnum = make_shared<ThreePointsNumericalDerivative>(f);
573 fnum->setInterval(0.0001);
579 fnum = make_shared<TwoPointsNumericalDerivative>(dynamic_pointer_cast<FirstOrderDerivable>(f));
580 fnum->setInterval(0.0001);
584 throw Exception(
"OptimizationTools::optimizeNumericalParameters2. Unknown optimization method: " + optopt.
optMethodDeriv);
592 optimizer->setVerbose(optopt.
verbose);
593 optimizer->setProfiler(optopt.
profiler);
594 optimizer->setMessageHandler(optopt.
messenger);
595 optimizer->setMaximumNumberOfEvaluations(optopt.
nbEvalMax);
596 optimizer->getStopCondition()->setTolerance(optopt.
tolerance);
600 auto nanListener = make_shared<NaNListener>(optimizer.get(), lik.get());
601 optimizer->addOptimizationListener(nanListener);
603 optimizer->addOptimizationListener(optopt.
listener);
606 optimizer->optimize();
612 return optimizer->getNumberOfEvaluations();
627 const std::string& param,
628 unsigned int verbose)
631 throw Exception(
"OptimizationTools::estimateDistanceMatrix. Invalid option param=" + param +
".");
646 auto matrix = estimationMethod.
getMatrix();
659 const std::string& param,
674 unique_ptr<TreeTemplate<Node>> tree =
nullptr;
678 auto process = std::shared_ptr<SubstitutionProcessInterface>(estimationMethod.
process().
clone());
679 auto autoProc = dynamic_pointer_cast<AutonomousSubstitutionProcessInterface>(process);
680 auto procMb = dynamic_pointer_cast<SubstitutionProcessCollectionMember>(process);
681 if (!autoProc && !procMb)
682 throw Exception(
"OptimizationTools::buildDistanceTree : unknown process type. Ask developpers.");
685 std::vector<unique_ptr<TreeTemplate<Node>>> vTree;
686 std::vector<double> vLik;
689 while (test && nstep<100)
696 auto matrix = estimationMethod.
getMatrix();
702 if (matrix->size() == 2)
706 Node* n2 =
new Node(1, matrix->getName(0));
708 Node* n3 =
new Node(2, matrix->getName(1));
721 tree = make_unique<TreeTemplate<Node>>(reconstructionMethod.
tree());
723 vTree.push_back(std::move(tree));
728 size_t nbTree = vTree.size();
729 const auto& ltree = vTree[nbTree-1];
733 for (
size_t iT = 0; iT < nbTree - 1; iT++)
735 const auto& pTree = vTree[iT];
753 autoProc->setPhyloTree(*phyloTree);
757 auto& coll = procMb->collection();
758 size_t maxTNb = procMb->getTreeNumber();
759 coll.replaceTree(phyloTree, maxTNb);
762 auto lik = make_shared<LikelihoodCalculationSingleProcess>(context, estimationMethod.
getData(), process);
763 auto tl = make_shared<SingleProcessPhyloLikelihood>(context, lik);
765 vLik.push_back(tl->getValue());
771 process->matchParametersValues(tl->getParameters());
775 auto trtemp = std::make_shared<ParametrizablePhyloTree>(*tl->tree());
781 auto tmp = process->getSubstitutionModelParameters(
true);
782 for (
unsigned int i = 0; i < tmp.size(); ++i)
786 tmp = process->getRootFrequenciesParameters(
true);
787 for (
unsigned int i = 0; i < tmp.size(); ++i)
791 tmp = process->getRateDistributionParameters(
true);
792 for (
unsigned int i = 0; i < tmp.size(); ++i)
800 size_t posM =
static_cast<size_t>(std::distance(vLik.begin(), std::min_element(vLik.begin(), vLik.end())));
802 return std::move(vTree[posM]);
Interface for agglomerative distance methods.
static std::string CONSTRAINTS_AUTO
Context for dataflow node construction.
std::unique_ptr< DistanceMatrix > getMatrix() const
Get the distance matrix.
void resetAdditionalParameters()
Reset all additional parameters.
const SubstitutionProcessInterface & process() const
void computeMatrix()
Perform the distance computation.
bool matchParametersValues(const ParameterList ¶meters)
size_t getVerbose() const
void setVerbose(size_t verbose)
std::shared_ptr< const AlignmentDataInterface > getData() const
void setAdditionalParameters(const ParameterList ¶meters)
Specify a list of parameters to be estimated.
virtual void setDistanceMatrix(const DistanceMatrix &matrix)=0
Set the distance matrix to use.
virtual void computeTree()=0
Perform the clustering.
virtual const Tree & tree() const =0
The phylogenetic node class.
virtual void setDistanceToFather(double distance)
Set or update the distance toward the father node.
virtual void addSon(size_t pos, Node *node)
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 deleteParameter(const std::string &name)
virtual const Parameter & parameter(const std::string &name) const
virtual void deleteParameters(const std::vector< std::string > &names, bool mustExist=true)
virtual void setParameter(size_t index, const Parameter ¶m)
virtual size_t whichParameterHasName(const std::string &name) const
virtual ParameterList createSubList(const std::vector< std::string > &names) const
virtual std::string parameter() const
virtual std::shared_ptr< const ConstraintInterface > getConstraint() const
virtual void setConstraint(std::shared_ptr< ConstraintInterface > constraint)
virtual const std::string & getName() const
virtual bool hasConstraint() const
This Optimizer implements Newton's algorithm for finding a minimum of a function. This is in fact a m...
size_t numberOfRemainingTokens() const
const std::string & nextToken()
bool hasMoreToken() const
virtual ParameterList getSubstitutionModelParameters(bool independent) const =0
Methods to retrieve the parameters of specific objects.
virtual ParameterList getRootFrequenciesParameters(bool independent) const =0
virtual SubstitutionProcessInterface * clone() const =0
virtual ParameterList getRateDistributionParameters(bool independent) const =0
The phylogenetic tree class.
double toDouble(const std::string &s, char dec='.', char scientificNotation='e')
bool isEmpty(const std::string &s)
std::string toString(T t)
Defines the basic types of data flow nodes.
double convert(const bpp::ExtendedFloat &ef)
ExtendedFloat abs(const ExtendedFloat &ef)