通过正常分布在24小时内分配负载,中午达到峰值?

问题描述 投票:0回答:2

我试图在一天中的每个小时不均匀地分配负载,当有更多人可用时,在中午左右进行高峰处理。基本上,我希望任务的“正态分布”与简单的n / 24 = hourly load相对应。

目标是大部分工作需要在一天中午分发,早上和深夜工作较少。

Normal Distribution Chart

这就是我已经产生了一些曲线。

// 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

产生这样的东西:

demo chart

javascript math normal-distribution
2个回答
1
投票

您需要实现高斯函数。以下链接可能会对您有所帮助: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});
}

1
投票

我想你可以近似了。在这种情况下,像y = sin(pi * x)^ 4这样的东西可能是一个相对好(和简单)的解决方案。然后,可以通过将y提高到接近1的某个幂来使该分布更宽或更细。

y = sin(pi * x) from 0 to 1

此外,它是循环的,所以通过做类似的事情来帮助实现

y = (sin(pi*hour/24))^4

并缩放以适应600个工作。

© www.soinside.com 2019 - 2024. All rights reserved.