Skip to content

Commit e9c1c7c

Browse files
authored
Merge pull request #62 from StructJuMP/bl/j1
Updates for Julia v1.0
2 parents 4f0c791 + 2ae5b6c commit e9c1c7c

File tree

10 files changed

+73
-61
lines changed

10 files changed

+73
-61
lines changed

.travis.yml

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ os:
33
- linux
44
- osx
55
julia:
6-
- 0.5
76
- 0.6
8-
- nightly
7+
- 0.7
8+
- 1.0
99
notifications:
1010
email: false
1111
sudo: false
@@ -15,9 +15,5 @@ addons:
1515
- liblapack-dev
1616
- libgmp-dev
1717
- libglpk-dev
18-
script:
19-
- if [[ -a .git/shallow ]]; then git fetch --unshallow; fi
20-
- julia -e 'Pkg.clone(pwd())'
21-
- julia --check-bounds=yes -e 'Pkg.test("StructJuMP", coverage=true)'
2218
after_success:
2319
- julia -e 'cd(Pkg.dir("StructJuMP")); Pkg.add("Coverage"); using Coverage; Coveralls.submit(Coveralls.process_folder())'

REQUIRE

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
julia 0.5
2-
JuMP 0.17
1+
julia 0.6
2+
JuMP 0.18.3
33
MathProgBase
44
ReverseDiffSparse
5+
Compat 1.0

src/BendersBridge.jl

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
using JuMP
22
using StructJuMP
3+
using Compat.SparseArrays
4+
using Compat.LinearAlgebra
35

46
include("Benders_pmap.jl")
57

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

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

44-
b = Array{Float64}(numRows)
46+
b = Vector{Float64}(undef, numRows)
4547

4648
I_m = Int[]
4749
J_m = Int[]
@@ -93,7 +95,7 @@ function conicconstraintdata(m::Model)
9395

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

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

194-
append!(K_dlp, [(cone, rows-1 + idx) for (cone, idx) in K_all[i]])
195-
append!(C_dlp, [(cone, cols-1 + idx) for (cone, idx) in C_all[i]])
196+
append!(K_dlp, [(cone, rows-1 .+ idx) for (cone, idx) in K_all[i]])
197+
append!(K_dlp, [(cone, rows-1 .+ idx) for (cone, idx) in K_all[i]])
198+
append!(C_dlp, [(cone, cols-1 .+ idx) for (cone, idx) in C_all[i]])
196199
rows = rows_end+1
197200
cols = cols_end+1
198201
end

src/Benders_pmap.jl

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
s.t. b_0 - A_0 x \in K_0,
66
b_i - A_i x - B_i y_i \in K_i, \forall i = 1,...,S
77
x \in C_0,
8-
x_I \in Z
8+
x_I \in Z
99
y_i \in C_i, \forall i = 1,...,S
1010
1111
where input to the Benders engine is:
@@ -20,18 +20,19 @@
2020
call with: julia -p <num_threads> <myscript>
2121
================================================================#
2222
using JuMP
23+
using Compat.Distributed
2324

2425
# this function loads and solves a conic problem and returns its dual
2526
function loadAndSolveConicProblem(c, A, b, K, C, solver)
26-
27+
2728
# load conic model
2829
model = MathProgBase.ConicModel(solver)
2930
MathProgBase.loadproblem!(model, c, A, b, K, C)
30-
31-
#println(model)
31+
32+
#println(model)
3233
#println("process id $(myid()) started")
3334
#@show c, A, b, K, C
34-
35+
3536
# solve conic model
3637
MathProgBase.optimize!(model)
3738
status = MathProgBase.status(model)
@@ -69,7 +70,7 @@ function loadMasterProblem(c, A, b, K, C, v, num_scen, solver)
6970
error("unrecognized cone $cone")
7071
end
7172
end
72-
end
73+
end
7374
# add variable cones
7475
for (cone, ind) in C
7576
if cone == :Zero
@@ -105,7 +106,7 @@ function addCuttingPlanes(master_model, num_scen, A_all, b_all, output, x, θ, s
105106
for i = 1:num_scen
106107
#@show typeof(b_all[i+1])
107108
coef = vec(output[i][2]' * A_all[i+1])
108-
rhs = vecdot(output[i][2], b_all[i+1])
109+
rhs = dot(output[i][2], b_all[i+1])
109110
#@show size(coef*separator'), size(rhs)
110111
# add an infeasibility cut
111112
if output[i][1] == :Infeasible
@@ -159,22 +160,22 @@ function Benders_pmap(c_all, A_all, B_all, b_all, K_all, C_all, v, master_solver
159160
separator = zeros(num_master_var)
160161
for i = 1:num_master_var
161162
separator[i] = getvalue(x[i])
162-
end
163+
end
163164
new_rhs = [zeros(num_bins[i]) for i in 1:num_scen]
164165
for i = 1:num_scen
165166
new_rhs[i] = b_all[i+1] - A_all[i+1] * separator
166167
end
167168

168-
output = pmap(loadAndSolveConicProblem,
169-
[c_all[i+1] for i = 1:num_scen],
170-
[B_all[i] for i = 1:num_scen],
171-
[new_rhs[i] for i = 1:num_scen],
172-
[K_all[i+1] for i = 1:num_scen],
173-
[C_all[i+1] for i = 1:num_scen],
169+
output = pmap(loadAndSolveConicProblem,
170+
[c_all[i+1] for i = 1:num_scen],
171+
[B_all[i] for i = 1:num_scen],
172+
[new_rhs[i] for i = 1:num_scen],
173+
[K_all[i+1] for i = 1:num_scen],
174+
[C_all[i+1] for i = 1:num_scen],
174175
[sub_solver for i = 1:num_scen])
175176

176177
#@show output
177178
cut_added = addCuttingPlanes(master_model, num_scen, A_all, b_all, output, x, θ, separator, TOL)
178179
end
179-
return status, objval, separator
180+
return status, objval, separator
180181
end

src/StructJuMP.jl

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
__precompile__() # need to be commented for examples/PowerGrid to be run properly
1+
VERSION < v"0.7.0-beta2.199" && __precompile__() # need to be commented for examples/PowerGrid to be run properly
22

33
module StructJuMP
44

5+
using Compat
6+
57
using JuMP # To reexport, should be using (not import)
68
import MathProgBase
79
import ReverseDiffSparse
@@ -23,56 +25,56 @@ end
2325
# ---------------
2426
# StructureData
2527
# ---------------
26-
type StructureData
27-
probability::Dict{Int,Float64}
28-
children::Dict{Int,JuMP.Model}
28+
mutable struct StructureData
29+
probability::Dict{Int, Float64}
30+
children::Dict{Int, JuMP.Model}
2931
parent
3032
num_scen::Int
31-
othermap::Dict{JuMP.Variable,JuMP.Variable}
33+
othermap::Dict{JuMP.Variable, JuMP.Variable}
3234
MPIWrapper # Empty unless StructJuMPwithMPI fills it
3335
end
34-
default_probability(m::JuMP.Model) = 1 / num_scenarios(m)
35-
default_probability(::Void) = 1.0
36+
default_probability(model::JuMP.Model) = inv(num_scenarios(model))
37+
default_probability(::Nothing) = 1.0
3638

3739
# ---------------
3840
# StructuredModel
3941
# ---------------
4042

41-
function structprinthook(io::IO, m::Model)
42-
print(io, m, ignore_print_hook=true)
43+
function structprinthook(io::IO, model::Model)
44+
print(io, model, ignore_print_hook=true)
4345
print(io, "*** children ***\n")
4446
# TODO it would be nice to indent all the children
4547
# essentially wrap the IO object (subclass it) to add 4 spaces before each line
4648
# this would then recursively be wrapped as more stages are added
47-
for (id, mm) in getchildren(m)
48-
@printf(io, "Child ID %d:\n", id)
49-
print(io, mm)
49+
for (id, child_model) in getchildren(model)
50+
Compat.Printf.@printf(io, "Child ID %d:\n", id)
51+
print(io, child_model)
5052
print(io, "\n")
5153
end
5254
end
5355

54-
type DummyMPIWrapper
56+
mutable struct DummyMPIWrapper
5557
comm::Int
5658
init::Function
5759

58-
DummyMPIWrapper() = new(-1,identity)
60+
DummyMPIWrapper() = new(-1, identity)
5961
end
6062
const dummy_mpi_wrapper = DummyMPIWrapper()
6163

6264
# Constructor with the number of scenarios
6365
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)
6466
_comm = (comm == nothing ? mpi_wrapper.comm : comm)
65-
m = JuMP.Model(solver=solver)
67+
model = JuMP.Model(solver=solver)
6668
if parent === nothing
6769
id = 0
6870
mpi_wrapper.init(_comm)
69-
if isdefined(:StructJuMPSolverInterface)
70-
JuMP.setsolvehook(m,StructJuMPSolverInterface.sj_solve)
71+
if isdefined(@__MODULE__, :StructJuMPSolverInterface)
72+
JuMP.setsolvehook(model, StructJuMPSolverInterface.sj_solve)
7173
end
7274
else
7375
@assert id != 0
7476
stoch = getStructure(parent)
75-
stoch.children[id] = m
77+
stoch.children[id] = model
7678
stoch.probability[id] = prob
7779
end
7880

@@ -86,12 +88,15 @@ function StructuredModel(;solver=JuMP.UnsetSolver(), parent=nothing, same_childr
8688
probability = Dict{Int, Float64}()
8789
children = Dict{Int, JuMP.Model}()
8890
end
89-
m.ext[:Stochastic] = StructureData(probability, children, parent, num_scenarios, Dict{JuMP.Variable,JuMP.Variable}(), mpi_wrapper)
91+
model.ext[:Stochastic] = StructureData(probability, children, parent,
92+
num_scenarios,
93+
Dict{JuMP.Variable,JuMP.Variable}(),
94+
mpi_wrapper)
9095

9196
# Printing children is important as well
92-
JuMP.setprinthook(m, structprinthook)
97+
JuMP.setprinthook(model, structprinthook)
9398

94-
m
99+
return model
95100
end
96101

97102
# -------------
@@ -112,7 +117,7 @@ function getProcIdxSet(m::JuMP.Model)
112117
return getProcIdxSet(getStructure(m).mpi_wrapper, numScens)
113118
end
114119

115-
macro second_stage(m,ind,code)
120+
macro second_stage(m, ind,code)
116121
return quote
117122
proc_idx_set = getProcIdxSet($(esc(m)))
118123
for $(esc(ind)) in proc_idx_set

test/REQUIRE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
Cbc
22
ECOS
3-
Clp
3+
GLPKMathProgInterface
44
Ipopt

test/benderstest.jl

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
1+
using Compat
2+
using Compat.LinearAlgebra # for norm
3+
using Compat.Test
4+
15
using JuMP
26
using StructJuMP
7+
38
using ECOS
49
using Cbc
5-
using Base.Test
610

711
misocp_solver = CbcSolver()
812
socp_solver = ECOS.ECOSSolver(verbose=false)

test/examples_smoketest.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using Base.Test
1+
using Compat, Compat.Test
22

33

44
include("../examples/easy_test.jl")

test/farmer.jl

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
using Base.Test
1+
using Compat
2+
using Compat.Test
23

3-
using Clp
4+
using GLPKMathProgInterface
45

56
@testset "farmer" begin
67
include("../examples/farmer.jl")
7-
status, objval, soln = DLP(m, ClpSolver())
8+
status, objval, soln = DLP(m, GLPKSolverLP())
89
@test status == :Optimal
9-
@test objval == -108390
10-
@test soln == [170, 80, 250]
10+
@test objval -108390
11+
@test soln [170, 80, 250]
1112
end

test/printhook.jl

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
using StructJuMP
2-
using Base.Test
1+
using Compat
2+
using Compat.Test
33

4+
using StructJuMP
45

56
@testset "printhook" begin
67

@@ -21,5 +22,5 @@ using Base.Test
2122
end
2223

2324
str = string(m)
24-
@test contains(str, "Child")
25+
@test occursin("Child", str)
2526
end

0 commit comments

Comments
 (0)