Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re^2: Unique Combos with Math::Combinatorics

by ketema (Scribe)
on Mar 02, 2008 at 02:13 UTC ( [id://671451]=note: print w/replies, xml ) Need Help??


in reply to Re: Unique Combos with Math::Combinatorics
in thread Unique Combos with Math::Combinatorics

OK with your idea I came up with this:
#!/usr/bin/perl #program will generate all unique combinations of 4 from a pool of pla +yers #without repeating any pair of players use strict; use Math::Combinatorics; my $count; my %seen; sub trackSeen{ my @players = @{$_[0]}; #For each set of 4 players the following positions are ones we do +not want to see again my @pairs = ($players[0].$players[1], #0,1 $players[0].$players[2], #0,2 $players[0].$players[3], #0,3 $players[1].$players[2], #1,2 $players[1].$players[3], #1,3 $players[2].$players[3] #2,3 ); foreach my $pair (@pairs){ $seen{$pair} = 1; } } sub notSeen{ my @players = @{$_[0]}; my @pairs = ($players[0].$players[1], #0,1 $players[0].$players[2], #0,2 $players[0].$players[3], #0,3 $players[1].$players[2], #1,2 $players[1].$players[3], #1,3 $players[2].$players[3] #2,3 ); foreach my $pair (@pairs){ if (scalar grep /$pair/, keys(%seen) ){ return 0; } } return 1; } my @n = 'A'..'X'; my $comboObj = Math::Combinatorics->new(count => 4, data => [@n]); print "combinations of 4 from: " . join(" ", @n)."\n"; print "-----------------------------" . ("----" x scalar(@n))."\n"; while ( my @combo = $comboObj->next_combination){ if ( notSeen(\@combo) == 1) { print join(', ', @combo) . "\n"; $count++; trackSeen(\@combo); } } print "$count combinations\n";
The magic number for 24 players seems to be 35. I am sure this is not the most efficient way of doing it, but it works. Thanks. Output:
A, B, C, D A, E, F, G A, H, I, J A, K, L, M A, N, O, P A, Q, R, S A, T, U, V B, E, H, K B, F, I, L B, G, J, M B, N, Q, T B, O, R, U B, P, S, V C, E, I, M C, F, H, N C, G, K, O C, J, L, P C, Q, U, W C, R, T, X D, E, J, N D, F, K, P D, G, H, L D, I, O, Q D, M, R, V D, S, T, W E, L, O, S E, P, Q, X F, J, O, T F, M, S, U F, V, W, X G, I, N, R H, M, O, W I, K, S, X J, K, Q, V L, N, U, X 35 combinations

Replies are listed 'Best First'.
Re^3: Unique Combos with Math::Combinatorics (games)
by tye (Sage) on Mar 02, 2008 at 04:57 UTC

    Unfortunately, that would mean that W only gets to play on 4 teams while everybody else gets to play on more and six players (A B C D F O) get to play on 7 teams. I doubt your players will consider that fair.

    Update: You can get pretty fair with this subset of teams:

    A, E, F, G A, H, I, J A, N, O, P A, Q, R, S A, T, U, V B, E, H, K B, F, I, L B, N, Q, T B, O, R, U B, P, S, V C, E, I, M C, G, K, O C, J, L, P C, Q, U, W C, R, T, X D, E, J, N D, G, H, L D, M, R, V D, S, T, W E, P, Q, X F, M, S, U F, V, W, X G, I, N, R H, M, O, W I, K, S, X J, K, Q, V L, N, U, X

    Which has half of the players on 5 teams (A B C E I N Q R S U V X) and half of them on 4 teams (D F G H J K L M O P T W).

    Update: And if you add the following teams:

    T, K, G, M D, F, H, P W, J, L, O

    Then every player gets to play on 5 teams and there are only 4 pairings that ever get repeated (DH KG WO JL).

    - tye        

      Update: OK I had no idea I had so many responses. I just read through them all. I am very pleased that I got so much interest. I will say I do not understand the math in depth, but I have been reading up on the plane stuff. I will update this thread a little more by stating that my goal was in creating a unique way to have a 4 on 4 league of basketball at my local gym. Here is the idea i submitted to the rec center people:
      Sample Schedule for Oviedo 4 on 4 Basketball Round Robin League Players: This example uses 24 total players. Multiple of 4 is best bu +t odd numbers can be accommodated. Rules: Courts: 2 Time: 8 am to 11 am on Sundays Game Rules: 15 min or 11 “Buckets” whichever comes first 3 team fouls. Offense inbounds from top of key when defense not in pe +nalty, shoots one free throw when defense is in penalty. Spirit of the league is to maintain competitive balance and promote te +am work by giving all players a chance to play with each other, while + also maintaing the spirit of “street ball”. Each player is responsi +ble only for paying his fee and showing up each week. Schedule is au +to generated based on number of signed up players. Kudos: Refs, stats, website, videos, jerseys. = More Fun! Scheduling example: 24 total players in league 3 hours of time = 180 min. 15 min per game = 12 Games per Sunday (max) 2 courts = 6 games per court 8 players playing per game = 16 players playing at one time = 8 player +s waiting at any one time. To honor the spirit of competitive balance, each player in the league +will play with every other player at least once. With the 24 example + players, that equals 35 possible “teams” where no two players are th +e same on any team. The schedule allows every player to play with an +d/or against every one else with each individual maintaining his own +W/L record. At the end of the “Regular” season the league players wi +ll be divided up by record into an “A” bracket and a “B” bracket. In + this example that equals 12 players per bracket. These players woul +d be divided into 3 fixed teams and play a double elimination tournam +ent for 1-3 place overall in the A bracket and 4-6 in the B bracket.
      Now what happened in real life was that the rec guys found that not every one shows up each week. So we wound up mostly tracking stuff by hand, but the perl was useful in generating teams for the people that showed up on any particular Sunday. We also found a nice piece of software called Splendid City that helped us with scheduling, but in the end since the pool of players was different each week and we never knew who was coming and who wasn't we wound up just tracking individuals records and making the teams based on who was present each individual Sunday. We tried to balance people from playing with the same ones too much but we had no real formula for it, and were really limited by just who was there.
      I will admit reading all the posts has me interested in coming up with a better way to have it organized and automated. I would love to have a program that would allow me to enter the total player pool, generate random teams on a daily basis and take into account factors such as what i described happened in real life:
      • who's present
      • who has played with each other already
      • >etc...
    Re^3: Unique Combos with Math::Combinatorics
    by ikegami (Patriarch) on Mar 02, 2008 at 03:05 UTC

      Why did you remove the loops in favour of all that typing and redundancy?

      And [ @n ] needlessly creates a copy of the array when you could simply use \@n.

        Simply because although I understood the idea of what you were doing, I did not understand how to use the code. I typed it out so i could visualize what was going on,each assignment etc... After reviewing your code and comparing I do see now how it works, but I never would see that on my own to start. Thanks for the help.

          I did not understand how to use the code.

          Change
          for each combination @c:
          to
          while ( my @c = $comboObj->next_combination){

          and change
          # emit combination here
          to
          print join(', ', @c) . "\n";

          After reviewing your code and comparing I do see now how it works,

          It was neither my code nor my post.

    Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Domain Nodelet?
    Node Status?
    node history
    Node Type: note [id://671451]
    help
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this?Last hourOther CB clients
    Other Users?
    Others studying the Monastery: (7)
    As of 2024-04-19 12:18 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      No recent polls found