From 8ae2be849e09f8ebd091cc628eee62c09d1f8663 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20M=C3=B6sl?= Date: Tue, 9 Mar 2021 22:34:40 +0100 Subject: [PATCH] fix: ordering for harmonic mean (less is always better) (#114) --- src/engine/rarity_simulation.rs | 52 +++++++++++---------------------- 1 file changed, 17 insertions(+), 35 deletions(-) diff --git a/src/engine/rarity_simulation.rs b/src/engine/rarity_simulation.rs index 91938be5..4fed81c3 100644 --- a/src/engine/rarity_simulation.rs +++ b/src/engine/rarity_simulation.rs @@ -38,14 +38,7 @@ use riscu::{ decode, types::*, DecodingError, Instruction, Program, ProgramSegment, Register, INSTRUCTION_SIZE as INSTR_SIZE, }; -use std::{ - cmp::{min, Ordering}, - collections::HashMap, - fmt, - iter::IntoIterator, - mem::size_of, - sync::Arc, -}; +use std::{cmp::min, collections::HashMap, fmt, iter::IntoIterator, mem::size_of, sync::Arc}; use strum::{EnumString, EnumVariantNames, IntoStaticStr}; use thiserror::Error; @@ -209,7 +202,7 @@ impl BugFinder for RaritySimulation { self.options.amount_of_states - running.len() ); - let (scores, ordering) = time_info!("Scoring states", { + let scores = time_info!("Scoring states", { let states: Vec<_> = running.iter().map(|e| e.state()).collect(); score_with_mean(&states[..], self.options.mean) @@ -217,7 +210,7 @@ impl BugFinder for RaritySimulation { let selection = min(self.options.selection, running.len()); - executors = select_rarest(running, selection, scores, ordering); + executors = select_rarest(running, selection, scores); info!("selecting rarest {} states", selection); } @@ -299,35 +292,24 @@ fn select_rarest( executors: impl IntoIterator>, selection: usize, scores: Vec, - ord: Ordering, ) -> Vec> { - let iter = executors.into_iter().zip(scores); - - let sorted = if ord == Ordering::Less { - iter.sorted_unstable_by(|l, r| l.1.partial_cmp(&r.1).expect("no NaN in scores")) - } else { - iter.sorted_unstable_by(|l, r| r.1.partial_cmp(&l.1).expect("no NaN in scores")) - }; - - sorted.map(|x| x.0).take(selection).collect() + executors + .into_iter() + .zip(scores) + .sorted_unstable_by(|l, r| l.1.partial_cmp(&r.1).expect("no NaN in scores")) + .map(|x| x.0) + .take(selection) + .collect() } -fn score_with_mean(states: &[&State], mean: MeanType) -> (Vec, Ordering) { +fn score_with_mean(states: &[&State], mean: MeanType) -> Vec { match mean { - MeanType::Harmonic => { - let scores = compute_scores(states, |cs| { - (cs.len() as f64) / cs.iter().map(|c| 1_f64 / (*c as f64)).sum::() - }); - - (scores, Ordering::Greater) - } - MeanType::Arithmetic => { - let scores = compute_scores(states, |cs| { - cs.iter().sum::() as f64 / (cs.len() as f64) - }); - - (scores, Ordering::Less) - } + MeanType::Harmonic => compute_scores(states, |cs| { + (cs.len() as f64) / cs.iter().map(|c| 1_f64 / (*c as f64)).sum::() + }), + MeanType::Arithmetic => compute_scores(states, |cs| { + cs.iter().sum::() as f64 / (cs.len() as f64) + }), } }