Crates.io | xu |
lib.rs | xu |
version | 0.2.3 |
source | src |
created_at | 2021-03-16 20:20:28.865477 |
updated_at | 2021-03-16 20:20:28.865477 |
description | xu is an evolutionary computation library. |
homepage | https://github.com/yuulive/xu |
repository | https://github.com/yuulive/xu |
max_upload_size | |
id | 369846 |
size | 79,995 |
An evolutionary computation library written in Rust.
Xu provides the generic logic required for genetic algorithms, allowing you to focus on the code specific to your particular use case.
It is still at an early stage so expect the API to change but also for improvements to be made.
Create a new project and add the following to the cargo.toml file:
[dependencies]
xu = "0.2.3"
In main.rs, start off with the following:
extern crate xu;
use xu::agent::Agent;
use xu::manager::create_manager;
use xu::fitness::ScoreError;
In the above code, we import a function to create a default Manager object, which will run the genetic algorithm system. We import Agent so that we can investigate the 'fittest' set of genes after running. We also import ScoreError for use when defining our fitness function.
Now, inside you main() function, delete the default code in there and let's add the code to create and run the manager:
let mut manager = create_manager(fitness_function, 0);
manager.set_number_of_genes(5, true);
manager.run(1250);;
We've created a new manager, passing in our fitness function. We define this function later in this example. We also pass in 0 as the second argument, which is for additional data. We aren't using the data parameter in this example, but you can look at some of examples in this library to see other ways that data can be used.
We also set the number of genes that each agent should have. The second argument is for saying whether agents have to have that number of genes, or whether it can vary a bit if we aren't getting any good scores. However, that 'varying' functionality has not yet been implemented.
Lastly, we run the system, specifying a score that the highest in the population must be greater than in order to complete the run. If we were to set that score too high, the system would currently run forever (until you press Ctrl+C to stop the program).
Once the run is complete, we'll want to get the agents and see what genes they had. Below your code for running the manager, add the following:
let agents = manager.get_population().get_agents();
To finish off the main function, we'll want to see what the highest scored agents are. Add this:
let mut viewing = 10;
for (score_index, agent) in population.get_agents().iter().rev() {
println!("Score: {}", score_index);
println!("{:?}", agent.get_genes());
viewing -= 1;
if viewing == 0 {
break;
}
}
So the last thing to do is add the fitness function. We're just wanting a quick example here, so we're just going to make the genes 'u8' (unsigned 8-bit integers), and the higher they are, the higher the score.
Add the following below your main function:
fn fitness_function(agent: &Agent<u8>, _data: &u8) -> isize {
let mut score = 0;
for gene in agent.get_genes() {
score += *gene as isize;
}
score
}
Now run your code with cargo run
.
You'll get a list of the top scores in the population, along with sets of 5 integers for each, which represent their 'genes'.
Have a look in the examples directory of this library. The example described above can be found in simplest.rs.
Xu is licensed under the Apache License, Version 2.0.