KmolYuan / metaheuristics-nature-rs

A collection of nature-inspired metaheuristic algorithms.
https://crates.io/crates/metaheuristics-nature
MIT License
14 stars 1 forks source link

Can you write some examples? #6

Closed mrlogick closed 8 months ago

mrlogick commented 8 months ago

As I just started learning genetic algorithms, it's not very clear to me how to use this crate. It would be good to have a simple example, like the Knapsack Problem, or something.

KmolYuan commented 8 months ago

I have a small example at https://docs.rs/metaheuristics-nature/latest/metaheuristics_nature/struct.Fx.html.

The algorithm will try to minimize the function's return value, and bound is the range of the variables.

use metaheuristics_nature::{Fx, Rga, Solver};

let bound = [[-50., 50.]; 4];
let f = Fx::new(&bound, |&[a, b, c, d]| a * a + 8. * b * b + c * c + d * d);
let s = Solver::build(Rga::default(), f)
    .seed(0)
    .task(|ctx| ctx.gen == 20)
    .solve();

Currently, this crate does not support integer problems. The a, b, c, and d are real numbers (f64).

mrlogick commented 8 months ago

Ok, it's clearer now, thanks!