use super::random_indexes_with_distance; use std::error::Error; use std::fs::File; use std::io::{BufRead, BufReader}; use std::path::Path; use std::collections::{HashMap, HashSet}; use std::iter::FromIterator; use serde::Deserialize; use rand::Rng; use rand::seq::SliceRandom; use rand::SeedableRng; use rand::rngs::StdRng; use loda_rust_core::parser::{InstructionId, ParseInstructionId}; use crate::common::parse_csv_data; type ValueAndWeight = (i32,u32); type ValueAndWeightVector = Vec; /// Instructions that takes a constant value. /// /// The most used combo: `add $0,1` (addition by 1) /// /// Almost as popular combo: `sub $0,1` (subtract by 1) /// /// Usecase: /// During mining, when mutating an `add` instruction. /// /// It's a time waster making poor choices of constants. /// The miner originally picked random integers, but it was excruciating slow. /// /// Time can be saved this way. /// /// Before mining: analyze all programs and build a histogram. /// /// During mining: make weighted choices from the histogram. #[derive(Clone, Debug)] pub struct HistogramInstructionConstant { instruction_and_valueweightvector: HashMap } impl HistogramInstructionConstant { pub fn number_of_items(&self) -> usize { self.instruction_and_valueweightvector.len() } #[allow(dead_code)] pub fn load_csv_file(path: &Path) -> Result> { let file = File::open(path)?; let mut reader = BufReader::new(file); Self::create(&mut reader) } const SHUFFLE_COUNT: usize = 0; fn create(reader: &mut dyn BufRead) -> Result> { let records_original: Vec = parse_csv_data::(reader)?; // Shuffle the items slightly let mut records: Vec = records_original.clone(); let seed: u64 = 1; let mut rng = StdRng::seed_from_u64(seed); let indexes: Vec = random_indexes_with_distance(&mut rng, records.len(), Self::SHUFFLE_COUNT); for index in indexes { records[index].count = records_original[index].count; } let instruction_and_valueweightvector: HashMap = Record::instruction_and_valueweightvector(&records); let result = Self { instruction_and_valueweightvector: instruction_and_valueweightvector }; Ok(result) } pub fn choose_weighted(&self, rng: &mut R, instruction_id: InstructionId) -> Option { let value_and_weight_vec: &ValueAndWeightVector = match self.instruction_and_valueweightvector.get(&instruction_id) { Some(value) => value, None => { return None; } }; let value: i32 = value_and_weight_vec.choose_weighted(rng, |item| item.1).unwrap().0; Some(value) } } #[derive(Clone, Debug, Deserialize)] struct Record { count: u32, instruction: String, constant: i32, } impl Record { fn unique_instruction_ids(records: &Vec) -> HashSet { let mut instruction_ids = Vec::::new(); for record in records { match InstructionId::parse(&record.instruction, 0) { Ok(instruction_id) => { instruction_ids.push(instruction_id); }, Err(_) => {} } } HashSet::from_iter(instruction_ids.iter().cloned()) } fn value_and_weight_vec(records: &Vec, instruction_id: InstructionId) -> ValueAndWeightVector { let mut value_and_weight_vec: ValueAndWeightVector = vec!(); let needle: String = instruction_id.to_string(); let mut already_known = HashSet::::new(); for record in records { if record.instruction != needle { continue; } if already_known.contains(&record.constant) { // Keep only the first value. Ignore following duplicates. continue; } let value = (record.constant, record.count); value_and_weight_vec.push(value); already_known.insert(record.constant); } value_and_weight_vec } fn instruction_and_valueweightvector(records: &Vec) -> HashMap { let instruction_ids: HashSet = Record::unique_instruction_ids(&records); let mut result: HashMap = HashMap::new(); for instruction_id in instruction_ids { let value_and_weight_vec: ValueAndWeightVector = Record::value_and_weight_vec(&records, instruction_id); if value_and_weight_vec.is_empty() { continue; } result.insert(instruction_id, value_and_weight_vec); } result } } #[cfg(test)] mod tests { use super::*; use rand::SeedableRng; use rand::rngs::StdRng; #[test] fn test_10000_parse_csv_data() { let data = "\ count;instruction;constant 36545;add;1 33648;sub;1 17147;mul;-2 "; let mut input: &[u8] = data.as_bytes(); let records: Vec = parse_csv_data(&mut input).unwrap(); let strings: Vec = records.iter().map(|record| { format!("{} {} {}", record.count, record.instruction, record.constant) }).collect(); let strings_joined: String = strings.join(","); assert_eq!(strings_joined, "36545 add 1,33648 sub 1,17147 mul -2"); } #[test] fn test_10001_unique_instruction_ids() { let data = "\ count;instruction;constant 36545;add;1 9232;add;2 666;unknown;23 555;sub;1 171;mul;-2 92;add;3 "; let mut input: &[u8] = data.as_bytes(); let records: Vec = parse_csv_data(&mut input).unwrap(); let actual: HashSet = Record::unique_instruction_ids(&records); let v = vec![InstructionId::Add, InstructionId::Subtract, InstructionId::Multiply]; let expected: HashSet = HashSet::from_iter(v); assert_eq!(actual, expected); } #[test] fn test_10002_value_and_weight_vec_typical_data() { let data = "\ count;instruction;constant 36545;add;1 92;add;2 9232;add;3 100;add;4 "; let mut input: &[u8] = data.as_bytes(); let records: Vec = parse_csv_data(&mut input).unwrap(); let actual: ValueAndWeightVector = Record::value_and_weight_vec(&records, InstructionId::Add); let expected: ValueAndWeightVector = vec![(1,36545),(2,92),(3,9232),(4,100)]; assert_eq!(actual, expected); } #[test] fn test_10003_value_and_weight_vec_without_duplicates() { let data = "\ count;instruction;constant 1000;add;1 1000;add;2 1000;add;1 1000;add;3 1001;add;1 "; let mut input: &[u8] = data.as_bytes(); let records: Vec = parse_csv_data(&mut input).unwrap(); let actual: ValueAndWeightVector = Record::value_and_weight_vec(&records, InstructionId::Add); let expected: ValueAndWeightVector = vec![(1,1000),(2,1000),(3,1000)]; assert_eq!(actual, expected); } #[test] fn test_10004_instruction_and_valueweightvector() { let data = "\ count;instruction;constant 1001;add;1 1002;add;2 999;div;2 998;mul;-1 "; let mut input: &[u8] = data.as_bytes(); let records: Vec = parse_csv_data(&mut input).unwrap(); let actual: HashMap = Record::instruction_and_valueweightvector(&records); assert_eq!(actual.len(), 3); } #[test] fn test_20000_instruction_and_valueweightvector_add() { let data = "\ count;instruction;constant 1000;add;1984 1000;mul;-1 "; let mut input: &[u8] = data.as_bytes(); let instance = HistogramInstructionConstant::create(&mut input).unwrap(); let mut rng = StdRng::seed_from_u64(0); let actual = instance.choose_weighted(&mut rng, InstructionId::Add); assert_eq!(actual, Some(1984)); } #[test] fn test_20001_instruction_and_valueweightvector_multiply() { let data = "\ count;instruction;constant 1000;add;1984 1000;mul;-1 "; let mut input: &[u8] = data.as_bytes(); let instance = HistogramInstructionConstant::create(&mut input).unwrap(); let mut rng = StdRng::seed_from_u64(0); let actual = instance.choose_weighted(&mut rng, InstructionId::Multiply); assert_eq!(actual, Some(-1)); } #[test] fn test_20002_instruction_and_valueweightvector_no_such_instruction() { let data = "\ count;instruction;constant 1000;add;1984 1000;mul;-1 "; let mut input: &[u8] = data.as_bytes(); let instance = HistogramInstructionConstant::create(&mut input).unwrap(); let mut rng = StdRng::seed_from_u64(0); let actual = instance.choose_weighted(&mut rng, InstructionId::GCD); assert_eq!(actual, None); } }