martinbiel / StochasticPrograms.jl

Julia package for formulating and analyzing stochastic recourse models.
MIT License
75 stars 25 forks source link

ERROR: MethodError: no method matching add_to_expression!(::DecisionAffExpr{Float64}, ::Int64, ::DecisionAffExpr{Float64}) #12

Closed maramos874 closed 3 years ago

maramos874 commented 3 years ago

Hi, I'm now working with a custom complicated model, and I'm having the Error stated above. By tracing the JuMP.add_to_expression! function, in one point StochasticPrograms.jl calls that function with the follow arguments (line 147, mutable_arithmetics.jl):

function MA.mutable_operate!(::typeof(-), expr::_DecisionAffOrQuadExpr, x)
    return JuMP.add_to_expression!(expr, -1, x)
end

Apparently, JuMP.add_to_expression! has not been implemented for Int64 on the middle argument, or for the combination of argument types: image

In my case, expr and x are StochasticPrograms.DecisionAffExpr{Float64} type. What can be the issue here?

Thanks in advance

martinbiel commented 3 years ago

Could you post a MWE of what you want to achieve so I can reproduce the error?

maramos874 commented 3 years ago

Finally managed to produce a MWE after figuring out what was causing the error, when using structures to store parameters, Note that by replacing p.cc by its value, the error is not raised.

using Gurobi
using StochasticPrograms

struct params
    cc::Float64
end

p = params(240.0)

function main()
    model = @stochastic_model begin
        @stage 1 begin
            @parameters begin
                tasks = 1:5
                shifts = 1:5
                agents = 1:5
                qualifs = 1:3
            end
            @decision(model, 0 <= extime[sh in shifts, ag in agents])
            @decision(model, 0 <= yqual[sh in shifts, ag in agents, qu in qualifs] <= 1, Int)
            @objective(model, Min, sum(yqual[sh, ag, qu] for sh in shifts for ag in agents for qu in qualifs))
            @constraint(model, sh_con5[sh=shifts, ag=agents],
            extime[sh, ag]
            <=
            p.cc *
            sum(yqual[sh, ag, qu]
            for qu in qualifs
            )
            )
        end
        @stage 2 begin
            @parameters begin
                c = 1:5
            end
            @uncertain e
            @variable(model, y[i in c] >= 0)
            @objective(model, Min, sum(y[i] for i in c))
            @constraint(model, test2[i in 2:5], y[i] + e >= y[i-1])
        end
    end

    ξ₁ = Scenario(e=1, probability = 1/3)
    ξ₂ = Scenario(e=2, probability = 1/3)
    ξ₃ = Scenario(e=3, probability = 1/3)

    model = instantiate(model, [ξ₁, ξ₂, ξ₃], optimizer=Gurobi.Optimizer)
    print(model)

    optimize!(model)
    x = optimal_decision(model)
    println("Wheat: $(x[1])")
    println("Corn: $(x[2])")
    println("Beets: $(x[3])")
    println("Profit: $(objective_value(model))")

    println("EVPI: $(EVPI(model))")
    println("VSS: $(VSS(model))")
end
main()
martinbiel commented 3 years ago

Thank you for the MWE. I am very busy this week with paper revision, but I will look into this as well as the other issues you have posted afterwards :slightly_smiling_face:

maramos874 commented 3 years ago

Ok, no problem! Happy to help.

martinbiel commented 3 years ago

Should be fixed now. Your MWE pointed me to another bug as well, so thanks a lot! :slightly_smiling_face:

maramos874 commented 3 years ago

Nice! Happy to help.