Crates.io | simple_neat |
lib.rs | simple_neat |
version | 0.1.2 |
source | src |
created_at | 2024-09-04 10:51:50.397973 |
updated_at | 2024-09-09 11:19:47.91998 |
description | A simple NEAT like create for rust |
homepage | https://github.com/ValpsZ/simple_neat |
repository | https://github.com/ValpsZ/simple_neat |
max_upload_size | |
id | 1363169 |
size | 14,444 |
simple_neat is a lightweight Rust library designed to implement the NEAT (NeuroEvolution of Augmenting Topologies) algorithm. This library provides essential tools for evolving neural networks, supporting dynamic topologies and enabling the evolution of both the structure and weights of the network.
Add simple_neat
to your Cargo.toml
:
[dependencies]
simple_neat = "0.1.1"
Here is a simple example demonstrating how to create agents, run them through multiple epochs, and evolve the top-performing agents:
use simple_neat::{Agent, TANH};
use rand::thread_rng;
fn main() {
let mut agents = Agent::create_agents(5, 2, 1, vec![TANH, TANH]);
let mut rng = thread_rng();
const EPOCHS: i32 = 10_000;
for epoch in 0..EPOCHS {
println!("Epoch: {}", epoch);
let input = vec![rng.gen_range(-1.0..1.0), 1.0];
let mut result: Vec<f32> = vec![];
for agent in agents.iter_mut() {
result.push(agent.calculate(&input)[0] / (agent.nodes + 1) as f32);
}
let index_of_max = result
.iter()
.enumerate()
.max_by(|(_, a), (_, b)| a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal))
.map(|(index, _)| index)
.unwrap();
let best_agent = agents.remove(index_of_max);
agents = vec![best_agent];
println!("Best result: {}", result[index_of_max]);
for _ in 0..4 {
agents.push(agents[0].reproduce(0.1, 0.15, 0.05, 0.05, 0.20, 0.15, 3.0));
}
}
agents[0].print();
}
Agent::create_agents
: Creates a vector of agents with the specified number of inputs, outputs, and activation functions.Agent::calculate
: Computes the output of the network based on the given inputs.Agent::reproduce
: Generates a new agent by applying mutations to the parent agent.Agent::print
: Outputs the structure of the network, including nodes and connections.Contributions are welcome! Please feel free to submit a pull request or open an issue to report bugs or suggest features.
This project is licensed under the MIT License. See the LICENSE file for more details.