Changed the way that new branches are created.

This commit is contained in:
vandomej 2021-06-24 16:59:21 -07:00
parent 4ea2d4cbbc
commit 8a66fe3ee6
3 changed files with 71 additions and 22 deletions

View file

@ -7,7 +7,7 @@ pub trait GeneticState {
/// - iterations: the number of iterations (learning cycles) that the current state should simulate /// - iterations: the number of iterations (learning cycles) that the current state should simulate
/// ///
/// This will be called for every node in a bracket before evaluating it's fitness against other nodes. /// This will be called for every node in a bracket before evaluating it's fitness against other nodes.
fn run_simulation(&mut self, iterations: u32); fn run_simulation(&mut self, iterations: u64);
/// Returns a fit score associated with the nodes performance. /// Returns a fit score associated with the nodes performance.
/// This will be used by a bracket in order to determine the most successful child. /// This will be used by a bracket in order to determine the most successful child.

View file

@ -31,7 +31,7 @@ impl fmt::Display for IterationScaling {
#[derive(Serialize, Deserialize, Clone, Debug)] #[derive(Serialize, Deserialize, Clone, Debug)]
pub struct Bracket<T> { pub struct Bracket<T> {
tree: tree::Tree<T>, tree: tree::Tree<T>,
step: u32, step: u64,
iteration_scaling: IterationScaling iteration_scaling: IterationScaling
} }
@ -62,28 +62,67 @@ impl<T> Bracket<T>
self self
} }
pub fn create_new_branch(&self, height: u64) -> tree::Tree<T>
{
if height == 1
{
let mut base_node = btree!(T::initialize());
base_node.val.run_simulation(
match self.iteration_scaling
{
IterationScaling::Linear(x) => (x * (height as u32)).into()
}
);
btree!(base_node.val.clone())
}
else
{
let left = self.create_new_branch(height - 1);
let right = self.create_new_branch(height - 1);
let mut new_val = if left.val.get_fit_score() >= right.val.get_fit_score()
{
left.val.clone()
}
else {
right.val.clone()
};
new_val.run_simulation(
match self.iteration_scaling
{
IterationScaling::Linear(x) => (x * (height as u32)).into()
}
);
btree!(new_val, Some(left), Some(right))
}
}
pub fn run_simulation_step(&mut self) -> &mut Self pub fn run_simulation_step(&mut self) -> &mut Self
{ {
let new_branch = self.create_new_branch(self.step + 1);
self.tree.val.run_simulation( self.tree.val.run_simulation(
match self.iteration_scaling match self.iteration_scaling
{ {
IterationScaling::Linear(x) => x IterationScaling::Linear(x) => ((x as u64) * (self.step + 1)).into()
} }
); );
let mut new_branch = btree!(T::initialize()); let new_val = if new_branch.val.get_fit_score() >= self.tree.val.get_fit_score()
new_branch.val.run_simulation(
match self.iteration_scaling
{ {
IterationScaling::Linear(x) => x * (self.step + 1) new_branch.val.clone()
} }
); else
{
self.tree.val.clone()
};
self.tree = btree!(new_val, Some(new_branch), Some(self.tree.clone()));
self.tree = btree!(
self.tree.val.clone(),
Some(self.tree.clone()),
Some(new_branch)
);
self.step += 1; self.step += 1;
self self

View file

@ -33,7 +33,7 @@ impl TestState {
} }
impl bracket::genetic_state::GeneticState for TestState { impl bracket::genetic_state::GeneticState for TestState {
fn run_simulation(&mut self, iterations: u32) fn run_simulation(&mut self, iterations: u64)
{ {
self.score += iterations as f64; self.score += iterations as f64;
} }
@ -81,15 +81,25 @@ fn test_run() {
format!("{}", bracket), format!("{}", bracket),
format!("{{\"tree\":{},\"step\":3,\"iteration_scaling\":{{\"enumType\":\"Linear\",\"enumContent\":2}}}}", format!("{{\"tree\":{},\"step\":3,\"iteration_scaling\":{{\"enumType\":\"Linear\",\"enumContent\":2}}}}",
btree!( btree!(
TestState::new(6.0), TestState::new(12.0),
Some(btree!( Some(btree!(
TestState::new(6.0), TestState::new(12.0),
Some(btree!(TestState::new(4.0), Some(btree!(TestState::new(6.0),
Some(btree!(TestState::new(2.0))), Some(btree!(TestState::new(2.0))),
Some(btree!(TestState::new(2.0))))), Some(btree!(TestState::new(2.0))))),
Some(btree!(TestState::new(4.0))) Some(btree!(TestState::new(6.0),
Some(btree!(TestState::new(2.0))),
Some(btree!(TestState::new(2.0)))))
)), )),
Some(btree!(TestState::new(6.0))) Some(btree!(
TestState::new(12.0),
Some(btree!(TestState::new(6.0),
Some(btree!(TestState::new(2.0))),
Some(btree!(TestState::new(2.0))))),
Some(btree!(TestState::new(6.0),
Some(btree!(TestState::new(2.0))),
Some(btree!(TestState::new(2.0)))))
))
)) ))
); );