I have a problem to solve. My algorithm is assigning Players to Positions. On the start I have a list of about 16 positions and 4 players. The problem is to assing every player to it's closest positions so the overall distance is as low as possible.
I've already done this:
List players = ...
List positions = ...
Set posPerms = positions.permutations().collect { it.subList(0, playmakers.size()) } as Set
List assignation = posPerms.min { List perm ->
int v = 0
perm.eachWithIndex { Position pos, int index ->
v += pos.distance(players[index])
}
v
}
[players, assignation].transpose().collectEntries { it }
I get what I want, but the performance is a problem. positions.permutations() generates massive lists. Is there any more optimal way to do this?