Skip to content

Commit

Permalink
feat(rarity): Implement copy-create ratio for newly created states
Browse files Browse the repository at this point in the history
  • Loading branch information
huksys authored and ChristianMoesl committed Mar 1, 2021
1 parent ba7d0ed commit 00e3f10
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 2 deletions.
24 changes: 24 additions & 0 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,21 @@ fn is_valid_memory_size(v: &str) -> Result<(), String> {
})
}

fn is_ratio(v: &str) -> Result<(), String> {
let valid_range = 0.0_f64..=1.0f64;

match v.parse::<f64>() {
Ok(ratio) => {
if valid_range.contains(&ratio) {
Ok(())
} else {
Err("Expected range between 0.0 and 1.0".to_string())
}
}
Err(err) => Err(err.to_string()),
}
}

pub fn args() -> App<'static> {
App::new("Monster")
.version(crate_version!())
Expand Down Expand Up @@ -190,6 +205,15 @@ pub fn args() -> App<'static> {
.value_name("NUMBER")
.default_value("20")
.validator(is_u64))
.arg(
Arg::new("copy-init-ratio")
.about("Determines how much new states are copied instead of started from the beginning")
.long("copy-init-ratio")
.takes_value(true)
.value_name("RATIO")
.default_value("0.6")
.validator(is_ratio)
)
)
.setting(AppSettings::SubcommandRequiredElseHelp)
.global_setting(AppSettings::GlobalVersion)
Expand Down
5 changes: 5 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,10 @@ fn main() -> Result<()> {
.value_of_t::<u64>("selection")
.expect("value is validated already");

let copy_ratio = args
.value_of_t::<f64>("copy-init-ratio")
.expect("value is validated already");

if let Some(bug) = rarity::execute(
input,
output,
Expand All @@ -130,6 +134,7 @@ fn main() -> Result<()> {
selection,
cycles,
iterations,
copy_ratio,
)? {
info!("bug found:\n{}", bug);
} else {
Expand Down
29 changes: 28 additions & 1 deletion src/rarity.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![allow(clippy::too_many_arguments)]

use crate::{
bug::Bug as BugDef,
engine::{instruction_to_str, SyscallId},
Expand Down Expand Up @@ -149,6 +151,10 @@ fn score_states(states: &[&State]) -> Vec<u64> {
scores
}

fn random_index(len: usize) -> usize {
rand::random::<usize>() % len
}

impl State {
#[allow(dead_code)]
fn write_to_file<P>(&self, path: P) -> Result<(), EngineError>
Expand Down Expand Up @@ -187,6 +193,7 @@ pub fn execute<P>(
selection: u64,
cycles: u64,
iterations: u64,
copy_ratio: f64,
) -> Result<Option<Bug>, EngineError>
where
P: AsRef<Path>,
Expand All @@ -201,6 +208,7 @@ where
selection,
cycles,
iterations,
copy_ratio,
)
}

Expand All @@ -212,6 +220,7 @@ fn create_and_run<P>(
selection: u64,
cycles: u64,
iterations: u64,
copy_ratio: f64,
) -> Result<Option<Bug>, EngineError>
where
P: AsRef<Path>,
Expand All @@ -226,8 +235,26 @@ where
info!("Running rarity simulation round {}...", iteration + 1);

let to_create = number_of_states as usize - engines.len();
let to_copy = if engines.is_empty() {
0
} else {
f64::round(to_create as f64 * copy_ratio) as usize
};
let to_init = to_create - to_copy;

info!("Creating {} new states", to_create);
engines.extend((0..to_create).map(|_| Engine::new(&program, memory_size)));
debug!(
" {} engines will be copied and {} engines will be created",
to_copy, to_init
);
let initial_engines = engines.len();
engines.extend(
(0..to_copy)
.map(|_| random_index(initial_engines))
.map(|idx| engines[idx].clone())
.collect::<Vec<Engine>>(),
);
engines.extend((0..to_init).map(|_| Engine::new(&program, memory_size)));

let results = time_info!("Running engines", {
let results: Vec<_> = engines
Expand Down
2 changes: 1 addition & 1 deletion tests/rarity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ fn test_rarity_simulation() {

let output_dir = std::env::current_dir().unwrap().join("dumps");

let result = execute(&object, &output_dir, ByteSize::mb(1), 1, 1, 1, 1);
let result = execute(&object, &output_dir, ByteSize::mb(1), 1, 1, 1, 1, 0.6);

trace!("execution finished: {:?}", result);

Expand Down

0 comments on commit 00e3f10

Please sign in to comment.