Skip to content

Commit

Permalink
Added peturb vector MoveFn, put moves in their own file
Browse files Browse the repository at this point in the history
  • Loading branch information
jhellewell14 committed Oct 31, 2024
1 parent 9b82761 commit fe55757
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 144 deletions.
79 changes: 3 additions & 76 deletions src/genetic_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use needletail::parse_fastx_file;
use crate::topology::Topology;
use ndarray::s;
use logaddexp::LogAddExp;
use rand::Rng;
use crate::moves::MoveFn;

const NEGINF: f64 = -f64::INFINITY;
// (A, C, G, T)
Expand Down Expand Up @@ -153,63 +153,9 @@ pub fn base_freq_logse(muta: ndarray::ArrayBase<ndarray::ViewRepr<&f64>, ndarray
.fold(0.0, |tot, (muta, bf)| tot + muta.exp() * bf)
.ln()
}

// A move produces a vector of nodes that have a new parent node
// Now Likelihood update
// Iterate over nodes that need changing from Topology
// Calculate new likelihoods at internal nodes, add to HashMap
// Get whole Topology likelihood at root node from HashMap
// If better, can move HashMap likelihoods into GeneticData


// A TryTopology struct that contains old Topology, new tree_vec, changes, reference to gen_data?, HashMap?

pub struct CandidateTopology{
new_topology: Topology,
changes: Option<Vec<usize>>,
}

pub fn peturb_vector(topology: &Topology) -> CandidateTopology {

let mut vout = topology.tree_vec.to_vec();
let n = vout.len().div_ceil(5);
let mut rng = rand::thread_rng();
let ind_rng = rand::thread_rng();
let distr = rand::distributions::Bernoulli::new(0.5).unwrap();
let ind_distr = rand::distributions::Uniform::new(0, vout.len());

let mut inds: Vec<usize> = ind_rng.sample_iter(ind_distr).take(n).collect();
inds.sort();

for ind in inds {
if ind.eq(&0) {
continue;
}

match rng.sample(distr) {
true => {
if vout[ind].lt(&(2 * (ind - 1))) {
vout[ind] += 1;
}
}
false => {
if vout[ind].gt(&0) {
vout[ind] -= 1;
}
}
};
}

let new_topology: Topology = Topology::from_vec(&vout);
let changes: Option<Vec<usize>> = topology.find_changes(&new_topology);
CandidateTopology{
new_topology,
changes,
}
}

pub fn hillclimb_accept(old_ll: &f64, new_ll: &f64) -> bool {
new_ll.gt(old_ll)
pub new_topology: Topology,
pub changes: Option<Vec<usize>>,
}

impl Topology {
Expand Down Expand Up @@ -306,22 +252,3 @@ impl Topology {
}
}

pub trait MoveFn {
fn generate_move(&self, current_topology: &Topology) -> CandidateTopology;
}

pub struct ExactMove {
pub target_vector: Vec<usize>,
}

impl MoveFn for ExactMove {
fn generate_move(&self, current_topology: &Topology) -> CandidateTopology {

let new_topology: Topology = Topology::from_vec(&self.target_vector);
let changes: Option<Vec<usize>> = current_topology.find_changes(&new_topology);
CandidateTopology{
new_topology,
changes,
}
}
}
68 changes: 0 additions & 68 deletions src/hillclimb.rs

This file was deleted.

2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ mod iterators;
mod rate_matrix;
mod topology;
mod genetic_data;
mod moves;

use rate_matrix::RateMatrix;
use topology::Topology;
Expand All @@ -15,6 +16,7 @@ use crate::cli::*;
use std::env::args;
use std::time::Instant;
use crate::genetic_data::*;
use crate::moves::*;

pub fn main() {
let args = cli_args();
Expand Down
76 changes: 76 additions & 0 deletions src/moves.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
use crate::Topology;
use crate::CandidateTopology;
use rand::Rng;

pub trait MoveFn {
fn generate_move(&self, current_topology: &Topology) -> CandidateTopology;
}

pub struct ExactMove {
pub target_vector: Vec<usize>,
}

impl MoveFn for ExactMove {
fn generate_move(&self, current_topology: &Topology) -> CandidateTopology {

let new_topology: Topology = Topology::from_vec(&self.target_vector);
let changes: Option<Vec<usize>> = current_topology.find_changes(&new_topology);
CandidateTopology{
new_topology,
changes,
}
}
}

pub struct PeturbVec {
pub n: usize,
}

impl MoveFn for PeturbVec {
fn generate_move(&self, current_topology: &Topology) -> CandidateTopology {
let mut vout = current_topology.tree_vec.to_vec();
let mut rng = rand::thread_rng();
let ind_rng = rand::thread_rng();
let distr = rand::distributions::Bernoulli::new(0.5).unwrap();
let ind_distr = rand::distributions::Uniform::new(0, vout.len());

let samp_n: usize = match self.n.gt(&vout.len()) {
true => {vout.len()},
false => {self.n},
};

let mut inds: Vec<usize> = ind_rng.sample_iter(ind_distr).take(samp_n).collect();
inds.sort();

for ind in inds {
if ind.eq(&0) {
continue;
}

match rng.sample(distr) {
true => {
if vout[ind].lt(&(2 * (ind - 1))) {
vout[ind] += 1;
}
}
false => {
if vout[ind].gt(&0) {
vout[ind] -= 1;
}
}
};
};

let new_topology: Topology = Topology::from_vec(&vout);
let changes: Option<Vec<usize>> = current_topology.find_changes(&new_topology);
CandidateTopology{
new_topology,
changes,
}
}
}


pub fn hillclimb_accept(old_ll: &f64, new_ll: &f64) -> bool {
new_ll.gt(old_ll)
}

0 comments on commit fe55757

Please sign in to comment.