use neat::*;
fn fitness(net: &NeuralNetwork<1, 1>) -> f32 {
let mut rng = rand::rng();
let mut total_fitness = 0.0;
for _ in 0..100 {
let input = rng.random_range(-10.0..10.0);
let output = net.predict([input])[0];
let expected_output = input.to_degrees();
total_fitness -= (output - expected_output).abs();
}
total_fitness
}
fn main() {
#[cfg(debug_assertions)]
println!("You're running on the debug profile, which is not optimized. Consider running with --release for significantly better performance.");
let mut rng = rand::rng();
let mut sim = GeneticSim::new(
Vec::gen_random(&mut rng, 250),
FitnessEliminator::new_without_observer(fitness),
CrossoverRepopulator::new(0.25, ReproductionSettings::default()),
);
for i in 0..=150 {
sim.next_generation();
let sample = &sim.genomes[0];
let fit = fitness(sample);
println!("Gen {i} sample fitness: {fit}");
}
println!("Training complete, now you can test the network!");
let net = &sim.genomes[0];
println!("Network in use: {:#?}", net);
loop {
let mut input_text = String::new();
println!("Enter a number to convert to degrees (or 'exit' to quit): ");
std::io::stdin().read_line(&mut input_text).unwrap();
let input_text = input_text.trim();
if input_text.eq_ignore_ascii_case("exit") {
break;
}
let input: f32 = match input_text.parse() {
Ok(num) => num,
Err(_) => {
println!("Invalid input, please enter a valid number.");
continue;
}
};
let output = net.predict([input])[0];
let expected_output = input.to_degrees();
println!(
"Network output: {}, Expected output: {}, Error: {}",
output,
expected_output,
(output - expected_output).abs()
);
}
}