|
| 1 | +package core |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "fmt" |
| 6 | + "math/rand" |
| 7 | + "sort" |
| 8 | + |
| 9 | + "github.com/jordi-reinsma/bagel/db" |
| 10 | + "github.com/jordi-reinsma/bagel/model" |
| 11 | +) |
| 12 | + |
| 13 | +func GenerateMatches(DB db.DB, users []model.User) ([]model.Match, error) { |
| 14 | + pairs, err := preparePairs(DB, users) |
| 15 | + if err != nil { |
| 16 | + return nil, err |
| 17 | + } |
| 18 | + |
| 19 | + return greedyMatcher(users, pairs) |
| 20 | +} |
| 21 | + |
| 22 | +func preparePairs(DB db.DB, users []model.User) ([]model.Match, error) { |
| 23 | + var pairs []model.Match |
| 24 | + for i := 0; i < len(users)-1; i++ { |
| 25 | + for j := i + 1; j < len(users); j++ { |
| 26 | + if users[i].ID == users[j].ID { |
| 27 | + continue |
| 28 | + } |
| 29 | + pairs = append(pairs, model.Match{A: users[i], B: users[j]}) |
| 30 | + } |
| 31 | + } |
| 32 | + return DB.AddAndGetPairs(pairs) |
| 33 | +} |
| 34 | + |
| 35 | +// greedyMatcher implements the greedy algorithm for the minimum weight perfect matching problem |
| 36 | +func greedyMatcher(users []model.User, pairs []model.Match) ([]model.Match, error) { |
| 37 | + // shuffle the pairs to remove bias towards lower ids |
| 38 | + swapPair := func(i, j int) { |
| 39 | + pairs[i], pairs[j] = pairs[j], pairs[i] |
| 40 | + } |
| 41 | + rand.Shuffle(len(pairs), swapPair) |
| 42 | + |
| 43 | + // stable sort the pairs by the least matches they have |
| 44 | + byFrequency := func(i, j int) bool { |
| 45 | + return pairs[i].Freq < pairs[j].Freq |
| 46 | + } |
| 47 | + sort.SliceStable(pairs, byFrequency) |
| 48 | + |
| 49 | + // number of matches is the ceil of the half of the number of users |
| 50 | + numMatches := len(users)/2 + len(users)%2 |
| 51 | + |
| 52 | + matches := make([]model.Match, 0, numMatches) |
| 53 | + matched := make(map[int]bool) |
| 54 | + for _, user := range users { |
| 55 | + matched[user.ID] = false |
| 56 | + } |
| 57 | + |
| 58 | + // iterate over the pairs and match unmatched users |
| 59 | + for _, pair := range pairs { |
| 60 | + // break the loop if we have enough matches |
| 61 | + if len(matches) == numMatches-len(users)%2 { |
| 62 | + break |
| 63 | + } |
| 64 | + ok1 := matched[pair.A.ID] |
| 65 | + ok2 := matched[pair.B.ID] |
| 66 | + if ok1 || ok2 { |
| 67 | + continue |
| 68 | + } |
| 69 | + matches = append(matches, pair) |
| 70 | + matched[pair.A.ID] = true |
| 71 | + matched[pair.B.ID] = true |
| 72 | + } |
| 73 | + |
| 74 | + // in the case of an odd number of users, we need to add the last unmatched user |
| 75 | + if len(matches) == numMatches-1 { |
| 76 | + var unmatched int |
| 77 | + for id, found := range matched { |
| 78 | + if !found { |
| 79 | + unmatched = id |
| 80 | + break |
| 81 | + } |
| 82 | + } |
| 83 | + if unmatched == 0 { |
| 84 | + return nil, errors.New("no unmatched user found but one match is missing") |
| 85 | + } |
| 86 | + // match the unmatched user to the first available pair |
| 87 | + for _, pair := range pairs { |
| 88 | + if pair.A.ID == unmatched || pair.B.ID == unmatched { |
| 89 | + matches = append(matches, pair) |
| 90 | + break |
| 91 | + } |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + if len(matches) != numMatches { |
| 96 | + return nil, fmt.Errorf("could not generate %d matches", numMatches) |
| 97 | + } |
| 98 | + |
| 99 | + return matches, nil |
| 100 | +} |
0 commit comments