-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfitness.rs
139 lines (124 loc) · 5.11 KB
/
fitness.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#![allow(dead_code)]
pub(crate) mod compared_fitness_functions;
pub(crate) mod failed_consensus_fitness;
pub(crate) mod validated_ledgers_fitness;
pub(crate) mod time_fitness;
pub(crate) mod delay_fitness;
pub(crate) mod state_accounting_fitness;
pub(crate) mod propose_seq_fitness;
use std::collections::HashMap;
use std::sync::mpsc::{Receiver, Sender};
use std::sync::{Arc, RwLock};
use std::thread;
use genevo::genetic::{AsScalar, Fitness, FitnessFunction, Genotype};
use std::fmt::{Display};
use std::time::{Duration as TimeDuration};
use log::debug;
use crate::ga::encoding::{ExtendedGenotype, ExtendedPhenotype};
use crate::node_state::MutexNodeStates;
use crate::test_harness::TestHarness;
pub trait ExtendedFitness: Fitness + AsScalar + Clone + Send + Sync + Display + serde::Serialize + 'static {
fn average(a: &[Self]) -> Self;
fn highest_possible_fitness() -> Self;
fn lowest_possible_fitness() -> Self;
fn run_harness(test_harness: &mut TestHarness<'static>, node_states: Arc<MutexNodeStates>) -> Self;
}
/// Fitness function communicates with scheduler handler for calculating and storing fitness of solutions.
#[derive(Clone, Debug)]
pub struct FitnessCalculation<T, G> where T: ExtendedFitness + Clone, G: Genotype {
pub(crate) fitness_values: Arc<RwLock<HashMap<G, T>>>,
pub(crate) sender: Sender<G>,
}
impl<T, G> FitnessFunction<G, T> for FitnessCalculation<T, G>
where T: ExtendedFitness, G: ExtendedGenotype
{
fn fitness_of(&self, delays_genotype: &G) -> T {
let mut sent_to_handler = false;
loop {
{
match self.fitness_values.read().unwrap().get(delays_genotype) {
Some(fitness) => {
println!("Fitness found: {:?} for genotype: {:?}", fitness, delays_genotype);
return fitness.clone();
}
None => {
if !sent_to_handler {
println!("Fitness not found for genotype: {:?}", delays_genotype);
self.sender.send(delays_genotype.clone()).expect("Fitness calculator receiver failed");
}
sent_to_handler = true;
}
}
}
thread::sleep(TimeDuration::from_millis(100));
}
}
fn average(&self, a: &[T]) -> T {
T::average(a)
}
fn highest_possible_fitness(&self) -> T {
T::highest_possible_fitness()
}
fn lowest_possible_fitness(&self) -> T {
T::lowest_possible_fitness()
}
}
pub trait SchedulerHandlerTrait {
fn run(self);
}
/// Scheduler handler is in charge of communicating new schedules to the scheduler
/// Fitness functions send to this handler to request fitness values for untested solutions
/// Calculated fitness values are stored in the fitness_values map and fitness functions will first check there
pub struct SchedulerHandler<T, G, P>
where T: ExtendedFitness, G: ExtendedGenotype, P: ExtendedPhenotype<G>
{
scheduler_sender: Sender<P>,
scheduler_receiver: Receiver<T>,
fitness_receiver: Receiver<G>,
fitness_values: Arc<RwLock<HashMap<G, T>>>,
}
impl<T, G, P> SchedulerHandler<T, G, P>
where T: ExtendedFitness, G: ExtendedGenotype, P: ExtendedPhenotype<G>
{
pub fn new(
scheduler_sender: Sender<P>,
scheduler_receiver: Receiver<T>,
fitness_receiver: Receiver<G>,
fitness_values: Arc<RwLock<HashMap<G, T>>>,
) -> Self {
SchedulerHandler { scheduler_sender, scheduler_receiver, fitness_receiver, fitness_values }
}
}
impl<T, G, P> SchedulerHandlerTrait for SchedulerHandler<T, G, P>
where T: ExtendedFitness, G: ExtendedGenotype, P: ExtendedPhenotype<G>
{
fn run(self) {
let mut current_individual = G::default();
self.fitness_values.write().unwrap().insert(current_individual.clone(), T::zero());
loop {
// Receive a new individual to test from a fitness function
match self.fitness_receiver.recv() {
Ok(individual) => {
debug!("Fitness function wants fitness for: {:?}", individual);
if current_individual != individual && self.fitness_values.read().unwrap().contains_key(¤t_individual) {
current_individual = individual;
}
}
Err(_) => {}
}
let current_phenotype = P::from_genes(¤t_individual);
debug!("{}", current_phenotype.display_genotype_by_message());
// Send the requested individual to the scheduler
self.scheduler_sender.send(current_phenotype)
.expect("Scheduler receiver failed");
// Receive fitness from scheduler
match self.scheduler_receiver.recv() {
Ok(fitness) => {
debug!("Received fitness of {:?} for individual: {:?}", fitness, current_individual);
self.fitness_values.write().unwrap().insert(current_individual.clone(), fitness);
}
Err(_) => {}
}
}
}
}