travopti/backend/util/score.js
2020-07-10 16:05:59 +02:00

79 lines
2.7 KiB
JavaScript

const _ = require('lodash')
// simple averaging function for desired amount of elements
exports.calculateAvgScore = (...scores) => {
return avgScore = scores.reduce((total, score) => total += score) / scores.length;
}
// extends scoring function to be used with high and low bounded search parameters
exports.calculateScoreRange = (transitionRange, regionVal, sLowVal, sHighVal) => {
// return full score when in range
if (regionVal >= sLowVal && regionVal <= sHighVal) return 10;
// choose value with smallest distance
let sVal = Math.abs(regionVal - sLowVal) < Math.abs(regionVal - sHighVal) ? sLowVal : sHighVal;
return this.calculateScore(transitionRange, regionVal, sVal);
}
/**
* calculates a score from the distance of two values. returns value between 0 and 10
* @param {Number} transitionRange transition range defines the width of the area between 0 and 10
* @param {Number} regionVal actual value
* @param {Number} searchVal desired value
* @returns {Number} Score
*/
exports.calculateScore = (transitionRange, regionVal, searchVal) => {
let score = 1 - (Math.abs(searchVal - regionVal) / transitionRange);
return (score) * 10;
//return score <= 0 ? 0 : score * 10;
}
// transistion function
exports.linear = function (x, exponent) {
if (x < 0) return 0
if (x > 10) return 10
return x
}
// transistion function
exports.easeOut = function (x, exponent) {
if (x < 0) return 0
if (x > 10) return 10
return (1 - Math.pow(1 - (x / 10), exponent)) * 10
}
// transistion function
exports.easeInOut = function (sc, exponent) {
const x = (sc ) / 10
// console.log(sc, x);
if (x<0) return 0
if (x>1) return 10
return x < 0.5 ? Math.pow(2, exponent-1) * Math.pow(x,exponent) * 10 : (1 - Math.pow(-2 * x + 2, exponent)/2) * 10
}
// transistion function
exports.easeInOutAsymmetric = function (sc, exponent) {
const x = (sc ) / 10
// console.log(sc, x);
if (x<0) return 0
if (x>1) return 10
return x < 0.5 ? (2 * x) - 0.5 * 10 : (1 - Math.pow(-2 * x + 2, exponent)/2) * 10
}
// transistion function
exports.sigmoid = function (x, exponent) {
// const sigm = (1 / (1 + Math.pow(Math.E, 5 * -x))) * 10 + 5
// const sigm = 10 / (1 + Math.pow(Math.E, 1.2 * -x + 6))
const sigm = 10 / (1 + 8 * Math.pow(Math.E, 3/4 * -x))
console.log('sigmoid (IN/OUT):', _.round(x,3), _.round(sigm, 3))
return sigm
}
exports.increaseTransitionForHighValues = function (transitionRange, searchVal) {
//console.log(transitionRange);
// console.log(transitionRange);
// console.log(((Math.pow(searchVal / 20, 2) / 100) + 1));
// console.log(((Math.pow(searchVal / 20, 2) / 100) + 1) * transitionRange);
let transFactor = ((Math.pow(searchVal / 20, 2) / 100) + 1)
return transFactor >= 4 ? 4 * transitionRange : transFactor * transitionRange
}