Skip to content

Commit

Permalink
Merge pull request #62 from StructJuMP/bl/j1
Browse files Browse the repository at this point in the history
Updates for Julia v1.0
  • Loading branch information
blegat authored Oct 9, 2018
2 parents 4f0c791 + 2ae5b6c commit e9c1c7c
Show file tree
Hide file tree
Showing 10 changed files with 73 additions and 61 deletions.
8 changes: 2 additions & 6 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ os:
- linux
- osx
julia:
- 0.5
- 0.6
- nightly
- 0.7
- 1.0
notifications:
email: false
sudo: false
Expand All @@ -15,9 +15,5 @@ addons:
- liblapack-dev
- libgmp-dev
- libglpk-dev
script:
- if [[ -a .git/shallow ]]; then git fetch --unshallow; fi
- julia -e 'Pkg.clone(pwd())'
- julia --check-bounds=yes -e 'Pkg.test("StructJuMP", coverage=true)'
after_success:
- julia -e 'cd(Pkg.dir("StructJuMP")); Pkg.add("Coverage"); using Coverage; Coveralls.submit(Coveralls.process_folder())'
5 changes: 3 additions & 2 deletions REQUIRE
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
julia 0.5
JuMP 0.17
julia 0.6
JuMP 0.18.3
MathProgBase
ReverseDiffSparse
Compat 1.0
13 changes: 8 additions & 5 deletions src/BendersBridge.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using JuMP
using StructJuMP
using Compat.SparseArrays
using Compat.LinearAlgebra

include("Benders_pmap.jl")

Expand Down Expand Up @@ -39,9 +41,9 @@ function conicconstraintdata(m::Model)
numRows = numLinRows + numBounds + numSOCRows + numSDPRows + numSymRows

# constr_to_row is not used but fill_bounds_constr! and fillconstr! for SDP needs them
constr_to_row = Array{Vector{Int}}(numBounds + 2*length(m.sdpconstr))
constr_to_row = Vector{Vector{Int}}(undef, numBounds + 2*length(m.sdpconstr))

b = Array{Float64}(numRows)
b = Vector{Float64}(undef, numRows)

I_m = Int[]
J_m = Int[]
Expand Down Expand Up @@ -93,7 +95,7 @@ function conicconstraintdata(m::Model)

# The conic MPB interface defines conic problems as
# always being minimization problems, so flip if needed
m.objSense == :Max && scale!(f_s, -1.0)
m.objSense == :Max && Compat.rmul!(f_s, -1.0)

if numMasterCols > 0
JuMP.rescaleSDcols!(spzeros(numMasterCols), J_m, V_m, parent)
Expand Down Expand Up @@ -191,8 +193,9 @@ function DLP(m::Model, solver)
A_dlp[rows:rows_end, cols:cols_end] = B_all[i-1]
end

append!(K_dlp, [(cone, rows-1 + idx) for (cone, idx) in K_all[i]])
append!(C_dlp, [(cone, cols-1 + idx) for (cone, idx) in C_all[i]])
append!(K_dlp, [(cone, rows-1 .+ idx) for (cone, idx) in K_all[i]])
append!(K_dlp, [(cone, rows-1 .+ idx) for (cone, idx) in K_all[i]])
append!(C_dlp, [(cone, cols-1 .+ idx) for (cone, idx) in C_all[i]])
rows = rows_end+1
cols = cols_end+1
end
Expand Down
31 changes: 16 additions & 15 deletions src/Benders_pmap.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
s.t. b_0 - A_0 x \in K_0,
b_i - A_i x - B_i y_i \in K_i, \forall i = 1,...,S
x \in C_0,
x_I \in Z
x_I \in Z
y_i \in C_i, \forall i = 1,...,S
where input to the Benders engine is:
Expand All @@ -20,18 +20,19 @@
call with: julia -p <num_threads> <myscript>
================================================================#
using JuMP
using Compat.Distributed

# this function loads and solves a conic problem and returns its dual
function loadAndSolveConicProblem(c, A, b, K, C, solver)

# load conic model
model = MathProgBase.ConicModel(solver)
MathProgBase.loadproblem!(model, c, A, b, K, C)
#println(model)

#println(model)
#println("process id $(myid()) started")
#@show c, A, b, K, C

# solve conic model
MathProgBase.optimize!(model)
status = MathProgBase.status(model)
Expand Down Expand Up @@ -69,7 +70,7 @@ function loadMasterProblem(c, A, b, K, C, v, num_scen, solver)
error("unrecognized cone $cone")
end
end
end
end
# add variable cones
for (cone, ind) in C
if cone == :Zero
Expand Down Expand Up @@ -105,7 +106,7 @@ function addCuttingPlanes(master_model, num_scen, A_all, b_all, output, x, θ, s
for i = 1:num_scen
#@show typeof(b_all[i+1])
coef = vec(output[i][2]' * A_all[i+1])
rhs = vecdot(output[i][2], b_all[i+1])
rhs = dot(output[i][2], b_all[i+1])
#@show size(coef*separator'), size(rhs)
# add an infeasibility cut
if output[i][1] == :Infeasible
Expand Down Expand Up @@ -159,22 +160,22 @@ function Benders_pmap(c_all, A_all, B_all, b_all, K_all, C_all, v, master_solver
separator = zeros(num_master_var)
for i = 1:num_master_var
separator[i] = getvalue(x[i])
end
end
new_rhs = [zeros(num_bins[i]) for i in 1:num_scen]
for i = 1:num_scen
new_rhs[i] = b_all[i+1] - A_all[i+1] * separator
end

output = pmap(loadAndSolveConicProblem,
[c_all[i+1] for i = 1:num_scen],
[B_all[i] for i = 1:num_scen],
[new_rhs[i] for i = 1:num_scen],
[K_all[i+1] for i = 1:num_scen],
[C_all[i+1] for i = 1:num_scen],
output = pmap(loadAndSolveConicProblem,
[c_all[i+1] for i = 1:num_scen],
[B_all[i] for i = 1:num_scen],
[new_rhs[i] for i = 1:num_scen],
[K_all[i+1] for i = 1:num_scen],
[C_all[i+1] for i = 1:num_scen],
[sub_solver for i = 1:num_scen])

#@show output
cut_added = addCuttingPlanes(master_model, num_scen, A_all, b_all, output, x, θ, separator, TOL)
end
return status, objval, separator
return status, objval, separator
end
49 changes: 27 additions & 22 deletions src/StructJuMP.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
__precompile__() # need to be commented for examples/PowerGrid to be run properly
VERSION < v"0.7.0-beta2.199" && __precompile__() # need to be commented for examples/PowerGrid to be run properly

module StructJuMP

using Compat

using JuMP # To reexport, should be using (not import)
import MathProgBase
import ReverseDiffSparse
Expand All @@ -23,56 +25,56 @@ end
# ---------------
# StructureData
# ---------------
type StructureData
probability::Dict{Int,Float64}
children::Dict{Int,JuMP.Model}
mutable struct StructureData
probability::Dict{Int, Float64}
children::Dict{Int, JuMP.Model}
parent
num_scen::Int
othermap::Dict{JuMP.Variable,JuMP.Variable}
othermap::Dict{JuMP.Variable, JuMP.Variable}
MPIWrapper # Empty unless StructJuMPwithMPI fills it
end
default_probability(m::JuMP.Model) = 1 / num_scenarios(m)
default_probability(::Void) = 1.0
default_probability(model::JuMP.Model) = inv(num_scenarios(model))
default_probability(::Nothing) = 1.0

# ---------------
# StructuredModel
# ---------------

function structprinthook(io::IO, m::Model)
print(io, m, ignore_print_hook=true)
function structprinthook(io::IO, model::Model)
print(io, model, ignore_print_hook=true)
print(io, "*** children ***\n")
# TODO it would be nice to indent all the children
# essentially wrap the IO object (subclass it) to add 4 spaces before each line
# this would then recursively be wrapped as more stages are added
for (id, mm) in getchildren(m)
@printf(io, "Child ID %d:\n", id)
print(io, mm)
for (id, child_model) in getchildren(model)
Compat.Printf.@printf(io, "Child ID %d:\n", id)
print(io, child_model)
print(io, "\n")
end
end

type DummyMPIWrapper
mutable struct DummyMPIWrapper
comm::Int
init::Function

DummyMPIWrapper() = new(-1,identity)
DummyMPIWrapper() = new(-1, identity)
end
const dummy_mpi_wrapper = DummyMPIWrapper()

# Constructor with the number of scenarios
function StructuredModel(;solver=JuMP.UnsetSolver(), parent=nothing, same_children_as=nothing, id=0, comm=nothing, num_scenarios::Int=0, prob::Float64=default_probability(parent), mpi_wrapper=dummy_mpi_wrapper)
_comm = (comm == nothing ? mpi_wrapper.comm : comm)
m = JuMP.Model(solver=solver)
model = JuMP.Model(solver=solver)
if parent === nothing
id = 0
mpi_wrapper.init(_comm)
if isdefined(:StructJuMPSolverInterface)
JuMP.setsolvehook(m,StructJuMPSolverInterface.sj_solve)
if isdefined(@__MODULE__, :StructJuMPSolverInterface)
JuMP.setsolvehook(model, StructJuMPSolverInterface.sj_solve)
end
else
@assert id != 0
stoch = getStructure(parent)
stoch.children[id] = m
stoch.children[id] = model
stoch.probability[id] = prob
end

Expand All @@ -86,12 +88,15 @@ function StructuredModel(;solver=JuMP.UnsetSolver(), parent=nothing, same_childr
probability = Dict{Int, Float64}()
children = Dict{Int, JuMP.Model}()
end
m.ext[:Stochastic] = StructureData(probability, children, parent, num_scenarios, Dict{JuMP.Variable,JuMP.Variable}(), mpi_wrapper)
model.ext[:Stochastic] = StructureData(probability, children, parent,
num_scenarios,
Dict{JuMP.Variable,JuMP.Variable}(),
mpi_wrapper)

# Printing children is important as well
JuMP.setprinthook(m, structprinthook)
JuMP.setprinthook(model, structprinthook)

m
return model
end

# -------------
Expand All @@ -112,7 +117,7 @@ function getProcIdxSet(m::JuMP.Model)
return getProcIdxSet(getStructure(m).mpi_wrapper, numScens)
end

macro second_stage(m,ind,code)
macro second_stage(m, ind,code)
return quote
proc_idx_set = getProcIdxSet($(esc(m)))
for $(esc(ind)) in proc_idx_set
Expand Down
2 changes: 1 addition & 1 deletion test/REQUIRE
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Cbc
ECOS
Clp
GLPKMathProgInterface
Ipopt
6 changes: 5 additions & 1 deletion test/benderstest.jl
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
using Compat
using Compat.LinearAlgebra # for norm
using Compat.Test

using JuMP
using StructJuMP

using ECOS
using Cbc
using Base.Test

misocp_solver = CbcSolver()
socp_solver = ECOS.ECOSSolver(verbose=false)
Expand Down
2 changes: 1 addition & 1 deletion test/examples_smoketest.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Base.Test
using Compat, Compat.Test


include("../examples/easy_test.jl")
Expand Down
11 changes: 6 additions & 5 deletions test/farmer.jl
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
using Base.Test
using Compat
using Compat.Test

using Clp
using GLPKMathProgInterface

@testset "farmer" begin
include("../examples/farmer.jl")
status, objval, soln = DLP(m, ClpSolver())
status, objval, soln = DLP(m, GLPKSolverLP())
@test status == :Optimal
@test objval == -108390
@test soln == [170, 80, 250]
@test objval -108390
@test soln [170, 80, 250]
end
7 changes: 4 additions & 3 deletions test/printhook.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using StructJuMP
using Base.Test
using Compat
using Compat.Test

using StructJuMP

@testset "printhook" begin

Expand All @@ -21,5 +22,5 @@ using Base.Test
end

str = string(m)
@test contains(str, "Child")
@test occursin("Child", str)
end

0 comments on commit e9c1c7c

Please sign in to comment.