Editing Constraints in the Picking Winners programs


#1

Hi there,

I’m trying to edit one of the constraints in the code_for_Github.jl file as developed here
https://github.com/dscotthunter/Fantasy-Hockey-IP-Code

The program generates lineups of sports players to submit in daily fantasy sports contests. The contests have certain rules for how lineups are constructed, and I’d like to change how one of these rules was programmed in the code.

The constraint in question was originally written for a contest in which the 8 skaters in a lineup had to play for exactly three different teams. It was written as follows:

# exactly 3 different teams for the 8 skaters constraint
@defVar(m, used_team[i=1:num_teams], Bin)
@addConstraint(m, constr[i=1:num_teams], used_team[i] <= sum{skaters_teams[t, i]*skaters_lineup[t], t=1:num_skaters})
@addConstraint(m, constr[i=1:num_teams], sum{skaters_teams[t, i]*skaters_lineup[t], t=1:num_skaters} <= 6*used_team[i])
@addConstraint(m, sum{used_team[i], i=1:num_teams} == 3)

For the contests I play in, each lineup can have up to a maximum of four players who play for the same team. Each lineup consists of 8 skaters and a goalie.

I initially wrote the following line:

@addConstraint(m, sum{skaters_teams[i]*skaters_lineup[i], i=1:num_skaters} + sum{goalie_teams[i]*goalies_lineup[i], i=1:num_goalies} <= 4)

but realized this limits each lineup to having players from no more than four teams, as opposed to preventing lineups from having more than four players that play for the same team.

So, instead of summing the total number of teams in each lineup, how do I get the program to count unique instances of each team in a given lineup, and limit this to a maximum of four?

Any help would be much appreciated, and let me know if I can provide more information.

Thank you.


#2

Is this a homework problem?


#3

It’s not related to any courses I’m taking, though I am a beginner and trying to learn as much as I can. I came across the program on my own and have been playing around with the constraints.

I think the key is to find the mode for teams on a given lineup, and limit this to four. As opposed to the current code, which limits the unique number of teams in a lineup to four.


#4

Alright. I believe this is how it should be done:

@addConstraint(m, constr[t=1:num_teams], sum{skaters_teams[i,t]*skaters_lineup[i], i=1:num_skaters} + sum{goalie_teams[i,t]*goalies_lineup[i], i=1:num_goalies} <= 4)

I am assuming that skaters_teams[i,t] is 1 iff skater i plays for team t and 0 otherwise, and similarly for goalie_teams.

Disclaimer: I know nothing about hockey, and I don’t think I have to.


#5

Thanks for your response Mohamed. I don’t think you have to know anything about hockey either :slight_smile:. Seems as though the program has only defined teams for the players, and not for the goalies, because it gets the error UndefVarError: goalie_teams not defined. As a result, num_teams seems to only consider the teams of the players, and not the goalie.

The program creates team indicators as follows:

teams = unique(skaters[:Team])

# Total number of teams
num_teams = size(teams)[1]

# player_info stores information on which team each player is on
player_info = zeros(Int, size(teams)[1])

# Populate player_info with the corresponding information
for j=1:size(teams)[1]
    if skaters[1, :Team] == teams[j]
        player_info[j] =1
    end
end
skaters_teams = player_info'


for i=2:num_skaters
    player_info = zeros(Int, size(teams)[1])
    for j=1:size(teams)[1]
        if skaters[i, :Team] == teams[j]
            player_info[j] =1
        end
    end
    skaters_teams = vcat(skaters_teams, player_info')
end

The program does consider goalie_teams in the following code, but they concern themselves with defining the goalie’s opponent.

# Create goalie identifiers so you know who they are playing
opponents = goalies[:Opponent]
goalie_teams = goalies[:Team]
goalie_opponents=[]
for num = 1:size(teams)[1]
    if opponents[1] == teams[num]
        goalie_opponents = skaters_teams[:, num]
    end
end
for num = 2:size(opponents)[1]
    for num_2 = 1:size(teams)[1]
        if opponents[num] == teams[num_2]
            goalie_opponents = hcat(goalie_opponents, skaters_teams[:,num_2])
        end
    end
end

So far I’ve been unsuccessful at defining goalie_teams, so as always any help would be appreciated. Thanks again.


#6

Yes I noticed this but the old program had no business defining it so I thought you will define it in your own version of the program. Anyways, I think this has nothing to do with Julia to be honest :slight_smile:

I think this line goalie_teams = goalies[:Team] shows you how to get the goalie_teams information but it doesn’t seem to be used in the section you posted so look further ahead in the code for its use and see if this is what you want or not and in the structure you want (i.e. binary matrix) or not. Good luck!


#7

Thanks for all your help. Finally got it to work! And learned a bunch in the process.