Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

change cfg macros to remove redundancy #40

Merged
merged 3 commits into from
Apr 16, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 17 additions & 65 deletions examples/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,92 +87,44 @@ fn fitness(dna: &AgentDNA) -> f32 {
fitness
}

#[cfg(all(not(feature = "crossover"), not(feature = "rayon")))]
fn main() {
#[cfg(not(feature = "rayon"))]
let mut rng = rand::thread_rng();

let mut sim = GeneticSim::new(
#[cfg(not(feature = "rayon"))]
Vec::gen_random(&mut rng, 100),
#[cfg(feature = "rayon")]
Vec::gen_random(100),
fitness,
#[cfg(not(feature = "crossover"))]
division_pruning_nextgen,
);

for _ in 0..100 {
sim.next_generation();
}

let fits: Vec<_> = sim.genomes.iter().map(fitness).collect();

let maxfit = fits
.iter()
.max_by(|a, b| a.partial_cmp(b).unwrap())
.unwrap();

dbg!(&fits, maxfit);
}

#[cfg(all(not(feature = "crossover"), feature = "rayon"))]
fn main() {
let mut sim = GeneticSim::new(Vec::gen_random(100), fitness, division_pruning_nextgen);

for _ in 0..100 {
sim.next_generation();
}

let fits: Vec<_> = sim.genomes.iter().map(fitness).collect();

let maxfit = fits
.iter()
.max_by(|a, b| a.partial_cmp(b).unwrap())
.unwrap();

dbg!(&fits, maxfit);
}

#[cfg(all(feature = "crossover", not(feature = "rayon")))]
fn main() {
let mut rng = rand::thread_rng();

let mut sim = GeneticSim::new(
Vec::gen_random(&mut rng, 100),
fitness,
#[cfg(feature = "crossover")]
crossover_pruning_nextgen,
);

for _ in 0..100 {
sim.next_generation();
}

let mut fits: Vec<_> = sim.genomes.iter().map(|e| (e, fitness(e))).collect();

fits.sort_by(|(_, a), (_, b)| a.partial_cmp(b).unwrap());

dbg!(&fits);

if cfg!(feature = "serde") {
let intermediate = NNTSerde::from(&fits[0].0.network);
let serialized = serde_json::to_string(&intermediate).unwrap();
println!("{}", serialized);
}
}

#[cfg(all(feature = "crossover", feature = "rayon"))]
fn main() {
let mut sim = GeneticSim::new(Vec::gen_random(100), fitness, crossover_pruning_nextgen);

for _ in 0..100 {
sim.next_generation();
}
#[cfg(not(feature = "serde"))]
let mut fits: Vec<_> = sim.genomes.iter().map(fitness).collect();

#[cfg(feature = "serde")]
let mut fits: Vec<_> = sim.genomes.iter().map(|e| (e, fitness(e))).collect();

fits.sort_by(|(_, a), (_, b)| a.partial_cmp(b).unwrap());
#[cfg(not(feature = "serde"))]
fits.sort_by(|a, b| a.partial_cmp(&b).unwrap());

#[cfg(feature = "serde")]
fits.sort_by(|(_, a), (_, b)| a.partial_cmp(&b).unwrap());

dbg!(&fits);

if cfg!(feature = "serde") {
#[cfg(feature = "serde")]
{
let intermediate = NNTSerde::from(&fits[0].0.network);
let serialized = serde_json::to_string(&intermediate).unwrap();
println!("serialized: {}", serialized);
println!("{}", serialized);
}
}