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
33module StructJuMP
44
5+ using Compat
6+
57using JuMP # To reexport, should be using (not import)
68import MathProgBase
79import ReverseDiffSparse
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
3335end
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
5254end
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)
5961end
6062const dummy_mpi_wrapper = DummyMPIWrapper ()
6163
6264# Constructor with the number of scenarios
6365function 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
95100end
96101
97102# -------------
@@ -112,7 +117,7 @@ function getProcIdxSet(m::JuMP.Model)
112117 return getProcIdxSet (getStructure (m). mpi_wrapper, numScens)
113118end
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
0 commit comments