GithubHelp home page GithubHelp logo

Comments (3)

odow avatar odow commented on July 19, 2024 1

Thanks for the report. I've reproduced this down to:

julia> using JuMP

julia> model = Model();

julia> data = Dict(2 => 1);

julia> @expression(model, sum(1 for p in [1, 2] if p in keys(data) && data[p] == 1))
ERROR: KeyError: key 1 not found
Stacktrace:
 [1] getindex(h::Dict{Int64, Int64}, key::Int64)
   @ Base ./dict.jl:498
 [2] macro expansion
   @ ~/.julia/packages/MutableArithmetics/NIXlP/src/rewrite_generic.jl:321 [inlined]
 [3] macro expansion
   @ ~/.julia/packages/MutableArithmetics/NIXlP/src/rewrite.jl:321 [inlined]
 [4] macro expansion
   @ ~/.julia/packages/JuMP/027Gt/src/macros.jl:239 [inlined]
 [5] macro expansion
   @ ~/.julia/packages/JuMP/027Gt/src/macros/@expression.jl:86 [inlined]
 [6] macro expansion
   @ ~/.julia/packages/JuMP/027Gt/src/macros.jl:375 [inlined]
 [7] top-level scope
   @ ./REPL[54]:1

The issue is because we're not short-circuiting the && operator:

julia> @macroexpand @expression(model, sum(1 for p in [1, 2] if p in keys(data) && data[p] == 1))
quote
    #= REPL[55]:1 =#
    JuMP._valid_model(model, :model)
    begin
        #= /Users/oscar/.julia/packages/JuMP/027Gt/src/macros.jl:374 =#
        let model = model
            #= /Users/oscar/.julia/packages/JuMP/027Gt/src/macros.jl:375 =#
            begin
                #= /Users/oscar/.julia/packages/JuMP/027Gt/src/macros/@expression.jl:86 =#
                begin
                    #= /Users/oscar/.julia/packages/JuMP/027Gt/src/macros.jl:239 =#
                    var"#815###431" = begin
                            #= /Users/oscar/.julia/packages/MutableArithmetics/NIXlP/src/rewrite.jl:325 =#
                            let
                                #= /Users/oscar/.julia/packages/MutableArithmetics/NIXlP/src/rewrite.jl:326 =#
                                begin
                                    #= /Users/oscar/.julia/packages/MutableArithmetics/NIXlP/src/rewrite.jl:321 =#
                                    var"#816###432" = MutableArithmetics.Zero()
                                    for p = [1, 2]
                                        #= /Users/oscar/.julia/packages/MutableArithmetics/NIXlP/src/rewrite_generic.jl:321 =#

# Problem is this next line. Not short circuiting

                                        if (NonlinearOperator(&, :&&))(p in keys(data), (NonlinearOperator(==, :==))(data[p], 1))
                                            #= /Users/oscar/.julia/packages/MutableArithmetics/NIXlP/src/rewrite_generic.jl:322 =#
                                            begin
                                                #= /Users/oscar/.julia/packages/MutableArithmetics/NIXlP/src/rewrite_generic.jl:268 =#
                                                var"#816###432" = (MutableArithmetics.operate!!)(MutableArithmetics.add_mul, var"#816###432", 1)
                                            end
                                        end
                                    end
                                end
                                #= /Users/oscar/.julia/packages/MutableArithmetics/NIXlP/src/rewrite.jl:327 =#
                                var"#816###432"
                            end
                        end
                    #= /Users/oscar/.julia/packages/JuMP/027Gt/src/macros.jl:240 =#
                    var"#817###433" = (JuMP.flatten!)(var"#815###431")
                end
                #= /Users/oscar/.julia/packages/JuMP/027Gt/src/macros/@expression.jl:89 =#
                JuMP._replace_zero(model, var"#817###433")
            end
        end
    end
end

Let me see if we can fix this.

from jump.jl.

odow avatar odow commented on July 19, 2024 1

A short-term work-around is

using JuMP
model = Model();
data = Dict(2 => 1);
check(data, p) = p in keys(data) && data[p] == 1
@expression(model, sum(1 for p in [1, 2] if check(data, p)))

from jump.jl.

alihaddad-dynideas avatar alihaddad-dynideas commented on July 19, 2024

Thank you Oscar!

from jump.jl.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.