1 /*
2 
3 PHYML :  a program that  computes maximum likelihood  phylogenies from
4 DNA or AA homologous sequences
5 
6 Copyright (C) Stephane Guindon. Oct 2003 onward
7 
8 All parts of  the source except where indicated  are distributed under
9 the GNU public licence.  See http://www.opensource.org for details.
10 
11 */
12 
13 #include <config.h>
14 
15 #ifndef OPTIMIZ_H
16 #define OPTIMIZ_H
17 
18 #include "utilities.h"
19 #include "lk.h"
20 #include "free.h"
21 #include "models.h"
22 #include "mg.h"
23 #include "tiporder.h"
24 
25 
26 void Optimize_Br_Len_Serie(int n_max_iter, t_tree *tree);
27 void      Optimiz_Ext_Br(t_tree *tree);
28 void      Optimize_Param_Parall(t_tree *tree);
29 phydbl    Optimize_Branch_Quad(t_tree *tree, calign *cdata, t_edge *b_fcus);
30 void      Optimize_After_Hide(t_tree *tree, calign *cdata, t_node *h);
31 void      Round_Optimize(t_tree *tree, int n_round_max);
32 int       Dist_Seq_Brak(phydbl *ax, phydbl *bx, phydbl *cx,
33 			phydbl *fa, phydbl *fb, phydbl *fc,
34 			calign *data, int num1, int num2, t_mod *mod);
35 phydbl    Dist_Seq_Brent(phydbl ax, phydbl bx, phydbl cx, phydbl tol,
36 			 phydbl *xmin, calign *data,
37 			 int num1, int num2, t_mod *mod);
38 phydbl    Kappa_Golden(phydbl ax, phydbl bx, phydbl cx, phydbl tol,
39 		       phydbl *xmin, t_tree *tree, calign *cdata);
40 phydbl    Lambda_Golden(phydbl ax, phydbl bx, phydbl cx, phydbl tol,
41 			phydbl *xmin, t_tree *tree, calign *cdata);
42 phydbl    Alpha_Golden_Br_Opt(phydbl ax, phydbl bx, phydbl cx, phydbl tol,
43 			      phydbl *xmin, t_tree *tree, calign *cdata,
44 			      int n_opt, phydbl *init_l);
45 phydbl    Alpha_Golden(phydbl ax, phydbl bx, phydbl cx, phydbl tol,phydbl *xmin,
46 		       t_tree *tree, calign *cdata);
47 phydbl    Br_Len_Golden(phydbl ax, phydbl bx, phydbl cx, phydbl tol,
48 			phydbl *xmin, t_edge *b_fcus, t_tree *tree);
49 phydbl Br_Len_Opt(phydbl *l, t_edge *b, t_tree *tree);
50 int       Br_Len_Brak(phydbl *ax, phydbl *bx, phydbl *cx,
51 		      phydbl *fa, phydbl *fb, phydbl *fc,
52 		      t_edge *b_fcus, t_tree *tree);
53 phydbl    Optimize_Path_Length(t_mod *mod, calign *cdata, t_edge *a,
54 			       int lra, t_edge *b, int lrb, phydbl i_len);
55 void      Optimize_Param_Serie(t_node *a, t_node *d, t_edge *b_fcus, t_tree *tree,
56 			       calign *cdata, int n_passes);
57 phydbl    Optimize_Dist(t_mod *mod, phydbl init, calign *twoseqs);
58 phydbl    Pinvar_Golden(phydbl ax, phydbl bx, phydbl cx, phydbl tol,
59 			phydbl *xmin, t_tree *tree, calign *cdata, int n_iter_max);
60 void      Optimize_Pinvar(t_tree *tree);
61 int       Lambda_Brak(phydbl *ax, phydbl *bx, phydbl *cx,
62 		      phydbl *fa, phydbl *fb, phydbl *fc,
63 		      t_tree *tree);
64 int       Kappa_Brak(phydbl *ax, phydbl *bx, phydbl *cx,
65 		      phydbl *fa, phydbl *fb, phydbl *fc,
66 		      t_tree *tree);
67 int       Alpha_Brak(phydbl *ax, phydbl *bx, phydbl *cx,
68 		      phydbl *fa, phydbl *fb, phydbl *fc,
69 		      t_tree *tree);
70 int       Pinvar_Brak(phydbl *ax, phydbl *bx, phydbl *cx,
71 		      phydbl *fa, phydbl *fb, phydbl *fc,
72 		      t_tree *tree);
73 void Optimiz_All_Free_Param(t_tree *tree, int verbose);
74 void      Optimiz_RRparam_GTR(t_tree *tree, int num_param);
75 phydbl    RRparam_GTR_Golden(phydbl ax, phydbl bx, phydbl cx, phydbl tol,
76 		   	     phydbl *xmin, t_tree *tree, calign *cdata, phydbl *param, int n_iter_max);
77 
78 int Powell_GTR_Param(t_tree *tree, phydbl *p, int n, phydbl ftol);
79 phydbl Linmin_GTR_Param(t_tree *tree,phydbl *p, phydbl *xi, int n);
80 phydbl F1dim(t_tree *tree, phydbl x, phydbl *p, phydbl *xi, phydbl n);
81 int Mnbrak_1dim(phydbl *ax, phydbl *bx, phydbl *cx,
82 		phydbl *fa, phydbl *fb, phydbl *fc,
83 		t_tree *tree,
84 		phydbl *p,  phydbl *xi, phydbl n);
85 phydbl Brent_1dim(phydbl ax, phydbl bx, phydbl cx,
86 		  phydbl tol, phydbl *xmin,
87 		  t_tree *tree,
88 		  phydbl *p, phydbl *xi, phydbl n);
89 
90 int Min_With_Derivatives(t_tree *tree, phydbl *p, int n, phydbl ftol, phydbl step_size,
91 			 phydbl (*func) (), void (*dfunc)(), phydbl (*linmin)());
92 void BFGS(t_tree *tree,
93 	  phydbl *p,
94 	  int n,
95 	  phydbl gtol,
96           phydbl difff,
97 	  phydbl step_size,
98           int logt,
99           int is_positive,
100 	  phydbl(*func)(t_tree *tree),
101 	  int(*dfunc)(t_tree *tree,phydbl *param,int n_param,phydbl stepsize,int logt,phydbl(*func)(t_tree *tree),phydbl *derivatives, int is_positive),
102 	  int(*lnsrch)(t_tree *tree, int n, phydbl *xold, phydbl fold,phydbl *g, phydbl *p, phydbl *x,phydbl *f, phydbl stpmax, int *check, int logt, int is_positive),
103 	  int *failed);
104 
105 void BFGS_Nonaligned(t_tree *tree,
106                      phydbl **p,
107                      int n,
108                      phydbl gtol,
109                      phydbl difff,
110                      phydbl step_size,
111                      int logt,
112                      int is_positive,
113                      phydbl(*func)(t_tree *tree),
114                      int(*dfunc_nonaligned)(t_tree *tree,phydbl **param,int n_param,phydbl stepsize,int logt,phydbl(*func)(t_tree *tree),phydbl *derivatives, int is_positive),
115                      int(*lnsrch_nonaligned)(t_tree *tree, int n, phydbl **xold, phydbl fold,phydbl *g, phydbl *p, phydbl *x,phydbl *f, phydbl stpmax, int *check, int logt, int is_positive),
116                      int *failed);
117 
118 
119 void Optimize_Single_Param_Generic(t_tree *tree, phydbl *param, phydbl lim_inf, phydbl lim_sup, phydbl tol, int n_max_iter, int quickdirty);
120 int Generic_Brak_Lk(phydbl *param,
121                     phydbl *ax, phydbl *bx, phydbl *cx,
122                     phydbl *fa, phydbl *fb, phydbl *fc,
123                     phydbl min, phydbl max,
124                     phydbl (*obj_func)(t_edge *,t_tree *,supert_tree *),
125                     t_edge *branch, t_tree *tree, supert_tree *stree);
126 int Generic_Brak(phydbl *param,
127 		 phydbl *ax, phydbl *bx, phydbl *cx,
128 		 phydbl *fa, phydbl *fb, phydbl *fc,
129 		 phydbl lim_inf, phydbl lim_sup,
130 		 t_tree *tree);
131 void Optimize_Br_Len_Serie_Post(t_node *a, t_node *d, t_edge *b_fcus, t_tree *tree);
132 void Optimize_Global_Rate(t_tree *tree);
133 phydbl Fast_Br_Len(t_edge *b, t_tree *tree, int approx);
134 
135 void EM_Dist(t_mod *mod, calign *data);
136 phydbl Dist_F_Brent(phydbl ax, phydbl bx, phydbl cx, phydbl tol, int n_iter_max,
137 		    phydbl *param, phydbl *F, t_mod *mod);
138 int Dist_F_Brak(phydbl *ax, phydbl *bx, phydbl *cx, phydbl *F, phydbl *param, t_mod *mod);
139 void Opt_Dist_F(phydbl *dist, phydbl *F, t_mod *mod);
140 phydbl Missing_Dist_Brent(phydbl ax, phydbl bx, phydbl cx, phydbl tol, int n_iter_max,
141 			  int x, int y, matrix *mat);
142 int Missing_Dist_Brak(phydbl *ax, phydbl *bx, phydbl *cx, int x, int y, matrix *mat);
143 void Opt_Missing_Dist(int x, int y, matrix *mat);
144 int Optimiz_Alpha_And_Pinv(t_tree *tree, int verbose);
145 phydbl Node_Time_Brent(phydbl ax, phydbl bx, phydbl cx, phydbl tol,
146 		       t_node *anc, t_node *des, t_tree *tree, int n_iter_max);
147 phydbl Time_Stamps_Mult_Brent(phydbl ax, phydbl bx, phydbl cx, phydbl tol,
148 			      t_tree *tree, int n_iter_max);
149 phydbl Branch_Rate_Shape_Brent(phydbl ax, phydbl bx, phydbl cx, phydbl tol,
150 			       phydbl *xmin, t_tree *tree, int n_iter_max);
151 phydbl Node_Time_Brent_Fixed_Br_Len(phydbl ax, phydbl bx, phydbl cx, phydbl tol,
152 				    t_node *n, t_tree *tree, int n_iter_max);
153 
154 int Generic_Brent_Lk(phydbl *param, phydbl ax, phydbl cx, phydbl tol,
155                      int n_iter_max, int quickdirty,
156                      phydbl (*obj_func)(t_edge *,t_tree *,supert_tree *),
157                      t_edge *branch, t_tree *tree, supert_tree *stree, int logt);
158 void Round_Optimize_Node_Heights(t_tree *tree);
159 void Opt_Node_Heights_Recurr_Pre(t_node *a, t_node *d, t_tree *tree);
160 void Opt_Node_Heights_Recurr(t_tree *tree);
161 
162 int Lnsrch(t_tree *tree, int n, phydbl *xold, phydbl fold, phydbl *g, phydbl *p, phydbl *x,
163 	   phydbl *f, phydbl stpmax, int *check, int logt, int is_positive);
164 
165 int Lnsrch_Nonaligned(t_tree *tree, int n, phydbl **xold, phydbl fold, phydbl *g, phydbl *p, phydbl *x,
166                       phydbl *f, phydbl stpmax, int *check, int logt, int is_positive);
167 
168 void Optimize_RR_Params(t_tree *mixt_tree, int verbose);
169 void Optimize_TsTv(t_tree *mixt_tree, int verbose);
170 void Optimize_Lambda(t_tree *mixt_tree, int verbose);
171 void Optimize_Alpha(t_tree *mixt_tree, int verbose);
172 void Optimize_Pinv(t_tree *mixt_tree, int verbose);
173 void Optimize_State_Freqs(t_tree *mixt_tree, int verbose);
174 void Optimize_Rmat_Weights(t_tree *mixt_tree, int verbose);
175 void Optimize_Efrq_Weights(t_tree *mixt_tree, int verbose);
176 void Optimize_Free_Rate(t_tree *mixt_tree, int verbose);
177 void Optimize_Free_Rate_Weights(t_tree *tree, int fast, int verbose);
178 void Optimize_Free_Rate_Rr(t_tree *tree, int fast, int verbose);
179 void Optimize_Br_Len_Multiplier(t_tree *tree, int verbose);
180 phydbl Generic_Brent(phydbl *param, phydbl ax, phydbl cx, phydbl tol,int n_iter_max,phydbl (*obj_func)(t_tree *),t_tree *tree);
181 void Least_Square_Node_Ages(t_tree *tree);
182 
183 #endif
184 
185