Skip to content

Contractor fails with 'KeyError: key getindex not found' when function uses @variables x[1:n] #227

Description

@dpsanders

Setup

Using array-valued symbolics (@variables x[1:n]) with a function that indexes into the vector produces a KeyError from ReversePropagation when building a Contractor.

Minimal repro

using IntervalArithmetic, IntervalConstraintProgramming, Symbolics

f(x) = (x[1] - 1)^2 + (x[2] - 2)^2        # typical vector-argument style

@variables s[1:2]
sv = collect(s)
expr = f(sv)
C = Contractor(expr, sv)    # ❌ ERROR

Error

ERROR: KeyError: key getindex not found
Stacktrace:
 [1] getindex(h::Dict{Any, Any}, key::Any) @ ./dict.jl:477
 [2] rev(f_val::Function, z::Num, x::Num, y::Num)
   @ ReversePropagation ~/.../ReversePropagation/src/reverse_icp.jl:76
 [3] rev(eq::SymbolicUtils.Code.Assignment, params::Vector{Any})
   @ ReversePropagation ~/.../ReversePropagation/src/reverse_icp.jl:39
 [...]
 [17] Contractor(ex::Num, vars::Vector{Num}) @ IntervalConstraintProgramming/src/contractor.jl:10

Workaround

Define the variables as scalars and pass them explicitly, so no getindex nodes appear in the traced expression:

@variables s1 s2
sv = [s1, s2]
expr = f(sv)     # Julia indexing on a Vector{Num} — no Symbolics getindex node
C = Contractor(expr, sv)   #

Suggestion

The friendly API is @variables s[1:n], so it would be nice if the Contractor either (a) supported getindex in the reverse-propagation dictionary, or (b) produced a clearer error message suggesting the scalar-variable workaround.

This is especially awkward for higher-dim problems (e.g. Lennard-Jones clusters with 6–9 variables) where enumerating scalar variables is verbose.

Environment

  • IntervalConstraintProgramming v0.15.0
  • ReversePropagation v0.4.2
  • IntervalArithmetic v1.0.6
  • Symbolics v6.58.0
  • Julia 1.12.5

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions