我试图在一天中的每个小时不均匀地分配负载,当有更多人可用时,在中午左右进行高峰处理。基本上,我希望任务的“正态分布”与简单的n / 24 = hourly load
相对应。
目标是大部分工作需要在一天中午分发,早上和深夜工作较少。
这就是我已经产生了一些曲线。
// Number per day
const numberPerDay = 600;
const numberPerHour = numberPerDay / 24;
let total = 0;
for (let hour = 1; hour < 24; hour++) {
// Normal Distribution should be higher at 12pm / noon
// This Inverse bell-curve is higher at 1am and 11pm
const max = Math.min(24 - hour, hour);
const min = Math.max(hour, 24 - hour);
const penalty = Math.max(1, Math.abs(max - min));
const percentage = Math.floor(100 * ((penalty - 1) / (24 - 1)));
const number = Math.floor(numberPerHour - (numberPerHour * percentage / 100));
console.log(`hour: ${hour}, penalty: ${penalty}, number: ${number}`);
total += number;
}
console.log('Expected for today:', numberPerDay);
console.log('Actual for today:', total);
住jsfiddle。
产生这样的东西:
您需要实现高斯函数。以下链接可能会对您有所帮助:https://math.stackexchange.com/questions/1236727/the-x-y-coordinates-for-points-on-a-bell-curve-normal-distribution
您需要选择平均值和标准偏差(sigma)。这是我发现的一个片段:
//taken from Jason Davies science library
// https://github.com/jasondavies/science.js/
function gaussian(x) {
var gaussianConstant = 1 / Math.sqrt(2 * Math.PI),
mean = 0,
sigma = 1;
x = (x - mean) / sigma;
return gaussianConstant * Math.exp(-.5 * x * x) / sigma;
};
https://gist.github.com/phil-pedruco/88cb8a51cdce45f13c7e
要使它达到0-24,你可以将均值设置为12并调整西格玛以根据需要扩展曲线。您还需要稍微调整“y”值。
我为你创建了一个JS小提琴,它描绘了我认为你需要的东西。 https://jsfiddle.net/arwmxc69/2/
var data = [];
var scaleFactor = 600
mean = 12,
sigma = 4;
function gaussian(x) {
var gaussianConstant = 1 / Math.sqrt(2 * Math.PI);
x = (x - mean) / sigma;
return gaussianConstant * Math.exp(-.5 * x * x) / sigma;
};
for(x=0;x<24;x+=1) {
var y = gaussian(x)
data.push({x:x,y:y*scaleFactor});
}