Comments (3)
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.
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.
Thank you Oscar!
from jump.jl.
Related Issues (20)
- NumFOCUS: GSoC 2024 Update HOT 1
- Error vcat(::NonlinearExpr, ::VariableRef, ::Float64) HOT 5
- Increase performance of SOC constraints HOT 4
- Iterating SparseAxisArray does not preserve order HOT 4
- How to improve the speed to build a complex model? HOT 2
- Coefficients of complex variables created with a GenericModel are always Float64
- LinearAlgebra.hermitian is incorrect
- *(::Real, ::Hermitian) is not hermitian HOT 5
- Cannot `convert` an object of type Float64 to an object of type JuMP.NonlinearExpr HOT 3
- Documentation Request: List whether a solver supports Indicator Constraints HOT 4
- Bullet point alignment in bibliography HOT 9
- Multiplication of matrix expression and variables leads to stack overflow and matmul error HOT 4
- Multiple Ranges for variables HOT 3
- Y' when Y is of type ::Matrix{NonlinearExpr} HOT 1
- Poor performance in complementarity models HOT 23
- Add a method for Complex(a,b) HOT 8
- MethodError with empty summations HOT 1
- Nonlinear subexpressions HOT 4
- Should error when trying to mix `NonlinearExpression` and `GenericNonlinearExpr` HOT 1
- Support normalized_coefficient for vector-valued constraints
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from jump.jl.