How can I create “teams” from a list of weighted “users” randomly, but using PHP?

What I hope to achieve is the ability to generate user “teams”. I will have x number of men weighted (decimal mental weight, for example 75.23 ) and y number of women (also with the weight value of the skill).

Given this list of users, I would take for input the number of commands to do (say, 6 teams). Then I go through the list of x and y and organize them so that the best average possible weighted teams are created. I would like the teams to be balanced (ratio of women to men)

I do not want the teams to be folded (best in one team). I would like an even distribution of weight.

Curious how could I achieve this in PHP? I would use a MySQL database to load users with weighted values. I would know in advance how many users I have and how many teams I would like to create.

I would appreciate any suggestions or links to a solution if someone found something similar. I'm just not a mathematical wiz, so I don’t know which formula will be applied here.

Thanks. I appreciate any input!

EDIT

After reviewing the answers, maybe I was not clear enough, so hopefully this helps a little more.

  • I want the teams to be about the same size.
  • I want the average (average) skill score for each team to be approximately equal.
  • I want the ratio of men and women in each team to be about the same (that is, if we get a distribution of 5 men and 3 women in a team, I would like to keep it the same). It’s not quite a question if I sort men first and women second (or vice versa).
  • I do not want a linear approach (command 1 becomes the highest, command 2, the highest second, command 3 .. so on). The Tim method of taking (if 6 teams) 6 people and randomizing and then spreading through a linear model seems to work fine.
+4
source share
3 answers

I don’t quite understand that you are here, so I’ll tell you that, as I understand it, they’re asking you. If this is not the case, you can clarify your requirements by editing your question:

You have a list of a certain number of men and a certain number of women. Each person has a known skill. You want to divide them into a certain number of teams with the following goals:

  • you want teams to be about the same size.
  • you want the average (average) skill score for each team to be approximately equal.
  • You want the ratio of men and women in each team to be approximately equal.

I would think that an easy way to achieve this would be as follows:

  • Create a list of all men in descending order of skill level.
  • Create a list of all women in descending order of points.
  • Add a list of women to the end of the list of men.
  • Start at the beginning of the combined list and select each person in turn in a circular fashion. (That is, assign the first person to team number one, the second to team number two, etc., until you select one person for each of the teams you want to create. Then start again with one team, highlighting people for each team in order, etc.)

With this approach, you are guaranteed the following results:

  • If possible (i.e. if the number of teams divides the total number of people), the teams will have the same number of people.
  • If the teams do not have the same size, the largest team will have exactly one more person than the smallest team.
  • If possible, all teams will have the same number of men.
  • If the teams do not have the same number of men, the team with most men is exactly one more person than the team with the smallest men.
  • If possible, all teams will have the same number of women.
  • If the teams do not have the same number of women, the team with most women is exactly one more man than the team with the smallest women.
  • Each team will have men with a range of skill ratings, ranging from the top of the range to the bottom of the range.
  • Each team will have women with a range of skill points, from close range to range to bottom of range.
  • With reasonable data, the average score for each team will be approximately equal (although one team will have a higher average score than team 2, etc. - there are ways to fix this).

If this simple approach does not meet your requirements, let us know what else you had in mind.

+3
source

This is similar to the “perfect match of the maximum / minimum weight”, so a match is made for more than two elements (note that this is a different weight from what you have (skill weight), namely: you must assign a weight to the match (matching will be the proposed "team")).

Well-known algorithms for perfect matching above (for example, the Edmond algorithm) may not adapt to the group case. I would probably study some simulated annealing methods or a simple genetic algorithm.

+2
source

If the number of people in each group (x, y) is relatively equal, and the total number of people is relatively high, a random sample should work reasonably well. See here how to select random rows from a MySQL database: http://dev.mysql.com/doc/refman/5.0/en/mathematical-functions.html#function_rand

A little editing to ensure justice in person, I would do something like that. Say you know that you want n team members. Then create a local variable that means n *, where is the average average skill level per person. Then, when your random selection of your team members does this within that limit.

eg.

 while(new random record){ if(team_skill+random person skill > n*mean){ next; } if(team_skill+random person skill < n*mean && selected team members =n){ team + random person; break; } } 
0
source

Source: https://habr.com/ru/post/1301238/