mirror of
https://github.com/kalmarek/PropertyT.jl.git
synced 2024-12-26 10:35:29 +01:00
move solve to a separate file
This commit is contained in:
parent
ca6a17acca
commit
35c5110a37
@ -15,6 +15,7 @@ import SymbolicWedderburn.PermutationGroups
|
||||
|
||||
include("constraint_matrix.jl")
|
||||
include("sos_sdps.jl")
|
||||
include("solve.jl")
|
||||
include("certify.jl")
|
||||
|
||||
include("sqadjop.jl")
|
||||
|
63
src/solve.jl
Normal file
63
src/solve.jl
Normal file
@ -0,0 +1,63 @@
|
||||
## Low-level solve
|
||||
|
||||
setwarmstart!(model::JuMP.Model, ::Nothing) = model
|
||||
|
||||
function setwarmstart!(model::JuMP.Model, warmstart)
|
||||
constraint_map = Dict(
|
||||
ct => JuMP.all_constraints(model, ct...) for
|
||||
ct in JuMP.list_of_constraint_types(model)
|
||||
)
|
||||
|
||||
JuMP.set_start_value.(JuMP.all_variables(model), warmstart.primal)
|
||||
|
||||
for (ct, idx) in pairs(constraint_map)
|
||||
JuMP.set_start_value.(idx, warmstart.slack[ct])
|
||||
JuMP.set_dual_start_value.(idx, warmstart.dual[ct])
|
||||
end
|
||||
return model
|
||||
end
|
||||
|
||||
function getwarmstart(model::JuMP.Model)
|
||||
constraint_map = Dict(
|
||||
ct => JuMP.all_constraints(model, ct...) for
|
||||
ct in JuMP.list_of_constraint_types(model)
|
||||
)
|
||||
|
||||
primal = value.(JuMP.all_variables(model))
|
||||
|
||||
slack = Dict(k => value.(v) for (k, v) in constraint_map)
|
||||
duals = Dict(k => JuMP.dual.(v) for (k, v) in constraint_map)
|
||||
|
||||
return (primal=primal, dual=duals, slack=slack)
|
||||
end
|
||||
|
||||
function solve(m::JuMP.Model, optimizer, warmstart=nothing)
|
||||
|
||||
JuMP.set_optimizer(m, optimizer)
|
||||
MOIU.attach_optimizer(m)
|
||||
|
||||
m = setwarmstart!(m, warmstart)
|
||||
|
||||
JuMP.optimize!(m)
|
||||
Base.Libc.flush_cstdio()
|
||||
|
||||
status = JuMP.termination_status(m)
|
||||
|
||||
return status, getwarmstart(m)
|
||||
end
|
||||
|
||||
function solve(solverlog::String, m::JuMP.Model, optimizer, warmstart=nothing)
|
||||
|
||||
isdir(dirname(solverlog)) || mkpath(dirname(solverlog))
|
||||
|
||||
Base.flush(Base.stdout)
|
||||
Base.Libc.flush_cstdio()
|
||||
status, warmstart = open(solverlog, "a+") do logfile
|
||||
redirect_stdout(logfile) do
|
||||
status, warmstart = solve(m, optimizer, warmstart)
|
||||
status, warmstart
|
||||
end
|
||||
end
|
||||
|
||||
return status, warmstart
|
||||
end
|
@ -326,67 +326,3 @@ function reconstruct!(
|
||||
return res
|
||||
end
|
||||
|
||||
##
|
||||
# Low-level solve
|
||||
|
||||
setwarmstart!(model::JuMP.Model, ::Nothing) = model
|
||||
|
||||
function setwarmstart!(model::JuMP.Model, warmstart)
|
||||
constraint_map = Dict(
|
||||
ct => JuMP.all_constraints(model, ct...) for
|
||||
ct in JuMP.list_of_constraint_types(model)
|
||||
)
|
||||
|
||||
JuMP.set_start_value.(JuMP.all_variables(model), warmstart.primal)
|
||||
|
||||
for (ct, idx) in pairs(constraint_map)
|
||||
JuMP.set_start_value.(idx, warmstart.slack[ct])
|
||||
JuMP.set_dual_start_value.(idx, warmstart.dual[ct])
|
||||
end
|
||||
return model
|
||||
end
|
||||
|
||||
function getwarmstart(model::JuMP.Model)
|
||||
constraint_map = Dict(
|
||||
ct => JuMP.all_constraints(model, ct...) for
|
||||
ct in JuMP.list_of_constraint_types(model)
|
||||
)
|
||||
|
||||
primal = value.(JuMP.all_variables(model))
|
||||
|
||||
slack = Dict(k => value.(v) for (k, v) in constraint_map)
|
||||
duals = Dict(k => JuMP.dual.(v) for (k, v) in constraint_map)
|
||||
|
||||
return (primal=primal, dual=duals, slack=slack)
|
||||
end
|
||||
|
||||
function solve(m::JuMP.Model, optimizer, warmstart=nothing)
|
||||
|
||||
JuMP.set_optimizer(m, optimizer)
|
||||
MOIU.attach_optimizer(m)
|
||||
|
||||
m = setwarmstart!(m, warmstart)
|
||||
|
||||
JuMP.optimize!(m)
|
||||
Base.Libc.flush_cstdio()
|
||||
|
||||
status = JuMP.termination_status(m)
|
||||
|
||||
return status, getwarmstart(m)
|
||||
end
|
||||
|
||||
function solve(solverlog::String, m::JuMP.Model, optimizer, warmstart=nothing)
|
||||
|
||||
isdir(dirname(solverlog)) || mkpath(dirname(solverlog))
|
||||
|
||||
Base.flush(Base.stdout)
|
||||
Base.Libc.flush_cstdio()
|
||||
status, warmstart = open(solverlog, "a+") do logfile
|
||||
redirect_stdout(logfile) do
|
||||
status, warmstart = solve(m, optimizer, warmstart)
|
||||
status, warmstart
|
||||
end
|
||||
end
|
||||
|
||||
return status, warmstart
|
||||
end
|
||||
|
Loading…
Reference in New Issue
Block a user