Skip to content

Commit

Permalink
More tidying and renaming
Browse files Browse the repository at this point in the history
  • Loading branch information
jhellewell14 committed May 1, 2024
1 parent 965b054 commit a0043d7
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 11 deletions.
4 changes: 2 additions & 2 deletions build.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
fn main() {
cxx_build::bridge("src/newick.rs")
cxx_build::bridge("src/build_tree.rs")
.file("src/phylo2vec.cpp")
.std("c++17")
.compile("phylo2vec");

println!("cargo:rerun-if-changed=src/newick.rs");
println!("cargo:rerun-if-changed=src/build_tree.rs");
println!("cargo:rerun-if-changed=src/phylo2vec.cpp");
println!("cargo:rerun-if-changed=src/phylo2vec.hpp");
}
18 changes: 13 additions & 5 deletions src/dspsa.rs → src/hillclimb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,24 +39,32 @@ impl Tree {
};

let mut candidate_vec: Vec<usize>;
let mut best_vec: Option<Vec<usize>> = None;
let mut best_likelihood: f64 = working_tree.get_tree_likelihood();
for k in 0..=iterations {

println!("Optimisation step {} out of {}", k, iterations);
// println!("Old vector {:?}", self.tree_vec);
println!("Tree log likelihood: {}", self.get_tree_likelihood());

candidate_vec = hill_peturb(&self.tree_vec, 28);
candidate_vec = hill_peturb(&self.tree_vec, self.tree_vec.len());
working_tree.update(&candidate_vec);
working_tree.update_likelihood(q);
let new_likelihood = working_tree.get_tree_likelihood();

// println!("New vector {:?}", candidate_vec);
println!("New likelihood {}", working_tree.get_tree_likelihood());
println!("New likelihood {}", new_likelihood);

if working_tree.get_tree_likelihood() > self.get_tree_likelihood() {
if new_likelihood > best_likelihood {
println!("Climbing hill!");
self.update(&working_tree.tree_vec);
self.update_likelihood(q);
best_vec = Some(working_tree.tree_vec.clone());
best_likelihood = new_likelihood;
}
};

if best_vec.is_some() {
self.update(&best_vec.unwrap());
self.update_likelihood(q);
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ mod node;
mod build_tree;
mod tests;
mod tree;
mod dspsa;
mod hillclimb;
mod tree_iterators;
mod tree_to_newick;

Expand Down Expand Up @@ -39,7 +39,7 @@ pub fn main() {
// let end = Instant::now();
tr.add_genetic_data(&args.alignment);

tr.update_likelihood_postorder(&q);
tr.initialise_likelihood(&q);

println!("{}", tr.get_tree_likelihood());
println!("{:?}", tr.newick());
Expand Down
3 changes: 2 additions & 1 deletion src/likelihoods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ impl Tree {

// Traverses tree below given node (except leaves), updating likelihood
// Used after initial tree constructions to fill in likelihood at all internal nodes
pub fn update_likelihood_postorder(&mut self, rate_matrix: &na::Matrix4<f64>) {
pub fn initialise_likelihood(&mut self, rate_matrix: &na::Matrix4<f64>) {
let nodes: Vec<usize> = self
.postorder_notips(self.get_root())
.map(|n| n.index)
Expand All @@ -72,6 +72,7 @@ impl Tree {
}
}

// Fetches likelihood value for a tree
pub fn get_tree_likelihood(&self) -> f64 {
self.mutation_lists
.get(self.get_root().unwrap().index)
Expand Down
2 changes: 1 addition & 1 deletion src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ mod tests {

tr.mutation_lists = genetic_data;

tr.update_likelihood_postorder(&q);
tr.initialise_likelihood(&q);

let old_likelihood = tr.get_tree_likelihood();

Expand Down

0 comments on commit a0043d7

Please sign in to comment.