0

I have a module object that contains a list of roles. These roles can represent one or more roles (Es: "RoleA", or maybe "RoleA;RoleB")

// Example

var roles = [      
    'Por',
    'Ds',
    'Dc',
    'Dc',
    'Dd',
    'M',
    'M;C',
    'T',
    'W',
    'W;A',
    'A;Pc'
]

I have another array. It's a list of players object. Each player has a 'role' and a 'value'. The role again can be single or multiple.

// Example

var players = [  
{    
    name: 'NameA',
    role : 'A',
    value: 15
},
{    
    name: 'NameB',
    role : 'A;W',
    value: 15
},
{    
    name: 'NameC',
    role : 'Dd;Dc',
    value: 15
},
...
]

My goal is to assign a player to each role of the 'roles' array maximizing the total value of the team (calculated by summing the value of the single players)

I've done it iterating over roles array trying to find each time the best option. It had some issues I fixed by adding some simple mechanisms but in some cases, i still have some problems.

Can anyone with some algorithm understanding explain what is the best way to face these kinds of problems?

    // Example

    var roles = [      
    ['Por', playerA],
    ['Ds', playerB],
    ['Dc', playerC],
    ['Dc', playerD],
    ['Dd', playerE],
    ['M', playerF],
    ['M;C', playerG],
    ['T', playerH],
    ['W', playerI],
    ['W;A', playerL],
    ['A;Pc, playerM]
    ]
DxW
  • 1,414
  • 3
  • 11
  • 23
  • 1
    Read up (rectangular) [Linear Assignment Problem](https://en.wikipedia.org/wiki/Assignment_problem) and more general [Min-cost max-flows](https://en.wikipedia.org/wiki/Minimum-cost_flow_problem) – sascha Aug 28 '19 at 22:54
  • It's not clear what is the rule for assigning a player to a role? Do you assign arbitrarily? – SomeDude Aug 28 '19 at 22:56
  • @SomeDude the rule is that each player can play one or more roles. I have a fixed array of roles that i must use to generate a squad – DxW Aug 28 '19 at 23:02

1 Answers1

1

This problem is known as: Maximum bipartite weighted matching. You can solve it reducing it to Maximum flow problem.

The idea is to create such a graph:

  • vertices: players and roles
  • edges: connect players with their possible roles from roles array (direction going from role to player)

What to do next is quite well described in accepted answer to this question.

Additional resources

Max flow problem: link

Ogochi
  • 320
  • 3
  • 12
  • 1
    After some reading I found this https://github.com/mattkrick/EdmondsBlossom. It works perfectly – DxW Aug 29 '19 at 03:31