Giter Club home page Giter Club logo

Comments (4)

odow avatar odow commented on June 4, 2024 1

So perhaps we do need an OrderedDict

from jump.jl.

odow avatar odow commented on June 4, 2024

This is actually pretty nasty.

We fallback to AbstractVector methods:

julia> using JuMP

julia> A = [[1, 2, 10], [2, 3, 30]]
2-element Vector{Vector{Int64}}:
 [1, 2, 10]
 [2, 3, 30]

julia> model = Model()
A JuMP Model
Feasibility problem with:
Variables: 0
Model mode: AUTOMATIC
CachingOptimizer state: NO_OPTIMIZER
Solver name: No optimizer attached.

julia> @variable(model, x[i in 1:2, j in A[i]])
JuMP.Containers.SparseAxisArray{VariableRef, 2, Tuple{Int64, Int64}} with 6 entries:
  [1, 1 ]  =  x[1,1]
  [1, 10]  =  x[1,10]
  [1, 2 ]  =  x[1,2]
  [2, 2 ]  =  x[2,2]
  [2, 3 ]  =  x[2,3]
  [2, 30]  =  x[2,30]

julia> @constraint(model, x[1, :] in SecondOrderCone())
[x[1,2], x[1,10], x[1,1]]  MathOptInterface.SecondOrderCone(3)

julia> x[1, :] isa AbstractVector{<:AbstractJuMPScalar}
true

entry point is:

function build_constraint(
::Function,
x::AbstractVector{<:Union{Number,AbstractJuMPScalar}},
set::MOI.AbstractVectorSet,
)
return VectorConstraint(x, set)
end

which calls:

JuMP.jl/src/constraints.jl

Lines 652 to 659 in a21e616

function VectorConstraint(
func::AbstractVector{<:Union{Number,AbstractJuMPScalar}},
set::MOI.AbstractVectorSet,
)
# collect() is not used here so that DenseAxisArray will work
f = [func[idx] for idx in eachindex(func)]
return VectorConstraint(f, set)
end

And eachindex isn't ordered:

julia> y = x[1, :]
JuMP.Containers.SparseAxisArray{VariableRef, 1, Tuple{Int64}} with 3 entries:
  [1 ]  =  x[1,1]
  [10]  =  x[1,10]
  [2 ]  =  x[1,2]

julia> [y[k] for k in eachindex(y)]
3-element Vector{VariableRef}:
 x[1,2]
 x[1,10]
 x[1,1]

I don't know what a good answer is.

We can't throw an error because that will break existing code like this:

julia> @constraint(model, x[1, :] >= 0)
[x[1,2], x[1,10], x[1,1]]  MathOptInterface.Nonnegatives(3)

The issue is that some vector sets are ordered, and some are not. Here's another set that is problematic:

julia> @constraint(model, x[1, :] in SecondOrderCone())
[x[1,2], x[1,10], x[1,1]]  MathOptInterface.SecondOrderCone(3)

from jump.jl.

blegat avatar blegat commented on June 4, 2024

The user could still do dot(a, x[1, :]) where a is some vector of constant so I don't think this is about vector constraints. Maybe we could use an OrderedDict

from jump.jl.

odow avatar odow commented on June 4, 2024

Hmm:

julia> using JuMP

julia> A = [[1, 2, 10], [2, 3, 30]]
2-element Vector{Vector{Int64}}:
 [1, 2, 10]
 [2, 3, 30]

julia> model = Model()
A JuMP Model
Feasibility problem with:
Variables: 0
Model mode: AUTOMATIC
CachingOptimizer state: NO_OPTIMIZER
Solver name: No optimizer attached.

julia> @variable(model, x[i in 1:2, j in A[i]])
JuMP.Containers.SparseAxisArray{VariableRef, 2, Tuple{Int64, Int64}} with 6 entries:
  [1, 1 ]  =  x[1,1]
  [1, 10]  =  x[1,10]
  [1, 2 ]  =  x[1,2]
  [2, 2 ]  =  x[2,2]
  [2, 3 ]  =  x[2,3]
  [2, 30]  =  x[2,30]

julia> y = x[1, :]
JuMP.Containers.SparseAxisArray{VariableRef, 1, Tuple{Int64}} with 3 entries:
  [1 ]  =  x[1,1]
  [10]  =  x[1,10]
  [2 ]  =  x[1,2]

julia> z = rand(3)
3-element Vector{Float64}:
 0.508529766285344
 0.32511007041899975
 0.6771958164053773

julia> using LinearAlgebra

julia> dot(z, y)
0.508529766285344 x[1,2] + 0.32511007041899975 x[1,10] + 0.6771958164053773 x[1,1]

This is more than just a slicing issue:

julia> model = Model();

julia> @variable(model, x[i in 1:2], container = SparseAxisArray)
JuMP.Containers.SparseAxisArray{VariableRef, 1, Tuple{Int64}} with 2 entries:
  [1]  =  x[1]
  [2]  =  x[2]

julia> dot(1:2, x)
x[2] + 2 x[1]

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.