Skip to content

Commit

Permalink
Fix type inconsistency in LDL factorization (#76)
Browse files Browse the repository at this point in the history
* Generic eltype for S matrix in LDL factorization
* Add example validating Float32
  • Loading branch information
matbesancon authored Dec 26, 2020
1 parent 039132b commit 5066f8b
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 2 deletions.
50 changes: 50 additions & 0 deletions examples/optimal_other_type.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
using Tulip
import MathOptInterface
const MOI = MathOptInterface
using Test

const T = Float32

lp = Tulip.Optimizer{T}()

# Create variables
x = MOI.add_variable(lp)
y = MOI.add_variable(lp)

# Set variable bounds
MOI.add_constraint(lp, MOI.SingleVariable(x), MOI.GreaterThan(T(0))) # x >= 0
MOI.add_constraint(lp, MOI.SingleVariable(y), MOI.GreaterThan(T(0))) # y >= 0

# Add constraints
row1 = MOI.add_constraint(lp,
MOI.ScalarAffineFunction(MOI.ScalarAffineTerm.(T[1.0, -1.0], [x, y]), T(0)),
MOI.GreaterThan(T(-2))
)
row2 = MOI.add_constraint(lp,
MOI.ScalarAffineFunction(MOI.ScalarAffineTerm.(T[2.0, -1.0], [x, y]), T(0)),
MOI.LessThan(T(4))
)
row3 = MOI.add_constraint(lp,
MOI.ScalarAffineFunction(MOI.ScalarAffineTerm.(T[1.0, 2.0], [x, y]), T(0)),
MOI.LessThan(T(7))
)

# Set the objective
MOI.set(lp,
MOI.ObjectiveFunction{MOI.ScalarAffineFunction{Float32}}(),
MOI.ScalarAffineFunction(MOI.ScalarAffineTerm.(T[-2.0, -1.0], [x, y]), T(0))
)
MOI.set(lp, MOI.ObjectiveSense(), MOI.MIN_SENSE)

MOI.optimize!(lp)

objval = MOI.get(lp, MOI.ObjectiveValue())
x_ = MOI.get(lp, MOI.VariablePrimal(), x)
y_ = MOI.get(lp, MOI.VariablePrimal(), y)

@test objval -8
@test x_ 3
@test y_ 2
@test objval isa Float32
@test x_ isa Float32
@test y_ isa Float32
2 changes: 1 addition & 1 deletion src/KKT/ldlfact.jl
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ mutable struct LDLFactSQD{T<:Real} <: AbstractKKTSolver{T}

S = [
spdiagm(0 => -θ) A';
spzeros(T, m, n) spdiagm(0 => ones(m))
spzeros(T, m, n) spdiagm(0 => ones(T, m))
]

# TODO: PSD-ness checks
Expand Down
4 changes: 4 additions & 0 deletions test/examples.jl
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,7 @@ end
@testset "$T" begin ex_unbounded(T, OutputLevel=0) end
end
end

@testset "Optimal Float32" begin
include(joinpath(examples_dir, "optimal_other_type.jl"))
end
2 changes: 1 addition & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ TLP = Tulip

import Convex

const TvTYPES = [Float64, BigFloat]
const TvTYPES = [Float32, Float64, BigFloat]

# write your own tests here
const testdir = dirname(@__FILE__)
Expand Down

0 comments on commit 5066f8b

Please sign in to comment.