Tweak scoring algo
This commit is contained in:
+12
-21
@@ -46,10 +46,10 @@ export class Router {
|
|||||||
|
|
||||||
// Utilities for processing hints
|
// Utilities for processing hints
|
||||||
|
|
||||||
getGroupScores = (groups: string[][]) => {
|
scoreGroups = (groups: string[][]) => {
|
||||||
const scores: RouteScores = {}
|
const scores: RouteScores = {}
|
||||||
|
|
||||||
groups.forEach((urls, i) => {
|
groups.filter(g => g?.length > 0).forEach((urls, i) => {
|
||||||
for (const url of shuffle(uniq(urls))) {
|
for (const url of shuffle(uniq(urls))) {
|
||||||
if (!scores[url]) {
|
if (!scores[url]) {
|
||||||
scores[url] = {score: 0, count: 0}
|
scores[url] = {score: 0, count: 0}
|
||||||
@@ -62,21 +62,18 @@ export class Router {
|
|||||||
|
|
||||||
// Use log-sum-exp to get a a weighted sum
|
// Use log-sum-exp to get a a weighted sum
|
||||||
for (const [url, score] of Object.entries(scores)) {
|
for (const [url, score] of Object.entries(scores)) {
|
||||||
const weight = Math.log(groups.length / score.count)
|
const quality = this.options.getRelayQuality?.(url) || 1
|
||||||
const thisScore = Math.log1p(Math.exp(score.score - score.count))
|
|
||||||
const thatScore = this.options.getRelayQuality?.(url) || 1
|
|
||||||
|
|
||||||
score.score = (weight + thisScore) * thatScore
|
score.score = score.score * Math.cbrt(score.count) * quality
|
||||||
}
|
}
|
||||||
|
|
||||||
return scores
|
const items = Object.entries(scores)
|
||||||
|
.sort((a, b) => a[1].score > b[1].score ? -1 : 1)
|
||||||
|
.map(([url, {score, count}]) => ({url, score, count}))
|
||||||
|
|
||||||
|
return items
|
||||||
}
|
}
|
||||||
|
|
||||||
urlsFromScores = (scores: RouteScores) =>
|
|
||||||
Object.entries(scores).sort((a, b) => a[1].score > b[1].score ? -1 : 1).map(p => p[0] as string)
|
|
||||||
|
|
||||||
groupsToUrls = (groups: string[][]) => this.urlsFromScores(this.getGroupScores(groups))
|
|
||||||
|
|
||||||
scenario = (groups: string[][]) => new RouterScenario(this, groups)
|
scenario = (groups: string[][]) => new RouterScenario(this, groups)
|
||||||
|
|
||||||
merge = (scenarios: RouterScenario[]) =>
|
merge = (scenarios: RouterScenario[]) =>
|
||||||
@@ -235,17 +232,13 @@ export class RouterScenario {
|
|||||||
|
|
||||||
getPolicy = () => this.options.policy || this.router.addMaximalFallbacks
|
getPolicy = () => this.options.policy || this.router.addMaximalFallbacks
|
||||||
|
|
||||||
getFallbackRelays = () => {
|
getFallbackRelays = () =>
|
||||||
const userRelays = shuffle(this.router.getUserRelays(this.options.mode))
|
shuffle(this.router.options.getDefaultRelays(this.options.mode))
|
||||||
const defaultRelays = shuffle(this.router.options.getDefaultRelays(this.options.mode))
|
|
||||||
|
|
||||||
return uniq([...userRelays, ...defaultRelays])
|
|
||||||
}
|
|
||||||
|
|
||||||
getUrls = () => {
|
getUrls = () => {
|
||||||
const fallbackPolicy = this.getPolicy()
|
const fallbackPolicy = this.getPolicy()
|
||||||
const limit = fallbackPolicy(this.getLimit())
|
const limit = fallbackPolicy(this.getLimit())
|
||||||
const urls = this.router.groupsToUrls(this.groups)
|
const urls = this.router.scoreGroups(this.groups).map(s => s.url)
|
||||||
|
|
||||||
for (const url of this.getFallbackRelays()) {
|
for (const url of this.getFallbackRelays()) {
|
||||||
if (urls.length >= limit) {
|
if (urls.length >= limit) {
|
||||||
@@ -262,5 +255,3 @@ export class RouterScenario {
|
|||||||
|
|
||||||
getUrl = () => first(this.limit(1).getUrls())
|
getUrl = () => first(this.limit(1).getUrls())
|
||||||
}
|
}
|
||||||
|
|
||||||
// Fallback Policy
|
|
||||||
|
|||||||
Reference in New Issue
Block a user