Julia novice. Integer programming code optimization error

question
jump

#1

Hello and thanks for reading this. I am new to Julia and am attempting to use JuMP to generate entries for Fanduel football tournament contests with a top heavy payoff structure. The code are my modifications to code originally presented in the paper Picking Winners Using Integer Programming by David Hunter, Juan Pablo Vielma, and Tauhid Zaman. My apologies if this not the right place to ask such a question (if so, I would appreciate direction to a more suitable place). I believe I am encountering an easily-fixable error, but so far I am unable to do so. I am using Julia 0.6.0. My code is:



I am not allowed to upload .csv files but to run the code one needs to supply an offense.csv and a defense.csv file that contain the variables and constraints needed to solve the optimization problem. If necessary, I can provide examples and I am uploading a .jpg example of the offense.csv

. The defense.csv file is similar, but lacks a “Position” column (because they are all defenses).

Finally, when I attempt to run the code, I get the error:

ERROR: LoadError: UndefVarError: i not defined
Stacktrace:
 [1] macro expansion at C:\Users\cbost\.julia\v0.6\JuMP\src\macros.jl:422 [inlined]
 [2] one_lineup_Type_3(::DataFrames.DataFrame, ::DataFrames.DataFrame, ::Array{Int64,2}, ::Int64, ::Int64, ::Int64, ::Array{Any,1}, ::Array{Any,1}, ::Array{Any,1}, ::Array{Any,1}, ::Array{Any,1}, ::Int64, ::Array{Int64,2}, ::Array{Int64,2}) at C:\Users\cbost\Desktop\FanDuel\fanduel.jl:53
 [3] create_lineups(::Int64, ::Int64, ::String, ::String, ::#one_lineup_Type_3, ::String) at C:\Users\cbost\Desktop\FanDuel\fanduel.jl:254
 [4] include_from_node1(::String) at .\loading.jl:569
 [5] include(::String) at .\sysimg.jl:14
while loading C:\Users\cbost\Desktop\FanDuel\fanduel.jl, in expression starting on line 330

It appears there is an index (i) error somewhere in the code, but because the stacktrace just tells me the error is in the final function call, I don’t know how to track it down. I would greatly appreciate any help or suggestions to go about solving this seemingly simple error. Thank you very much for reading this topic!


#2

Could you please highlight which line is 53 in your snippet?

I’m guessing that it might be this:

@constraint(m, sum(defense_lineup[i], i=1:num_defense) == 1)

I think this syntax was deprecated in favor of this:

@constraint(m, sum(defense_lineup[i] for i=1:num_defense) == 1)

#3

Thanks very much for the reply. You are correct that is line 53. I edited the code to use “emphasis” on line 53 because I couldn’t find a “highlight” option.

_@constraint(m, sum(defense_lineup[i], i=1:num_defense) == 1)_

You are also correct about the syntax being deprecated. A somewhat interesting side note is that when I initially reformatted the original code and tried to run it, Julia returned warnings about that syntax being deprecated, but when I more recently tried to run the code, the warnings no longer appeared and I clearly didn’t fix them.

Anyway, I applied the correct syntax to lines 53, 56, 60, 63, 66, 69, 72, 76, 80, 81, 85, 88, and 91 (the constraints and objective) and it appears to have solved the i not defined error. I get a new error :slight_smile:

ERROR: LoadError: MethodError: no method matching *(::Type{Array{Int64,0}}, ::JuMP.Variable)
Closest candidates are:
  *(::Any, ::Any, ::Any, ::Any...) at operators.jl:424
  *(::JuMP.Variable, ::JuMP.Variable) at C:\Users\cbost\.julia\v0.6\JuMP\src\operators.jl:56
  *(::JuMP.GenericAffExpr{Float64,JuMP.Variable}, ::JuMP.Variable) at C:\Users\cbost\.julia\v0.6\JuMP\src\operators.jl:133
  ...
Stacktrace:
 [1] addtoexpr_reorder(::JuMP.GenericAffExpr{Float64,JuMP.Variable}, ::Type{Array{Int64,0}}, ::JuMP.Variable) at C:\Users\cbost\.julia\v0.6\JuMP\src\parseExpr_staged.jl:280
 [2] macro expansion at C:\Users\cbost\.julia\v0.6\JuMP\src\parseExpr_staged.jl:489 [inlined]
 [3] macro expansion at C:\Users\cbost\.julia\v0.6\JuMP\src\macros.jl:422 [inlined]
 [4] one_lineup_Type_3(::DataFrames.DataFrame, ::DataFrames.DataFrame, ::Array{Int64,2}, ::Int64, ::Int64, ::Int64, ::Array{Any,1}, ::Array{Any,1}, ::Array{Any,1}, ::Array{Any,1}, ::Array{Any,1}, ::Int64, ::Array{Int64,2}, ::Array{Int64,2}) at C:\Users\cbost\Desktop\FanDuel\fanduel.jl:60
 [5] create_lineups(::Int64, ::Int64, ::String, ::String, ::#one_lineup_Type_3, ::String) at C:\Users\cbost\Desktop\FanDuel\fanduel.jl:254
 [6] include_from_node1(::String) at .\loading.jl:569
 [7] include(::String) at .\sysimg.jl:14
while loading C:\Users\cbost\Desktop\FanDuel\fanduel.jl, in expression starting on line 330

Does anyone have any suggestions for this one? Thank you!


#4

It turns out I was incorrectly calling the Array function (again because the old code used a deprecated construct that I didn’t understand). Reading the documentation saves the day again! :rofl:. Special thanks to leethargo for your help!