PropertyT.jl/src/SDPs.jl

80 lines
2.1 KiB
Julia
Raw Normal View History

2017-03-13 14:49:55 +01:00
using JuMP
import MathProgBase: AbstractMathProgSolver
function constraints_from_pm(pm, total_length=maximum(pm))
2017-03-13 14:49:55 +01:00
n = size(pm,1)
constraints = constraints = [Array{Int,1}[] for x in 1:total_length]
for j in 1:n
2017-03-14 16:42:04 +01:00
for i in 1:n
2017-03-13 14:49:55 +01:00
idx = pm[i,j]
push!(constraints[idx], [i,j])
end
end
return constraints
end
function splaplacian{TT<:Group}(RG::GroupRing{TT}, S, Id=RG.group(), T::Type=Float64)
result = RG(T)
result[Id] = T(length(S))
for s in S
result[s] -= one(T)
end
return result
end
function splaplacian{TT<:Ring}(RG::GroupRing{TT}, S, Id=one(RG.group), T::Type=Float64)
result = RG(T)
result[Id] = T(length(S))
2017-03-13 14:49:55 +01:00
for s in S
2017-06-06 11:51:15 +02:00
result[s] -= one(T)
2017-03-13 14:49:55 +01:00
end
return result
end
function create_SDP_problem(Δ::GroupRingElem, matrix_constraints; upper_bound=Inf)
N = size(parent(Δ).pm, 1)
2017-03-17 16:27:01 +01:00
Δ² = Δ*Δ
@assert length(Δ.coeffs) == length(matrix_constraints)
2017-03-13 14:49:55 +01:00
m = JuMP.Model();
JuMP.@variable(m, P[1:N, 1:N])
2017-04-01 15:21:57 +02:00
JuMP.@SDconstraint(m, P >= 0)
JuMP.@constraint(m, sum(P[i] for i in eachindex(P)) == 0)
2017-03-20 21:41:12 +01:00
2017-03-13 14:49:55 +01:00
if upper_bound < Inf
2017-04-01 15:21:57 +02:00
JuMP.@variable(m, 0.0 <= λ <= upper_bound)
2017-03-20 21:41:12 +01:00
else
2017-04-01 15:21:57 +02:00
JuMP.@variable(m, λ >= 0)
2017-03-13 14:49:55 +01:00
end
for (pairs, δ², δ) in zip(matrix_constraints, Δ².coeffs, Δ.coeffs)
2017-04-01 15:21:57 +02:00
JuMP.@constraint(m, sum(P[i,j] for (i,j) in pairs) == δ² - λ*δ)
2017-03-13 14:49:55 +01:00
end
2017-03-20 21:41:12 +01:00
2017-04-01 15:21:57 +02:00
JuMP.@objective(m, Max, λ)
2017-03-20 21:41:12 +01:00
return m, λ, P
2017-03-13 14:49:55 +01:00
end
2017-06-04 20:26:05 +02:00
function solve_SDP(SDP_problem)
2017-03-15 18:16:53 +01:00
info(logger, Base.repr(SDP_problem))
2017-03-13 14:49:55 +01:00
# to change buffering mode of stdout to _IOLBF (line bufferin)
# see https://github.com/JuliaLang/julia/issues/8765
ccall((:printf, "libc"), Int, (Ptr{UInt8},), "\n");
2017-03-20 22:12:10 +01:00
o = redirect_stdout(solver_logger.handlers["solver_log"].io)
2017-03-16 09:35:32 +01:00
2017-03-17 16:32:20 +01:00
t = @timed solution_status = JuMP.solve(SDP_problem)
info(logger, timed_msg(t))
Base.Libc.flush_cstdio()
2017-03-16 09:35:32 +01:00
redirect_stdout(o)
2017-03-13 14:49:55 +01:00
if solution_status != :Optimal
2017-03-15 17:52:22 +01:00
warn(logger, "The solver did not solve the problem successfully!")
2017-03-13 14:49:55 +01:00
end
info(logger, solution_status)
2017-03-13 14:49:55 +01:00
2017-06-04 20:26:05 +02:00
return 0
2017-03-13 14:49:55 +01:00
end