views:

465

answers:

3

I am trying to do the schedule for the upcoming season for my simulation baseball team. I have an existing Postgresql database that contains the old schedule.

There are 648 rows in the database: 27 weeks of series for 24 teams. The problem is that the schedule has gotten predictable and allows teams to know in advance about weak parts of their schedule. What I want to do is take the existing schedule and randomize it. That way teams are still playing each other the proper number of times but not in the same order as before.

There is one rule that has been tripping me up: each team can only play one home and one road series PER week. I had been fooling around with SELECT statements based on ORDER BY RANDOM() but I haven't figured out how to make sure a team only has one home and one road series per week.

Now, I could do this in PHP (which is the language I am most comfortable with) but I am trying to make the shift to Python so I'm not sure how to get this done in Python. I know that Python doesn't seem to handle two dimensional arrays very well.

Any help would be greatly appreciated.

+1  A: 

I'm not sure I fully understand the problem, but here is how I would do it: 1. create a complete list of matches that need to happen 2. iterate over the weeks, selecting which match needs to happen in this week.

You can use Python lists to represent the matches that still need to happen, and, for each week, the matches that are happening in this week.

In step 2, selecting a match to happen would work this way: a. use random.choice to select a random match to happen. b. determine which team has a home round for this match, using random.choice([1,2]) (if it could have been a home round for either team) c. temporarily remove all matches that get blocked by this selection. a match is blocked if one of its teams has already two matches in the week, or if both teams already have a home match in this week, or if both teams already have a road match in this week. d. when there are no available matches anymore for a week, proceed to the next week, readding all the matches that got blocked for the previous week.

Martin v. Löwis
+1  A: 

Have you considered keeping your same "schedule", and just shuffling the teams? Generating a schedule where everyone plays each other the proper number of times is possible, but if you already have such a schedule then it's much easier to just shuffle the teams.

You could keep your current table, but replace each team in it with an id (0-23, or A-X, or whatever), then randomly generate into another table where you assign each team to each id (0 = TeamJoe, 1 = TeamBob, etc). Then when it's time to shuffle again next year, just regenerate that mapping table.

Not sure if this answers the question the way you want, but is probably what I would go with (and is actually how I do it on my fantasy football website).

Chris J
+1  A: 

I think I've understood your question correctly, but anyhow, you can make use of Python's set datatype and generator functionality:

import random

def scheduler(teams):
    """ schedule generator: only accepts an even number of teams! """
    if 0 != len(teams) % 2:
        return

    while teams:
        home_team = random.choice(list(teams))
        teams.remove(home_team)
        away_team = random.choice(list(teams))
        teams.remove(away_team)
        yield(home_team, away_team)

# team list from sql select statement
teams = set(["TEAM A", "TEAM B", "TEAM C", "TEAM D"])

for team in scheduler(teams):
    print(team)

This keeps SQL processing to a minimum and should be very easy to add to new rules, like the ones I didn't understand ;) Good luck

EDIT:

Ah, makes more sense now, should have had one less beer! In which case, I'd definitely recommend NumPy. Take a look through the tutorial and look at consuming the 2-dimensional home-away teams array as you are grabbing random fixtures. It would probably be best to feed the home and away teams from the home day into the away day, so you can ensure there that each team plays home and away each week.

Daz
+1: give up on SQL and use Python.
S.Lott
I think we're getting closer. However, what I want is to keep the existing matchups in the schedule (it's not random) but have the games occur at different times with the same "only one road and one home series per week" rule
GrumpyCanuck