Skip to content

Commit 97e7ce7

Browse files
authored
Merge pull request #10 from tkoolen/tk/fix-bounds-modification
Don't modify input argument in bounds modification functions.
2 parents ac2359b + 24781f3 commit 97e7ce7

File tree

2 files changed

+26
-27
lines changed

2 files changed

+26
-27
lines changed

src/interface.jl

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,23 +13,17 @@ end
1313

1414
mutable struct Model
1515
workspace::Ptr{OSQP.Workspace}
16+
lcache::Vector{Float64} # to facilitate converting l to use OSQP_INFTY
17+
ucache::Vector{Float64} # to facilitate converting u to use OSQP_INFTY
1618

1719
"""
1820
Module()
1921
2022
Initialize OSQP module
2123
"""
2224
function Model()
23-
# TODO: Change me to more elegant way
24-
# a = Array{Ptr{OSQP.Workspace}}(1)[1]
25-
a = C_NULL
26-
27-
# Create new model
28-
model = new(a)
29-
30-
# Add finalizer
25+
model = new(C_NULL, Float64[], Float64[])
3126
@compat finalizer(OSQP.clean!, model)
32-
3327
return model
3428

3529
end
@@ -123,6 +117,10 @@ function setup!(model::OSQP.Model;
123117
u = min.(u, OSQP_INFTY)
124118
l = max.(l, -OSQP_INFTY)
125119

120+
# Resize caches
121+
resize!(model.lcache, m)
122+
resize!(model.ucache, m)
123+
126124
# Create managed matrices to avoid segfaults (See SCS.jl)
127125
managedP = OSQP.ManagedCcsc(P)
128126
managedA = OSQP.ManagedCcsc(A)
@@ -150,10 +148,8 @@ function setup!(model::OSQP.Model;
150148

151149
# Perform setup
152150
@compat_gc_preserve managedP Pdata managedA Adata q l u begin
153-
model.workspace = ccall((:osqp_setup, OSQP.osqp),
154-
Ptr{OSQP.Workspace}, (Ptr{OSQP.Data},
155-
Ptr{OSQP.Settings}),
156-
Ref(data), Ref(stgs))
151+
model.workspace = ccall((:osqp_setup, OSQP.osqp), Ptr{OSQP.Workspace},
152+
(Ptr{OSQP.Data}, Ptr{OSQP.Settings}), Ref(data), Ref(stgs))
157153
end
158154

159155
if model.workspace == C_NULL
@@ -231,8 +227,8 @@ function update_l!(model::OSQP.Model, l::Vector{Float64})
231227
if length(l) != m
232228
error("l must have length m = $(m)")
233229
end
234-
l .= max.(l, -OSQP_INFTY) # Convert values to OSQP_INFTY
235-
exitflag = ccall((:osqp_update_lower_bound, OSQP.osqp), Cc_int, (Ptr{OSQP.Workspace}, Ptr{Cdouble}), model.workspace, l)
230+
model.lcache .= max.(l, -OSQP_INFTY)
231+
exitflag = ccall((:osqp_update_lower_bound, OSQP.osqp), Cc_int, (Ptr{OSQP.Workspace}, Ptr{Cdouble}), model.workspace, model.lcache)
236232
if exitflag != 0 error("Error updating l") end
237233
end
238234

@@ -241,8 +237,8 @@ function update_u!(model::OSQP.Model, u::Vector{Float64})
241237
if length(u) != m
242238
error("u must have length m = $(m)")
243239
end
244-
u .= min.(u, OSQP_INFTY) # Convert values to OSQP_INFTY
245-
exitflag = ccall((:osqp_update_upper_bound, OSQP.osqp), Cc_int, (Ptr{OSQP.Workspace}, Ptr{Cdouble}), model.workspace, u)
240+
model.ucache .= min.(u, OSQP_INFTY)
241+
exitflag = ccall((:osqp_update_upper_bound, OSQP.osqp), Cc_int, (Ptr{OSQP.Workspace}, Ptr{Cdouble}), model.workspace, model.ucache)
246242
if exitflag != 0 error("Error updating u") end
247243
end
248244

@@ -254,7 +250,10 @@ function update_bounds!(model::OSQP.Model, l::Vector{Float64}, u::Vector{Float64
254250
if length(u) != m
255251
error("u must have length m = $(m)")
256252
end
257-
exitflag = ccall((:osqp_update_bounds, OSQP.osqp), Cc_int, (Ptr{OSQP.Workspace}, Ptr{Cdouble}, Ptr{Cdouble}), model.workspace, l, u)
253+
model.lcache .= max.(l, -OSQP_INFTY)
254+
model.ucache .= min.(u, OSQP_INFTY)
255+
exitflag = ccall((:osqp_update_bounds, OSQP.osqp), Cc_int, (Ptr{OSQP.Workspace}, Ptr{Cdouble}, Ptr{Cdouble}),
256+
model.workspace, model.lcache, model.ucache)
258257
if exitflag != 0 error("Error updating bounds l and u") end
259258
end
260259

test/runtests.jl

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@ using Compat.Test, Compat.SparseArrays, Compat.LinearAlgebra, Compat.Random
44

55

66
tests = [
7-
# "basic.jl",
8-
# "dual_infeasibility.jl",
9-
# "feasibility.jl",
10-
# "polishing.jl",
11-
# "primal_infeasibility.jl",
12-
# "unconstrained.jl",
13-
# "warm_start.jl",
14-
# "update_matrices.jl",
15-
# "mpbinterface.jl",
7+
"basic.jl",
8+
"dual_infeasibility.jl",
9+
"feasibility.jl",
10+
"polishing.jl",
11+
"primal_infeasibility.jl",
12+
"unconstrained.jl",
13+
"warm_start.jl",
14+
"update_matrices.jl",
15+
"mpbinterface.jl",
1616
"MathOptInterfaceOSQP.jl"
1717
]
1818

0 commit comments

Comments
 (0)