Skip to content

Commit

Permalink
Add warm start (#1)
Browse files Browse the repository at this point in the history
* Add warm start to wrapper.

* Fix maybe functionality.

* Add tests warm start.

* Better testing warm start.

* Apply suggestions from code review

Co-Authored-By: Lorenzo Stella <lorenzostella@gmail.com>

* Fix random generation.
  • Loading branch information
WimVanRoy authored and lostella committed May 29, 2019
1 parent 41e61d0 commit cb2be6b
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 5 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
deps/lib/
deps/deps.jl
28 changes: 23 additions & 5 deletions src/wrappers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,24 @@ mutable struct Results
Results() = new(Float64[], Float64[], QPALM.Info(), Float64[], Float64[])
end

function solve!(model::QPALM.Model, results::QPALM.Results=Results())
function warm_start!(model::QPALM.Model;
x_warm_start::Maybe{Vector{Float64}} = nothing,
y_warm_start::Maybe{Vector{Float64}} = nothing)
if model.workspace == C_NULL
error("QPALM Model not setup")
end

ccall(
(:qpalm_warm_start, LIBQPALM_PATH),
Cvoid,
(Ptr{QPALM.Workspace}, Ptr{Float64}, Ptr{Float64}),
model.workspace,
x_warm_start != nothing ? pointer(x_warm_start) : C_NULL,
y_warm_start != nothing ? pointer(y_warm_start) : C_NULL,
)
end

function solve!(model::QPALM.Model, results::QPALM.Results=Results())::QPALM.Results
ccall(
(:qpalm_solve, LIBQPALM_PATH),
Cvoid,
Expand Down Expand Up @@ -272,11 +289,12 @@ function solve!(model::QPALM.Model, results::QPALM.Results=Results())
end
end

if results.info.status == :Non_convex
results.info.obj_val = NaN
end
# An objective value is not calculated at this moment.
# if results.info.status == :Non_convex
# results.info.obj_val = NaN
# end

results
return results
end

function cleanup!(model::QPALM.Model)
Expand Down
1 change: 1 addition & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
include("feasible.jl")
include("infeasible.jl")
include("warm_start.jl")
include("linear_programs.jl")
include("update.jl")
55 changes: 55 additions & 0 deletions test/warm_start.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
using Test

@testset "Warm start" begin

using QPALM
using LinearAlgebra
using SparseArrays
using Random

Random.seed!(0)

n, m = 10, 20
act = 5
F = randn(n, n-1)
Q = sparse(F*F' + 1e-3*I)
A = sparse(randn(m, n))
x_star = randn(n)
y_star = [rand(act); zeros(m-act)]
q = -Q*x_star - A'*y_star
b = [A[1:act, :]*x_star; A[act+1:end, :]*x_star + rand(m-act)]

model = QPALM.Model()
QPALM.setup!(model, Q=Q, q=q, A=A, bmax=b)
results = QPALM.solve!(model)
@test results.info.iter > 5

@testset "Not setup" begin
model = QPALM.Model()

@test_throws ErrorException QPALM.warm_start!(model, x_warm_start=results.x)
end

@testset "Normal usage (1)" begin
model = QPALM.Model()

QPALM.setup!(model, Q=Q, q=q, A=A, bmax=b)
QPALM.warm_start!(model, x_warm_start=results.x, y_warm_start=results.y)
res = QPALM.solve!(model)
@test res.info.status == :Solved
@test res.info.iter < results.info.iter
end

@testset "Normal usage (2)" begin
model = QPALM.Model()

QPALM.setup!(model, Q=Q, q=q, A=A, bmax=b)
QPALM.warm_start!(model, y_warm_start=results.y)
QPALM.warm_start!(model, x_warm_start=results.x)

res = QPALM.solve!(model)
@test res.info.status == :Solved
@test res.info.iter < results.info.iter
end

end

0 comments on commit cb2be6b

Please sign in to comment.