From cb4db124b293dd66207014cee2d94c7122c8751e Mon Sep 17 00:00:00 2001 From: Uladzislau Hubar Date: Tue, 16 Jan 2024 12:03:28 +0100 Subject: [PATCH] Moved Maclaurin Series degree for e^x approximation to be a parameter in the smart contract --- src/service/proximity-scoring-service.js | 9 +++++---- .../mocks/blockchain-module-manager-mock.js | 1 + 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/src/service/proximity-scoring-service.js b/src/service/proximity-scoring-service.js index b47d923b35..1cfd4fd439 100644 --- a/src/service/proximity-scoring-service.js +++ b/src/service/proximity-scoring-service.js @@ -105,12 +105,12 @@ class ProximityScoringService { } // Using Maclaurin Series to approximate e^x - _approximateExp(x, precision = 20) { + _approximateExp(x, degree) { let xPow = x; let factorial = 1; let result = 1; - for (let i = 1; i <= precision; i += 1) { + for (let i = 1; i <= degree; i += 1) { factorial *= i; result += xPow / factorial; xPow *= x; @@ -122,7 +122,8 @@ class ProximityScoringService { async LinearLogisticSum(blockchain, distance, stake, maxNeighborhoodDistance) { const linearLogisticSumParams = await this.blockchainModuleManager.getLinearLogisticSumParams(blockchain); - const { distanceScaleFactor, exponentMultiplier, x0, w1, w2 } = linearLogisticSumParams; + const { distanceScaleFactor, exponentMultiplier, maclaurinSeriesDegree, x0, w1, w2 } = + linearLogisticSumParams; let dividend = distance; let divisor = maxNeighborhoodDistance; @@ -136,7 +137,7 @@ class ProximityScoringService { parseFloat(divResult.toString()) / parseFloat(distanceScaleFactor.toString()); const exponentPart = exponentMultiplier * (stake - x0); - const mappedStake = 2 / (1 + this._approximateExp(exponentPart)) - 1; + const mappedStake = 2 / (1 + this._approximateExp(exponentPart, maclaurinSeriesDegree)) - 1; const proximityScore = w1 * (1 - mappedDistance); const stakeScore = w2 * mappedStake; diff --git a/tools/knowledge-assets-distribution-simulation/mocks/blockchain-module-manager-mock.js b/tools/knowledge-assets-distribution-simulation/mocks/blockchain-module-manager-mock.js index c4a260ab52..a4488d398b 100644 --- a/tools/knowledge-assets-distribution-simulation/mocks/blockchain-module-manager-mock.js +++ b/tools/knowledge-assets-distribution-simulation/mocks/blockchain-module-manager-mock.js @@ -37,6 +37,7 @@ class BlockchainModuleManagerMock { return { distanceScaleFactor: '1000000000000000000', exponentMultiplier: -0.000001, + maclaurinSeriesDegree: 20, x0: 50000, w1: 1, w2: 1,