Skip to content

Commit 2e51ebd

Browse files
author
Samuel Dare
committed
test: compute alpha for 256 miners
1 parent a4d6dfd commit 2e51ebd

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

pallets/subtensor/tests/epoch.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ use sp_core::U256;
77
use sp_runtime::DispatchError;
88
use std::time::Instant;
99
use substrate_fixed::types::I32F32;
10+
use substrate_fixed::transcendental::exp;
11+
1012
mod mock;
1113

1214
pub fn fixed(val: f32) -> I32F32 {
@@ -2281,6 +2283,34 @@ fn test_compute_alpha_values() {
22812283
assert_approx_eq(alpha[2], expected_alpha_2, epsilon);
22822284
}
22832285

2286+
#[test]
2287+
fn test_compute_alpha_values_256_miners() {
2288+
// Define the consensus values for 256 miners.
2289+
let consensus: Vec<I32F32> = (0..256).map(|i| I32F32::from_num(i as f32 / 255.0)).collect();
2290+
// Define the logistic function parameters 'a' and 'b'.
2291+
let a = I32F32::from_num(1.0);
2292+
let b = I32F32::from_num(0.0);
2293+
2294+
// Compute the alpha values using the function.
2295+
let alpha = SubtensorModule::compute_alpha_values(&consensus, a, b);
2296+
2297+
// Ensure the length of the alpha vector matches the consensus vector.
2298+
assert_eq!(alpha.len(), consensus.len());
2299+
2300+
// Manually compute the expected alpha values for each consensus value.
2301+
// The logistic function is: 1 / (1 + exp(b - a * c))
2302+
// where c is the consensus value.
2303+
2304+
// Define an epsilon for approximate equality checks.
2305+
let epsilon = I32F32::from_num(1e-6);
2306+
2307+
for (i, &c) in consensus.iter().enumerate() {
2308+
let exp_val = exp(b - a * c).unwrap_or(I32F32::from_num(0.0));
2309+
let expected_alpha = I32F32::from_num(1.0).saturating_div(I32F32::from_num(1.0).saturating_add(exp_val));
2310+
// Assert that the computed alpha values match the expected values within the epsilon.
2311+
assert_approx_eq(alpha[i], expected_alpha, epsilon);
2312+
}
2313+
}
22842314
#[test]
22852315
fn test_clamp_alpha_values() {
22862316
// Define the alpha values.

0 commit comments

Comments
 (0)