Skip to content

Commit

Permalink
Use exponential moving average to adjust solution signature threshold.
Browse files Browse the repository at this point in the history
  • Loading branch information
FiveMovesAhead committed Jun 17, 2024
1 parent 98d2d21 commit 4d432bd
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 21 deletions.
40 changes: 20 additions & 20 deletions tig-protocol/src/add_block.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
use crate::context::*;
use logging_timer::time;
use rand::{prelude::SliceRandom, rngs::StdRng, SeedableRng};
use std::{
collections::{HashMap, HashSet},
ops::Mul,
};
use std::collections::{HashMap, HashSet};
use tig_structs::{config::*, core::*};
use tig_utils::*;

Expand Down Expand Up @@ -455,21 +452,8 @@ async fn update_solution_signature_thresholds<T: Context>(ctx: &mut T, block: &B
}

for challenge_id in block.data().active_challenge_ids.iter() {
let num_new_solutions = *num_new_solutions_by_challenge
.get(challenge_id)
.unwrap_or(&0) as f64;
let equilibrium_rate = config.qualifiers.total_qualifiers_threshold as f64
/ config.benchmark_submissions.lifespan_period as f64;
let percentage_error = 1f64
- num_new_solutions
/ (config.solution_signature.equilibrium_rate_multiplier * equilibrium_rate);
let max_threshold = u32::MAX as f64;
let percent_delta = (percentage_error * config.solution_signature.percent_error_multiplier)
.abs()
.clamp(0f64, config.solution_signature.max_percent_delta)
.mul(if percentage_error < 0f64 { -1f64 } else { 1f64 });

let prev_solution_signature_threshold =
let current_threshold =
match get_challenge_by_id(ctx, challenge_id, Some(&block.details.prev_block_id))
.await
.unwrap_or_else(|e| panic!("get_challenge_by_id error: {:?}", e))
Expand All @@ -478,14 +462,30 @@ async fn update_solution_signature_thresholds<T: Context>(ctx: &mut T, block: &B
Some(data) => *data.solution_signature_threshold() as f64,
None => max_threshold,
};
let current_probability = current_threshold / max_threshold;
let current_rate = *num_new_solutions_by_challenge
.get(challenge_id)
.unwrap_or(&0) as f64;

let equilibrium_rate = config.qualifiers.total_qualifiers_threshold as f64
/ config.benchmark_submissions.lifespan_period as f64;
let target_rate = config.solution_signature.equilibrium_rate_multiplier * equilibrium_rate;
let target_probability = if current_rate == 0.0 {
1.0
} else {
(current_probability * target_rate / current_rate).clamp(0.0, 1.0)
};
let target_threshold = (target_probability * max_threshold).clamp(0.0, max_threshold);

let threshold_decay = config.solution_signature.threshold_decay.unwrap_or(0.99);
let mut block_data = get_challenge_by_id(ctx, challenge_id, Some(&block.id))
.await
.unwrap_or_else(|e| panic!("get_challenge_by_id error: {:?}", e))
.block_data()
.clone();
block_data.solution_signature_threshold = Some(
(prev_solution_signature_threshold + percent_delta * max_threshold)
.clamp(0f64, max_threshold) as u32,
(current_threshold * threshold_decay + target_threshold * (1.0 - threshold_decay))
.clamp(0.0, max_threshold) as u32,
);

ctx.update_challenge_block_data(challenge_id, &block.id, &block_data)
Expand Down
2 changes: 1 addition & 1 deletion tig-structs/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ serializable_struct_with_getters! {
}
serializable_struct_with_getters! {
SolutionSignatureConfig {
max_percent_delta: f64,
threshold_decay: Option<f64>,
equilibrium_rate_multiplier: f64,
percent_error_multiplier: f64,
}
Expand Down

0 comments on commit 4d432bd

Please sign in to comment.