Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
pkalivas committed Jan 20, 2025
2 parents 66372ec + b9d842e commit 866a419
Show file tree
Hide file tree
Showing 9 changed files with 49 additions and 30 deletions.
2 changes: 1 addition & 1 deletion examples/regression-tree/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use radiate::*;
use radiate_gp::*;

const MIN_SCORE: f32 = 0.01;
const MAX_SECONDS: f64 = 5.0;
const MAX_SECONDS: f64 = 1.0;

fn main() {
random_provider::set_seed(42069);
Expand Down
2 changes: 1 addition & 1 deletion radiate-gp/src/collections/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
/// ```
/// **Note**: The above is not guaranteed, but is a good example of what
/// the tree will look like. It isn't guaranteed because the `TreeBuilder`
/// uses a random number generator to pick the value for each node.
/// uses the `random_provider` to pick the value (`Op<f32>` in this case) for each node.
pub trait Builder {
type Output;
fn build(&self) -> Self::Output;
Expand Down
1 change: 0 additions & 1 deletion radiate-gp/src/collections/graphs/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ use radiate::random_provider;
///
/// # Type Parameters
/// 'C' - The type of the node cell that the graph will contain.
///
pub struct GraphBuilder<C: NodeCell> {
store: CellStore<C>,
}
Expand Down
15 changes: 14 additions & 1 deletion radiate-gp/src/collections/reducers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,19 @@
/// ],
/// );
///
/// // the above tree can also be created in the following way:
/// let mut other_root = TreeNode::new(Op::add())
/// .attach(
/// TreeNode::new(Op::mul())
/// .attach(TreeNode::new(Op::value(2.0)))
/// .attach(TreeNode::new(Op::value(3.0))),
/// )
/// .attach(
/// TreeNode::new(Op::add())
/// .attach(TreeNode::new(Op::value(2.0)))
/// .attach(TreeNode::new(Op::var(0))),
/// );
///
/// // And the result of evaluating this tree with an input of `1` would be:
/// let result = root.reduce(&vec![1_f32]);
/// assert_eq!(result, 9.0);
Expand All @@ -40,7 +53,7 @@
///
/// This can also be thought of (and is functionally equivalent) as:
/// ```text
/// (2 * 3) + (2 + x)
/// f(x) = (2 * 3) + (2 + x)
/// ```
pub trait Reduce<T> {
type Input;
Expand Down
6 changes: 6 additions & 0 deletions radiate-gp/src/collections/trees/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,12 @@ impl<C: NodeCell + Default> Builder for TreeBuilder<C> {
}
}

impl<C: NodeCell + Default> Default for TreeBuilder<C> {
fn default() -> Self {
TreeBuilder::new(1)
}
}

#[cfg(test)]
mod tests {
use crate::Op;
Expand Down
4 changes: 2 additions & 2 deletions radiate-gp/src/collections/trees/crossover.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ use radiate::engines::genome::*;
use radiate::{random_provider, Alter, AlterAction, Crossover, EngineCompoment};

pub struct TreeCrossover {
pub rate: f32,
rate: f32,
}

impl TreeCrossover {
pub fn new(rate: f32) -> Self {
Self { rate }
TreeCrossover { rate }
}
}

Expand Down
5 changes: 5 additions & 0 deletions radiate-gp/src/collections/trees/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ impl<C: NodeCell> TreeNode<C> {
}
}

pub fn attach(mut self, other: TreeNode<C>) -> Self {
self.add_child(other);
self
}

pub fn children(&self) -> Option<&Vec<TreeNode<C>>> {
self.children.as_ref()
}
Expand Down
43 changes: 20 additions & 23 deletions radiate-gp/src/collections/trees/reduce.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::{Op, Reduce, TreeNode};
use super::Tree;

/// Implements the `Reduce` trait for `Tree<Op<T>>`. All this really does is
/// call the `reduce` method on the root node of the `Tree`. the real work is
/// call the `reduce` method on the root node of the `Tree`. The real work is
/// done in the `TreeNode` implementation below.
impl<T: Clone> Reduce<T> for Tree<Op<T>> {
type Input = Vec<T>;
Expand Down Expand Up @@ -71,27 +71,24 @@ mod tests {

#[test]
fn test_tree_reduce_complex() {
let mut root = TreeNode::with_children(
Op::add(),
vec![
TreeNode::with_children(
Op::mul(),
vec![TreeNode::new(Op::value(2.0)), TreeNode::new(Op::value(3.0))],
),
TreeNode::with_children(
Op::add(),
vec![TreeNode::new(Op::value(2.0)), TreeNode::new(Op::var(0))],
),
],
);

let result = root.reduce(&vec![1_f32]);
assert_eq!(result, 9.0);

let result = root.reduce(&vec![2_f32]);
assert_eq!(result, 10.0);

let result = root.reduce(&vec![3_f32]);
assert_eq!(result, 11.0);
let mut root = TreeNode::new(Op::add())
.attach(
TreeNode::new(Op::mul())
.attach(TreeNode::new(Op::value(2.0)))
.attach(TreeNode::new(Op::value(3.0))),
)
.attach(
TreeNode::new(Op::add())
.attach(TreeNode::new(Op::value(2.0)))
.attach(TreeNode::new(Op::var(0))),
);

let nine = root.reduce(&vec![1_f32]);
let ten = root.reduce(&vec![2_f32]);
let eleven = root.reduce(&vec![3_f32]);

assert_eq!(nine, 9.0);
assert_eq!(ten, 10.0);
assert_eq!(eleven, 11.0);
}
}
1 change: 0 additions & 1 deletion radiate/src/engines/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ use std::time::Duration;
/// # Type Parameters
/// - `C`: The type of chromosome used in the genotype, which must implement the `Chromosome` trait.
/// - `T`: The type of the best individual in the population.
///
pub struct EngineContext<C, T>
where
C: Chromosome,
Expand Down

0 comments on commit 866a419

Please sign in to comment.