├── example_goalies.csv ├── README.md ├── example_skaters.csv ├── LICENSE.md └── code_for_Github.jl /example_goalies.csv: -------------------------------------------------------------------------------- 1 | First Name,Last Name,Salary,Team,Opponent,Projection John,Doe_goalie_1,6700,A,B,5.3 John,Doe_goalie_2,6800,B,A,5.7 John,Doe_goalie_3,6900,C,D,4.5 John,Doe_goalie_4,6900,D,C,4.7 John,Doe_goalie_5,6800,E,F,8.7 John,Doe_goalie_6,6700,F,E,8.9 John,Doe_goalie_7,6000,G,H,7.6 John,Doe_goalie_8,5800,H,G,5.2 John,Doe_goalie_9,4400,I,J,6.7 John,Doe_goalie_10,6500,J,I,6.7 -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Fantasy Hockey IP Code 2 | ====================== 3 | 4 | This is the code for the paper [Picking Winners Using Integer Programming](http://arxiv.org/pdf/1604.01455v2.pdf) by [David Hunter](http://orc.scripts.mit.edu/people/student.php?name=dshunter), [Juan Pablo Vielma](http://www.mit.edu/~jvielma/), and [Tauhid Zaman](http://zlisto.scripts.mit.edu/home/). Below are details on the required software, and instructions on how to run different variations of the code. 5 | 6 | ## Required Software 7 | - [Julia](http://julialang.org/) 8 | - [GLPK](https://www.gnu.org/software/glpk/) 9 | - [JuMP](https://github.com/JuliaOpt/JuMP.jl) 10 | - [DataFrames.jl](https://github.com/JuliaStats/DataFrames.jl) 11 | - [GLPKMathProgInterface.jl](https://github.com/JuliaOpt/GLPKMathProgInterface.jl) 12 | 13 | To start off, you should download Julia from the corresponding site above. Then, open Julia and run the following commands 14 | ```julia 15 | julia> Pkg.add("JuMP") 16 | julia> Pkg.add("DataFrames") 17 | julia> Pkg.add("GLPKMathProgInterface") 18 | ``` 19 | 20 | As we noted in the paper, [GLPK](https://www.gnu.org/software/glpk/) and [Cbc](https://projects.coin-or.org/Cbc) are both open-source solvers. This code uses GLPK because we found that it was slightly faster in practice for the formulations considered. For those that want to build more sophisticated models, they can buy [Gurobi](http://www.gurobi.com/). Please consult the [JuMP homepage](https://github.com/JuliaOpt/JuMP.jl) for details on how to use different solvers. JuMP makes it easy to change between a number of open-source and commercial solvers. 21 | 22 | 23 | 24 | ## Downloading the Code 25 | 26 | You can download the code and the example csv files by calling 27 | 28 | ``` 29 | $ git clone https://github.com/dscotthunter/Fantasy-Hockey-IP-Code 30 | ``` 31 | 32 | Alternatively, you can download the zip file from above. 33 | 34 | 35 | 36 | ## Running the Code 37 | Open the file ```code_for_Github.jl```. By default the code will create 100 lineups with a maximum overlap of 7 players between each lineup. To change this, see lines 26 and 29 in the file. For instance, if you want to create 10 lineups with a maximum overlap of 4 players, you change lines 26 and 29 to the following 38 | 39 | ``` 40 | num_lineups = 10 41 | num_overlap = 4 42 | ``` 43 | 44 | By default, the code creates the lineup using the Type 4 formulation considered in the paper. To change this, see line 608 in the file. For instance, if you want to create lineups using the Type 5 formulation, you change line 608 to the folowing 45 | 46 | ``` 47 | formulation = one_lineup_Type_5 48 | ``` 49 | 50 | Lastly, you can change the ```path_skaters```, ```path_goalies```, and ```path_to_output``` variables defined on line 32, 35, and 38 respectively. For instance, if you place ```goalies.csv``` and ```skaters.csv``` in your home directory, and would like the lineps to be outputted to a file ```output.csv``` in your home directory, you would change the code to the following 51 | 52 | ``` 53 | path_skaters = "/users/home/skaters.csv" 54 | path_goalies = "/users/home/goalies.csv" 55 | path_to_output = "/users/home/output.csv" 56 | ``` 57 | 58 | where ```home``` above is changed to correspond with your computers home directory. 59 | 60 | Now that you have done this, you should be done. You should not need to change anything else in the file ```code_for_Github.jl```. Now you can build your own lineups! 61 | 62 | We have made an attempt to describe the code in great detail, with the hope that you will use your expertise to build better formulations. Good luck! 63 | -------------------------------------------------------------------------------- /example_skaters.csv: -------------------------------------------------------------------------------- 1 | First Name,Last Name,Salary,Position,Team,Opponent,Line,Power_Play,Projection John,Doe1,4300,C,A,B,1,1,7.9 John,Doe2,4200,W,A,B,1,1,6.4 John,Doe3,4200,W,A,B,1,1,4 John,Doe4,4700,C,A,B,2,1,4.3 John,Doe5,4700,W,A,B,2,2,5.4 John,Doe6,4500,W,A,B,2,2,3 John,Doe7,4900,C,A,B,3,2,3.1 John,Doe8,4400,W,A,B,3,2,3 John,Doe9,3600,W,A,B,3,x,4 John,Doe10,3200,C,A,B,4,x,1.6 John,Doe11,3000,W,A,B,4,x,0.7 John,Doe12,3000,W,A,B,4,x,1.5 John,Doe13,6300,D,A,B,x,1,2.5 John,Doe14,7800,D,A,B,x,2,2.2 John,Doe15,4400,D,A,B,x,x,0 John,Doe16,5800,D,A,B,x,x,2.3 John,Doe17,5800,D,A,B,x,x,2.2 John,Doe18,4000,D,A,B,x,x,0.5 John,Doe19,4000,C,B,A,1,1,3 John,Doe20,6500,W,B,A,1,1,7 John,Doe21,5100,W,B,A,1,1,5.7 John,Doe22,4200,C,B,A,2,1,5.1 John,Doe23,5800,W,B,A,2,2,5.6 John,Doe24,5800,W,B,A,2,2,4.5 John,Doe25,3100,C,B,A,3,2,2.6 John,Doe26,3200,W,B,A,3,2,2.6 John,Doe27,3700,W,B,A,3,x,2.2 John,Doe28,3100,C,B,A,4,x,1.8 John,Doe29,3100,W,B,A,4,x,0 John,Doe30,3500,W,B,A,4,x,0.1 John,Doe31,4600,D,B,A,x,1,1.4 John,Doe32,4900,D,B,A,x,2,2.1 John,Doe33,4400,D,B,A,x,x,0.9 John,Doe34,7000,D,B,A,x,x,0.4 John,Doe35,7600,D,B,A,x,x,1.5 John,Doe36,7200,D,B,A,x,x,0.1 John,Doe37,5500,C,C,D,1,1,6.1 John,Doe38,7500,W,C,D,1,1,5.6 John,Doe39,6300,W,C,D,1,1,5.7 John,Doe40,5600,C,C,D,2,1,6 John,Doe41,5400,W,C,D,2,2,5.7 John,Doe42,5600,W,C,D,2,2,4 John,Doe43,3400,C,C,D,3,2,2.7 John,Doe44,4600,W,C,D,3,2,3.7 John,Doe45,3100,W,C,D,3,x,2 John,Doe46,3100,C,C,D,4,x,0.8 John,Doe47,3300,W,C,D,4,x,0.7 John,Doe48,3400,W,C,D,4,x,1.8 John,Doe49,6500,D,C,D,x,1,2.7 John,Doe50,8000,D,C,D,x,2,1.1 John,Doe51,6500,D,C,D,x,x,2.6 John,Doe52,7000,D,C,D,x,x,0.4 John,Doe53,5900,D,C,D,x,x,1.8 John,Doe54,5000,D,C,D,x,x,2.9 John,Doe55,5400,C,D,C,1,1,4.7 John,Doe56,4400,W,D,C,1,1,4.9 John,Doe57,7800,W,D,C,1,1,4.8 John,Doe58,3700,C,D,C,2,1,4.1 John,Doe59,3400,W,D,C,2,2,3.6 John,Doe60,3100,W,D,C,2,2,5.3 John,Doe61,4500,C,D,C,3,2,3.2 John,Doe62,3900,W,D,C,3,2,3.7 John,Doe63,3800,W,D,C,3,x,2.4 John,Doe64,3100,C,D,C,4,x,1.6 John,Doe65,3200,W,D,C,4,x,2 John,Doe66,3500,W,D,C,4,x,1.9 John,Doe67,6500,D,D,C,x,1,3.4 John,Doe68,5200,D,D,C,x,2,1.7 John,Doe69,6700,D,D,C,x,x,2.4 John,Doe70,4700,D,D,C,x,x,0.6 John,Doe71,4300,D,D,C,x,x,0.8 John,Doe72,5700,D,D,C,x,x,1.4 John,Doe73,6300,C,E,F,1,1,5.6 John,Doe74,7200,W,E,F,1,1,4.8 John,Doe75,5600,W,E,F,1,1,7.7 John,Doe76,3300,C,E,F,2,1,5.9 John,Doe77,4900,W,E,F,2,2,5.6 John,Doe78,4400,W,E,F,2,2,3.8 John,Doe79,5000,C,E,F,3,2,2.1 John,Doe80,4700,W,E,F,3,2,3.5 John,Doe81,3700,W,E,F,3,x,3.1 John,Doe82,3100,C,E,F,4,x,1.9 John,Doe83,3300,W,E,F,4,x,0.8 John,Doe84,3400,W,E,F,4,x,1.9 John,Doe85,4000,D,E,F,x,1,1.7 John,Doe86,4700,D,E,F,x,2,4 John,Doe87,7700,D,E,F,x,x,1.5 John,Doe88,7900,D,E,F,x,x,2.5 John,Doe89,7200,D,E,F,x,x,0.8 John,Doe90,4800,D,E,F,x,x,1.3 John,Doe91,7700,C,F,E,1,1,6.7 John,Doe92,7500,W,F,E,1,1,3.3 John,Doe93,7500,W,F,E,1,1,7.6 John,Doe94,3200,C,F,E,2,1,5.4 John,Doe95,4700,W,F,E,2,2,5 John,Doe96,3500,W,F,E,2,2,5.3 John,Doe97,4700,C,F,E,3,2,3.2 John,Doe98,3500,W,F,E,3,2,3.8 John,Doe99,3900,W,F,E,3,x,2.7 John,Doe100,3000,C,F,E,4,x,0.9 John,Doe101,3000,W,F,E,4,x,0.1 John,Doe102,3100,W,F,E,4,x,0.9 John,Doe103,7200,D,F,E,x,1,2.6 John,Doe104,7100,D,F,E,x,2,2.7 John,Doe105,5600,D,F,E,x,x,2.3 John,Doe106,6900,D,F,E,x,x,1.7 John,Doe107,4300,D,F,E,x,x,0 John,Doe108,7000,D,F,E,x,x,1.8 John,Doe109,6500,C,G,H,1,1,6.8 John,Doe110,4300,W,G,H,1,1,3.3 John,Doe111,7800,W,G,H,1,1,6.1 John,Doe112,5400,C,G,H,2,1,4.7 John,Doe113,3400,W,G,H,2,2,3.8 John,Doe114,4900,W,G,H,2,2,6 John,Doe115,3000,C,G,H,3,2,3.1 John,Doe116,3600,W,G,H,3,2,2.4 John,Doe117,3500,W,G,H,3,x,3.7 John,Doe118,3000,C,G,H,4,x,0.5 John,Doe119,3400,W,G,H,4,x,1.4 John,Doe120,3500,W,G,H,4,x,1.5 John,Doe121,5200,D,G,H,x,1,3.1 John,Doe122,5400,D,G,H,x,2,3.4 John,Doe123,5400,D,G,H,x,x,0.3 John,Doe124,6500,D,G,H,x,x,2.6 John,Doe125,6000,D,G,H,x,x,3 John,Doe126,5400,D,G,H,x,x,1.5 John,Doe127,7200,C,H,G,1,1,3.3 John,Doe128,6500,W,H,G,1,1,4.5 John,Doe129,8000,W,H,G,1,1,6.1 John,Doe130,4900,C,H,G,2,1,4.8 John,Doe131,5400,W,H,G,2,2,5.5 John,Doe132,5800,W,H,G,2,2,3.8 John,Doe133,4000,C,H,G,3,2,2.4 John,Doe134,3100,W,H,G,3,2,2.2 John,Doe135,4000,W,H,G,3,x,3.8 John,Doe136,3200,C,H,G,4,x,0.6 John,Doe137,3000,W,H,G,4,x,1.2 John,Doe138,3100,W,H,G,4,x,0.8 John,Doe139,6200,D,H,G,x,1,3.1 John,Doe140,7200,D,H,G,x,2,1.2 John,Doe141,4800,D,H,G,x,x,0.8 John,Doe142,4800,D,H,G,x,x,0.6 John,Doe143,7400,D,H,G,x,x,0.5 John,Doe144,4000,D,H,G,x,x,1.8 John,Doe145,4300,C,I,J,1,1,8 John,Doe146,5400,W,I,J,1,1,3.3 John,Doe147,6600,W,I,J,1,1,7.2 John,Doe148,4600,C,I,J,2,1,3.6 John,Doe149,3900,W,I,J,2,2,5.8 John,Doe150,5700,W,I,J,2,2,5.4 John,Doe151,3900,C,I,J,3,2,2.1 John,Doe152,3500,W,I,J,3,2,2.6 John,Doe153,3500,W,I,J,3,x,2.8 John,Doe154,3100,C,I,J,4,x,0.9 John,Doe155,3200,W,I,J,4,x,1.7 John,Doe156,3000,W,I,J,4,x,1.4 John,Doe157,5700,D,I,J,x,1,1.7 John,Doe158,6800,D,I,J,x,2,3.4 John,Doe159,6700,D,I,J,x,x,1.9 John,Doe160,6600,D,I,J,x,x,2.4 John,Doe161,5800,D,I,J,x,x,2.9 John,Doe162,7600,D,I,J,x,x,0 John,Doe163,7000,C,J,I,1,1,7.5 John,Doe164,7800,W,J,I,1,1,7.3 John,Doe165,7000,W,J,I,1,1,4.8 John,Doe166,5800,C,J,I,2,1,4.5 John,Doe167,5800,W,J,I,2,2,5.9 John,Doe168,4800,W,J,I,2,2,4.3 John,Doe169,3200,C,J,I,3,2,3.8 John,Doe170,4200,W,J,I,3,2,3.6 John,Doe171,4000,W,J,I,3,x,2.8 John,Doe172,3500,C,J,I,4,x,1.5 John,Doe173,3300,W,J,I,4,x,1.4 John,Doe174,3100,W,J,I,4,x,2 John,Doe175,4900,D,J,I,x,1,2.5 John,Doe176,6100,D,J,I,x,2,1.9 John,Doe177,7300,D,J,I,x,x,2.3 John,Doe178,4700,D,J,I,x,x,1 John,Doe179,8000,D,J,I,x,x,2.1 John,Doe180,5100,D,J,I,x,x,1.6 -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | The code is licensed under the **[MPL]** version 2.0: 2 | 3 | [MPL]: https://www.mozilla.org/MPL/2.0/ 4 | 5 | Mozilla Public License Version 2.0 6 | ================================== 7 | 8 | 1. Definitions 9 | -------------- 10 | 11 | 1.1. "Contributor" 12 | means each individual or legal entity that creates, contributes to 13 | the creation of, or owns Covered Software. 14 | 15 | 1.2. "Contributor Version" 16 | means the combination of the Contributions of others (if any) used 17 | by a Contributor and that particular Contributor's Contribution. 18 | 19 | 1.3. "Contribution" 20 | means Covered Software of a particular Contributor. 21 | 22 | 1.4. "Covered Software" 23 | means Source Code Form to which the initial Contributor has attached 24 | the notice in Exhibit A, the Executable Form of such Source Code 25 | Form, and Modifications of such Source Code Form, in each case 26 | including portions thereof. 27 | 28 | 1.5. "Incompatible With Secondary Licenses" 29 | means 30 | 31 | (a) that the initial Contributor has attached the notice described 32 | in Exhibit B to the Covered Software; or 33 | 34 | (b) that the Covered Software was made available under the terms of 35 | version 1.1 or earlier of the License, but not also under the 36 | terms of a Secondary License. 37 | 38 | 1.6. "Executable Form" 39 | means any form of the work other than Source Code Form. 40 | 41 | 1.7. "Larger Work" 42 | means a work that combines Covered Software with other material, in 43 | a separate file or files, that is not Covered Software. 44 | 45 | 1.8. "License" 46 | means this document. 47 | 48 | 1.9. "Licensable" 49 | means having the right to grant, to the maximum extent possible, 50 | whether at the time of the initial grant or subsequently, any and 51 | all of the rights conveyed by this License. 52 | 53 | 1.10. "Modifications" 54 | means any of the following: 55 | 56 | (a) any file in Source Code Form that results from an addition to, 57 | deletion from, or modification of the contents of Covered 58 | Software; or 59 | 60 | (b) any new file in Source Code Form that contains any Covered 61 | Software. 62 | 63 | 1.11. "Patent Claims" of a Contributor 64 | means any patent claim(s), including without limitation, method, 65 | process, and apparatus claims, in any patent Licensable by such 66 | Contributor that would be infringed, but for the grant of the 67 | License, by the making, using, selling, offering for sale, having 68 | made, import, or transfer of either its Contributions or its 69 | Contributor Version. 70 | 71 | 1.12. "Secondary License" 72 | means either the GNU General Public License, Version 2.0, the GNU 73 | Lesser General Public License, Version 2.1, the GNU Affero General 74 | Public License, Version 3.0, or any later versions of those 75 | licenses. 76 | 77 | 1.13. "Source Code Form" 78 | means the form of the work preferred for making modifications. 79 | 80 | 1.14. "You" (or "Your") 81 | means an individual or a legal entity exercising rights under this 82 | License. For legal entities, "You" includes any entity that 83 | controls, is controlled by, or is under common control with You. For 84 | purposes of this definition, "control" means (a) the power, direct 85 | or indirect, to cause the direction or management of such entity, 86 | whether by contract or otherwise, or (b) ownership of more than 87 | fifty percent (50%) of the outstanding shares or beneficial 88 | ownership of such entity. 89 | 90 | 2. License Grants and Conditions 91 | -------------------------------- 92 | 93 | 2.1. Grants 94 | 95 | Each Contributor hereby grants You a world-wide, royalty-free, 96 | non-exclusive license: 97 | 98 | (a) under intellectual property rights (other than patent or trademark) 99 | Licensable by such Contributor to use, reproduce, make available, 100 | modify, display, perform, distribute, and otherwise exploit its 101 | Contributions, either on an unmodified basis, with Modifications, or 102 | as part of a Larger Work; and 103 | 104 | (b) under Patent Claims of such Contributor to make, use, sell, offer 105 | for sale, have made, import, and otherwise transfer either its 106 | Contributions or its Contributor Version. 107 | 108 | 2.2. Effective Date 109 | 110 | The licenses granted in Section 2.1 with respect to any Contribution 111 | become effective for each Contribution on the date the Contributor first 112 | distributes such Contribution. 113 | 114 | 2.3. Limitations on Grant Scope 115 | 116 | The licenses granted in this Section 2 are the only rights granted under 117 | this License. No additional rights or licenses will be implied from the 118 | distribution or licensing of Covered Software under this License. 119 | Notwithstanding Section 2.1(b) above, no patent license is granted by a 120 | Contributor: 121 | 122 | (a) for any code that a Contributor has removed from Covered Software; 123 | or 124 | 125 | (b) for infringements caused by: (i) Your and any other third party's 126 | modifications of Covered Software, or (ii) the combination of its 127 | Contributions with other software (except as part of its Contributor 128 | Version); or 129 | 130 | (c) under Patent Claims infringed by Covered Software in the absence of 131 | its Contributions. 132 | 133 | This License does not grant any rights in the trademarks, service marks, 134 | or logos of any Contributor (except as may be necessary to comply with 135 | the notice requirements in Section 3.4). 136 | 137 | 2.4. Subsequent Licenses 138 | 139 | No Contributor makes additional grants as a result of Your choice to 140 | distribute the Covered Software under a subsequent version of this 141 | License (see Section 10.2) or under the terms of a Secondary License (if 142 | permitted under the terms of Section 3.3). 143 | 144 | 2.5. Representation 145 | 146 | Each Contributor represents that the Contributor believes its 147 | Contributions are its original creation(s) or it has sufficient rights 148 | to grant the rights to its Contributions conveyed by this License. 149 | 150 | 2.6. Fair Use 151 | 152 | This License is not intended to limit any rights You have under 153 | applicable copyright doctrines of fair use, fair dealing, or other 154 | equivalents. 155 | 156 | 2.7. Conditions 157 | 158 | Sections 3.1, 3.2, 3.3, and 3.4 are conditions of the licenses granted 159 | in Section 2.1. 160 | 161 | 3. Responsibilities 162 | ------------------- 163 | 164 | 3.1. Distribution of Source Form 165 | 166 | All distribution of Covered Software in Source Code Form, including any 167 | Modifications that You create or to which You contribute, must be under 168 | the terms of this License. You must inform recipients that the Source 169 | Code Form of the Covered Software is governed by the terms of this 170 | License, and how they can obtain a copy of this License. You may not 171 | attempt to alter or restrict the recipients' rights in the Source Code 172 | Form. 173 | 174 | 3.2. Distribution of Executable Form 175 | 176 | If You distribute Covered Software in Executable Form then: 177 | 178 | (a) such Covered Software must also be made available in Source Code 179 | Form, as described in Section 3.1, and You must inform recipients of 180 | the Executable Form how they can obtain a copy of such Source Code 181 | Form by reasonable means in a timely manner, at a charge no more 182 | than the cost of distribution to the recipient; and 183 | 184 | (b) You may distribute such Executable Form under the terms of this 185 | License, or sublicense it under different terms, provided that the 186 | license for the Executable Form does not attempt to limit or alter 187 | the recipients' rights in the Source Code Form under this License. 188 | 189 | 3.3. Distribution of a Larger Work 190 | 191 | You may create and distribute a Larger Work under terms of Your choice, 192 | provided that You also comply with the requirements of this License for 193 | the Covered Software. If the Larger Work is a combination of Covered 194 | Software with a work governed by one or more Secondary Licenses, and the 195 | Covered Software is not Incompatible With Secondary Licenses, this 196 | License permits You to additionally distribute such Covered Software 197 | under the terms of such Secondary License(s), so that the recipient of 198 | the Larger Work may, at their option, further distribute the Covered 199 | Software under the terms of either this License or such Secondary 200 | License(s). 201 | 202 | 3.4. Notices 203 | 204 | You may not remove or alter the substance of any license notices 205 | (including copyright notices, patent notices, disclaimers of warranty, 206 | or limitations of liability) contained within the Source Code Form of 207 | the Covered Software, except that You may alter any license notices to 208 | the extent required to remedy known factual inaccuracies. 209 | 210 | 3.5. Application of Additional Terms 211 | 212 | You may choose to offer, and to charge a fee for, warranty, support, 213 | indemnity or liability obligations to one or more recipients of Covered 214 | Software. However, You may do so only on Your own behalf, and not on 215 | behalf of any Contributor. You must make it absolutely clear that any 216 | such warranty, support, indemnity, or liability obligation is offered by 217 | You alone, and You hereby agree to indemnify every Contributor for any 218 | liability incurred by such Contributor as a result of warranty, support, 219 | indemnity or liability terms You offer. You may include additional 220 | disclaimers of warranty and limitations of liability specific to any 221 | jurisdiction. 222 | 223 | 4. Inability to Comply Due to Statute or Regulation 224 | --------------------------------------------------- 225 | 226 | If it is impossible for You to comply with any of the terms of this 227 | License with respect to some or all of the Covered Software due to 228 | statute, judicial order, or regulation then You must: (a) comply with 229 | the terms of this License to the maximum extent possible; and (b) 230 | describe the limitations and the code they affect. Such description must 231 | be placed in a text file included with all distributions of the Covered 232 | Software under this License. Except to the extent prohibited by statute 233 | or regulation, such description must be sufficiently detailed for a 234 | recipient of ordinary skill to be able to understand it. 235 | 236 | 5. Termination 237 | -------------- 238 | 239 | 5.1. The rights granted under this License will terminate automatically 240 | if You fail to comply with any of its terms. However, if You become 241 | compliant, then the rights granted under this License from a particular 242 | Contributor are reinstated (a) provisionally, unless and until such 243 | Contributor explicitly and finally terminates Your grants, and (b) on an 244 | ongoing basis, if such Contributor fails to notify You of the 245 | non-compliance by some reasonable means prior to 60 days after You have 246 | come back into compliance. Moreover, Your grants from a particular 247 | Contributor are reinstated on an ongoing basis if such Contributor 248 | notifies You of the non-compliance by some reasonable means, this is the 249 | first time You have received notice of non-compliance with this License 250 | from such Contributor, and You become compliant prior to 30 days after 251 | Your receipt of the notice. 252 | 253 | 5.2. If You initiate litigation against any entity by asserting a patent 254 | infringement claim (excluding declaratory judgment actions, 255 | counter-claims, and cross-claims) alleging that a Contributor Version 256 | directly or indirectly infringes any patent, then the rights granted to 257 | You by any and all Contributors for the Covered Software under Section 258 | 2.1 of this License shall terminate. 259 | 260 | 5.3. In the event of termination under Sections 5.1 or 5.2 above, all 261 | end user license agreements (excluding distributors and resellers) which 262 | have been validly granted by You or Your distributors under this License 263 | prior to termination shall survive termination. 264 | 265 | ************************************************************************ 266 | * * 267 | * 6. Disclaimer of Warranty * 268 | * ------------------------- * 269 | * * 270 | * Covered Software is provided under this License on an "as is" * 271 | * basis, without warranty of any kind, either expressed, implied, or * 272 | * statutory, including, without limitation, warranties that the * 273 | * Covered Software is free of defects, merchantable, fit for a * 274 | * particular purpose or non-infringing. The entire risk as to the * 275 | * quality and performance of the Covered Software is with You. * 276 | * Should any Covered Software prove defective in any respect, You * 277 | * (not any Contributor) assume the cost of any necessary servicing, * 278 | * repair, or correction. This disclaimer of warranty constitutes an * 279 | * essential part of this License. No use of any Covered Software is * 280 | * authorized under this License except under this disclaimer. * 281 | * * 282 | ************************************************************************ 283 | 284 | ************************************************************************ 285 | * * 286 | * 7. Limitation of Liability * 287 | * -------------------------- * 288 | * * 289 | * Under no circumstances and under no legal theory, whether tort * 290 | * (including negligence), contract, or otherwise, shall any * 291 | * Contributor, or anyone who distributes Covered Software as * 292 | * permitted above, be liable to You for any direct, indirect, * 293 | * special, incidental, or consequential damages of any character * 294 | * including, without limitation, damages for lost profits, loss of * 295 | * goodwill, work stoppage, computer failure or malfunction, or any * 296 | * and all other commercial damages or losses, even if such party * 297 | * shall have been informed of the possibility of such damages. This * 298 | * limitation of liability shall not apply to liability for death or * 299 | * personal injury resulting from such party's negligence to the * 300 | * extent applicable law prohibits such limitation. Some * 301 | * jurisdictions do not allow the exclusion or limitation of * 302 | * incidental or consequential damages, so this exclusion and * 303 | * limitation may not apply to You. * 304 | * * 305 | ************************************************************************ 306 | 307 | 8. Litigation 308 | ------------- 309 | 310 | Any litigation relating to this License may be brought only in the 311 | courts of a jurisdiction where the defendant maintains its principal 312 | place of business and such litigation shall be governed by laws of that 313 | jurisdiction, without reference to its conflict-of-law provisions. 314 | Nothing in this Section shall prevent a party's ability to bring 315 | cross-claims or counter-claims. 316 | 317 | 9. Miscellaneous 318 | ---------------- 319 | 320 | This License represents the complete agreement concerning the subject 321 | matter hereof. If any provision of this License is held to be 322 | unenforceable, such provision shall be reformed only to the extent 323 | necessary to make it enforceable. Any law or regulation which provides 324 | that the language of a contract shall be construed against the drafter 325 | shall not be used to construe this License against a Contributor. 326 | 327 | 10. Versions of the License 328 | --------------------------- 329 | 330 | 10.1. New Versions 331 | 332 | Mozilla Foundation is the license steward. Except as provided in Section 333 | 10.3, no one other than the license steward has the right to modify or 334 | publish new versions of this License. Each version will be given a 335 | distinguishing version number. 336 | 337 | 10.2. Effect of New Versions 338 | 339 | You may distribute the Covered Software under the terms of the version 340 | of the License under which You originally received the Covered Software, 341 | or under the terms of any subsequent version published by the license 342 | steward. 343 | 344 | 10.3. Modified Versions 345 | 346 | If you create software not governed by this License, and you want to 347 | create a new license for such software, you may create and use a 348 | modified version of this License if you rename the license and remove 349 | any references to the name of the license steward (except to note that 350 | such modified license differs from this License). 351 | 352 | 10.4. Distributing Source Code Form that is Incompatible With Secondary 353 | Licenses 354 | 355 | If You choose to distribute Source Code Form that is Incompatible With 356 | Secondary Licenses under the terms of this version of the License, the 357 | notice described in Exhibit B of this License must be attached. 358 | 359 | Exhibit A - Source Code Form License Notice 360 | ------------------------------------------- 361 | 362 | This Source Code Form is subject to the terms of the Mozilla Public 363 | License, v. 2.0. If a copy of the MPL was not distributed with this 364 | file, You can obtain one at http://mozilla.org/MPL/2.0/. 365 | 366 | If it is not possible or desirable to put the notice in a particular 367 | file, then You may include the notice in a location (such as a LICENSE 368 | file in a relevant directory) where a recipient would be likely to look 369 | for such a notice. 370 | 371 | You may add additional accurate notices of copyright ownership. 372 | 373 | Exhibit B - "Incompatible With Secondary Licenses" Notice 374 | --------------------------------------------------------- 375 | 376 | This Source Code Form is "Incompatible With Secondary Licenses", as 377 | defined by the Mozilla Public License, v. 2.0. 378 | 379 | -------------------------------------------------------------------------------- /code_for_Github.jl: -------------------------------------------------------------------------------- 1 | #= 2 | This code implements the No Stacking, Type 1, Type 2, Type 3, Type 4, and Type 5 formulations 3 | described in the paper Winning Daily Fantasy Hockey Contests Using Integer Programming by 4 | Hunter, Vielma, and Zaman. We have made an attempt to describe the code in great detail, with the 5 | hope that you will use your expertise to build better formulations. 6 | =# 7 | 8 | # To install DataFrames, simply run Pkg.add("DataFrames") 9 | using DataFrames 10 | 11 | #= 12 | GLPK is an open-source solver, and additionally Cbc is an open-source solver. This code uses GLPK 13 | because we found that it was slightly faster than Cbc in practice. For those that want to build 14 | very sophisticated models, they can buy Gurobi. To install GLPKMathProgInterface, simply run 15 | Pkg.add("GLPKMathProgInterface") 16 | =# 17 | using GLPKMathProgInterface 18 | 19 | # Once again, to install run Pkg.add("JuMP") 20 | using JuMP 21 | 22 | #= 23 | Variables for solving the problem (change these) 24 | =# 25 | # num_lineups is the total number of lineups 26 | num_lineups = 100 27 | 28 | # num_overlap is the maximum overlap of players between the lineups that you create 29 | num_overlap = 7 30 | 31 | # path_skaters is a string that gives the path to the csv file with the skaters information (see example file for suggested format) 32 | path_skaters = "example_skaters.csv" 33 | 34 | # path_goalies is a string that gives the path to the csv file with the goalies information (see example file for suggested format) 35 | path_goalies = "example_goalies.csv" 36 | 37 | # path_to_output is a string that gives the path to the csv file that will give the outputted results 38 | path_to_output= "output.csv" 39 | 40 | 41 | 42 | # This is a function that creates one lineup using the No Stacking formulation from the paper 43 | function one_lineup_no_stacking(skaters, goalies, lineups, num_overlap, num_skaters, num_goalies, centers, wingers, defenders, num_teams, skaters_teams, goalie_opponents, team_lines, num_lines, P1_info) 44 | m = Model(solver=GLPKSolverMIP()) 45 | 46 | # Variable for skaters in lineup. 47 | @defVar(m, skaters_lineup[i=1:num_skaters], Bin) 48 | 49 | # Variable for goalie in lineup. 50 | @defVar(m, goalies_lineup[i=1:num_goalies], Bin) 51 | 52 | 53 | # One goalie constraint 54 | @addConstraint(m, sum{goalies_lineup[i], i=1:num_goalies} == 1) 55 | 56 | # Eight Skaters constraint 57 | @addConstraint(m, sum{skaters_lineup[i], i=1:num_skaters} == 8) 58 | 59 | # between 2 and 3 centers 60 | @addConstraint(m, sum{centers[i]*skaters_lineup[i], i=1:num_skaters} <= 3) 61 | @addConstraint(m, 2 <= sum{centers[i]*skaters_lineup[i], i=1:num_skaters}) 62 | 63 | # between 3 and 4 wingers 64 | @addConstraint(m, sum{wingers[i]*skaters_lineup[i], i=1:num_skaters} <= 4) 65 | @addConstraint(m, 3<=sum{wingers[i]*skaters_lineup[i], i=1:num_skaters}) 66 | 67 | # between 2 and 3 defenders 68 | @addConstraint(m, 2 <= sum{defenders[i]*skaters_lineup[i], i=1:num_skaters}) 69 | @addConstraint(m, sum{defenders[i]*skaters_lineup[i], i=1:num_skaters} <= 3) 70 | 71 | # Financial Constraint 72 | @addConstraint(m, sum{skaters[i,:Salary]*skaters_lineup[i], i=1:num_skaters} + sum{goalies[i,:Salary]*goalies_lineup[i], i=1:num_goalies} <= 50000) 73 | 74 | # at least 3 different teams for the 8 skaters constraints 75 | @defVar(m, used_team[i=1:num_teams], Bin) 76 | @addConstraint(m, constr[i=1:num_teams], used_team[i] <= sum{skaters_teams[t, i]*skaters_lineup[t], t=1:num_skaters}) 77 | @addConstraint(m, sum{used_team[i], i=1:num_teams} >= 3) 78 | 79 | # Overlap Constraint 80 | @addConstraint(m, constr[i=1:size(lineups)[2]], sum{lineups[j,i]*skaters_lineup[j], j=1:num_skaters} + sum{lineups[num_skaters+j,i]*goalies_lineup[j], j=1:num_goalies} <= num_overlap) 81 | 82 | 83 | # Objective 84 | @setObjective(m, Max, sum{skaters[i,:Projection]*skaters_lineup[i], i=1:num_skaters} + sum{goalies[i,:Projection]*goalies_lineup[i], i=1:num_goalies}) 85 | 86 | 87 | # Solve the integer programming problem 88 | println("Solving Problem...") 89 | @printf("\n") 90 | status = solve(m); 91 | 92 | 93 | # Puts the output of one lineup into a format that will be used later 94 | if status==:Optimal 95 | skaters_lineup_copy = Array(Int64, 0) 96 | for i=1:num_skaters 97 | if getValue(skaters_lineup[i]) >= 0.9 && getValue(skaters_lineup[i]) <= 1.1 98 | skaters_lineup_copy = vcat(skaters_lineup_copy, fill(1,1)) 99 | else 100 | skaters_lineup_copy = vcat(skaters_lineup_copy, fill(0,1)) 101 | end 102 | end 103 | for i=1:num_goalies 104 | if getValue(goalies_lineup[i]) >= 0.9 && getValue(goalies_lineup[i]) <= 1.1 105 | skaters_lineup_copy = vcat(skaters_lineup_copy, fill(1,1)) 106 | else 107 | skaters_lineup_copy = vcat(skaters_lineup_copy, fill(0,1)) 108 | end 109 | end 110 | return(skaters_lineup_copy) 111 | end 112 | end 113 | 114 | 115 | 116 | 117 | 118 | # This is a function that creates one lineup using the Type 1 formulation from the paper 119 | function one_lineup_Type_1(skaters, goalies, lineups, num_overlap, num_skaters, num_goalies, centers, wingers, defenders, num_teams, skaters_teams, goalie_opponents, team_lines, num_lines, P1_info) 120 | m = Model(solver=GLPKSolverMIP()) 121 | 122 | # Variable for skaters in lineup 123 | @defVar(m, skaters_lineup[i=1:num_skaters], Bin) 124 | 125 | # Variable for goalie in lineup 126 | @defVar(m, goalies_lineup[i=1:num_goalies], Bin) 127 | 128 | 129 | # One goalie constraint 130 | @addConstraint(m, sum{goalies_lineup[i], i=1:num_goalies} == 1) 131 | 132 | # Eight skaters constraint 133 | @addConstraint(m, sum{skaters_lineup[i], i=1:num_skaters} == 8) 134 | 135 | 136 | # between 2 and 3 centers 137 | @addConstraint(m, sum{centers[i]*skaters_lineup[i], i=1:num_skaters} <= 3) 138 | @addConstraint(m, 2 <= sum{centers[i]*skaters_lineup[i], i=1:num_skaters}) 139 | 140 | # between 3 and 4 wingers 141 | @addConstraint(m, sum{wingers[i]*skaters_lineup[i], i=1:num_skaters} <= 4) 142 | @addConstraint(m, 3<=sum{wingers[i]*skaters_lineup[i], i=1:num_skaters}) 143 | 144 | # between 2 and 3 defenders 145 | @addConstraint(m, 2 <= sum{defenders[i]*skaters_lineup[i], i=1:num_skaters}) 146 | @addConstraint(m, sum{defenders[i]*skaters_lineup[i], i=1:num_skaters} <= 3) 147 | 148 | 149 | # Financial Constraint 150 | @addConstraint(m, sum{skaters[i,:Salary]*skaters_lineup[i], i=1:num_skaters} + sum{goalies[i,:Salary]*goalies_lineup[i], i=1:num_goalies} <= 50000) 151 | 152 | 153 | # At least 3 different teams for the 8 skaters constraint 154 | @defVar(m, used_team[i=1:num_teams], Bin) 155 | @addConstraint(m, constr[i=1:num_teams], used_team[i] <= sum{skaters_teams[t, i]*skaters_lineup[t], t=1:num_skaters}) 156 | @addConstraint(m, sum{used_team[i], i=1:num_teams} >= 3) 157 | 158 | 159 | # No goalies going against skaters constraint 160 | @addConstraint(m, constr[i=1:num_goalies], 6*goalies_lineup[i] + sum{goalie_opponents[k, i]*skaters_lineup[k], k=1:num_skaters}<=6) 161 | 162 | 163 | # Must have at least one complete line in each lineup 164 | @defVar(m, line_stack[i=1:num_lines], Bin) 165 | @addConstraint(m, constr[i=1:num_lines], 3*line_stack[i] <= sum{team_lines[k,i]*skaters_lineup[k], k=1:num_skaters}) 166 | @addConstraint(m, sum{line_stack[i], i=1:num_lines} >= 1) 167 | 168 | 169 | # Must have at least 2 lines with at least two people 170 | @defVar(m, line_stack2[i=1:num_lines], Bin) 171 | @addConstraint(m, constr[i=1:num_lines], 2*line_stack2[i] <= sum{team_lines[k,i]*skaters_lineup[k], k=1:num_skaters}) 172 | @addConstraint(m, sum{line_stack2[i], i=1:num_lines} >= 2) 173 | 174 | 175 | # Overlap Constraint 176 | @addConstraint(m, constr[i=1:size(lineups)[2]], sum{lineups[j,i]*skaters_lineup[j], j=1:num_skaters} + sum{lineups[num_skaters+j,i]*goalies_lineup[j], j=1:num_goalies} <= num_overlap) 177 | 178 | 179 | # Objective 180 | @setObjective(m, Max, sum{skaters[i,:Projection]*skaters_lineup[i], i=1:num_skaters} + sum{goalies[i,:Projection]*goalies_lineup[i], i=1:num_goalies} ) 181 | 182 | 183 | # Solve the integer programming problem 184 | println("Solving Problem...") 185 | @printf("\n") 186 | status = solve(m); 187 | 188 | 189 | # Puts the output of one lineup into a format that will be used later 190 | if status==:Optimal 191 | skaters_lineup_copy = Array(Int64, 0) 192 | for i=1:num_skaters 193 | if getValue(skaters_lineup[i]) >= 0.9 && getValue(skaters_lineup[i]) <= 1.1 194 | skaters_lineup_copy = vcat(skaters_lineup_copy, fill(1,1)) 195 | else 196 | skaters_lineup_copy = vcat(skaters_lineup_copy, fill(0,1)) 197 | end 198 | end 199 | for i=1:num_goalies 200 | if getValue(goalies_lineup[i]) >= 0.9 && getValue(goalies_lineup[i]) <= 1.1 201 | skaters_lineup_copy = vcat(skaters_lineup_copy, fill(1,1)) 202 | else 203 | skaters_lineup_copy = vcat(skaters_lineup_copy, fill(0,1)) 204 | end 205 | end 206 | return(skaters_lineup_copy) 207 | end 208 | end 209 | 210 | 211 | 212 | 213 | 214 | # This is a function that creates one lineup using the Type 2 formulation from the paper 215 | function one_lineup_Type_2(skaters, goalies, lineups, num_overlap, num_skaters, num_goalies, centers, wingers, defenders, num_teams, skaters_teams, goalie_opponents, team_lines, num_lines, P1_info) 216 | m = Model(solver=GLPKSolverMIP()) 217 | 218 | # Variable for skaters in lineup 219 | @defVar(m, skaters_lineup[i=1:num_skaters], Bin) 220 | 221 | # Variable for goalie in lineup 222 | @defVar(m, goalies_lineup[i=1:num_goalies], Bin) 223 | 224 | 225 | # One goalie constraint 226 | @addConstraint(m, sum{goalies_lineup[i], i=1:num_goalies} == 1) 227 | 228 | # Eight skaters constraint 229 | @addConstraint(m, sum{skaters_lineup[i], i=1:num_skaters} == 8) 230 | 231 | 232 | # between 2 and 3 centers 233 | @addConstraint(m, sum{centers[i]*skaters_lineup[i], i=1:num_skaters} <= 3) 234 | @addConstraint(m, 2 <= sum{centers[i]*skaters_lineup[i], i=1:num_skaters}) 235 | 236 | # between 3 and 4 wingers 237 | @addConstraint(m, sum{wingers[i]*skaters_lineup[i], i=1:num_skaters} <= 4) 238 | @addConstraint(m, 3<=sum{wingers[i]*skaters_lineup[i], i=1:num_skaters}) 239 | 240 | # exactly 2 defenders 241 | @addConstraint(m, 2 == sum{defenders[i]*skaters_lineup[i], i=1:num_skaters}) 242 | 243 | # Financial Constraint 244 | @addConstraint(m, sum{skaters[i,:Salary]*skaters_lineup[i], i=1:num_skaters} + sum{goalies[i,:Salary]*goalies_lineup[i], i=1:num_goalies} <= 50000) 245 | 246 | 247 | # at least 3 different teams for the 8 skaters constraint 248 | @defVar(m, used_team[i=1:num_teams], Bin) 249 | @addConstraint(m, constr[i=1:num_teams], used_team[i] <= sum{skaters_teams[t, i]*skaters_lineup[t], t=1:num_skaters}) 250 | @addConstraint(m, sum{used_team[i], i=1:num_teams} >= 3) 251 | 252 | 253 | # No goalies going against skaters constraint 254 | @addConstraint(m, constr[i=1:num_goalies], 6*goalies_lineup[i] + sum{goalie_opponents[k, i]*skaters_lineup[k], k=1:num_skaters}<=6) 255 | 256 | 257 | # Must have at least one complete line in each lineup 258 | @defVar(m, line_stack[i=1:num_lines], Bin) 259 | @addConstraint(m, constr[i=1:num_lines], 3*line_stack[i] <= sum{team_lines[k,i]*skaters_lineup[k], k=1:num_skaters}) 260 | @addConstraint(m, sum{line_stack[i], i=1:num_lines} >= 1) 261 | 262 | 263 | # Must have at least 2 lines with at least two people 264 | @defVar(m, line_stack2[i=1:num_lines], Bin) 265 | @addConstraint(m, constr[i=1:num_lines], 2*line_stack2[i] <= sum{team_lines[k,i]*skaters_lineup[k], k=1:num_skaters}) 266 | @addConstraint(m, sum{line_stack2[i], i=1:num_lines} >= 2) 267 | 268 | 269 | # Overlap Constraint 270 | @addConstraint(m, constr[i=1:size(lineups)[2]], sum{lineups[j,i]*skaters_lineup[j], j=1:num_skaters} + sum{lineups[num_skaters+j,i]*goalies_lineup[j], j=1:num_goalies} <= num_overlap) 271 | 272 | 273 | # Objective 274 | @setObjective(m, Max, sum{skaters[i,:Projection]*skaters_lineup[i], i=1:num_skaters} + sum{goalies[i,:Projection]*goalies_lineup[i], i=1:num_goalies} ) 275 | 276 | 277 | # Solve the integer programming problem 278 | println("Solving Problem...") 279 | @printf("\n") 280 | status = solve(m); 281 | 282 | 283 | # Puts the output of one lineup into a format that will be used later 284 | if status==:Optimal 285 | skaters_lineup_copy = Array(Int64, 0) 286 | for i=1:num_skaters 287 | if getValue(skaters_lineup[i]) >= 0.9 && getValue(skaters_lineup[i]) <= 1.1 288 | skaters_lineup_copy = vcat(skaters_lineup_copy, fill(1,1)) 289 | else 290 | skaters_lineup_copy = vcat(skaters_lineup_copy, fill(0,1)) 291 | end 292 | end 293 | for i=1:num_goalies 294 | if getValue(goalies_lineup[i]) >= 0.9 && getValue(goalies_lineup[i]) <= 1.1 295 | skaters_lineup_copy = vcat(skaters_lineup_copy, fill(1,1)) 296 | else 297 | skaters_lineup_copy = vcat(skaters_lineup_copy, fill(0,1)) 298 | end 299 | end 300 | return(skaters_lineup_copy) 301 | end 302 | end 303 | 304 | 305 | 306 | 307 | # This is a function that creates one lineup using the Type 3 formulation from the paper 308 | function one_lineup_Type_3(skaters, goalies, lineups, num_overlap, num_skaters, num_goalies, centers, wingers, defenders, num_teams, skaters_teams, goalie_opponents, team_lines, num_lines, P1_info) 309 | m = Model(solver=GLPKSolverMIP()) 310 | 311 | 312 | # Variable for skaters in lineup 313 | @defVar(m, skaters_lineup[i=1:num_skaters], Bin) 314 | 315 | # Variable for goalie in lineup 316 | @defVar(m, goalies_lineup[i=1:num_goalies], Bin) 317 | 318 | 319 | # One goalie constraint 320 | @addConstraint(m, sum{goalies_lineup[i], i=1:num_goalies} == 1) 321 | 322 | # Eight Skaters constraint 323 | @addConstraint(m, sum{skaters_lineup[i], i=1:num_skaters} == 8) 324 | 325 | 326 | # between 2 and 3 centers 327 | @addConstraint(m, sum{centers[i]*skaters_lineup[i], i=1:num_skaters} <= 3) 328 | @addConstraint(m, 2 <= sum{centers[i]*skaters_lineup[i], i=1:num_skaters}) 329 | 330 | # between 3 and 4 wingers 331 | @addConstraint(m, sum{wingers[i]*skaters_lineup[i], i=1:num_skaters} <= 4) 332 | @addConstraint(m, 3<=sum{wingers[i]*skaters_lineup[i], i=1:num_skaters}) 333 | 334 | # between 2 and 3 defenders 335 | @addConstraint(m, 2 <= sum{defenders[i]*skaters_lineup[i], i=1:num_skaters}) 336 | @addConstraint(m, sum{defenders[i]*skaters_lineup[i], i=1:num_skaters} <= 3) 337 | 338 | 339 | # Financial Constraint 340 | @addConstraint(m, sum{skaters[i,:Salary]*skaters_lineup[i], i=1:num_skaters} + sum{goalies[i,:Salary]*goalies_lineup[i], i=1:num_goalies} <= 50000) 341 | 342 | 343 | # at least 3 different teams for the 8 skaters constraint 344 | @defVar(m, used_team[i=1:num_teams], Bin) 345 | @addConstraint(m, constr[i=1:num_teams], used_team[i] <= sum{skaters_teams[t, i]*skaters_lineup[t], t=1:num_skaters}) 346 | @addConstraint(m, sum{used_team[i], i=1:num_teams} >= 3) 347 | 348 | 349 | 350 | # No goalies going against skaters 351 | @addConstraint(m, constr[i=1:num_goalies], 6*goalies_lineup[i] + sum{goalie_opponents[k, i]*skaters_lineup[k], k=1:num_skaters}<=6) 352 | 353 | # Must have at least one complete line in each lineup 354 | @defVar(m, line_stack[i=1:num_lines], Bin) 355 | @addConstraint(m, constr[i=1:num_lines], 3*line_stack[i] <= sum{team_lines[k,i]*skaters_lineup[k], k=1:num_skaters}) 356 | @addConstraint(m, sum{line_stack[i], i=1:num_lines} >= 1) 357 | 358 | 359 | # Must have at least 2 lines with at least two people 360 | @defVar(m, line_stack2[i=1:num_lines], Bin) 361 | @addConstraint(m, constr[i=1:num_lines], 2*line_stack2[i] <= sum{team_lines[k,i]*skaters_lineup[k], k=1:num_skaters}) 362 | @addConstraint(m, sum{line_stack2[i], i=1:num_lines} >= 2) 363 | 364 | 365 | 366 | # The defenders must be on Power Play 1 constraint 367 | @addConstraint(m, sum{sum{defenders[i]*P1_info[i,j]*skaters_lineup[i], i=1:num_skaters}, j=1:num_teams} == sum{defenders[i]*skaters_lineup[i], i=1:num_skaters}) 368 | 369 | 370 | # Overlap Constraint 371 | @addConstraint(m, constr[i=1:size(lineups)[2]], sum{lineups[j,i]*skaters_lineup[j], j=1:num_skaters} + sum{lineups[num_skaters+j,i]*goalies_lineup[j], j=1:num_goalies} <= num_overlap) 372 | 373 | 374 | 375 | # Objective 376 | @setObjective(m, Max, sum{skaters[i,:Projection]*skaters_lineup[i], i=1:num_skaters} + sum{goalies[i,:Projection]*goalies_lineup[i], i=1:num_goalies} ) 377 | 378 | 379 | # Solve the integer programming problem 380 | println("Solving Problem...") 381 | @printf("\n") 382 | status = solve(m); 383 | 384 | 385 | # Puts the output of one lineup into a format that will be used later 386 | if status==:Optimal 387 | skaters_lineup_copy = Array(Int64, 0) 388 | for i=1:num_skaters 389 | if getValue(skaters_lineup[i]) >= 0.9 && getValue(skaters_lineup[i]) <= 1.1 390 | skaters_lineup_copy = vcat(skaters_lineup_copy, fill(1,1)) 391 | else 392 | skaters_lineup_copy = vcat(skaters_lineup_copy, fill(0,1)) 393 | end 394 | end 395 | for i=1:num_goalies 396 | if getValue(goalies_lineup[i]) >= 0.9 && getValue(goalies_lineup[i]) <= 1.1 397 | skaters_lineup_copy = vcat(skaters_lineup_copy, fill(1,1)) 398 | else 399 | skaters_lineup_copy = vcat(skaters_lineup_copy, fill(0,1)) 400 | end 401 | end 402 | return(skaters_lineup_copy) 403 | end 404 | end 405 | 406 | 407 | 408 | 409 | # This is a function that creates one lineup using the Type 4 formulation from the paper 410 | function one_lineup_Type_4(skaters, goalies, lineups, num_overlap, num_skaters, num_goalies, centers, wingers, defenders, num_teams, skaters_teams, goalie_opponents, team_lines, num_lines, P1_info) 411 | m = Model(solver=GLPKSolverMIP()) 412 | 413 | 414 | # Variable for skaters in lineup 415 | @defVar(m, skaters_lineup[i=1:num_skaters], Bin) 416 | 417 | # Variable for goalie in lineup 418 | @defVar(m, goalies_lineup[i=1:num_goalies], Bin) 419 | 420 | 421 | # One goalie constraint 422 | @addConstraint(m, sum{goalies_lineup[i], i=1:num_goalies} == 1) 423 | 424 | # Eight Skaters constraint 425 | @addConstraint(m, sum{skaters_lineup[i], i=1:num_skaters} == 8) 426 | 427 | # between 2 and 3 centers 428 | @addConstraint(m, sum{centers[i]*skaters_lineup[i], i=1:num_skaters} <= 3) 429 | @addConstraint(m, 2 <= sum{centers[i]*skaters_lineup[i], i=1:num_skaters}) 430 | 431 | # between 3 and 4 wingers 432 | @addConstraint(m, sum{wingers[i]*skaters_lineup[i], i=1:num_skaters} <= 4) 433 | @addConstraint(m, 3<=sum{wingers[i]*skaters_lineup[i], i=1:num_skaters}) 434 | 435 | # between 2 and 3 defenders 436 | @addConstraint(m, 2 <= sum{defenders[i]*skaters_lineup[i], i=1:num_skaters}) 437 | @addConstraint(m, sum{defenders[i]*skaters_lineup[i], i=1:num_skaters} <= 3) 438 | 439 | # Financial Constraint 440 | @addConstraint(m, sum{skaters[i,:Salary]*skaters_lineup[i], i=1:num_skaters} + sum{goalies[i,:Salary]*goalies_lineup[i], i=1:num_goalies} <= 50000) 441 | 442 | 443 | # exactly 3 different teams for the 8 skaters constraint 444 | @defVar(m, used_team[i=1:num_teams], Bin) 445 | @addConstraint(m, constr[i=1:num_teams], used_team[i] <= sum{skaters_teams[t, i]*skaters_lineup[t], t=1:num_skaters}) 446 | @addConstraint(m, constr[i=1:num_teams], sum{skaters_teams[t, i]*skaters_lineup[t], t=1:num_skaters} <= 6*used_team[i]) 447 | @addConstraint(m, sum{used_team[i], i=1:num_teams} == 3) 448 | 449 | 450 | # No goalies going against skaters 451 | @addConstraint(m, constr[i=1:num_goalies], 6*goalies_lineup[i] + sum{goalie_opponents[k, i]*skaters_lineup[k], k=1:num_skaters}<=6) 452 | 453 | 454 | # Must have at least one complete line in each lineup 455 | @defVar(m, line_stack[i=1:num_lines], Bin) 456 | @addConstraint(m, constr[i=1:num_lines], 3*line_stack[i] <= sum{team_lines[k,i]*skaters_lineup[k], k=1:num_skaters}) 457 | @addConstraint(m, sum{line_stack[i], i=1:num_lines} >= 1) 458 | 459 | 460 | # Must have at least 2 lines with at least two people 461 | @defVar(m, line_stack2[i=1:num_lines], Bin) 462 | @addConstraint(m, constr[i=1:num_lines], 2*line_stack2[i] <= sum{team_lines[k,i]*skaters_lineup[k], k=1:num_skaters}) 463 | @addConstraint(m, sum{line_stack2[i], i=1:num_lines} >= 2) 464 | 465 | 466 | 467 | # The defenders must be on Power Play 1 468 | @addConstraint(m, sum{sum{defenders[i]*P1_info[i,j]*skaters_lineup[i], i=1:num_skaters}, j=1:num_teams} == sum{defenders[i]*skaters_lineup[i], i=1:num_skaters}) 469 | 470 | 471 | # Overlap Constraint 472 | @addConstraint(m, constr[i=1:size(lineups)[2]], sum{lineups[j,i]*skaters_lineup[j], j=1:num_skaters} + sum{lineups[num_skaters+j,i]*goalies_lineup[j], j=1:num_goalies} <= num_overlap) 473 | 474 | 475 | 476 | # Objective 477 | @setObjective(m, Max, sum{skaters[i,:Projection]*skaters_lineup[i], i=1:num_skaters} + sum{goalies[i,:Projection]*goalies_lineup[i], i=1:num_goalies} ) 478 | 479 | 480 | # Solve the integer programming problem 481 | println("Solving Problem...") 482 | @printf("\n") 483 | status = solve(m); 484 | 485 | 486 | # Puts the output of one lineup into a format that will be used later 487 | if status==:Optimal 488 | skaters_lineup_copy = Array(Int64, 0) 489 | for i=1:num_skaters 490 | if getValue(skaters_lineup[i]) >= 0.9 && getValue(skaters_lineup[i]) <= 1.1 491 | skaters_lineup_copy = vcat(skaters_lineup_copy, fill(1,1)) 492 | else 493 | skaters_lineup_copy = vcat(skaters_lineup_copy, fill(0,1)) 494 | end 495 | end 496 | for i=1:num_goalies 497 | if getValue(goalies_lineup[i]) >= 0.9 && getValue(goalies_lineup[i]) <= 1.1 498 | skaters_lineup_copy = vcat(skaters_lineup_copy, fill(1,1)) 499 | else 500 | skaters_lineup_copy = vcat(skaters_lineup_copy, fill(0,1)) 501 | end 502 | end 503 | return(skaters_lineup_copy) 504 | end 505 | end 506 | 507 | 508 | # This is a function that creates one lineup using the Type 5 formulation from the paper 509 | function one_lineup_Type_5(skaters, goalies, lineups, num_overlap, num_skaters, num_goalies, centers, wingers, defenders, num_teams, skaters_teams, goalie_opponents, team_lines, num_lines, P1_info) 510 | m = Model(solver=GLPKSolverMIP()) 511 | 512 | # Variable for skaters in lineup 513 | @defVar(m, skaters_lineup[i=1:num_skaters], Bin) 514 | 515 | # Variable for goalie in lineup 516 | @defVar(m, goalies_lineup[i=1:num_goalies], Bin) 517 | 518 | 519 | # One goalie constraint 520 | @addConstraint(m, sum{goalies_lineup[i], i=1:num_goalies} == 1) 521 | 522 | # Eight skaters constraint 523 | @addConstraint(m, sum{skaters_lineup[i], i=1:num_skaters} == 8) 524 | 525 | 526 | 527 | # between 2 and 3 centers 528 | @addConstraint(m, sum{centers[i]*skaters_lineup[i], i=1:num_skaters} <= 3) 529 | @addConstraint(m, 2 <= sum{centers[i]*skaters_lineup[i], i=1:num_skaters}) 530 | 531 | # between 3 and 4 wingers 532 | @addConstraint(m, sum{wingers[i]*skaters_lineup[i], i=1:num_skaters} <= 4) 533 | @addConstraint(m, 3<=sum{wingers[i]*skaters_lineup[i], i=1:num_skaters}) 534 | 535 | # between 2 and 3 defenders 536 | @addConstraint(m, 2 <= sum{defenders[i]*skaters_lineup[i], i=1:num_skaters}) 537 | @addConstraint(m, sum{defenders[i]*skaters_lineup[i], i=1:num_skaters} <= 3) 538 | 539 | 540 | # Financial Constraint 541 | @addConstraint(m, sum{skaters[i,:Salary]*skaters_lineup[i], i=1:num_skaters} + sum{goalies[i,:Salary]*goalies_lineup[i], i=1:num_goalies} <= 50000) 542 | 543 | 544 | # exactly 3 different teams for the 8 skaters constraint 545 | @defVar(m, used_team[i=1:num_teams], Bin) 546 | @addConstraint(m, constr[i=1:num_teams], used_team[i] <= sum{skaters_teams[t, i]*skaters_lineup[t], t=1:num_skaters}) 547 | @addConstraint(m, constr[i=1:num_teams], sum{skaters_teams[t, i]*skaters_lineup[t], t=1:num_skaters} <= 6*used_team[i]) 548 | @addConstraint(m, sum{used_team[i], i=1:num_teams} == 3) 549 | 550 | 551 | 552 | # No goalies going against skaters 553 | @addConstraint(m, constr[i=1:num_goalies], 6*goalies_lineup[i] + sum{goalie_opponents[k, i]*skaters_lineup[k], k=1:num_skaters}<=6) 554 | 555 | 556 | 557 | # Must have at least one complete line in each lineup 558 | @defVar(m, line_stack[i=1:num_lines], Bin) 559 | @addConstraint(m, constr[i=1:num_lines], 3*line_stack[i] <= sum{team_lines[k,i]*skaters_lineup[k], k=1:num_skaters}) 560 | @addConstraint(m, sum{line_stack[i], i=1:num_lines} >= 1) 561 | 562 | # Must have at least 2 lines with at least two people 563 | @defVar(m, line_stack2[i=1:num_lines], Bin) 564 | @addConstraint(m, constr[i=1:num_lines], 2*line_stack2[i] <= sum{team_lines[k,i]*skaters_lineup[k], k=1:num_skaters}) 565 | @addConstraint(m, sum{line_stack2[i], i=1:num_lines} >= 2) 566 | 567 | 568 | # Overlap Constraint 569 | @addConstraint(m, constr[i=1:size(lineups)[2]], sum{lineups[j,i]*skaters_lineup[j], j=1:num_skaters} + sum{lineups[num_skaters+j,i]*goalies_lineup[j], j=1:num_goalies} <= num_overlap) 570 | 571 | 572 | # Objective 573 | @setObjective(m, Max, sum{skaters[i,:Projection]*skaters_lineup[i], i=1:num_skaters} + sum{goalies[i,:Projection]*goalies_lineup[i], i=1:num_goalies} ) 574 | 575 | 576 | # Solve the integer programming problem 577 | println("Solving Problem...") 578 | @printf("\n") 579 | status = solve(m); 580 | 581 | 582 | # Puts the output of one lineup into a format that will be used later 583 | if status==:Optimal 584 | skaters_lineup_copy = Array(Int64, 0) 585 | for i=1:num_skaters 586 | if getValue(skaters_lineup[i]) >= 0.9 && getValue(skaters_lineup[i]) <= 1.1 587 | skaters_lineup_copy = vcat(skaters_lineup_copy, fill(1,1)) 588 | else 589 | skaters_lineup_copy = vcat(skaters_lineup_copy, fill(0,1)) 590 | end 591 | end 592 | for i=1:num_goalies 593 | if getValue(goalies_lineup[i]) >= 0.9 && getValue(goalies_lineup[i]) <= 1.1 594 | skaters_lineup_copy = vcat(skaters_lineup_copy, fill(1,1)) 595 | else 596 | skaters_lineup_copy = vcat(skaters_lineup_copy, fill(0,1)) 597 | end 598 | end 599 | return(skaters_lineup_copy) 600 | end 601 | end 602 | 603 | 604 | 605 | #= 606 | formulation is the type of formulation that you would like to use. Feel free to customize the formulations. In our paper we considered 607 | the Type 4 formulation in great detail, but we have included the code for all of the formulations dicussed in the paper here. For instance, 608 | if you would like to create lineups without stacking, change one_lineup_Type_4 below to one_lineup_no_stacking 609 | =# 610 | formulation = one_lineup_Type_4 611 | 612 | 613 | 614 | 615 | 616 | 617 | 618 | 619 | function create_lineups(num_lineups, num_overlap, path_skaters, path_goalies, formulation, path_to_output) 620 | #= 621 | num_lineups is an integer that is the number of lineups 622 | num_overlap is an integer that gives the overlap between each lineup 623 | path_skaters is a string that gives the path to the skaters csv file 624 | path_goalies is a string that gives the path to the goalies csv file 625 | formulation is the type of formulation you would like to use (for instance one_lineup_Type_1, one_lineup_Type_2, etc.) 626 | path_to_output is a string where the final csv file with your lineups will be 627 | =# 628 | 629 | 630 | # Load information for skaters table 631 | skaters = readtable(path_skaters) 632 | 633 | # Load information for goalies table 634 | goalies = readtable(path_goalies) 635 | 636 | # Number of skaters 637 | num_skaters = size(skaters)[1] 638 | 639 | # Number of goalies 640 | num_goalies = size(goalies)[1] 641 | 642 | # wingers stores the information on which players are wings 643 | wingers = Array(Int64, 0) 644 | 645 | # centers stores the information on which players are centers 646 | centers = Array(Int64, 0) 647 | 648 | # defenders stores the information on which players are defenders 649 | defenders = Array(Int64, 0) 650 | 651 | #= 652 | Process the position information in the skaters file to populate the wingers, 653 | centers, and defenders with the corresponding correct information 654 | =# 655 | for i =1:num_skaters 656 | if skaters[i,:Position] == "LW" || skaters[i,:Position] == "RW" || skaters[i,:Position] == "W" 657 | wingers=vcat(wingers,fill(1,1)) 658 | centers=vcat(centers,fill(0,1)) 659 | defenders=vcat(defenders,fill(0,1)) 660 | elseif skaters[i,:Position] == "C" 661 | wingers=vcat(wingers,fill(0,1)) 662 | centers=vcat(centers,fill(1,1)) 663 | defenders=vcat(defenders,fill(0,1)) 664 | elseif skaters[i,:Position] == "D" || skaters[i,:Position] == "LD" || skaters[i,:Position] == "RD" 665 | wingers=vcat(wingers,fill(0,1)) 666 | centers=vcat(centers,fill(0,1)) 667 | defenders=vcat(defenders,fill(1,1)) 668 | else 669 | wingers=vcat(wingers,fill(0,1)) 670 | centers=vcat(centers,fill(0,1)) 671 | defenders=vcat(defenders,fill(1,1)) 672 | end 673 | end 674 | 675 | 676 | # A forward is either a center or a winger 677 | forwards = centers+wingers 678 | 679 | 680 | 681 | # Create team indicators from the information in the skaters file 682 | teams = unique(skaters[:Team]) 683 | 684 | # Total number of teams 685 | num_teams = size(teams)[1] 686 | 687 | # player_info stores information on which team each player is on 688 | player_info = zeros(Int, size(teams)[1]) 689 | 690 | # Populate player_info with the corresponding information 691 | for j=1:size(teams)[1] 692 | if skaters[1, :Team] == teams[j] 693 | player_info[j] =1 694 | end 695 | end 696 | skaters_teams = player_info' 697 | 698 | 699 | for i=2:num_skaters 700 | player_info = zeros(Int, size(teams)[1]) 701 | for j=1:size(teams)[1] 702 | if skaters[i, :Team] == teams[j] 703 | player_info[j] =1 704 | end 705 | end 706 | skaters_teams = vcat(skaters_teams, player_info') 707 | end 708 | 709 | 710 | 711 | # Create goalie identifiers so you know who they are playing 712 | opponents = goalies[:Opponent] 713 | goalie_teams = goalies[:Team] 714 | goalie_opponents=[] 715 | for num = 1:size(teams)[1] 716 | if opponents[1] == teams[num] 717 | goalie_opponents = skaters_teams[:, num] 718 | end 719 | end 720 | for num = 2:size(opponents)[1] 721 | for num_2 = 1:size(teams)[1] 722 | if opponents[num] == teams[num_2] 723 | goalie_opponents = hcat(goalie_opponents, skaters_teams[:,num_2]) 724 | end 725 | end 726 | end 727 | 728 | 729 | 730 | 731 | # Create line indicators so you know which players are on which lines 732 | L1_info = zeros(Int, num_skaters) 733 | L2_info = zeros(Int, num_skaters) 734 | L3_info = zeros(Int, num_skaters) 735 | L4_info = zeros(Int, num_skaters) 736 | for num=1:size(skaters)[1] 737 | if skaters[:Team][num] == teams[1] 738 | if skaters[:Line][num] == "1" 739 | L1_info[num] = 1 740 | elseif skaters[:Line][num] == "2" 741 | L2_info[num] = 1 742 | elseif skaters[:Line][num] == "3" 743 | L3_info[num] = 1 744 | elseif skaters[:Line][num] == "4" 745 | L4_info[num] = 1 746 | end 747 | end 748 | end 749 | team_lines = hcat(L1_info, L2_info, L3_info, L4_info) 750 | 751 | 752 | for num2 = 2:size(teams)[1] 753 | L1_info = zeros(Int, num_skaters) 754 | L2_info = zeros(Int, num_skaters) 755 | L3_info = zeros(Int, num_skaters) 756 | L4_info = zeros(Int, num_skaters) 757 | for num=1:size(skaters)[1] 758 | if skaters[:Team][num] == teams[num2] 759 | if skaters[:Line][num] == "1" 760 | L1_info[num] = 1 761 | elseif skaters[:Line][num] == "2" 762 | L2_info[num] = 1 763 | elseif skaters[:Line][num] == "3" 764 | L3_info[num] = 1 765 | elseif skaters[:Line][num] == "4" 766 | L4_info[num] = 1 767 | end 768 | end 769 | end 770 | team_lines = hcat(team_lines, L1_info, L2_info, L3_info, L4_info) 771 | end 772 | num_lines = size(team_lines)[2] 773 | 774 | 775 | 776 | 777 | # Create power play indicators 778 | PP_info = zeros(Int, num_skaters) 779 | for num=1:size(skaters)[1] 780 | if skaters[:Team][num]==teams[1] 781 | if skaters[:Power_Play][num] == "1" 782 | PP_info[num] = 1 783 | end 784 | end 785 | end 786 | 787 | P1_info = PP_info 788 | 789 | for num2=2:size(teams)[1] 790 | PP_info = zeros(Int, num_skaters) 791 | for num=1:size(skaters)[1] 792 | if skaters[:Team][num] == teams[num2] 793 | if skaters[:Power_Play][num] == "1" 794 | PP_info[num]=1 795 | end 796 | end 797 | end 798 | P1_info = hcat(P1_info, PP_info) 799 | end 800 | 801 | 802 | # Lineups using formulation as the stacking type 803 | the_lineup= formulation(skaters, goalies, hcat(zeros(Int, num_skaters + num_goalies), zeros(Int, num_skaters + num_goalies)), num_overlap, num_skaters, num_goalies, centers, wingers, defenders, num_teams, skaters_teams, goalie_opponents, team_lines, num_lines, P1_info) 804 | the_lineup2 = formulation(skaters, goalies, hcat(the_lineup, zeros(Int, num_skaters + num_goalies)), num_overlap, num_skaters, num_goalies, centers, wingers, defenders, num_teams, skaters_teams, goalie_opponents, team_lines, num_lines, P1_info) 805 | tracer = hcat(the_lineup, the_lineup2) 806 | for i=1:(num_lineups-2) 807 | try 808 | thelineup=formulation(skaters, goalies, tracer, num_overlap, num_skaters, num_goalies, centers, wingers, defenders, num_teams, skaters_teams, goalie_opponents, team_lines, num_lines, P1_info) 809 | tracer = hcat(tracer,thelineup) 810 | catch 811 | break 812 | end 813 | end 814 | 815 | 816 | # Create the output csv file 817 | lineup2 = "" 818 | for j = 1:size(tracer)[2] 819 | lineup = ["" "" "" "" "" "" "" "" ""] 820 | for i =1:num_skaters 821 | if tracer[i,j] == 1 822 | if centers[i]==1 823 | if lineup[1]=="" 824 | lineup[1] = string(skaters[i,1], " ", skaters[i,2]) 825 | elseif lineup[2]=="" 826 | lineup[2] = string(skaters[i,1], " ", skaters[i,2]) 827 | elseif lineup[9] =="" 828 | lineup[9] = string(skaters[i,1], " ", skaters[i,2]) 829 | end 830 | elseif wingers[i] == 1 831 | if lineup[3] == "" 832 | lineup[3] = string(skaters[i,1], " ", skaters[i,2]) 833 | elseif lineup[4] == "" 834 | lineup[4] = string(skaters[i,1], " ", skaters[i,2]) 835 | elseif lineup[5] == "" 836 | lineup[5] = string(skaters[i,1], " ", skaters[i,2]) 837 | elseif lineup[9] == "" 838 | lineup[9] = string(skaters[i,1], " ", skaters[i,2]) 839 | end 840 | elseif defenders[i]==1 841 | if lineup[6] == "" 842 | lineup[6] = string(skaters[i,1], " ", skaters[i,2]) 843 | elseif lineup[7] =="" 844 | lineup[7] = string(skaters[i,1], " ", skaters[i,2]) 845 | elseif lineup[9] == "" 846 | lineup[9] = string(skaters[i,1], " ", skaters[i,2]) 847 | end 848 | end 849 | end 850 | end 851 | for i =1:num_goalies 852 | if tracer[num_skaters+i,j] == 1 853 | lineup[8] = string(goalies[i,1], " ", goalies[i,2]) 854 | end 855 | end 856 | for name in lineup 857 | lineup2 = string(lineup2, name, ",") 858 | end 859 | lineup2 = chop(lineup2) 860 | lineup2 = string(lineup2, """ 861 | 862 | """) 863 | end 864 | outfile = open(path_to_output, "w") 865 | write(outfile, lineup2) 866 | close(outfile) 867 | end 868 | 869 | 870 | 871 | 872 | # Running the code 873 | create_lineups(num_lineups, num_overlap, path_skaters, path_goalies, formulation, path_to_output) 874 | --------------------------------------------------------------------------------