66 lines
2.3 KiB
JavaScript
66 lines
2.3 KiB
JavaScript
const _ = require('lodash')
|
|
|
|
exports.calculateAvgScore = (...scores) => {
|
|
return avgScore = scores.reduce((total, score) => total += score) / scores.length;
|
|
}
|
|
|
|
exports.calculateScoreRange = (transitionRange, regionVal, sLowVal, sHighVal) => {
|
|
//console.log('scores.calculateScoreRange:', min, max, multiplier, 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);
|
|
}
|
|
|
|
exports.calculateScore = (transitionRange, regionVal, searchVal) => {
|
|
let score = 1 - (Math.abs(searchVal - regionVal) / transitionRange);
|
|
return (score) * 10;
|
|
//return score <= 0 ? 0 : score * 10;
|
|
}
|
|
|
|
exports.linear = function (x, exponent) {
|
|
if (x < 0) return 0
|
|
if (x > 10) return 10
|
|
return x
|
|
}
|
|
|
|
exports.easeOut = function (x, exponent) {
|
|
if (x < 0) return 0
|
|
if (x > 10) return 10
|
|
return (1 - Math.pow(1 - (x / 10), exponent)) * 10
|
|
}
|
|
|
|
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
|
|
}
|
|
|
|
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
|
|
}
|
|
|
|
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
|
|
} |