Skip to content

Commit

Permalink
Sort list for weight application
Browse files Browse the repository at this point in the history
  • Loading branch information
samuelmanzanera committed Mar 21, 2023
1 parent cf5d7fb commit 875cf0f
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 18 deletions.
29 changes: 14 additions & 15 deletions lib/archethic/beacon_chain.ex
Original file line number Diff line number Diff line change
Expand Up @@ -463,6 +463,7 @@ defmodule Archethic.BeaconChain do
%{sum_weight: sum_weight, sum_weighted_list: sum_weighted_list} =
list
|> clean_outliers()
# We want to apply a weight based on the tier of the latency
|> Utils.chunk_list_in(3)
|> weight_list()
|> Enum.reduce(%{sum_weight: 0.0, sum_weighted_list: 0.0}, fn {weight, weighted_list},
Expand All @@ -478,37 +479,35 @@ defmodule Archethic.BeaconChain do
defp clean_outliers(list) do
list_size = Enum.count(list)

sorted_list = Enum.sort(list)

# Compute percentiles (P80, P20) to remove the outliers
p1 = (0.8 * list_size) |> trunc()
p2 = (0.2 * list_size) |> trunc()

max = Enum.at(list, p1)
min = Enum.at(list, p2)
max = Enum.at(sorted_list, p1)
min = Enum.at(sorted_list, p2)

Enum.map(list, fn x ->
cond do
x < min ->
min
Enum.map(list, fn
x when x < min ->
min

x > max ->
max
x when x > max ->
max

true ->
x
end
x ->
x
end)
end

defp weight_list(list) do
list
|> Enum.with_index()
|> Enum.map(fn {list, i} ->
# Apply weight of the tier
weight = (i + 1) * (1 / 3)

weighted_list =
list
|> Enum.reverse()
|> Enum.map(&(&1 * weight))
weighted_list = Enum.map(list, &(&1 * weight))

{weight, weighted_list}
end)
Expand Down
2 changes: 1 addition & 1 deletion lib/archethic/beacon_chain/network_coordinates.ex
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ defmodule Archethic.BeaconChain.NetworkCoordinates do
ordered: false
)
|> Stream.filter(fn
# Filter slots without P2P view
# Filter slots with P2P view
{:ok, {:ok, %NetworkStats{stats: stats}}} when map_size(stats) > 0 ->
true

Expand Down
4 changes: 2 additions & 2 deletions test/archethic/beacon_chain_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -675,8 +675,8 @@ defmodule Archethic.BeaconChainTest do
Enum.map(node2_slots, &SummaryCache.add_slot(<<0>>, &1, "node2"))

assert %{
"node1" => [%{latency: 118}, %{latency: 185}, %{latency: 71}],
"node2" => [%{latency: 75}, %{latency: 108}, %{latency: 135}]
"node1" => [%{latency: 118}, %{latency: 138}, %{latency: 71}],
"node2" => [%{latency: 75}, %{latency: 118}, %{latency: 128}]
} = BeaconChain.get_network_stats(<<0>>)
end
end
Expand Down

0 comments on commit 875cf0f

Please sign in to comment.