Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add HighestToScore Picker #105

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
})
}
}