Skip to content

Commit

Permalink
Merge pull request #85 from StructJuMP/kkim
Browse files Browse the repository at this point in the history
Update JuMP to v1.0.0 and others
  • Loading branch information
kibaekkim authored Apr 8, 2022
2 parents e0b66e9 + 90a4634 commit ac55b4d
Show file tree
Hide file tree
Showing 11 changed files with 62 additions and 27 deletions.
10 changes: 5 additions & 5 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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"
Expand Down
47 changes: 47 additions & 0 deletions examples/dcap.jl
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion examples/parmodel1.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion examples/parmodel2.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion examples/parmodel3.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion examples/parmodel4.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion examples/transportation.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
6 changes: 1 addition & 5 deletions src/BendersBridge.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
4 changes: 2 additions & 2 deletions src/Benders_pmap.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down
2 changes: 0 additions & 2 deletions src/StructJuMP.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
10 changes: 2 additions & 8 deletions test/printhook.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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

2 comments on commit ac55b4d

@kibaekkim
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/58184

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.3.0 -m "<description of version>" ac55b4d18c87e76e5e162918127a7d6926a06627
git push origin v0.3.0

Please sign in to comment.