问题
How the dataset looks like:
I'm trying to build an optimization tool for fantasy football, but I'm having difficulty forcing the model to use players from the same team.
9 players form a lineup, need to be under 50k and we are maximizing "proj" projected points
self.salary_cap = 50000
self.Minsalary_cap = 0
self.header = ['QB', 'RB', 'RB','WR', 'WR', 'WR', 'TE','FLEX', 'Def']
#define the pulp object problem
prob = pulp.LpProblem('NFL', pulp.LpMaximize)
#define the player variabless
players_lineup = [pulp.LpVariable("player_{}".format(i+1), cat="Binary") for i in range(self.num_players)]
QBs_lineup = [pulp.LpVariable("QB_{}".format(i+1), cat="Binary") for i in range(self.num_QBs)]
RBs_lineup = [pulp.LpVariable("RB_{}".format(i+1), cat="Binary") for i in range(self.num_RBs)]
WRs_lineup = [pulp.LpVariable("WR_{}".format(i+1), cat="Binary") for i in range(self.num_WRs)]
TEs_lineup = [pulp.LpVariable("TE_{}".format(i+1), cat="Binary") for i in range(self.num_TEs)]
FLEXs_lineup = [pulp.LpVariable("FLEX_{}".format(i+1), cat="Binary") for i in range(self.num_FLEXs)]
Defs_lineup = [pulp.LpVariable("Def_{}".format(i+1), cat="Binary") for i in range(self.num_Defs)]
#add the max player constraints
#prob += (pulp.lpSum(players_lineup[i] for i in range(self.num_players)) == 9)
prob += (pulp.lpSum(QBs_lineup[i] for i in range(self.num_QBs)) == 1)
prob += (pulp.lpSum(RBs_lineup[i] for i in range(self.num_RBs)) == 2)
prob += (pulp.lpSum(WRs_lineup[i] for i in range(self.num_WRs)) == 3)
prob += (pulp.lpSum(TEs_lineup[i] for i in range(self.num_TEs)) == 1)
prob += (pulp.lpSum(FLEXs_lineup[i] for i in range(self.num_FLEXs)) == 1)
prob += (pulp.lpSum(Defs_lineup[i] for i in range(self.num_Defs)) == 1)
#add the salary constraint
prob += (self.Minsalary_cap <= (pulp.lpSum(self.QBs.loc[i, 'sal']*QBs_lineup[i] for i in range(self.num_QBs)) +
pulp.lpSum(self.RBs.loc[i, 'sal']*RBs_lineup[i] for i in range(self.num_RBs)) +
pulp.lpSum(self.WRs.loc[i, 'sal']*WRs_lineup[i] for i in range(self.num_WRs)) +
pulp.lpSum(self.TEs.loc[i, 'sal']*TEs_lineup[i] for i in range(self.num_TEs)) +
pulp.lpSum(self.FLEXs.loc[i, 'sal']*FLEXs_lineup[i] for i in range(self.num_FLEXs)) +
pulp.lpSum(self.Defs.loc[i, 'sal']*Defs_lineup[i] for i in range(self.num_Defs))))
prob += ((pulp.lpSum(self.QBs.loc[i, 'sal']*QBs_lineup[i] for i in range(self.num_QBs)) +
pulp.lpSum(self.RBs.loc[i, 'sal']*RBs_lineup[i] for i in range(self.num_RBs)) +
pulp.lpSum(self.WRs.loc[i, 'sal']*WRs_lineup[i] for i in range(self.num_WRs)) +
pulp.lpSum(self.TEs.loc[i, 'sal']*TEs_lineup[i] for i in range(self.num_TEs)) +
pulp.lpSum(self.FLEXs.loc[i, 'sal']*FLEXs_lineup[i] for i in range(self.num_FLEXs)) +
pulp.lpSum(self.Defs.loc[i, 'sal']*Defs_lineup[i] for i in range(self.num_Defs))) <= self.salary_cap)
The problem I'm facing is how would I force it to make the QB and a WR on the same team?
回答1:
If I understood correctly, you want to control that, if QB is from team X, there is at least one WR from team X. Or something around this.
Then you could enforce that for each team the sum of selected QB from that team is greater is limited by the number of selected WR from that team.
# teams is the list of all teams
teams = []
# we fill a dictionary that, for each team, stores a list of the players on that team.
QBs_from_team = {t: [player for player in QBs[QBs['team']==t]] for t in teams}
WRs_from_team = {}
# then you can create the pulp constraint:
# if QB is from team "X", then at least one WR will be from that team
for team in teams:
prob += pulp.lpSum(QBs_lineup[player] for player in QBs_from_team[t]) <= \
pulp.lpSum(WRs_lineup[player] for player in WRs_from_team[t])
# if all 3 WR need to be from the team, just add a 3 multiplying the number of QB.
If you want something else, maybe this constraint inspires you to conceive it. If it doesn't, feel free to give more information in your question.
来源:https://stackoverflow.com/questions/62218594/optimization-problem-how-to-add-same-team-constraint