-
Notifications
You must be signed in to change notification settings - Fork 16
/
main.rs
66 lines (54 loc) · 1.95 KB
/
main.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
use radiate::*;
use radiate_extensions::*;
const MAX_INDEX: i32 = 500;
const MIN_SCORE: f32 = 0.01;
fn main() {
let factory = NodeFactory::<f32>::regression(2).outputs(vec![op::sigmoid()]);
let graph_codex = GraphCodex::from_shape(2, 1, &factory);
let regression = Regression::new(get_sample_set(), ErrorFunction::MSE);
let engine = GeneticEngine::from_codex(&graph_codex)
.minimizing()
.alterer(vec![
GraphCrossover::alterer(0.5, 0.5),
OpMutator::alterer(factory.clone(), 0.01, 0.05),
GraphMutator::alterer(
factory.clone(),
vec![
NodeMutate::Forward(NodeType::Weight, 0.05),
NodeMutate::Forward(NodeType::Aggregate, 0.03),
NodeMutate::Forward(NodeType::Gate, 0.03),
],
),
])
.fitness_fn(move |genotype: Graph<f32>| {
let mut reducer = GraphReducer::new(&genotype);
Score::from_f32(regression.error(|input| reducer.reduce(&input)))
})
.build();
let result = engine.run(|output| {
println!("[ {:?} ]: {:?}", output.index, output.score().as_float());
output.index == MAX_INDEX || output.score().as_float() < MIN_SCORE
});
display(&result);
}
fn display(result: &EngineContext<Node<f32>, Ops<f32>, Graph<f32>>) {
let mut reducer = GraphReducer::new(&result.best);
for sample in get_sample_set().get_samples().iter() {
let output = &reducer.reduce(&sample.1);
println!(
"{:?} -> epected: {:?}, actual: {:.3?}",
sample.1, sample.2, output
);
}
println!("{:?}", result)
}
fn get_sample_set() -> SampleSet<f32> {
let inputs = vec![
vec![0.0, 0.0],
vec![1.0, 1.0],
vec![1.0, 0.0],
vec![0.0, 1.0],
];
let answers = vec![vec![0.0], vec![0.0], vec![1.0], vec![1.0]];
SampleSet::from_vecs(inputs, answers)
}