-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscaling.rs
188 lines (168 loc) · 6.31 KB
/
scaling.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
use std::{io::BufWriter, fs::File, sync::{mpsc::{Sender, Receiver}, Arc}, path::Path};
use std::io::Write;
use rand::SeedableRng;
use rand_chacha::ChaCha8Rng;
use crate::{ga::{encoding::{delay_encoding::{DelayMapPhenotype, DelayGenotype}, ExtendedPhenotype, num_genes}}, node_state::MutexNodeStates, locality::sample_delays_genotype};
use crate::ga::encoding::priority_encoding::{PriorityGenotype, PriorityMapPhenotype};
use crate::ga::fitness::ExtendedFitness;
use crate::locality::sample_priority_genotype;
use crate::trace_comparisons::transform_to_message_type_graph;
#[allow(unused)]
pub struct ScalingExperiment<F: ExtendedFitness> {
scaling_file: BufWriter<File>,
scheduler_sender: Sender<DelayMapPhenotype>,
scheduler_receiver: Receiver<F>,
node_states: Arc<MutexNodeStates>
}
impl<F: ExtendedFitness> ScalingExperiment<F> {
#[allow(unused)]
pub fn new(scheduler_sender: Sender<DelayMapPhenotype>, scheduler_receiver: Receiver<F>, node_states: Arc<MutexNodeStates>) -> Self {
let scaling_file = File::create(Path::new("scaling.txt")).expect("Creating scaling file failed");
Self {
scaling_file: BufWriter::new(scaling_file),
scheduler_sender,
scheduler_receiver,
node_states
}
}
#[allow(unused)]
pub fn create_random_schedules(&mut self) {
let mut rng = ChaCha8Rng::seed_from_u64(8);
let mut schedules = vec![];
let mut genotypes = vec![];
let num_schedules = 500;
for _ in 0..num_schedules {
genotypes.push(sample_delays_genotype(num_genes(), 0, 4000, &mut rng));
}
for i in 0..num_schedules {
self.execute_schedule(&genotypes[i]);
schedules.push(transform_to_message_type_graph(&self.node_states.get_dependency_graph()));
}
let buf = serde_json::to_vec(&schedules).unwrap();
self.scaling_file.write_all(&buf[..]).unwrap();
}
#[allow(unused)]
fn execute_schedule(&self, genotype: &DelayGenotype) {
self.scheduler_sender.send(DelayMapPhenotype::from_genes(&genotype)).expect("Scheduler receiver failed");
// If the event cap is exceeded, something went wrong and we need to run again
match self.scheduler_receiver.recv() {
Ok(fit) => if fit == F::lowest_possible_fitness() {
self.execute_schedule(&genotype);
}
Err(_) => {}
}
}
}
#[allow(unused)]
pub struct PriorityScalingExperiment<F: ExtendedFitness> {
scaling_file: BufWriter<File>,
scheduler_sender: Sender<PriorityMapPhenotype>,
scheduler_receiver: Receiver<F>,
node_states: Arc<MutexNodeStates>
}
impl<F: ExtendedFitness> PriorityScalingExperiment<F> {
#[allow(unused)]
pub fn new(scheduler_sender: Sender<PriorityMapPhenotype>, scheduler_receiver: Receiver<F>, node_states: Arc<MutexNodeStates>) -> Self {
let scaling_file = File::create(Path::new("scaling.txt")).expect("Creating scaling file failed");
Self {
scaling_file: BufWriter::new(scaling_file),
scheduler_sender,
scheduler_receiver,
node_states
}
}
#[allow(unused)]
pub fn create_random_schedules(&mut self) {
let mut rng = ChaCha8Rng::seed_from_u64(8);
let mut schedules = vec![];
let mut genotypes = vec![];
let num_schedules = 500;
for _ in 0..num_schedules {
genotypes.push(sample_priority_genotype(num_genes(), &mut rng));
}
for i in 0..num_schedules {
self.execute_schedule(&genotypes[i]);
schedules.push(transform_to_message_type_graph(&self.node_states.get_dependency_graph()));
}
let buf = serde_json::to_vec(&schedules).unwrap();
self.scaling_file.write_all(&buf[..]).unwrap();
}
#[allow(unused)]
fn execute_schedule(&self, genotype: &PriorityGenotype) {
self.scheduler_sender.send(PriorityMapPhenotype::from_genes(&genotype)).expect("Scheduler receiver failed");
// If the event cap is exceeded, something went wrong and we need to run again
match self.scheduler_receiver.recv() {
Ok(fit) => if fit == F::lowest_possible_fitness() {
self.execute_schedule(&genotype);
}
Err(_) => {}
}
}
}
#[allow(unused)]
pub fn run_scaling_experiment<F: ExtendedFitness>(scheduler_sender: Sender<DelayMapPhenotype>, scheduler_receiver: Receiver<F>, node_states: Arc<MutexNodeStates>) {
let mut exp = ScalingExperiment::new(scheduler_sender, scheduler_receiver, node_states);
exp.create_random_schedules();
}
#[allow(unused)]
pub fn run_priority_scaling_experiment<F: ExtendedFitness>(scheduler_sender: Sender<PriorityMapPhenotype>, scheduler_receiver: Receiver<F>, node_states: Arc<MutexNodeStates>) {
let mut exp = PriorityScalingExperiment::new(scheduler_sender, scheduler_receiver, node_states);
exp.create_random_schedules();
}
pub fn mean(data: &[f64]) -> Option<f64> {
let sum = data.iter().sum::<f64>();
let count = data.len();
match count {
positive if positive > 0 => Some(sum / count as f64),
_ => None,
}
}
pub fn std_deviation(data: &[f64]) -> Option<f64> {
match (mean(data), data.len()) {
(Some(data_mean), count) if count > 0 => {
let variance = data.iter().map(|value| {
let diff = data_mean - (*value as f64);
diff * diff
}).sum::<f64>() / count as f64;
Some(variance.sqrt())
},
_ => None
}
}
#[cfg(test)]
mod tests {
use std::collections::HashMap;
use std::fs;
use std::io::BufReader;
use itertools::Itertools;
use petgraph::Graph;
use crate::node_state::MessageTypeDependencyEvent;
use crate::scaling::{mean, std_deviation};
fn import_schedules() -> Vec<Graph<MessageTypeDependencyEvent, ()>> {
let file = fs::File::open("scaling.txt")
.expect("Something went wrong opening the file");
let mut reader = BufReader::new(file);
serde_json::from_reader(&mut reader).unwrap()
}
#[test]
fn scaling_experiment() {
let schedules = import_schedules();
assert_eq!(schedules.len(), 500);
let mut frequencies_map: HashMap<String, Vec<f64>> = HashMap::new();
for schedule in schedules {
let size = schedule.node_count() as f64;
schedule.node_weights()
.counts_by(|node_weight| node_weight.message_type.clone())
.iter()
.for_each(|(message_type, count)| match frequencies_map.get_mut(message_type) {
Some(list) => list.push(*count as f64 / size),
None => {
frequencies_map.insert(message_type.clone(), vec![*count as f64 / size]);
}
});
}
for (message_type, frequency_list) in frequencies_map.into_iter() {
println!("{} -> mean: {}, std: {}", message_type, mean(frequency_list.as_slice()).unwrap(), std_deviation(frequency_list.as_slice()).unwrap());
}
}
}