Skip to content

Commit

Permalink
feat(picker): add HighestToScore Picker (#105)
Browse files Browse the repository at this point in the history
  • Loading branch information
chriba authored Nov 30, 2024
1 parent db1509d commit ab0385d
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ pub mod prelude {
pub use big_brain_derive::{ActionBuilder, ScorerBuilder};
pub use evaluators::{Evaluator, LinearEvaluator, PowerEvaluator, SigmoidEvaluator};
pub use measures::{ChebyshevDistance, Measure, WeightedProduct, WeightedSum};
pub use pickers::{FirstToScore, Highest, Picker};
pub use pickers::{FirstToScore, Highest, HighestToScore, Picker};
pub use scorers::{
AllOrNothing, EvaluatingScorer, FixedScore, MeasuredScorer, ProductOfScorers, Score,
ScorerBuilder, SumOfScorers, WinningScorer,
Expand Down
42 changes: 42 additions & 0 deletions src/pickers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,3 +83,45 @@ impl Picker for Highest {
})
}
}

/// Picker that chooses the highest `Choice` with a [`Score`] higher than its
/// configured `threshold`.
///
/// ### Example
///
/// ```
/// # use big_brain::prelude::*;
/// # fn main() {
/// Thinker::build()
/// .picker(HighestToScore::new(0.8))
/// // .when(...)
/// # ;
/// # }
/// ```
#[derive(Debug, Clone, Default)]
pub struct HighestToScore {
pub threshold: f32,
}

impl HighestToScore {
pub fn new(threshold: f32) -> Self {
Self { threshold }
}
}

impl Picker for HighestToScore {
fn pick<'a>(&self, choices: &'a [Choice], scores: &Query<&Score>) -> Option<&'a Choice> {
let mut highest_score = 0f32;

choices.iter().fold(None, |acc, choice| {
let score = choice.calculate(scores);

if score <= self.threshold || score <= highest_score {
return acc;
}

highest_score = score;
Some(choice)
})
}
}

0 comments on commit ab0385d

Please sign in to comment.