diff --git a/Project.toml b/Project.toml index 12df826..01b5448 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "StructJuMP" uuid = "34f15cae-5318-50c9-93d3-9feadd34e321" repo = "https://github.com/StructJuMP/StructJuMP.jl.git" -version = "0.2.0" +version = "0.3.0" [deps] JuMP = "4076af6c-e467-56ae-b986-b466b2749572" @@ -10,10 +10,10 @@ MathOptInterface = "b8f27783-ece8-5eb3-8dc8-9495eed66fee" ParameterJuMP = "774612a8-9878-5177-865a-ca53ae2495f9" [compat] -JuMP = "0.21.3" -MathOptInterface = "0.9.14" -ParameterJuMP = "0.2" -julia = "1" +JuMP = "0.23, 1" +MathOptInterface = "1" +ParameterJuMP = "0.4.1" +julia = "1.6" [extras] ECOS = "e2685f51-7e38-5353-a97d-a921fd2c8199" diff --git a/examples/dcap.jl b/examples/dcap.jl new file mode 100644 index 0000000..f45612f --- /dev/null +++ b/examples/dcap.jl @@ -0,0 +1,47 @@ +using StructJuMP +using Random + +function DCAP(nR::Int, nN::Int, nT::Int, nS::Int, seed::Int=1)::StructuredModel + + # set random seed (default=1) + Random.seed!(seed) + + # generate & store instance data + ## sets + R = 1:nR + N = 1:nN + T = 1:nT + S = 1:nS + + ## parameters + a = rand(nR, nT) * 5 .+ 5 + b = rand(nR, nT) * 40 .+ 10 + c = rand(nR, nN, nT, nS) * 5 .+ 5 + c0 = rand(nN, nT, nS) * 500 .+ 500 + d = rand(nN, nT, nS) .+ 0.5 + Pr = ones(nS)/nS + + # construct JuMP.Model + model = StructuredModel(num_scenarios = nS) + + ## 1st stage + @variable(model, x[i=R,t=T] >= 0) + @variable(model, u[i=R,t=T], Bin) + @objective(model, Min, sum(a[i,t]*x[i,t] + b[i,t]*u[i,t] for i in R for t in T)) + @constraint(model, [i=R,t=T], x[i,t] - u[i,t] <= 0) + + ## 2nd stage + for s in S + sb = StructuredModel(parent=model, id = s, prob = Pr[s]) + # @variable(sb, y[i=R, j=N, t=T], Bin) + @variable(sb, 0 <= y[i=R, j=N, t=T] <= 1) + #@variable(sb, z[j=N,t=T] >= 0) # originally implemented variable (continuous) + # @variable(sb, z[j=N,t=T], Bin) # modify as SIPLIB 1.0 + @variable(sb, 0 <= z[j=N,t=T] <= 1) + @objective(sb, Min, sum(c[i,j,t,s]*y[i,j,t] for i in R for j in N for t in T) + sum(c0[j,t,s]*z[j,t] for j in N for t in T)) + @constraint(sb, [i=R, t=T], -sum(x[i,tau] for tau in 1:t) + sum(d[j,t,s]*y[i,j,t] for j in N) <= 0) + @constraint(sb, [j=N, t=T], sum(y[i,j,t] for i in R) + z[j,t] == 1) + end + + return model +end \ No newline at end of file diff --git a/examples/parmodel1.jl b/examples/parmodel1.jl index de86b48..632ea40 100644 --- a/examples/parmodel1.jl +++ b/examples/parmodel1.jl @@ -6,7 +6,7 @@ firststage = StructuredModel() @constraint(firststage, x[1] * x[2] == 10) for scen in 1:2 - bl = StructuredModel(parent=firststage, id=scen) + local bl = StructuredModel(parent=firststage, id=scen) @variable(bl, y) @constraint(bl, x[2]^2 + x[1]*y ≤ 5) @objective(bl, Min, (x[1]+x[2])*y) diff --git a/examples/parmodel2.jl b/examples/parmodel2.jl index 7c024a7..0c364b4 100644 --- a/examples/parmodel2.jl +++ b/examples/parmodel2.jl @@ -6,7 +6,7 @@ firststage = StructuredModel() @objective(firststage, Min, x[1]^2 + x[2]^2) for scen in 1:2 - bl = StructuredModel(parent=firststage, id=scen) + local bl = StructuredModel(parent=firststage, id=scen) @variable(bl, y[1:2]) @constraint(bl, x[3-scen] + sum(y) ≥ 0) @constraint(bl, x[3-scen] + sum(y) ≤ 50) diff --git a/examples/parmodel3.jl b/examples/parmodel3.jl index e99bc1f..1541abe 100644 --- a/examples/parmodel3.jl +++ b/examples/parmodel3.jl @@ -6,7 +6,7 @@ firststage = StructuredModel() @objective(firststage, Min, x[1]^2 + x[2]^2 + x[1]*x[2]) for scen in 1:2 - bl = StructuredModel(parent=firststage, id=scen) + local bl = StructuredModel(parent=firststage, id=scen) @variable(bl, y[1:2]) @constraint(bl, x[3-scen] + sum(y) ≥ 0) @constraint(bl, x[3-scen] + sum(y) ≤ 50) diff --git a/examples/parmodel4.jl b/examples/parmodel4.jl index fba731b..3330ca6 100644 --- a/examples/parmodel4.jl +++ b/examples/parmodel4.jl @@ -6,7 +6,7 @@ firststage = StructuredModel() @objective(firststage, Min, x[1]^2 + x[2]^2 + x[1]*x[2]) for scen in 1:100 - bl = StructuredModel(parent=firststage, id=scen) + local bl = StructuredModel(parent=firststage, id=scen) @variable(bl, y[1:2]) idx = (isodd(scen) ? 1 : 2) @constraint(bl, x[idx] + sum(y) ≥ 0) diff --git a/examples/transportation.jl b/examples/transportation.jl index 071e6b0..1a8b03f 100644 --- a/examples/transportation.jl +++ b/examples/transportation.jl @@ -40,7 +40,7 @@ for i in factories end for (s, elem) in enumerate(scenarios) - bl = StructuredModel(parent=m, id=s) + local bl = StructuredModel(parent=m, id=s) @variable(bl, 0 <= salesw[i=centers] <= demand[i,s]) @variable(bl, wastew[centers] >= 0) @objective(bl, Max, sum(price*prob[s]*salesw[j] for j=centers) - sum(wastecost*prob[s]*wastew[j] for j=centers)) diff --git a/src/BendersBridge.jl b/src/BendersBridge.jl index 7c5ed37..61bdd40 100644 --- a/src/BendersBridge.jl +++ b/src/BendersBridge.jl @@ -82,11 +82,7 @@ struct ParametrizedModel θ::Dict{Int, JuMP.VariableRef} end function ParametrizedModel(structured_model::StructuredModel, args...; kwargs...) - if structured_model.parent === nothing - model = Model(args...; kwargs...) - else - model = ModelWithParams(args...; kwargs...) - end + model = Model(args...; kwargs...) variable_map = Dict{Int, JuMP.VariableRef}() for (index, var) in structured_model.variables name = structured_model.varnames[index] diff --git a/src/Benders_pmap.jl b/src/Benders_pmap.jl index 4e1e3aa..7aaecd7 100644 --- a/src/Benders_pmap.jl +++ b/src/Benders_pmap.jl @@ -47,7 +47,7 @@ function set_parent_solution!(model::ParametrizedModel, parent::ParametrizedMode for (index, parameter) in model.parameter_map vref = parent.variable_map[index] value = parent_solution.variable_value[vref] - JuMP.fix(parameter, value) + JuMP.set_value(parameter, value) end end @@ -66,7 +66,7 @@ function add_cutting_planes(master_model, master_solution, sub_models, sub_solut if sol.feasible JuMP.add_to_expression!(aff, -1.0, master_model.θ[id]) # Check if the cut is useful - if JuMP.value(aff, vref -> master_solution.variable_value[vref]) - TOL < 0 + if JuMP.value(vref -> master_solution.variable_value[vref], aff) - TOL < 0 continue end else diff --git a/src/StructJuMP.jl b/src/StructJuMP.jl index 81b96e3..2cfbbd3 100644 --- a/src/StructJuMP.jl +++ b/src/StructJuMP.jl @@ -88,7 +88,6 @@ Base.copy(v::StructuredVariableRef) = v Base.:(==)(v::StructuredVariableRef, w::StructuredVariableRef) = v.model === w.model && v.idx == w.idx JuMP.owner_model(v::StructuredVariableRef) = v.model JuMP.isequal_canonical(v::StructuredVariableRef, w::StructuredVariableRef) = v == w -JuMP.variable_type(::StructuredModel) = StructuredVariableRef function JuMP.add_variable(m::StructuredModel, v::JuMP.AbstractVariable, name::String="") m.nextvaridx += 1 vref = StructuredVariableRef(m, m.nextvaridx) @@ -231,7 +230,6 @@ struct StructuredConstraintRef model::StructuredModel # `model` owning the constraint idx::Int # Index in `model.constraints` end -JuMP.constraint_type(::StructuredModel) = StructuredConstraintRef function JuMP.add_constraint(m::StructuredModel, c::JuMP.AbstractConstraint, name::String="") m.nextconidx += 1 cref = StructuredConstraintRef(m, m.nextconidx) diff --git a/test/printhook.jl b/test/printhook.jl index d6e87d5..02c8588 100644 --- a/test/printhook.jl +++ b/test/printhook.jl @@ -27,16 +27,10 @@ Subject to x + y = 1.0 """ @test sprint(show, parent) == """ -A JuMP Model +An Abstract JuMP Model Minimization problem with: Variables: 2 -Objective function type: GenericQuadExpr{Float64,StructJuMP.StructuredVariableRef} +Objective function type: GenericQuadExpr{Float64, StructJuMP.StructuredVariableRef} Constraint: 1 Names registered in the model: x, y""" - @test sprint(show, "text/latex", parent) == """ -\$\$ \\begin{alignat*}{1}\\min\\quad & x^2 + y\\\\ -\\text{Subject to} \\quad & x + y = 1.0\\\\ -\\end{alignat*} - \$\$""" - #@test occursin("Child", str) end