mirror of
https://github.com/Cleanuparr/Cleanuparr.git
synced 2026-10-28 02:38:55 -04:00
removed duplicated weighted sampling loop from selectors
This commit is contained in:
1 parent
6759fe161d
commit
9a53bc3e57
3 files changed
+12
-57
No files matched your search
+1
-28
@@ -30,37 +30,10 @@ public sealed class BalancedWeightedSelector : IItemSelector
|
||||
.ToDictionary(x => x.Id, x => x.Rank);
|
||||
|
||||
// Composite weight = average of both ranks (higher = more likely to be selected)
|
||||
var selected = new List<long>(count);
|
||||
var pool = candidates
|
||||
.Select(c => (c.Id, Weight: (searchRanks[c.Id] + ageRanks[c.Id]) / 2.0))
|
||||
.ToList();
|
||||
|
||||
for (int i = 0; i < count && pool.Count > 0; i++)
|
||||
{
|
||||
double totalWeight = 0;
|
||||
for (int j = 0; j < pool.Count; j++)
|
||||
{
|
||||
totalWeight += pool[j].Weight;
|
||||
}
|
||||
|
||||
double target = Random.Shared.NextDouble() * totalWeight;
|
||||
double cumulative = 0;
|
||||
int selectedIndex = pool.Count - 1;
|
||||
|
||||
for (int j = 0; j < pool.Count; j++)
|
||||
{
|
||||
cumulative += pool[j].Weight;
|
||||
if (cumulative >= target)
|
||||
{
|
||||
selectedIndex = j;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
selected.Add(pool[selectedIndex].Id);
|
||||
pool.RemoveAt(selectedIndex);
|
||||
}
|
||||
|
||||
return selected;
|
||||
return OldestSearchWeightedSelector.WeightedRandom(pool, count);
|
||||
}
|
||||
}
|
||||
+1
-28
@@ -25,37 +25,10 @@ public sealed class CustomFormatGapWeightedSelector : IItemSelector
|
||||
double maxGap = candidates.Max(c => c.Gap);
|
||||
|
||||
// The recency half is at least 1/n, so every weight stays above zero
|
||||
List<long> selected = new(count);
|
||||
List<(long Id, double Weight)> pool = candidates
|
||||
.Select(c => (c.Id, Weight: 0.5 * (maxGap > 0 ? c.Gap / maxGap : 0) + 0.5 * searchRanks[c.Id] / n))
|
||||
.ToList();
|
||||
|
||||
for (int i = 0; i < count && pool.Count > 0; i++)
|
||||
{
|
||||
double totalWeight = 0;
|
||||
for (int j = 0; j < pool.Count; j++)
|
||||
{
|
||||
totalWeight += pool[j].Weight;
|
||||
}
|
||||
|
||||
double target = Random.Shared.NextDouble() * totalWeight;
|
||||
double cumulative = 0;
|
||||
int selectedIndex = pool.Count - 1;
|
||||
|
||||
for (int j = 0; j < pool.Count; j++)
|
||||
{
|
||||
cumulative += pool[j].Weight;
|
||||
if (cumulative >= target)
|
||||
{
|
||||
selectedIndex = j;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
selected.Add(pool[selectedIndex].Id);
|
||||
pool.RemoveAt(selectedIndex);
|
||||
}
|
||||
|
||||
return selected;
|
||||
return OldestSearchWeightedSelector.WeightedRandom(pool, count);
|
||||
}
|
||||
}
|
||||
+10
-1
@@ -33,7 +33,6 @@ public sealed class OldestSearchWeightedSelector : IItemSelector
|
||||
int count)
|
||||
{
|
||||
int n = ranked.Count;
|
||||
var selected = new List<long>(count);
|
||||
|
||||
// Build initial weights from rank: highest rank (index 0) gets weight N, lowest gets 1
|
||||
var pool = new List<(long Id, double Weight)>(n);
|
||||
@@ -42,6 +41,16 @@ public sealed class OldestSearchWeightedSelector : IItemSelector
|
||||
pool.Add((ranked[i].Id, n - i));
|
||||
}
|
||||
|
||||
return WeightedRandom(pool, count);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Performs weighted random selection without replacement. Consumes <paramref name="pool"/>.
|
||||
/// </summary>
|
||||
internal static List<long> WeightedRandom(List<(long Id, double Weight)> pool, int count)
|
||||
{
|
||||
List<long> selected = new(count);
|
||||
|
||||
for (int i = 0; i < count && pool.Count > 0; i++)
|
||||
{
|
||||
double totalWeight = 0;
|
||||
|
||||
Reference in new issue
Block a user