【问题标题】:Distribute load via normal distribution over 24 hours with the peak at noon?24 小时内通过正态分布分配负载,高峰在中午?
【发布时间】:2019-04-02 21:43:49
【问题描述】:

我正在尝试在一天中的每个小时内不均匀地分配负载,并在中午有更多可用人员时进行高峰处理。基本上,我想要一个与简单的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.

这会产生这样的结果:

【问题讨论】:

  • 对于正态分布,您需要公式中的方差和异常分布。您的意思是数学normal distribution 的真正含义还是您正试图找到类似于上面曲线的东西?

标签: javascript math normal-distribution


【解决方案1】:

您需要实现一个高斯函数。以下链接可能会有所帮助: https://math.stackexchange.com/questions/1236727/the-x-y-coordinates-for-points-on-a-bell-curve-normal-distribution

您需要选择平均值和标准差 (sigma)。这是我找到的一个 sn-p:

//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 并调整 sigma 以尽可能多地展开曲线。您还需要稍微缩放“y”值。

更新

我为你创建了一个 JS Fiddle,它绘制了我认为你需要的东西。 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-24(小时)范围内会是什么样子。
  • 我在答案中添加了更多内容并添加了 JSFiddle。
  • 您是否有机会将此标记为最佳答案、作为可行的解决方案,或者如果您需要我添加任何内容,请告诉我?
  • 谢谢。 Stackoverflow 对某些事情(例如接受答案或奖励赏金)有最短时间限制。
【解决方案2】:

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

此外,它是循环的,因此可以通过执行类似的操作来帮助实现

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

并扩展以适应 600 个工作。

【讨论】:

    猜你喜欢
    • 2010-09-14
    • 2014-04-15
    • 1970-01-01
    • 2016-07-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-16
    • 2021-06-10
    相关资源
    最近更新 更多