Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 30 additions & 8 deletions src/nlp.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ struct ConstraintNull <: AbstractConstraint end

struct Variable{S,O} <: AbstractVariable
size::S
length::O
offset::O
end
Base.show(io::IO, v::Variable) = print(
Expand Down Expand Up @@ -245,13 +246,33 @@ function ExaModel(c::C; prod = nothing) where {C<:ExaCore}
)
end

@inline Base.getindex(v::V, i) where {V<:Variable} =
@inline function Base.getindex(v::V, i) where {V<:Variable}
_bound_check(v.size, i)
Var(i + (v.offset - _start(v.size[1]) + 1))
@inline Base.getindex(v::V, i, j) where {V<:Variable} = Var(
i +
j * _length(v.size[1]) +
(v.offset - _start(v.size[1]) + 1 - _start(v.size[2]) * _length(v.size[1])),
)
end
@inline function Base.getindex(v::V, is...) where {V<:Variable}
@assert(length(is) == length(v.size), "Variable index dimension error")
_bound_check(v.size, is)
Var(v.offset + idxx(is .- (_start.(v.size) .- 1), _length.(v.size)))
end

function _bound_check(sizes, i::I) where I <: Integer
__bound_check(sizes[1], i)
end
function _bound_check(sizes, is::NTuple{N,I}) where {I <: Integer, N}
__bound_check(sizes[1], is[1])
_bound_check(sizes[2:end], is[2:end])
end
_bound_check(sizes, is) = nothing
_bound_check(sizes, is::Tuple{}) = nothing

function __bound_check(a::I,b::I) where I <: Integer
@assert(1<= b <= a, "Variable index bound error")
end
function __bound_check(a::UnitRange{Int},b::I) where I <: Integer
@assert(b in a, "Variable index bound error")
end


function append!(backend, a, b::Base.Generator, lb)
b = _adapt_gen(b)
Expand Down Expand Up @@ -332,12 +353,13 @@ function variable(


o = c.nvar
c.nvar += total(ns)
len = total(ns)
c.nvar += len
c.x0 = append!(c.backend, c.x0, start, total(ns))
c.lvar = append!(c.backend, c.lvar, lvar, total(ns))
c.uvar = append!(c.backend, c.uvar, uvar, total(ns))

return Variable(ns, o)
return Variable(ns, len, o)

end

Expand Down