|
|
|
@ -206,7 +206,15 @@ public class AssemblePaperDomainServiceImpl implements AssemblePaperDomainServic |
|
|
|
}); |
|
|
|
// 带权乱序(A-Res算法)
|
|
|
|
return kpList.stream() |
|
|
|
.sorted(Comparator.comparingDouble(kp -> -Math.pow(ThreadLocalRandom.current().nextDouble(), 1.0 / kp.getWeightScore()))) |
|
|
|
// 1. 先把对象和计算好的随机分数封装起来(保证每个元素在本次排序中分数固定)
|
|
|
|
.map(kp -> { |
|
|
|
double score = Math.pow(ThreadLocalRandom.current().nextDouble(), 1.0 / kp.getWeightScore()); |
|
|
|
return new AbstractMap.SimpleEntry<>(kp, score); |
|
|
|
}) |
|
|
|
// 2. 根据预先计算好的分数进行排序(降序排列,分数越高权重越大)
|
|
|
|
.sorted((e1, e2) -> Double.compare(e2.getValue(), e1.getValue())) |
|
|
|
// 3. 还原回原始对象并取结果
|
|
|
|
.map(Map.Entry::getKey) |
|
|
|
.limit(totalNum) |
|
|
|
.toList(); |
|
|
|
} |
|
|
|
|