From 0d17e4b877f3ca1708cdf3cdeb3b61c9d189fe8e Mon Sep 17 00:00:00 2001 From: kalmarek Date: Tue, 2 Jan 2018 02:52:45 +0100 Subject: [PATCH] rework lambdaandP && solve_SDP --- src/PropertyT.jl | 53 ++++++++++++++---------------------------------- src/SDPs.jl | 32 +++++++++++++++++------------ 2 files changed, 34 insertions(+), 51 deletions(-) diff --git a/src/PropertyT.jl b/src/PropertyT.jl index 9638d0b..46607bb 100644 --- a/src/PropertyT.jl +++ b/src/PropertyT.jl @@ -126,60 +126,37 @@ function λandP(name::String) return λ, P end -function λandP(name::String, SDP_problem::JuMP.Model, varλ, varP, warmstart=false) +function λandP(name::String, SDP::JuMP.Model, varλ, varP, warmstart=true) - handler = DefaultHandler( - joinpath(name, "solver_$(string(now())).log"), - DefaultFormatter("{date}| {msg}") - ) - handler.levels.x = LOGGER_SOLVER.levels - LOGGER_SOLVER.handlers["solver_log"] = handler - - if warmstart && isfile(joinpath(name, "warmstart.jld")) - ws = load(joinpath(name, "warmstart.jld"), "warmstart") + if warmstart && isfile(filename(name, :warm)) + ws = load(filename(name, :warm), "warmstart") else ws = nothing end - λ, P, warmstart = compute_λandP(SDP_problem, varλ, varP, warmstart=ws) + solver_log = solverlogger(name) - delete!(LOGGER_SOLVER.handlers, "solver_log") + Base.Libc.flush_cstdio() + o = redirect_stdout(solver_log.handlers["solver_log"].io) + Base.Libc.flush_cstdio() + + λ, P, warmstart = solve_SDP(SDP, varλ, varP, warmstart=ws) + + Base.Libc.flush_cstdio() + redirect_stdout(o) + + delete!(solver_log.handlers, "solver_log") if λ > 0 save(filename(name, :λ), "λ", λ) save(filename(name, :P), "P", P) - save(joinpath(name, "warmstart.jld"), "warmstart", warmstart) + save(filename(name, :warm), "warmstart", warmstart) else throw(ErrorException("Solver did not produce a valid solution: λ = $λ")) end return λ, P end -function compute_λandP(m, varλ, varP; warmstart=nothing) - λ = 0.0 - P = nothing - - traits = JuMP.ProblemTraits(m, relaxation=false) - - JuMP.build(m, traits=traits) - if warmstart != nothing - p_sol, d_sol, s = warmstart - MathProgBase.SolverInterface.setwarmstart!(m.internalModel, p_sol; dual_sol = d_sol, slack=s); - end - solve_SDP(m) - λ = MathProgBase.getobjval(m.internalModel) - - warmstart = (m.internalModel.primal_sol, m.internalModel.dual_sol, - m.internalModel.slack) - - fillfrominternal!(m, traits) - - P = JuMP.getvalue(varP) - λ = JuMP.getvalue(varλ) - - return λ, P, warmstart -end - Kazhdan_from_sgap(λ,N) = sqrt(2*λ/N) function check_property_T(name::String, S, Id, solver, upper_bound, tol, radius) diff --git a/src/SDPs.jl b/src/SDPs.jl index c8439f0..9c9275e 100644 --- a/src/SDPs.jl +++ b/src/SDPs.jl @@ -55,23 +55,29 @@ function create_SDP_problem(Δ::GroupRingElem, matrix_constraints; upper_bound=I return m, λ, P end -function solve_SDP(SDP_problem) - info(LOGGER, Base.repr(SDP_problem)) +function solve_SDP(m, varλ, varP; warmstart=nothing) - o = redirect_stdout(LOGGER_SOLVER.handlers["solver_log"].io) - Base.Libc.flush_cstdio() + traits = JuMP.ProblemTraits(m, relaxation=false) - @logtime LOGGER solution_status = MathProgBase.optimize!(SDP_problem.internalModel) - Base.Libc.flush_cstdio() - - redirect_stdout(o) - - if solution_status != :Optimal - warn(LOGGER, "The solver did not solve the problem successfully!") + JuMP.build(m, traits=traits) + if warmstart != nothing + p_sol, d_sol, s = warmstart + MathProgBase.SolverInterface.setwarmstart!(m.internalModel, p_sol; dual_sol = d_sol, slack=s); end - info(LOGGER, solution_status) - return 0 + MathProgBase.optimize!(m.internalModel) + + λ = MathProgBase.getobjval(m.internalModel) + + warmstart = (m.internalModel.primal_sol, m.internalModel.dual_sol, + m.internalModel.slack) + + fillfrominternal!(m, traits) + + P = JuMP.getvalue(varP) + λ = JuMP.getvalue(varλ) + + return λ, P, warmstart end function fillfrominternal!(m::JuMP.Model, traits)