Refactor implementation to make it more efficient

This commit is contained in:
Manuel Bustillo 2024-08-01 19:24:29 +02:00
parent 41589af738
commit 3971fab196

View File

@ -12,13 +12,13 @@ module Tables
private private
def cohesion_penalty def cohesion_penalty
table.map { |guest| guest.affinity_group_list.first }.combination(2).sum do |a, b| table.map { |guest| guest.affinity_group_list.first }.tally.to_a.combination(2).sum do |(a, count_a), (b, count_b)|
distance = AffinityGroupsHierarchy.instance.distance(a, b) distance = AffinityGroupsHierarchy.instance.distance(a, b)
next 1 if distance.nil? next count_a * count_b if distance.nil?
next 0 if distance.zero? next 0 if distance.zero?
Rational(distance, distance + 1) count_a * count_b * Rational(distance, distance + 1)
end end
end end
end end