Unbiased team selection algorithm with squads - algorithm

Hi I am currently working on a multiplayer game with the following setup.
Say the game has N players. We want to divide players into two teams, team A and team B where
x < N of the players will be assigned to team A. Additionally before the team division starts a player can join a squad. That is, a player can either choose to play solo or as squad of size 2, 3 or 4. After the team division, players should end up on the same team as their squad members. The algorithm should result in teamA always having x players while having the probability of a player being selected for teamA be the same regardless of whether they are in a squad or not.
The current idea is as follows:
Keep randomly selecting players for teamA until the size of teamA is x. If a randomly selected player is part of a squad of size q > 1 and there is enough room for the whole squad in teamA, add the whole squad to teamA. If there is not enough room for that squad in teamA, all squad members are added to teamB. However since only one squad member needs to be randomly selected, players who are part of squads would be more likely to be selected for teamA. To offset this we first have to pass a check of probability 1/q before adding all the squad members to teamA. If this check fails we do nothing and randomly select another player who has yet to be assigned to teamA. Once we reach x players, all remaining players will be assigned teamB.
The problem is essentially that say N = 100 and x = 25, once 24 players have been selected for team A.
Players who are part of a squad can never be selected since there is no room. As a result, players who are not part of a squad will be assigned to teamA more often.
Wondering if anyone has come across a similar type of problem.

Related

Best way to represent a ladder in a database

I have a requirement to represent ladder results, that were previously kept in an excel sheet, on a website.
The players on the ladder don't have scores. If player A beat player B, player A's going to be placed above player B in the ladder.
My problem is the following:
Let's say I have 5 players, ranked as follows:
Player 1
Player 2
Player 3
Player 4
Player 5
If Player 5 beat Player 1, Player 5 now moves on the top of the ladder. If the player order is decided on the ranking number I have to update the ranking number of all players between those who actually played which just sounds wrong to me.
I had an idea of assigning scores to each player, if Player A beats Player B then Player A gets Player B's score + 1, then the ranking is score in descending order. This works reasonably well up to the point when players end up on the same score. I can get around that too with adding a "LastUpdated" field in the database so whichever score got last updated goes below the other (the LastUpdated player hasn't actually beaten the player on the same score, they were just moved up there). This then fails at the point when a person does multiple adjustments on the same day so multiple players end up on the same score and the ranking order purely depends on which player got updated last. This is a problem as there's a requirement for the admin to be able to move players up and down freely. It also makes it impossible to add a player between players that have the same score (the winning player would have to jump the whole group).
My last resort would be to get the current ranking order from the database, put it all in a table, allow the admin to freely move players wherever they want, then have a single save operation that would get the current ranking of all the players and update everybody. This sounds horribly wrong though.
There are about 160 players in the ladder so the operation wouldn't be particularly lengthy, just sounds wrong.
So, what would the best way to change ranking order and get it saved in the database be?
If your database support exact numeric data types, it can be easily solved by having the ranking column an exact numeric.
Then when player 5 beat player 2, all you have to do is set player 5's rank between player 2's and player 1's. since it's an exact numeric your limit is the precision limit of the data type of choice.
Sample data:
id name rank
1. Player 1 1.0
2. Player 2 2.0
3. Player 3 3.0
4. Player 4 4.0
5. Player 5 5.0
Now player 5 beat player 2. so all you have to do is set it's rank to 1.5, that's between player 2's rank and the first rank higher then player 2's.
If player 4 would beat player 1, I would set it's rank to 0.5, since player 1 has no one ranked higher.

Split M people to N teams with proportion constrains

I have the following problem and I need an algorithm for this. I need to write a program that split M student (in my case they are about 170 student) to N teams (12 teams) with the same number of students in each team as much as possible (in my case 14 or 15 student in a team), and there are 3 constrains.
the first constrain is proportions of female/male between the teams should be equal as much as possible.
the second constrain is proportions of outstanding/not outstanding students between the teams should be equal as much as possible.
the second constrain is proportions of student live in the city/outside the city between the teams should be equal as much as possible.
I don't need to find the optimal split, but split that good enough and I don't have definition what is good enough, maybe the maximum different in the proportion can be an input.
I have all the info I need about the students.
Thanks!!!
First, here's a general procedure for splitting a group of M students into N teams as evenly as possible:
Assign RoundDown(M/N) students to each of the N teams, in any manner.
If N is not divisible by M, then N - (M % N) < N "extra" students remain. Assign each of them to a different team.
After doing this, team sizes differ by at most 1 (some teams got no extra students, while some may have gotten exactly 1 extra student). Notice that if we have several separate groups of students then we can perform this procedure several times in succession to build up the N teams, and provided we always add any "extra" students to the smallest teams first, we will always maintain the property that team sizes differ by at most 1.
You have 3 separate criteria, so each student is in one of 2^3 = 8 groups defined by them (e.g. the group of male, non-outstanding, city students). So you can simply perform the above procedure 8 times, once for each group.
This will result in:
Team sizes will differ by at most 1.
The number of males (or of females, or of outstanding students, etc.) in any two teams will differ by at most 4, since there are 4 groups that include males (males who are/aren't oustanding, and who live/don't live in the city), and we know that for each of these groups, the number of students from that group in a team differs by at most 1.
In practice, it's unlikely that the number of people in any category will differ by as much as 4 between teams. You can mitigate this even further by being careful about which smallest teams get extra students first -- e.g. if you have 3 extra male, non-outstanding, city students left over and there are 7 small teams, you can put them in the 3 teams that have the fewest males (or non-outstanding students, or whichever criterion you want to prioritise). The same applies if there are more extra students in some group than small teams -- if there are 4 small teams and 9 extra students in some group, the first 4 students have to go to the small teams, but the remaining 5 students can go to whichever or the remaining 8 teams gives the best, say, gender balance.

Algorithm for dividing people into 1-to-1 or 2-to-2 teams

I have a number of badminton players as input, as well as a number of timeslots in 1 day and need to organize a playing schedule so in the end all players have played a number of games, and we have a final winner.
There are multiple algorithms that could be use for putting players into game slots, given a variety of constraints (minimizing games each player plays, minimizing the amount of time slots, maximizing the games each player plays, etc.).
Some examples that you might want to look into, for your specific constraints:
Round Robin
Ladder
In either case, if you want to determine who should go in which group or should be ranked in what ladder, you should seed your players (rank them approximately by how good they are known to be) and try to ensure that games later in the tournament would involve the clash of the highest seeds.
For example, look at the mixed-style system used by the international organization FIFA for generating their World Cup tournament game schedule:
First, they order the teams based on their ranking
They create n groups, with m teams each, and put teams in each group to ensure that something is evenly spread out. In their case, it is a combination of ranking and origin/location.
For example, if you have teams A B C D E F G H I J K L, ranked from best to worst, and you have three groups of four, you could perhaps do: (for simplicity's sake, ignoring trying to spread out teams by origin/location)
GROUP 1: A F G L
GROUP 2: B E H K
GROUP 3: C D I J
Each team in a group plays every other team. For example, in Group 1, Team A would play team F, G, and L. Team F would play with A, G, L. Team G with A, F, L, etc.
At the end, the best-faring (or best two) team(s) of each group face off in a Single Elimination Tournament, or a "knockout" tournament. The way this is arranged is so that teams in the same group play each other as late in the tournament as possible.
This system ensures that every team/player is guaranteed to play at least a few games, and can "come back" after a first loss. It also ensures that many games are played.
Alternatively, in a "pure round robin" tournament, you may not even use a single elimination tournament, and have everyone play in one giant Round Robin, and have the best of that big group be the winner.

Algorithm to create fair / evenly matched teams based on player rankings

I have a data set of players' skill ranking, age and sex and would like to create evenly matched teams.
Teams will have the same number of players (currently 8 teams of 12 players).
Teams should have the same or similar male to female ratio.
Teams should have similar age curve/distribution.
I would like to try this in Haskell but the choice of coding language is the least important aspect of this problem.
This is a bin packing problem, or a multi-dimensional knapsack problem. Björn B. Brandenburg has made a bin packing heuristics library in Haskell that you may find useful.
You need something like...
data Player = P { skill :: Int, gender :: Bool, age :: Int }
Decide on a number of teams n (I'm guessing this is a function of the total number of players).
Find the desired total skill per team:
teamSkill n ps = sum (map skill ps) / n
Find the ideal gender ratio:
genderRatio ps = sum (map (\x -> if gender x then 1 else 0)) / length ps
Find the ideal age variance (you'll want the Math.Statistics package):
ageDist ps = pvar (map age ps)
And you must assign the three constraints some weights to come up with a scoring for a given team:
score skillW genderW ageW team = skillW * sk + genderW * g + ageW * a
where (sk, (g, a)) = (teamSkill 1 &&& genderRatio &&& ageDist) team
The problem reduces to the minimization of the difference in scores between teams. A brute force approach will take time proportional to Θ(nk−1). Given the size of your problem (8 teams of 12 players each), this translates to about 6 to 24 hours on a typical modern PC.
EDIT
An approach that may work well for you (since you don't need an exact solution in practise) is simulated annealing, or continual improvement by random permutation:
Pick teams at random.
Get a score for this configuration (see above).
Randomly swap players between two or more teams.
Get a score for the new configuration. If it's better than the previous one, keep it and recurse to step 3. Otherwise discard the new configuration and try step 3 again.
When the score has not improved for some fixed number of iterations (experiment to find the knee of this curve), stop. It's likely that the configuration you have at this point will be close enough to the ideal. Run this algorithm a few times to gain confidence that you have not hit on some local optimum that is considerably worse than ideal.
Given the number of players per team and the gender ration (which you can easily compute). The remaining problem is called n-partition problem, which is unfortunately NP-complete and thus very hard to solve exactly. You will have to use approximative or heuristic allgorithms (evolutionary algorithms), if your problem size is too big for a brute force solution. A very simple approximation would be sorting by age and assign in an alternating way.
Assign point values to the skill levels, gender, and age
Assign the sum of the points for each criteria to each player
Sort players by their calculated point value
Assign the next player to the first team
Assign players to the second team until it has >= total points than the first team or the team reaches the maximum players.
Perform 5 for each team, looping back to the first team, until all players are assigned
You can tweak the skill level, gender, and age point values to change the distribution of each.
Lets say you have six players (for a simple example). We can use the same algorithm which pairs opponents in single-elimination tournaments and adapt that to generate "even" teams based on any criteria you choose.
First rank your players best-to-worst. Don't take this too literally. You want a list of players sorted by the criteria you wish to separate them.
Why?
Let's look at single elimination tournaments for a second. The idea of using an algorithm to generate optimal single-elimination matches is to avoid the problem of the "top players" meeting too soon in the tournament. If top players meet too soon, one of the top players will be eliminated early on, making the tournament less interesting. We can use this "optimal" pairing to generate teams in which the "top" players are spread out evenly across the teams. Then spread out the the second top players, etc, etc.
So list you players by the criteria you want them separated: men first, then women... sorted by age second. We get (for example):
Player 1: Male - 18
Player 2: Male - 26
Player 3: Male - 45
Player 4: Female - 18
Player 5: Female - 26
Player 6: Female - 45
Then we'll apply the single-elimination algorithm which uses their "rank" (which is just their player number) to create "good match ups".
The single-elimination tournament generator basically works like this: take their rank (player number) and reverse the bits (binary). This new number you come up with become their "slot" in the tournament.
Player 1 in binary (001), reversed becomes 100 (4 decimal) = slot 4
Player 2 in binary (010), reversed becomes 010 (2 decimal) = slot 2
Player 3 in binary (011), reversed becomes 110 (6 decimal) = slot 6
Player 4 in binary (100), reversed becomes 001 (1 decimal) = slot 1
Player 5 in binary (101), reversed becomes 101 (5 decimal) = slot 5
Player 6 in binary (110), reversed becomes 011 (3 decimal) = slot 3
In a single-elimination tournament, slot 1 plays slot 2, 3-vs-4, 5-vs-6. We're going to uses these "pair ups" to generate optimal teams.
Looking at the player number above, ordered by their "slot number", here is the list we came up with:
Slot 1: Female - 18
Slot 2: Male - 26
Slot 3: Female - 45
Slot 4: Male - 18
Slot 5: Female - 26
Slot 6: Male - 45
When you split the slots up into teams (two or more) you get the players in slot 1-3 vs players in slot 4-6. That is the best/optimal grouping you can get.
This technique scales very well with many more players, multiple criteria (just group them together correctly), and multiple teams.
Idea:
Sort players by skill
Assign best players in order (i.e.: team A: 1st player, team B: 2nd player, ...)
Assign worst players in order
Loop on 2
Evaluate possible corrections and perform them (i.e.: if team A has a total skill of 19 with a player with skill 5 and team B has a total skill of 21 with a player with skill 4, interchange them)
Evaluate possible corrections on gender distribution and perform them
Evaluate possible corrections on age distribution and perform them
Almost trivial approach for two teams:
Sort all player by your skill/rank assessment.
Assign team A the best player.
Assign team B the next two best players
Assign team A the next two best players
goto 3
End when you're out of players.
Not very flexible, and only works on one column ranking, so it won't try to get similar gender or age profiles. But it does make fair well matched teams if the input distribution is reasonably smooth. Plus it doesn't always end with team A have the spare player when there are an odd number.
Well,
My answer is not about scoring strategies of teams/players because all the posted are good, but I would try a brute force or a random search approach.
I don't think it's worth create a genetic algorithm.
Regards.

I have an algorithm problem having to do with scheduling teams in rotation as fairly as possible

I have a project for school where I have to come up with an algorithm for scheduling 4 teams to play volleyball on one court, such that each team gets as close to the same amount of time as possible to play.
If you always have the winners stay in and rotate out the loser, then the 4th ranked team will never play and the #1 team always will.
The goal is to have everybody play the same amount of time.
The simplest answer is team 1 play team 2, then team 3 play team 4 and keep switching, but then team 1 never gets to play team 3 or 4 and so on.
So I'm trying to figure out an algorithm that will allow everybody to play everybody else at some point without having one team sit out a lot more than any other team.
Suggestions?
How about this: Make a hashtable H of size NC2, in this case, 6. It looks like:
H[12] = 0
H[13] = 0
H[14] = 0
H[23] = 0
H[24] = 0
H[34] = 0
I am assuming it would be trivial to generate the keys.
Now to schedule a game, scan through the hash and pick the key with the lowest value (one pass). The teams denoted by the key play the game and you increment the value by one.
EDIT:
To add another constraint that no team should wait too long, make another hash W:
W[1] = 0
W[2] = 0
W[3] = 0
W[4] = 0
After every game increment the W value for the team that did not play, by one.
Now when picking up the least played team if there are more than one team combo with low play score, take help from this hash to determine which team must play next.
well you should play 1-2 3-4, 1-3 2-4, 1-4 2-3 and then start all over again.
If there are N teams and you want all pairs of them to play once, then there are "N choose 2" = N*(N-1)/2 games you need to run.
To enumerate them, just put the teams in an ordered list and have the first team play every other team, then have the second team play all the teams below it in the list, and so on. If you want to spread the games out so teams have similar rest intervals between games, then see Knuth.
Check out the wikipedia entry on round robin scheduling.
pretend it's a small sports league, and repeat the "seasons"...
(in most sports leagues in Europe, all teams play against all other teams a couple of times during a season)
The REQUIREMENTS for the BALANCED ROUND ROBIN algorithm, for the Team championship scheduling may be found here:
Constellation Algorithm - Balanced Round Robin
The requirements of the algorithm can be defined by these four constraints:
1) All versus all
Each team must meet exactly once, and once only, the other teams in the division/ league.
If the division is composed of n teams, the championship takes place in the n-1 rounds.
2) Alternations HOME / AWAY rule
The sequence of alternations HOME / AWAY matches for every teams in the division league, should be retained if possible. For any team in the division league at most once in the sequence of consecutive matches HAHA, occurs the BREAK of the rhythm, i.e. HH or AA match in the two consecutive rounds.
3) The rule of the last slot number
The team with the highest slot number must always be positioned in the last row of the grid. For each subsequent iteration the highest slot number of grid alternates left and right position; left column (home) and right (away).
The system used to compose the league schedule is "counter-clockwise circuit." In the construction of matches in one round of the championship, a division with an even number of teams. If in a division is present odd number of teams, it will be inserted a BYE/Dummy team in the highest slot number of grid/ring.
4) HH and AA non-terminal and not initial
Cadence HH or AA must never happen at the beginning or at the end of the of matches for any team in the division.

Resources