Skip to content

Commit

Permalink
consider rating regulator orthogonal, make conversions more explicit
Browse files Browse the repository at this point in the history
  • Loading branch information
niklasf committed Nov 2, 2024
1 parent f6b67bd commit 08dab0a
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 46 deletions.
1 change: 0 additions & 1 deletion research/src/bin/replay_encounters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -479,7 +479,6 @@ fn main() -> Result<(), Box<dyn StdError>> {
for &rating_periods_per_day in &opt.rating_periods_per_day {
experiments.push(Experiment {
rating_system: RatingSystem::builder()
.rating_regulator_factor(1.0)
.min_rating(RatingScalar(-f64::INFINITY))
.max_rating(RatingScalar(f64::INFINITY))
.min_deviation(RatingDifference(min_deviation))
Expand Down
21 changes: 6 additions & 15 deletions src/internal_rating.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,15 @@ use crate::rating::RatingDifference;
#[derive(Debug, Clone, Copy)]
pub(crate) struct InternalRatingDifference(pub f64);

impl From<InternalRatingDifference> for f64 {
#[inline]
fn from(InternalRatingDifference(difference): InternalRatingDifference) -> f64 {
difference
}
}

impl From<RatingDifference> for InternalRatingDifference {
#[inline]
fn from(RatingDifference(difference): RatingDifference) -> InternalRatingDifference {
impl InternalRatingDifference {
pub fn from_external(
RatingDifference(difference): RatingDifference,
) -> InternalRatingDifference {
InternalRatingDifference(difference / INTERNAL_RATING_SCALE)
}
}

impl From<InternalRatingDifference> for RatingDifference {
#[inline]
fn from(InternalRatingDifference(difference): InternalRatingDifference) -> RatingDifference {
RatingDifference(difference * INTERNAL_RATING_SCALE)
pub fn to_external(self) -> RatingDifference {
RatingDifference(self.0 * INTERNAL_RATING_SCALE)
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/rating.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ impl RatingDifference {

#[inline]
pub(crate) fn to_internal(self) -> InternalRatingDifference {
InternalRatingDifference::from(self)
InternalRatingDifference::from_external(self)
}
}

Expand Down
35 changes: 6 additions & 29 deletions src/rating_system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ pub struct RatingSystemBuilder {
max_convergence_iterations: u32,

max_rating_delta: RatingDifference,
rating_regulator_factor: f64,
}

impl RatingSystemBuilder {
Expand Down Expand Up @@ -137,14 +136,6 @@ impl RatingSystemBuilder {
self
}

/// Set the factor by which rating gains (but not losses) are multiplied.
/// The default is `1.015`.
pub fn rating_regulator_factor(&mut self, rating_regulator_factor: f64) -> &mut Self {
assert!(rating_regulator_factor >= 0.0);
self.rating_regulator_factor = rating_regulator_factor;
self
}

pub fn build(&self) -> RatingSystem {
assert!(self.min_rating <= self.max_rating);
assert!(self.min_deviation <= self.max_deviation);
Expand All @@ -170,7 +161,6 @@ impl RatingSystemBuilder {
max_convergence_iterations: self.max_convergence_iterations,

max_rating_delta: self.max_rating_delta,
rating_regulator_factor: self.rating_regulator_factor,
}
}
}
Expand Down Expand Up @@ -202,7 +192,6 @@ pub struct RatingSystem {
max_convergence_iterations: u32,

max_rating_delta: RatingDifference,
rating_regulator_factor: f64,
}

impl Default for RatingSystem {
Expand Down Expand Up @@ -238,7 +227,6 @@ impl RatingSystem {
max_convergence_iterations: 1000,

max_rating_delta: RatingDifference(700.0),
rating_regulator_factor: 1.015,
}
}

Expand Down Expand Up @@ -298,10 +286,6 @@ impl RatingSystem {
self.max_rating_delta
}

pub fn rating_regulator_factor(&self) -> f64 {
self.rating_regulator_factor
}

/// Construct an initial rating for a new player.
pub fn new_rating(&self) -> Rating {
Rating {
Expand All @@ -319,11 +303,12 @@ impl RatingSystem {
pub fn preview_deviation(&self, rating: &Rating, at: Instant) -> RatingDifference {
let rating = self.clamp_rating(rating);

RatingDifference::from(new_deviation(
new_deviation(
rating.deviation.to_internal(),
rating.volatility,
at.elapsed_since(rating.at),
))
)
.to_external()
.clamp(self.min_deviation, self.max_deviation)
}

Expand Down Expand Up @@ -460,23 +445,15 @@ impl RatingSystem {
// Step 8
Ok(self.clamp_rating(&Rating {
rating: us.rating
+ self
.regulate(RatingDifference::from(mu_prime_diff))
+ mu_prime_diff
.to_external()
.clamp(-self.max_rating_delta, self.max_rating_delta),
deviation: RatingDifference::from(phi_prime),
deviation: phi_prime.to_external(),
volatility: sigma_prime,
at: now,
}))
}

fn regulate(&self, diff: RatingDifference) -> RatingDifference {
if diff > RatingDifference(0.0) {
self.rating_regulator_factor * diff
} else {
diff
}
}

fn clamp_rating(&self, rating: &Rating) -> Rating {
Rating {
rating: rating.rating.clamp(self.min_rating, self.max_rating),
Expand Down

0 comments on commit 08dab0a

Please sign in to comment.