Skip to content
Merged
Show file tree
Hide file tree
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
24 changes: 12 additions & 12 deletions examples/quadrotor.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ N = 3

n = 9
p = 4
d(i, j, N) =
(j == 1 ? 1 * sin(2 * pi / N * i) : 0.0) +
(j == 3 ? 2 * sin(4 * pi / N * i) : 0.0) +
(j == 5 ? 2 * i / N : 0.0)
function d(i, j, N)
return (j == 1 ? 1 * sin(2 * pi / N * i) : 0.0) +
(j == 3 ? 2 * sin(4 * pi / N * i) : 0.0) +
(j == 5 ? 2 * i / N : 0.0)
end
dt = 1/N
R = fill(1 / 10, 4)
Q = [1, 0, 1, 0, 1, 0, 1, 1, 1]
Expand All @@ -27,12 +28,7 @@ model = Model()
using GenOpt
container = ParametrizedArray

@constraint(
model,
[i in 1:n],
x[1, i] == x0[i],
container = container,
)
@constraint(model, [i in 1:n], x[1, i] == x0[i], container = container,)
@constraint(
model,
[i in 1:N],
Expand Down Expand Up @@ -84,13 +80,17 @@ container = ParametrizedArray
[i in 1:N],
x[i+1, 7] ==
x[i, 7] +
(u[i, 2] * cos(x[i, 7]) / cos(x[i, 8]) + u[i, 3] * sin(x[i, 7]) / cos(x[i, 8])) * dt,
(
u[i, 2] * cos(x[i, 7]) / cos(x[i, 8]) +
u[i, 3] * sin(x[i, 7]) / cos(x[i, 8])
) * dt,
container = container,
)
@constraint(
model,
[i in 1:N],
x[i+1, 8] == x[i, 8] + (-u[i, 2] * sin(x[i, 7]) + u[i, 3] * cos(x[i, 7])) * dt,
x[i+1, 8] ==
x[i, 8] + (-u[i, 2] * sin(x[i, 7]) + u[i, 3] * cos(x[i, 7])) * dt,
container = container,
)
@constraint(
Expand Down
20 changes: 15 additions & 5 deletions src/JuMP_wrapper.jl
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,26 @@ end

Base.size(array::ArrayOfVariables) = array.size
function Base.getindex(A::ArrayOfVariables{T}, I...) where {T}
index = A.offset + Base._to_linear_index(Base.CartesianIndices(A.size), I...)
index =
A.offset + Base._to_linear_index(Base.CartesianIndices(A.size), I...)
return JuMP.GenericVariableRef{T}(A.model, MOI.VariableIndex(index))
end

JuMP._is_real(::ArrayOfVariables) = true
JuMP.moi_function(array::ArrayOfVariables) = ContiguousArrayOfVariables(array.offset, array.size)
function JuMP.jump_function(model::JuMP.GenericModel{T}, array::ContiguousArrayOfVariables{N}) where {T,N}
function JuMP.moi_function(array::ArrayOfVariables)
return ContiguousArrayOfVariables(array.offset, array.size)
end
function JuMP.jump_function(
model::JuMP.GenericModel{T},
array::ContiguousArrayOfVariables{N},
) where {T,N}
return ArrayOfVariables{T,N}(model, array.offset, array.size)
end

function Base.convert(::Type{ArrayOfVariables{T,N}}, array::Array{JuMP.GenericVariableRef{T},N}) where {T,N}
function Base.convert(
::Type{ArrayOfVariables{T,N}},
array::Array{JuMP.GenericVariableRef{T},N},
) where {T,N}
model = JuMP.owner_model(array[1])
offset = JuMP.index(array[1]).value - 1
for i in eachindex(array)
Expand Down Expand Up @@ -106,7 +115,8 @@ end

function Base.getindex(expr::ExprGenerator, i::Integer)
idx = CartesianIndices(Base.OneTo.(_size(expr)))[i]
values = [expr.iterators[i].values[idx[i]] for i in eachindex(expr.iterators)]
values =
[expr.iterators[i].values[idx[i]] for i in eachindex(expr.iterators)]
return index_iterators(expr.expr.expr, values)
end

Expand Down
15 changes: 12 additions & 3 deletions src/MOI_wrapper.jl
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,10 @@ end
function MOI.Utilities.is_canonical(f::FunctionGenerator)
return MOI.Utilities.is_canonical(f.func)
end
function MOI.Utilities.is_coefficient_type(::Type{FunctionGenerator{E}}, ::Type{T}) where {E,T}
function MOI.Utilities.is_coefficient_type(
::Type{FunctionGenerator{E}},
::Type{T},
) where {E,T}
return MOI.Utilities.is_coefficient_type(E, T)
end

Expand All @@ -54,12 +57,18 @@ function Base.copy(f::SumGenerator{F}) where {F}
return SumGenerator{F}(copy(f.func), f.iterators)
end

function MOI.Utilities.map_indices(::MOI.Utilities.IndexMap, func::Union{FunctionGenerator,SumGenerator})
function MOI.Utilities.map_indices(
::MOI.Utilities.IndexMap,
func::Union{FunctionGenerator,SumGenerator},
)
# TODO check it's identity
return func
end

function MOI.Utilities.map_indices(::Function, func::Union{FunctionGenerator,SumGenerator})
function MOI.Utilities.map_indices(
::Function,
func::Union{FunctionGenerator,SumGenerator},
)
# TODO check it's identity
return func
end
Loading
Loading