diff --git a/src/gp/args.rs b/src/gp/args.rs index 10fbdfd..74ca9c0 100644 --- a/src/gp/args.rs +++ b/src/gp/args.rs @@ -22,8 +22,9 @@ pub struct PushArgs { pub elitism: bool, // Whether to always add the best individual to next generation pub error_function: Option) -> Vec>, // The error function pub instructions: Option>, // Instructions to use in a run - pub max_init_plushy_size: usize, // max initial plushy size pub max_generations: usize, // Max amount of generations + pub max_init_plushy_size: usize, // max initial plushy size + pub max_stack_size: usize, // max size a stack is allowed to reach during execution pub parent_selection: Selection, // Selection to use, TODO change this later. pub pop_size: usize, // Population size pub replacement_rate: f64, // For uniform replacement, rate items replaced @@ -55,8 +56,9 @@ impl PushArgs { elitism: false, error_function: None, instructions: None, - max_init_plushy_size: 100, max_generations: 1000, + max_init_plushy_size: 100, + max_stack_size: 100, parent_selection: Selection::Lexicase, pop_size: 1000, replacement_rate: 0.1, diff --git a/src/gp/mod.rs b/src/gp/mod.rs index 6a6616e..6d4e2e1 100644 --- a/src/gp/mod.rs +++ b/src/gp/mod.rs @@ -9,15 +9,15 @@ pub mod utils; pub mod variation; pub fn gp_loop(push_args: PushArgs) -> bool { - let pop_size = push_args.pop_size; - let max_gens = push_args.max_generations; - let error_func = push_args.error_function; - let solution_error_threshold = push_args.solution_error_threshold; - let dont_end = push_args.dont_end; - let elitism = push_args.elitism; - let training_data = push_args.training_data; - let testing_data = push_args.testing_data; - let simplification = push_args.use_simplification; + let _pop_size = push_args.pop_size; + let _max_gens = push_args.max_generations; + let _error_func = push_args.error_function; + let _solution_error_threshold = push_args.solution_error_threshold; + let _dont_end = push_args.dont_end; + let _elitism = push_args.elitism; + let _training_data = push_args.training_data; + let _testing_data = push_args.testing_data; + let _simplification = push_args.use_simplification; true } diff --git a/src/gp/utils.rs b/src/gp/utils.rs index c7fed49..461a6e7 100644 --- a/src/gp/utils.rs +++ b/src/gp/utils.rs @@ -1,10 +1,163 @@ use crate::gp::args::ClosingType; use crate::gp::genome::OPEN_MAP; use crate::push::state::Gene; +use polars::prelude::*; use rand::Rng; use rand::seq::IndexedRandom; use rust_decimal::prelude::*; +pub fn polars_to_gene(polars_value: &AnyValue) -> Gene { + match polars_value { + AnyValue::Int8(val) => Gene::GeneInt(*val as i128), + AnyValue::Int16(val) => Gene::GeneInt(*val as i128), + AnyValue::Int32(val) => Gene::GeneInt(*val as i128), + AnyValue::Int64(val) => Gene::GeneInt(*val as i128), + AnyValue::Int128(val) => Gene::GeneInt(*val as i128), + AnyValue::UInt8(val) => Gene::GeneInt(*val as i128), + AnyValue::UInt16(val) => Gene::GeneInt(*val as i128), + AnyValue::UInt32(val) => Gene::GeneInt(*val as i128), + AnyValue::UInt64(val) => Gene::GeneInt(*val as i128), + AnyValue::Float32(val) => Gene::GeneFloat(Decimal::from_f32(*val).unwrap()), + AnyValue::Float64(val) => Gene::GeneFloat(Decimal::from_f64(*val).unwrap()), + AnyValue::Boolean(val) => Gene::GeneBoolean(*val), + AnyValue::String(val) => Gene::GeneString(val.chars().collect()), + AnyValue::List(series) => match series.dtype() { + DataType::Int8 => { + let vec = series + .i8() + .unwrap() + .into_iter() + .map(|opt| opt.map(|v| v as i128)) + .collect::>>() + .unwrap(); + Gene::GeneVectorInt(vec) + } + DataType::Int16 => { + let vec = series + .i16() + .unwrap() + .into_iter() + .map(|opt| opt.map(|v| v as i128)) + .collect::>>() + .unwrap(); + Gene::GeneVectorInt(vec) + } + DataType::Int32 => { + let vec = series + .i32() + .unwrap() + .into_iter() + .map(|opt| opt.map(|v| v as i128)) + .collect::>>() + .unwrap(); + Gene::GeneVectorInt(vec) + } + DataType::Int64 => { + let vec = series + .i64() + .unwrap() + .into_iter() + .map(|opt| opt.map(|v| v as i128)) + .collect::>>() + .unwrap(); + Gene::GeneVectorInt(vec) + } + DataType::Int128 => { + let vec = series + .i64() // i64 will have to do + .unwrap() + .into_iter() + .map(|opt| opt.map(|v| v as i128)) + .collect::>>() + .unwrap(); + Gene::GeneVectorInt(vec) + } + DataType::UInt8 => { + let vec = series + .u8() + .unwrap() + .into_iter() + .map(|opt| opt.map(|v| v as i128)) + .collect::>>() + .unwrap(); + Gene::GeneVectorInt(vec) + } + DataType::UInt16 => { + let vec = series + .u16() + .unwrap() + .into_iter() + .map(|opt| opt.map(|v| v as i128)) + .collect::>>() + .unwrap(); + Gene::GeneVectorInt(vec) + } + DataType::UInt32 => { + let vec = series + .u32() + .unwrap() + .into_iter() + .map(|opt| opt.map(|v| v as i128)) + .collect::>>() + .unwrap(); + Gene::GeneVectorInt(vec) + } + DataType::UInt64 => { + let vec = series + .u64() + .unwrap() + .into_iter() + .map(|opt| opt.map(|v| v as i128)) + .collect::>>() + .unwrap(); + Gene::GeneVectorInt(vec) + } + DataType::Float32 => { + let vec = series + .f32() + .unwrap() + .into_iter() + .map(|opt| opt.map(|v| Decimal::from_f32(v).unwrap())) + .collect::>>() + .unwrap(); + Gene::GeneVectorFloat(vec) + } + DataType::Float64 => { + let vec = series + .f64() + .unwrap() + .into_iter() + .map(|opt| opt.map(|v| Decimal::from_f64(v).unwrap())) + .collect::>>() + .unwrap(); + Gene::GeneVectorFloat(vec) + } + DataType::Boolean => { + let vec = series + .bool() + .unwrap() + .into_iter() + .map(|opt| opt.map(|v| v as bool)) + .collect::>>() + .unwrap(); + Gene::GeneVectorBoolean(vec) + } + DataType::String => { + let vec = series + .str() + .unwrap() + .into_iter() + .map(|opt| opt.map(|v| v.chars().collect())) + .collect::>>() + .unwrap(); + Gene::GeneVectorString(vec) + } + _ => unimplemented!("Type {:?} not handled inside a vector", polars_value), + }, + _ => unimplemented!("Type {:?} not handled", polars_value), + } +} + pub fn random_instruction( instructions: Vec, closing_type: ClosingType, diff --git a/src/push/interpreter.rs b/src/push/interpreter.rs index b8b781c..3b3deee 100644 --- a/src/push/interpreter.rs +++ b/src/push/interpreter.rs @@ -25,6 +25,10 @@ pub fn gene_to_stack(state: &mut PushState, gene: Gene) { Gene::CrossoverPadding => { panic!("CrossoverPadding found in the exec stack, this should not happen!") } + Gene::Place(idx) => { + let var = state.input[idx].clone(); + state.exec.push(var) + } } } diff --git a/src/push/state.rs b/src/push/state.rs index 6029a16..40c8f87 100644 --- a/src/push/state.rs +++ b/src/push/state.rs @@ -56,6 +56,7 @@ pub enum Gene { Skip, Block(Vec), CrossoverPadding, + Place(usize), } impl Gene {