I have a python script where I use itertools.combinations to select a number of combinations within a fixed range.
To give an example, I have a csv file that contains a list of golfers and they each have a ranking between 1 and 3
Seamus Power,10500,1
Brian Harman,10300,2
Tom Hoge,9800,1
Taylor Montgomery,9600,1
Jason Day,9400,1
Keith Mitchell,9300,2
Joel Dahmen,9200,1
Denny McCarthy,9100,1
Matthew NeSmith,9000,1
Matt Kuchar,8900,1
Mackenzie Hughes,8600,1
Davis Riley,8100,3
Brendon Todd,8000,3
Andrew Putnam,7900,1
J.J. Spaun,7800,3
Aaron Rai,7800,1
Harris English,7700,2
Will Gordon,7700,1
Greyson Sigg,7500,1
J.T. Poston,7500,1
Seonghyeon Kim,7500,3
Troy Merritt,7400,1
Sepp Straka,7200,1
Justin Suh,7200,2
Adam Long,7100,1
Kevin Streelman,7000,2
Ben Taylor,7000,3
John Huh,6900,2
Austin Cook,6800,2
David Lingmerth,6700,3
In my code I then place each golfer into it's own list based on it's ranking and then in my tertools.combinations I state give me a set amount per tier. As I want 6 golfers (and it's got to be 6 golfers to create my lineup) in the example below I state give me 2 combination of golfers from tier 1 and 4 combination of golfers from tier 2 and set a lineup with them like so:
tier1Range = []
tier2Range = []
tier3Range = []
with open('combinations.csv', 'r') as file:
reader = csv.reader(file)
for row in reader:
if int(row[2]) == 1:
tier1Range.append(row)
elif int(row[2]) == 2:
tier2Range.append(row)
elif int(row[2]) == 3:
tier3Range.append(row)
lineup = []
finalLineup = []
finalRandomLineup = []
#Edit below to fit creiteria of which ranges and how many per range
sel_tier1 = itertools.combinations(tier1Range,2)
sel_tier2 = itertools.combinations(tier2Range,4)
lineup = [p + q for p,q in itertools.product(sel_tier1,sel_tier2)]
finalLineup = [x for x in lineup if int(x[0][1]) + int(x[1][1]) + int(x[2][1]) + int(x[3][1]) + int(x[4][1]) + int(x[5][1]) >= 49700
and int(x[0][1]) + int(x[1][1]) + int(x[2][1]) + int(x[3][1]) + int(x[4][1]) + int(x[5][1]) <= 50000]
finalRandomLineup += ((random.sample(finalLineup,20)))
totalSalary = int
with open('temp.csv', 'w') as f:
for line in finalRandomLineup:
totalSalary = int(line[0][1]) + int(line[1][1]) + int(line[2][1]) + int(line[3][1]) + int(line[4][1]) + int(line[5][1])
f.write(line[0][0] + ',' + line[1][0] + ',' + line[2][0] + ',' + line[3][0] + ',' + line[4][0] + ',' + line[5][0] + ' ^ total salary ' + str(totalSalary)+ '\n\n')
However, I want my combinations to be more dynamic than that and this is my question. I want to have the ability to states I want a 2-3 golfers from tier 1 and 3-4 golfers from tier 2 as an example.
So rather than a fixed range of 2 from tier 1 and 4 from tier 2, I want the ability to pick combinations of 2 or 3 golfers from tier 1 and combinations of 3 or 4 golfers from tier 2.
At the end when the lineup is created, it has to be a max of 6 golfers. So I can't have 3 golfers from tier 1 combined with 4 golfers from tier 2 as it goes over the threshold.
Does anybody know the best way to achieve this?
EDIT
I updated the code above to include salary, where the lineups it generates has to be between the salary threshold of 49700 and 50000 based on the second column from the csv (which has been updated). So the csv goes, golfer name, salary, rank per row.
So above it picks 2 golfers from tier 1, and 4 from tier 2 and then when it creates my lineups, it only picks those where the combined salary of all 6 golfers golfers is between 49700 and 50000