refactor election score (#10834)

* refactor election score

* Test for ord

* remove reference

* vec -> slice

* change iter to iter_by_significance

* improve doc

* fix typo

* add explanation about [u128; 3]

* consolidate threshold and epsilon

* random fixes

* rename

* remove Into

* make iter_by_sig private

* remove vec

* Fix tests
This commit is contained in:
Kian Paimani
2022-02-16 10:40:16 +00:00
committed by GitHub
parent 9d8e5639d5
commit 6b7eb2e2d7
9 changed files with 281 additions and 124 deletions
@@ -23,8 +23,8 @@ use common::*;
use honggfuzz::fuzz;
use rand::{self, SeedableRng};
use sp_npos_elections::{
assignment_ratio_to_staked_normalized, is_score_better, seq_phragmen, to_supports,
ElectionResult, EvaluateSupport, VoteWeight,
assignment_ratio_to_staked_normalized, seq_phragmen, to_supports, ElectionResult,
EvaluateSupport, VoteWeight,
};
use sp_runtime::Perbill;
@@ -60,7 +60,7 @@ fn main() {
.unwrap();
let score = to_supports(staked.as_ref()).evaluate();
if score[0] == 0 {
if score.minimal_stake == 0 {
// such cases cannot be improved by balancing.
return
}
@@ -80,7 +80,8 @@ fn main() {
to_supports(staked.as_ref()).evaluate()
};
let enhance = is_score_better(balanced_score, unbalanced_score, Perbill::zero());
let enhance =
balanced_score.strict_threshold_better(unbalanced_score, Perbill::zero());
println!(
"iter = {} // {:?} -> {:?} [{}]",
@@ -90,9 +91,9 @@ fn main() {
// The only guarantee of balancing is such that the first and third element of the
// score cannot decrease.
assert!(
balanced_score[0] >= unbalanced_score[0] &&
balanced_score[1] == unbalanced_score[1] &&
balanced_score[2] <= unbalanced_score[2]
balanced_score.minimal_stake >= unbalanced_score.minimal_stake &&
balanced_score.sum_stake == unbalanced_score.sum_stake &&
balanced_score.sum_stake_squared <= unbalanced_score.sum_stake_squared
);
}
});
@@ -23,8 +23,8 @@ use common::*;
use honggfuzz::fuzz;
use rand::{self, SeedableRng};
use sp_npos_elections::{
assignment_ratio_to_staked_normalized, is_score_better, phragmms, to_supports, ElectionResult,
EvaluateSupport, VoteWeight,
assignment_ratio_to_staked_normalized, phragmms, to_supports, ElectionResult, EvaluateSupport,
VoteWeight,
};
use sp_runtime::Perbill;
@@ -60,7 +60,7 @@ fn main() {
.unwrap();
let score = to_supports(&staked).evaluate();
if score[0] == 0 {
if score.minimal_stake == 0 {
// such cases cannot be improved by balancing.
return
}
@@ -77,7 +77,7 @@ fn main() {
to_supports(staked.as_ref()).evaluate()
};
let enhance = is_score_better(balanced_score, unbalanced_score, Perbill::zero());
let enhance = balanced_score.strict_threshold_better(unbalanced_score, Perbill::zero());
println!(
"iter = {} // {:?} -> {:?} [{}]",
@@ -87,9 +87,9 @@ fn main() {
// The only guarantee of balancing is such that the first and third element of the score
// cannot decrease.
assert!(
balanced_score[0] >= unbalanced_score[0] &&
balanced_score[1] == unbalanced_score[1] &&
balanced_score[2] <= unbalanced_score[2]
balanced_score.minimal_stake >= unbalanced_score.minimal_stake &&
balanced_score.sum_stake == unbalanced_score.sum_stake &&
balanced_score.sum_stake_squared <= unbalanced_score.sum_stake_squared
);
});
}