简体   繁体   English

在 groupby 后获得平均值以实现公平平衡

[英]Get average after groupby for a fair balancing

I would like to test if the count (after grouping by technician, numberOfTasksByTechnician) above average for a good balance of tasks for technicians (every technician must do the same number of tasks), how can I get the average (hard coded value 4)我想测试计数(按技术员分组后,numberOfTasksByTechnician)是否高于平均水平以实现技术人员任务的良好平衡(每个技术人员必须执行相同数量的任务),我如何获得平均值(硬编码值 4)

Constraint technicianCharge(ConstraintFactory constraintFactory)  {
        return constraintFactory.from(Task.class)
            .groupBy(
                    Task::getTechnician, ConstraintCollectors.countDistinct()
            )
            .filter((technician, count)-> {
                return count > 4; // return count > (?average)
            })
            .penalize("technicianCharge", HardSoftScore.ONE_SOFT);
}

At the moment, to collect average, you need to write a custom constraint collector.目前,要收集平均值,您需要编写一个自定义约束收集器。 It's not hard to do, check out count() implementation for inspiration.做起来并不难,查看count() 实现以获取灵感。 When you have your constraint collector, your constraint stream would then look like this:当您拥有约束收集器时,您的约束流将如下所示:

Constraint technicianCharge(ConstraintFactory constraintFactory)  {
    return constraintFactory.from(Task.class)
        .groupBy(
             Task::getTechnician, 
             ConstraintCollectors.countDistinct(),
             MyConstraintCollectors.average(task -> ...)
        )
        .filter((technician, count, average)-> {
            return count > average;
        })
        .penalize("technicianCharge", 
            HardSoftScore.ONE_SOFT,
            (technician, count, average) -> count - average);
}

In the future, we may provide the average constraint collector out-of-the-box .将来,我们可能会提供开箱即用的平均约束收集器。

As a footnote, since you mentioned fairness, you may want to see our blog on OptaPlanner load balancing and fairness .作为脚注,既然您提到了公平性,您可能希望查看我们关于OptaPlanner 负载平衡和公平性的博客。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM