Created
January 25, 2023 22:09
-
-
Save 1xtr/7daa7c9dec53880eff9a86e94f8453ea to your computer and use it in GitHub Desktop.
Distribution by weight with counter. (Leads, task)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const managers = [ | |
{ name: "Manager 1", weight: 0.6, tasks: 0 }, | |
{ name: "Manager 2", weight: 0.2, tasks: 0 }, | |
{ name: "Manager 3", weight: 0.1, tasks: 0 }, | |
{ name: "Manager 4", weight: 0.1, tasks: 0 } | |
]; | |
// Create a cumulative weight array | |
let cumWeight = 0; | |
for (let i = 0; i < managers.length; i++) { | |
cumWeight += managers[i].weight; | |
managers[i].cumWeight = cumWeight; | |
} | |
// Define a function to randomly select a manager based on their weight | |
function weightedRandom() { | |
let rand = Math.random() * cumWeight; | |
for (let i = 0; i < managers.length; i++) { | |
if (rand < managers[i].cumWeight) { | |
managers[i].tasks++; | |
return managers[i].name; | |
} | |
} | |
} | |
// Define a function to check if any manager has exceeded their allotted number of tasks | |
function checkTasks() { | |
let totalTasks = managers.map(m => m.tasks).reduce((a, b) => a + b); | |
let avgTasks = totalTasks / managers.length; | |
managers.forEach(manager => { | |
if (manager.tasks > avgTasks * 1.1) { | |
manager.weight *= 0.9; | |
cumWeight = 0; | |
for (let i = 0; i < managers.length; i++) { | |
cumWeight += managers[i].weight; | |
managers[i].cumWeight = cumWeight; | |
} | |
} else if (manager.tasks < avgTasks * 0.9) { | |
manager.weight *= 1.1; | |
cumWeight = 0; | |
for (let i = 0; i < managers.length; i++) { | |
cumWeight += managers[i].weight; | |
managers[i].cumWeight = cumWeight; | |
} | |
} | |
}); | |
} | |
// Assign tasks to managers as they come in | |
while (true) { | |
let task = receiveTask(); // function to receive new tasks | |
let manager = weightedRandom(); | |
assignTask(task, manager); // function to assign tasks to managers | |
checkTasks(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment