Skip to content

Commit

Permalink
Remove gene pool from rosomaxa
Browse files Browse the repository at this point in the history
  • Loading branch information
reinterpretcat committed Oct 5, 2024
1 parent 49c0f84 commit e2ee0cd
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 47 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ are already published. So, I stick to it for now.
### Removed

* replace `compare_floats` with built-in `total_cmp`
* gene pool in rosomaxa


## [1.24.0] 2024-07-13
Expand Down
51 changes: 4 additions & 47 deletions rosomaxa/src/population/rosomaxa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,14 +125,13 @@ where
}

fn select<'a>(&'a self) -> Box<dyn Iterator<Item = &Self::Individual> + 'a> {
#![allow(clippy::unnecessary_cast)]
match &self.phase {
RosomaxaPhases::Exploration { network, coordinates, selection_size, statistics, .. } => {
let random = self.environment.random.as_ref();

let (elite_explore_size, node_explore_size) = match *selection_size {
value if value > 6 => {
let ratio = statistics.improvement_1000_ratio as f64;
let ratio = statistics.improvement_1000_ratio;
let elite_exlr_prob = (1. - 1. / (1. + E.powf(-10. * (ratio - 0.166)))) as Float;
let elite_size = (1..=2).fold(0, |acc, idx| {
acc + if random.is_hit(elite_exlr_prob / idx as Float) { 2 } else { 1 }
Expand Down Expand Up @@ -236,25 +235,12 @@ where
individuals.drain(0..4).collect(),
);

let initial = std::mem::take(individuals);
let initial = initial.into_iter().map(init_individual).collect::<Vec<_>>();
initial.iter().for_each(|individual| network.store(individual.deep_copy(), 0));

// create gene pool to keep track of population progress
let gene_pool_size = self.config.selection_size.clamp(4, 8);
let gene_pool_selection_size = (gene_pool_size / 2).max(4);
let mut gene_pool = Elitism::new_with_dedup(
self.objective.clone(),
self.environment.random.clone(),
gene_pool_size,
gene_pool_selection_size,
create_dedup_fn(0.05),
);
gene_pool.add_all(initial);
std::mem::take(individuals)
.into_iter()
.for_each(|individual| network.store(init_individual(individual), 0));

self.phase = RosomaxaPhases::Exploration {
network,
gene_pool,
coordinates: vec![],
statistics: statistics.clone(),
selection_size,
Expand All @@ -263,7 +249,6 @@ where
}
RosomaxaPhases::Exploration {
network,
gene_pool,
coordinates,
statistics: old_statistics,
selection_size: old_selection_size,
Expand All @@ -277,8 +262,6 @@ where
*old_statistics = statistics.clone();
*old_selection_size = selection_size;

Self::reintroduce_gene_pool(network, &self.elite, gene_pool, statistics, &self.config);

Self::optimize_network(network, statistics, &self.config);

Self::fill_populations(network, coordinates, self.environment.random.as_ref());
Expand All @@ -296,31 +279,6 @@ where
best_known.map_or(true, |best_known| self.objective.total_order(individual, best_known) != Ordering::Greater)
}

fn reintroduce_gene_pool(
network: &mut IndividualNetwork<O, S>,
elite: &Elitism<O, S>,
gene_pool: &mut Elitism<O, S>,
statistics: &HeuristicStatistics,
config: &RosomaxaConfig,
) {
let frequency = match statistics.speed {
HeuristicSpeed::Slow { .. } => config.rebalance_memory.min(10),
_ => (config.rebalance_memory / 2).max(20),
};

if statistics.generation > 0 && statistics.generation % frequency == 0 {
network.store_batch(
gene_pool.select().map(|i| i.deep_copy()).collect(),
statistics.generation,
init_individual,
);

if let Some(best_known) = elite.select().next() {
gene_pool.add(best_known.deep_copy());
}
}
}

fn optimize_network(
network: &mut IndividualNetwork<O, S>,
statistics: &HeuristicStatistics,
Expand Down Expand Up @@ -429,7 +387,6 @@ where
},
Exploration {
network: IndividualNetwork<O, S>,
gene_pool: Elitism<O, S>,
coordinates: Vec<Coordinate>,
statistics: HeuristicStatistics,
selection_size: usize,
Expand Down

0 comments on commit e2ee0cd

Please sign in to comment.