0

Updated: Not sure I explained it well first time.

I have a scheduling problem, or more accurately, a "first come first served" problem. A list of available assets are assigned a set of spaces, available in pairs (think cars:parking spots, diners:tables, teams:games). I need a rough simulation (random) that chooses the first two to arrive from available pairs, then chooses the next two from remaining available pairs, and so on, until all spaces are filled.

Started using teams:games to cut my teeth. The first pair is easy enough. How do I then whittle it down to fill the next two spots from among the remaining available entities? Tried a bunch of different things, but coming up short. Help appreciated.

import itertools
import numpy as np
import pandas as pd

a = ['Georgia','Oregon','Florida','Texas'], ['Georgia','Oregon','Florida','Texas']
b = [(x,y) for x,y in itertools.product(*a) if x != y]
c = pd.DataFrame(b)
c.columns = ['home', 'away']
print(c)
d = c.sample(n = 2, replace = False)
print(d)

The first results is all possible combinations. But, once the first slots are filled, there can be no repeats. in example below, once Oregon and Georgia are slated in, the only remaining options to choose from are Forlida:Texas or Texas:Florida. Obviously just the sample function alone produces duplicates frequently. I will need this to scale up to dozens, then hundreds of entities:slots. Many thanks in advance!

       home     away
0   Georgia   Oregon
1   Georgia  Florida
2   Georgia    Texas
3    Oregon  Georgia
4    Oregon  Florida
5    Oregon    Texas
6   Florida  Georgia
7   Florida   Oregon
8   Florida    Texas
9     Texas  Georgia
10    Texas   Oregon
11    Texas  Florida
     home     away
3  Oregon  Georgia
5  Oregon    Texas
mayord
  • 19
  • 5

1 Answers1

0

Not exactly sure what you are trying to do. But if you want to randomly pair your unique entities you can simply randomly order them and then place them in a 2-columns dataframe. I wrote this with all the US states minus one (Wyomi):

states = ['Alaska','Alabama','Arkansas','Arizona','California',
        'Colorado','Connecticut','District of Columbia','Delaware',
        'Florida','Georgia','Hawaii','Iowa','Idaho','Illinois',
        'Indiana','Kansas','Kentucky','Louisiana','Massachusetts',
        'Maryland','Maine','Michigan','Minnesota','Missouri',
        'Mississippi','Montana','North Carolina','North Dakota',
        'Nebraska','New Hampshire','New Jersey','New Mexico',
        'Nevada','New York','Ohio','Oklahoma','Oregon',
        'Pennsylvania','Rhode Island','South Carolina',
        'South Dakota','Tennessee','Texas','Utah','Virginia',
        'Vermont','Washington','Wisconsin','West Virginia']

a=states.copy()
random.shuffle(states)
c = pd.DataFrame({'home':a[::2],'away':a[1::2]})
print(c)

#Output
                    home            away
0          West Virginia       Minnesota
1          New Hampshire       Louisiana
2                 Nevada         Florida
3                Alabama         Indiana
4               Delaware    North Dakota
5                Georgia    Rhode Island
6                 Oregon    Pennsylvania
7               New York    South Dakota
8               Maryland          Kansas
9                   Ohio          Hawaii
10              Colorado       Wisconsin
11                  Iowa           Idaho
12              Illinois        Missouri
13               Arizona     Mississippi
14           Connecticut         Montana
15  District of Columbia         Vermont
16             Tennessee        Kentucky
17                Alaska      Washington
18            California        Michigan
19              Arkansas      New Jersey
20         Massachusetts            Utah
21              Oklahoma      New Mexico
22              Virginia  South Carolina
23        North Carolina           Maine
24                 Texas        Nebraska

Not sure if this is exactly what you were asking for though.

If you need to schedule all the fixtures of the season, you can check this answer --> League fixture generator in python

atteggiani
  • 94
  • 4