simplification tested
Some checks failed
/ Test-Suite (push) Failing after 32s

This commit is contained in:
Rowan Torbitzky-Lane 2025-04-30 02:38:38 -05:00
parent f424a7bd3a
commit d8698f7fdf
5 changed files with 171 additions and 11 deletions

View File

@ -22,8 +22,9 @@ pub struct PushArgs {
pub elitism: bool, // Whether to always add the best individual to next generation
pub error_function: Option<fn(&PushArgs, &DataFrame, Vec<Gene>) -> Vec<Decimal>>, // The error function
pub instructions: Option<Vec<Gene>>, // 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,

View File

@ -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
}

View File

@ -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::<Option<Vec<_>>>()
.unwrap();
Gene::GeneVectorInt(vec)
}
DataType::Int16 => {
let vec = series
.i16()
.unwrap()
.into_iter()
.map(|opt| opt.map(|v| v as i128))
.collect::<Option<Vec<_>>>()
.unwrap();
Gene::GeneVectorInt(vec)
}
DataType::Int32 => {
let vec = series
.i32()
.unwrap()
.into_iter()
.map(|opt| opt.map(|v| v as i128))
.collect::<Option<Vec<_>>>()
.unwrap();
Gene::GeneVectorInt(vec)
}
DataType::Int64 => {
let vec = series
.i64()
.unwrap()
.into_iter()
.map(|opt| opt.map(|v| v as i128))
.collect::<Option<Vec<_>>>()
.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::<Option<Vec<_>>>()
.unwrap();
Gene::GeneVectorInt(vec)
}
DataType::UInt8 => {
let vec = series
.u8()
.unwrap()
.into_iter()
.map(|opt| opt.map(|v| v as i128))
.collect::<Option<Vec<_>>>()
.unwrap();
Gene::GeneVectorInt(vec)
}
DataType::UInt16 => {
let vec = series
.u16()
.unwrap()
.into_iter()
.map(|opt| opt.map(|v| v as i128))
.collect::<Option<Vec<_>>>()
.unwrap();
Gene::GeneVectorInt(vec)
}
DataType::UInt32 => {
let vec = series
.u32()
.unwrap()
.into_iter()
.map(|opt| opt.map(|v| v as i128))
.collect::<Option<Vec<_>>>()
.unwrap();
Gene::GeneVectorInt(vec)
}
DataType::UInt64 => {
let vec = series
.u64()
.unwrap()
.into_iter()
.map(|opt| opt.map(|v| v as i128))
.collect::<Option<Vec<_>>>()
.unwrap();
Gene::GeneVectorInt(vec)
}
DataType::Float32 => {
let vec = series
.f32()
.unwrap()
.into_iter()
.map(|opt| opt.map(|v| Decimal::from_f32(v).unwrap()))
.collect::<Option<Vec<_>>>()
.unwrap();
Gene::GeneVectorFloat(vec)
}
DataType::Float64 => {
let vec = series
.f64()
.unwrap()
.into_iter()
.map(|opt| opt.map(|v| Decimal::from_f64(v).unwrap()))
.collect::<Option<Vec<_>>>()
.unwrap();
Gene::GeneVectorFloat(vec)
}
DataType::Boolean => {
let vec = series
.bool()
.unwrap()
.into_iter()
.map(|opt| opt.map(|v| v as bool))
.collect::<Option<Vec<_>>>()
.unwrap();
Gene::GeneVectorBoolean(vec)
}
DataType::String => {
let vec = series
.str()
.unwrap()
.into_iter()
.map(|opt| opt.map(|v| v.chars().collect()))
.collect::<Option<Vec<_>>>()
.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<Gene>,
closing_type: ClosingType,

View File

@ -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)
}
}
}

View File

@ -56,6 +56,7 @@ pub enum Gene {
Skip,
Block(Vec<Gene>),
CrossoverPadding,
Place(usize),
}
impl Gene {