Editing Constraints in the Picking Winners programs

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.

Is this a homework problem?

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.

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.

1 Like

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.

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!

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

2 Likes

Is this topic still active?

If so, I have some customization questions that are similar.

Much appreciated.

To help with the conversation. Here are some items that I’m hoping to get some clarity with.

Raw Source Code from GitHub

  • Is there a way to limit the number of occurrences a specific play appears in the output, either set as a “global exposure” or limited to the specific position (i.e. Goalie)? I assume you would add that to the constraints in the “Type” section, but doing a quick dig, I would guess that “maximum()” would work with this?

  • One of the constraints is to have one complete line (line 454). I’m wondering if there is a way to correlate this so that the goalie from the same team is included in this constraint, or if there is an option to ensure that we have the option to correlate goalies in general with one team.

  • In trying to set the four players per team constraint, I was given an error, with the stack trace at line 871. I also added goalie_teams to line 804 and 805 in that array, as I assume that is where you need to put the credentials for it to work. I can share the full error later, but is there anything I am missing to make a customization to this constraint?

I’m new to Julia and have been playing around with the file to edit constraints so that defenders can be on either PP1 or PP2.

Please let me know if I can provide any additional details (I’ll be adding the entire error later today).

Thanks again @Cameron_Taylor and @mohamed82008

if you don’t mind can you share the code? I have been trying to get it to run for but I cant seem to get anywhere with it as I am not experienced with julia