Reputation: 21
There is a error in my code, can anyone help me please?
My code:
function funP(u, τ::Float64)
w = (τ*max(u, 0)) + ((1-τ)*max(-u, 0))
return w
end
τ = 0.2
modelquant = Model(with_optimizer(OSQP.Optimizer))
@variable(modelquant, β[i=0:1])
@variable(modelquant, erro[1:T])
@constraint(modelquant,[i=1:T], erro[i] >= contratos[i] - β[0] - β[1]*spot[i])
@constraint(modelquant,[i=1:T], erro[i] >= -contratos[i] + β[0] + β[1]*spot[i])
@objective(modelquant, Min, sum(funP(erro[i], τ) for i=1:T))
optimize!(modelquant)
objective_value(modelquant)
𝐁 = JuMP.value.(β)
The error is:
julia> @objective(modelquant, Min, sum(funP(erro[i], τ) for i=1:T)) ERROR: MethodError: no method matching isless(::Int64, ::VariableRef) Closest candidates are: isless(::Missing, ::Any) at missing.jl:87 isless(::Real, ::AbstractFloat) at operators.jl:166 isless(::Integer, ::ForwardDiff.Dual{Ty,V,N} where N where V) where Ty at C:\Users\Fernanda.julia\packages\ForwardDiff\kU1ce\src\dual.jl:140 ... Stacktrace: [1] max(::VariableRef, ::Int64) at .\operators.jl:417 [2] funP(::VariableRef, ::Float64) at .\REPL[8]:2 [3] macro expansion at C:\Users\Fernanda.julia\packages\MutableArithmetics\bPWR4\src\rewrite.jl:276 [inlined] [4] macro expansion at C:\Users\Fernanda.julia\packages\JuMP\qhoVb\src\macros.jl:830 [inlined] [5] top-level scope at .\REPL[20]:1
Thank you so much!
Upvotes: 2
Views: 326
Reputation: 42214
You need to re-engineer your model to replace the max
function with a binary variable.
In your case the code will look like this (check for typos):
@variable(modelquant, erro_neg[1:T], Bin)
@variable(modelquant, erro_pos[1:T], Bin)
@constraint(modelquant,for i=1:T,erro_neg[i]+erro_pos[i]==1)
@constraint(modelquant,for i=1:T,erro[i]*erro_pos[i] >= 0)
@constraint(modelquant,for i=1:T,erro[i]*erro_neg[i] <= 0)
@objective(modelquant, Min, sum( τ*erro_neg[i]*erro[i]+ (1-τ)*erro[i]*erro_npos[i] for i=1:T))
Please note that in my version you could actually safely remove the Bin
condition from erro_neg
and erro_pos
and the model will still work (you need to test empirically what your solver prefers)
Upvotes: 2