polars for type ease, more done
Some checks failed
/ Test-Suite (push) Failing after 1m18s

This commit is contained in:
Rowan Torbitzky-Lane 2025-04-28 01:00:49 -05:00
parent be4bf37a35
commit 9c9126b3a8
5 changed files with 91 additions and 15 deletions

View File

@ -8,3 +8,4 @@ rand = "0.9.1"
paste = "1.0.15" paste = "1.0.15"
rust_decimal = { version = "1.37", features = ["macros", "maths"] } rust_decimal = { version = "1.37", features = ["macros", "maths"] }
rush_macro = { path = "rush_macro" } rush_macro = { path = "rush_macro" }
polars = { version = "0.46.0", features = ["lazy"]}

37
src/gp/args.rs Normal file
View File

@ -0,0 +1,37 @@
use crate::gp::variation::Variation;
use crate::push::state::Gene;
use polars::prelude::*;
use rust_decimal::prelude::*;
use std::collections::HashMap;
pub enum ClosingType {
Specified,
Balanced,
None,
}
struct PushArgs {
alignment_deviation: usize, // For alternation, std dev of deviation of index when alternating
alternation_rate: Decimal, // For alternation, prob of switching parents at each location
closes: ClosingType, // How push should automatically place Gene::Close into a plushy
dont_end: bool, // If true, keep running until limit regardless of success
// downsample: bool, // Whether or not to downsample. TODO later with all the related args
elitism: bool, // Whether to always add the best individual to next generation
error_function: fn(&PushArgs, DataFrame, Vec<Gene>) -> Series, // The error function
instructions: Vec<Gene>, // Instructions to use in a run
max_init_plushy_size: usize, // max initial plushy size
max_generations: usize, // Max amount of generations
parent_selection: usize, // Selection to use, TODO change this later.
pop_size: usize, // Population size
replacement_rate: Decimal, // For uniform replacement, rate items replaced
use_simplification: bool, // Whether to use simplification at end of run
simplification_k: usize, // Max amt of genes to attempt removal during one round of simplification process
simplification_steps: usize, // How many attempts to find simplified genomes
use_single_thread: bool, // if true, only single threaded
step_limit: usize, // Amount of steps a push interpreter can run for
testing_data: DataFrame, // The testing data, must be formatted the same as training data
tournament_size: usize, // Tournament size for tournament selection
training_data: DataFrame, // The training data, must be formatted the same as testing data
umad_rate: Decimal, // addition rate (deletion rate derived) for UMAD
variation: HashMap<Variation, Decimal>, // genetic operators and probability for use. should sum to 1,
}

View File

@ -103,26 +103,26 @@ pub fn plushy_to_push(genes: Vec<Gene>) -> Vec<Gene> {
if plushy_buffer.is_empty() && has_openers(&push_buffer) { if plushy_buffer.is_empty() && has_openers(&push_buffer) {
plushy_buffer.push(Gene::Close); plushy_buffer.push(Gene::Close);
} else if plushy_buffer.is_empty() { } else if plushy_buffer.is_empty() {
return plushy_buffer; return push_buffer;
} else { } else {
let first_gene = plushy_buffer.pop().unwrap(); let first_gene = plushy_buffer.remove(0);
match &first_gene { match &first_gene {
Gene::Close => { Gene::Close => {
if has_openers(&push_buffer) { if has_openers(&push_buffer) {
let mut index: Option<usize> = None; let mut index: Option<usize> = None;
let mut opener: Option<&Gene> = None; let mut opener: Option<Gene> = None;
// not the most optimal iterating through the entire genome. // not the most optimal iterating through the entire genome.
// Will do for now. // Will do for now.
for (ndx, el) in push_buffer.iter().enumerate() { for (ndx, el) in push_buffer.clone().into_iter().enumerate() {
if is_opener(&el) { if is_opener(&el) {
index = Some(ndx); index = Some(ndx);
opener = Some(el); opener = Some(el);
} }
} }
let post_open = push_buffer[(index.unwrap() + 1)..].to_vec(); let post_open: Vec<_> = push_buffer.drain((index.unwrap() + 1)..).collect();
let mut push_buffer = push_buffer[..index.unwrap()].to_vec(); push_buffer.pop(); // Pop the close here
push_buffer.extend(post_open); push_buffer.push(Gene::Block(post_open));
if get_opener_count(opener.unwrap()) > &1u8 { if get_opener_count(&opener.clone().unwrap()) > &1u8 {
let opener_new = dec_opener(opener.unwrap().clone()); let opener_new = dec_opener(opener.unwrap().clone());
push_buffer.push(opener_new); push_buffer.push(opener_new);
} }
@ -136,7 +136,7 @@ pub fn plushy_to_push(genes: Vec<Gene>) -> Vec<Gene> {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
// use super::*; use super::*;
// use crate::instructions::vector::{string_iterate, vector_float_maximum}; // use crate::instructions::vector::{string_iterate, vector_float_maximum};
use crate::instructions::common::*; use crate::instructions::common::*;
use crate::instructions::numeric::*; use crate::instructions::numeric::*;
@ -156,13 +156,39 @@ mod tests {
#[test] #[test]
fn plushy_to_push_test() { fn plushy_to_push_test() {
let plushy = vec![ let plushy = vec![
Gene::StateFunc(float_flush), Gene::StateFunc(exec_swap),
Gene::StateFunc(exec_pop), Gene::StateFunc(float_tan),
Gene::StateFunc(int_add), Gene::StateFunc(int_pop),
Gene::StateFunc(float_rem),
Gene::Close, Gene::Close,
Gene::StateFunc(float_sub), Gene::StateFunc(exec_flush),
Gene::Close,
Gene::StateFunc(boolean_pop),
];
let res_push = plushy_to_push(plushy);
assert_eq!(
res_push,
vec![
StateFunc(exec_swap),
Gene::Block(vec![Gene::StateFunc(float_tan), Gene::StateFunc(int_pop)]),
Gene::Block(vec![Gene::StateFunc(exec_flush)]),
Gene::StateFunc(boolean_pop),
]
);
let plushy = vec![
Gene::StateFunc(exec_swap),
Gene::StateFunc(float_tan),
Gene::StateFunc(int_pop),
Gene::Close, Gene::Close,
]; ];
let res_push = plushy_to_push(plushy);
assert_eq!(
res_push,
vec![
StateFunc(exec_swap),
Gene::Block(vec![Gene::StateFunc(float_tan), Gene::StateFunc(int_pop)]),
Gene::Block(vec![]),
]
)
} }
} }

View File

@ -1 +1,5 @@
pub mod args;
pub mod genome; pub mod genome;
pub mod variation;
// pub fn gp_loop

8
src/gp/variation.rs Normal file
View File

@ -0,0 +1,8 @@
pub enum Variation {
Crossover,
Alternation,
TailAlignedCrossover,
UniformAddition,
UniformReplacement,
UniformDeletion,
}