Add track_stability option to CalibrationStage (#781)

* add

* Update gramatron.rs

* Update emu.rs

* try

* clp
This commit is contained in:
Dongjia "toka" Zhang 2022-09-13 09:39:17 +02:00 committed by GitHub
parent 7f7e0ee6ac
commit 7aadf31246
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 53 additions and 32 deletions

View File

@ -79,6 +79,7 @@ crate::impl_serdeany!(GramatronIdxMapMetadata);
impl GramatronIdxMapMetadata { impl GramatronIdxMapMetadata {
/// Creates a new [`struct@GramatronIdxMapMetadata`]. /// Creates a new [`struct@GramatronIdxMapMetadata`].
#[must_use] #[must_use]
#[allow(clippy::or_fun_call)]
pub fn new(input: &GramatronInput) -> Self { pub fn new(input: &GramatronInput) -> Self {
let mut map = HashMap::default(); let mut map = HashMap::default();
for i in 0..input.terminals().len() { for i in 0..input.terminals().len() {

View File

@ -73,6 +73,7 @@ where
map_observer_name: String, map_observer_name: String,
map_name: String, map_name: String,
stage_max: usize, stage_max: usize,
track_stability: bool,
phantom: PhantomData<(I, O, OT, S)>, phantom: PhantomData<(I, O, OT, S)>,
} }
@ -142,12 +143,13 @@ where
.ok_or_else(|| Error::key_not_found("MapObserver not found".to_string()))? .ok_or_else(|| Error::key_not_found("MapObserver not found".to_string()))?
.to_vec(); .to_vec();
let mut unstable_entries: Vec<usize> = vec![];
let map_len: usize = map_first.len();
// Run CAL_STAGE_START - 1 times, increase by 2 for every time a new // Run CAL_STAGE_START - 1 times, increase by 2 for every time a new
// run is found to be unstable, with CAL_STAGE_MAX total runs. // run is found to be unstable, with CAL_STAGE_MAX total runs.
let mut i = 1; let mut i = 1;
let mut has_errors = false; let mut has_errors = false;
let mut unstable_entries: Vec<usize> = vec![];
let map_len: usize = map_first.len();
while i < iter { while i < iter {
let input = state let input = state
.corpus() .corpus()
@ -182,35 +184,37 @@ where
.observers_mut() .observers_mut()
.post_exec_all(state, &input, &exit_kind)?; .post_exec_all(state, &input, &exit_kind)?;
let map = &executor if self.track_stability {
.observers() let map = &executor
.match_name::<O>(&self.map_observer_name) .observers()
.ok_or_else(|| Error::key_not_found("MapObserver not found".to_string()))? .match_name::<O>(&self.map_observer_name)
.to_vec(); .ok_or_else(|| Error::key_not_found("MapObserver not found".to_string()))?
.to_vec();
let history_map = &mut state let history_map = &mut state
.named_metadata_mut() .named_metadata_mut()
.get_mut::<MapFeedbackMetadata<O::Entry>>(&self.map_name) .get_mut::<MapFeedbackMetadata<O::Entry>>(&self.map_name)
.unwrap() .unwrap()
.history_map; .history_map;
if history_map.len() < map_len { if history_map.len() < map_len {
history_map.resize(map_len, O::Entry::default()); history_map.resize(map_len, O::Entry::default());
} }
for (idx, (first, (cur, history))) in map_first for (idx, (first, (cur, history))) in map_first
.iter() .iter()
.zip(map.iter().zip(history_map.iter_mut())) .zip(map.iter().zip(history_map.iter_mut()))
.enumerate() .enumerate()
{ {
if *first != *cur && *history != O::Entry::max_value() { if *first != *cur && *history != O::Entry::max_value() {
*history = O::Entry::max_value(); *history = O::Entry::max_value();
unstable_entries.push(idx); unstable_entries.push(idx);
}; };
} }
if !unstable_entries.is_empty() && iter < CAL_STAGE_MAX { if !unstable_entries.is_empty() && iter < CAL_STAGE_MAX {
iter += 2; iter += 2;
}
} }
i += 1; i += 1;
} }
@ -302,6 +306,26 @@ where
map_observer_name: map_feedback.observer_name().to_string(), map_observer_name: map_feedback.observer_name().to_string(),
map_name: map_feedback.name().to_string(), map_name: map_feedback.name().to_string(),
stage_max: CAL_STAGE_START, stage_max: CAL_STAGE_START,
track_stability: true,
phantom: PhantomData,
}
}
/// Create a new [`CalibrationStage`], but without checking stability.
#[must_use]
pub fn ignore_stability<N, R>(map_feedback: &MapFeedback<I, N, O, R, S, O::Entry>) -> Self
where
O::Entry:
PartialEq + Default + Copy + 'static + Serialize + serde::de::DeserializeOwned + Debug,
R: Reducer<O::Entry>,
for<'it> O: AsIter<'it, Item = O::Entry>,
N: IsNovel<O::Entry>,
{
Self {
map_observer_name: map_feedback.observer_name().to_string(),
map_name: map_feedback.name().to_string(),
stage_max: CAL_STAGE_START,
track_stability: false,
phantom: PhantomData, phantom: PhantomData,
} }
} }

View File

@ -433,11 +433,7 @@ extern "C" fn gdb_cmd(buf: *const u8, len: usize, data: *const ()) -> i32 {
let closure = &mut *(data as *mut Box<dyn for<'r> FnMut(&Emulator, &'r str) -> bool>); let closure = &mut *(data as *mut Box<dyn for<'r> FnMut(&Emulator, &'r str) -> bool>);
let cmd = std::str::from_utf8_unchecked(std::slice::from_raw_parts(buf, len)); let cmd = std::str::from_utf8_unchecked(std::slice::from_raw_parts(buf, len));
let emu = Emulator::new_empty(); let emu = Emulator::new_empty();
if closure(&emu, cmd) { i32::from(closure(&emu, cmd))
1
} else {
0
}
} }
} }