I have a dataset like so
| Location | Item 1 | Item 2 | Weight |
|---|---|---|---|
| 1 | A | B | 3 |
| 2 | A | C | 3 |
| 3 | B | C | 1 |
| 4 | C | D | 1 |
| 4 | A | B | 1 |
Each row represents an opportunity to swap in items X and Y. I can swap X in for Item 1 and Y in for Item 2 or vice versa. Each row has to get X and Y. It cannot get 2 of X or 2 of Y. A swap out item can only be related to one swap in item. For example, if I chose to swap out A for X and B for Y in location 1, A can only ever be swapped for X and B for Y for the remaining locations. This means if I were to make the swap I just mentioned, then in location 2 I swap A for X and C for Y, location 3 no longer has a valid swap opportunity because B and C are both assigned to Y. Each location could possibly have multiple opportunities (such as 4 in the example) but each location can only be used once (the Weight of all swaps in a given location are the same). The goal is to maximize the sum of the Weight column after making optimal swaps and eliminating ineligible swaps.
I've tried a very naive approach of iteratively sorting by the largest group of either Item 1 or Item 2 then swapping Item 1 for Item X and Item 2 for Item Y. After that I remove any swap opportunities that have become ineligible (because both items are set to X/Y or the location has already been used) and repeat the process.
I'm sure there's a better way to do this but I can't figure it out.