1 个不稳定版本
0.2.0 | 2020年9月11日 |
---|
#34 in #评分
15KB
300 行
Heredity
Heredity 是一个最小化的通用遗传算法库。
只要你的问题可以表示为一个 Vec<u8>
,并且你可以给任何给定的解决方案分配一个 f64
评分,Heredity 就可以找到解决方案!
快速入门
use heredity::{FitnessEvaluator, Parameters, ThreadSim};
struct SumEvaluator {}
impl FitnessEvaluator for SumEvaluator {
fn evaluate(&self, code: &Vec<u8>) -> f64 {
return code.iter().map(|&b| b as f64).sum();
}
}
pub fn main() {
const MAX_IT_BEFORE_CONVERGE: usize = 10; // Each result is 1000 results, so this is really 10k iterations.
let params = Parameters::new(SumEvaluator {});
let expected_max_score = (params.genetic_code_length * (std::u8::MAX as usize)) as f64; // in our demo, the score of the genetic code is the sum of all bytes, so we can calculate the maximal score.
let sim = ThreadSim::start(params);
let result_stream = sim.results();
let mut it_count = 0;
let mut last_score = -1.0;
loop {
match result_stream.recv() {
Ok(result) => {
assert!(result.score >= last_score);
last_score = result.score;
// Success condition: within 2% of the maximum.
if (result.score - expected_max_score).abs() <= (expected_max_score * 0.02) {
break;
}
}
Err(_e) => {
break;
}
}
it_count += 1;
if it_count > MAX_IT_BEFORE_CONVERGE {
panic!("Took too long.");
}
}
sim.stop().unwrap();
}
依赖
~2MB
~48K SLoC