[英]Best combination of multiple list by picking only one value per list
I've multiple lists and would like to find the best combination by picking only 1 value from each list.我有多个列表,并希望通过从每个列表中仅选择 1 个值来找到最佳组合。 The best combination is where the total value is the highest.最好的组合是总值最高的地方。 eg:例如:
In the table below the idea combination would be event 1 from person 2, event 2 from person 3 and event 3 from person 1. Please note that this is an example and that the number of 'events' and 'persons' may vary.在下表中,想法组合将是来自人 2 的事件 1、来自人 3 的事件 2 和来自人 1 的事件 3。请注意,这是一个示例,“事件”和“人”的数量可能会有所不同。
Person人 | Event 1活动一 | Event 2活动 2 | Event 3活动 3 |
---|---|---|---|
Person 1人 1 | 5 5 | 2 2 | 6 6 |
Person 2人 2 | 10 10 | 3 3 | 6 6 |
Person 3人 3 | 1 1 | 7 7 | 2 2 |
The best combination would be:最好的组合是:
I first thought that this could be achieved with the Cartesian Product.我首先认为这可以通过笛卡尔积来实现。 For more details see https://en.wikipedia.org/wiki/Cartesian_product .有关更多详细信息,请参阅https://en.wikipedia.org/wiki/Cartesian_product 。 However that will only sum all combination and don't take the 'pick-only-one-value-from-each-list' into account.但是,这只会汇总所有组合,并且不会考虑“从每个列表中选择一个值”。
private static List<Integer> listOfPerson1 = List.of(5, 2, 6);
private static List<Integer> listOfPerson2 = List.of(10, 3, 6);
private static List<Integer> listOfPerson3 = List.of(1, 7, 2);
public static void main(String[] args) {
List<List<Integer>> listOfPersons = new ArrayList<>();
listOfPersons.add(listOfPerson1);
listOfPersons.add(listOfPerson2);
listOfPersons.add(listOfPerson3);
List<Integer> result = new ArrayList<>();
generatePermutations(listOfPersons, result, 0, 0);
for (Integer i : result) {
System.out.println(i);
}
}
static void generatePermutations(List<List<Integer>> lists, List<Integer> result, int depth, Integer current) {
if (depth == lists.size()) {
result.add(current);
return;
}
for (int i = 0; i < lists.get(depth).size(); i++) {
generatePermutations(lists, result, depth + 1, current + lists.get(depth).get(i));
}
}
Any idea how this can be achieved or what the name of such algorithm/mathematics is?知道如何实现这一点,或者这种算法/数学的名称是什么?
This problem is actually called the Assignment Problem which involves assigning agents to tasks in a way that optimises the total cost incurred.这个问题实际上被称为分配问题,它涉及以优化总成本的方式将代理分配给任务。
In terms of algorithms to solve it, the one that I have seen used the most is the Hungarian Algorithm (also the pioneering algorithm for this problem) which has polynomial time complexity.在解决它的算法方面,我见过使用最多的是具有多项式时间复杂度的匈牙利算法(也是该问题的开创性算法)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.