| Crates.io | neural |
| lib.rs | neural |
| version | 0.3.0 |
| created_at | 2025-04-15 20:18:10.734344+00 |
| updated_at | 2025-04-20 16:08:25.424664+00 |
| description | Genetic Algorithms in Rust |
| homepage | https://docs.rs/neural |
| repository | https://github.com/DarkCeptor44/neural |
| max_upload_size | |
| id | 1635185 |
| size | 36,257 |
Neural is a library for Genetic Algorithms in Rust.
with_print method on a PopulationBuilder to print the population's best chromosome after each generation and enables colored output.cargo add neural
# or add it with the print feature
cargo add neural --features print
Or add it as a dependency in your Cargo.toml:
[dependencies]
neural = "^0.3"
# or add it with the print feature
[dependencies]
neural = { version = "^0.3", features = ["print"] }
use neural::{Gene, Population, PopulationBuilder, Result, TournamentSelection, UniformCrossover};
use rand::{rngs::ThreadRng, Rng};
use std::fmt::Display;
#[derive(Debug, Clone, PartialEq, PartialOrd)]
struct F64(f64);
impl Eq for F64 {}
impl Display for F64 {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.0)
}
}
impl From<f64> for F64 {
fn from(value: f64) -> Self {
Self(value)
}
}
impl From<F64> for f64 {
fn from(val: F64) -> Self {
val.0
}
}
impl Gene for F64 {
fn generate_gene<R>(rng: &mut R) -> Self
where
R: Rng + ?Sized,
{
rng.random_range(-1.0..=1.0).into()
}
}
fn main() -> Result<()> {
let mut pop: Population<F64, TournamentSelection, UniformCrossover, _, ThreadRng> =
PopulationBuilder::new(None, |c| c.value.iter().map(|g: &F64| g.0).sum::<f64>())
.with_chromo_size(50)
.with_population_size(100)
.with_mutation_rate(0.02)
.with_elitism(true)
// .with_print(true) // uncomment to print the best chromosome after each generation. requires the `print` feature
.build()?;
let num_generations = 200;
match pop.evolve(num_generations) {
Some(best) => {
println!("Evolution complete after {num_generations} generations.");
println!(
"Best fitness found: {}, chromosome: {:?}",
best.fitness, best.value
);
}
None => println!("Evolution ended with an empty population."),
}
Ok(())
}
This library is distributed under the terms of the MIT License.