# Editing Constraints in the Picking Winners programs

**URL:** <https://discourse.julialang.org/t/editing-constraints-in-the-picking-winners-programs/8336>\
**Category:** New to Julia\
**Created:** [January 13, 2018, 4:33am UTC](https://discourse.julialang.org/t/editing-constraints-in-the-picking-winners-programs/8336 "2018-01-13T04:33:42Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Cameron\_Taylor](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cameron_taylor/32/17679_2.png) [@Cameron\_Taylor](https://discourse.julialang.org/u/Cameron_Taylor)\
**Post date:** [January 13, 2018, 4:33am UTC](https://discourse.julialang.org/t/editing-constraints-in-the-picking-winners-programs/8336/1 "2018-01-13T04:33:42Z")

</div>

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](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.

---

<div class="post-metadata">

**Author:** ![mohamed82008](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mohamed82008/32/18171_2.png) [@mohamed82008](https://discourse.julialang.org/u/mohamed82008)\
**Post date:** [January 13, 2018, 7:09am UTC](https://discourse.julialang.org/t/editing-constraints-in-the-picking-winners-programs/8336/2 "2018-01-13T07:09:58Z")

</div>

Is this a homework problem?

---

<div class="post-metadata">

**Author:** ![Cameron\_Taylor](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cameron_taylor/32/17679_2.png) [@Cameron\_Taylor](https://discourse.julialang.org/u/Cameron_Taylor)\
**Post date:** [January 13, 2018, 4:53pm UTC](https://discourse.julialang.org/t/editing-constraints-in-the-picking-winners-programs/8336/3 "2018-01-13T16:53:56Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![mohamed82008](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mohamed82008/32/18171_2.png) [@mohamed82008](https://discourse.julialang.org/u/mohamed82008)\
**Post date:** [January 13, 2018, 11:23pm UTC](https://discourse.julialang.org/t/editing-constraints-in-the-picking-winners-programs/8336/4 "2018-01-13T23:23:10Z")

</div>

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

```julia
@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.

---

<div class="post-metadata">

**Author:** ![Cameron\_Taylor](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cameron_taylor/32/17679_2.png) [@Cameron\_Taylor](https://discourse.julialang.org/u/Cameron_Taylor)\
**Post date:** [January 14, 2018, 2:42am UTC](https://discourse.julialang.org/t/editing-constraints-in-the-picking-winners-programs/8336/5 "2018-01-14T02:42:30Z")

</div>

Thanks for your response Mohamed. I don’t think you have to know anything about hockey either 🙂. 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.

---

<div class="post-metadata">

**Author:** ![mohamed82008](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mohamed82008/32/18171_2.png) [@mohamed82008](https://discourse.julialang.org/u/mohamed82008)\
**Post date:** [January 14, 2018, 6:23am UTC](https://discourse.julialang.org/t/editing-constraints-in-the-picking-winners-programs/8336/6 "2018-01-14T06:23:30Z")

</div>

> [@Cameron\_Taylor](#):
>
> Seems as though the program has only defined teams for the players, and not for the goalies

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 🙂

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!

---

<div class="post-metadata">

**Author:** ![Cameron\_Taylor](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cameron_taylor/32/17679_2.png) [@Cameron\_Taylor](https://discourse.julialang.org/u/Cameron_Taylor)\
**Post date:** [January 24, 2018, 6:52pm UTC](https://discourse.julialang.org/t/editing-constraints-in-the-picking-winners-programs/8336/7 "2018-01-24T18:52:15Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Adam\_Sardinha](https://avatars.discourse-cdn.com/v4/letter/a/ee7513/32.png) [@Adam\_Sardinha](https://discourse.julialang.org/u/Adam_Sardinha)\
**Post date:** [January 16, 2019, 1:13pm UTC](https://discourse.julialang.org/t/editing-constraints-in-the-picking-winners-programs/8336/8 "2019-01-16T13:13:29Z")

</div>

Is this topic still active?

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

Much appreciated.

---

<div class="post-metadata">

**Author:** ![Adam\_Sardinha](https://avatars.discourse-cdn.com/v4/letter/a/ee7513/32.png) [@Adam\_Sardinha](https://discourse.julialang.org/u/Adam_Sardinha)\
**Post date:** [January 16, 2019, 2:46pm UTC](https://discourse.julialang.org/t/editing-constraints-in-the-picking-winners-programs/8336/9 "2019-01-16T14:46:34Z")

</div>

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

[Raw Source Code from GitHub](https://raw.githubusercontent.com/zlisto/Daily-Fantasy-Hockey-for-DraftKings/master/code_for_Github.jl)

- 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

---

<div class="post-metadata">

**Author:** ![sonolio](https://avatars.discourse-cdn.com/v4/letter/s/a3d4f5/32.png) [@sonolio](https://discourse.julialang.org/u/sonolio)\
**Post date:** [January 9, 2020, 12:06am UTC](https://discourse.julialang.org/t/editing-constraints-in-the-picking-winners-programs/8336/10 "2020-01-09T00:06:16Z")

</div>

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
