Skip to content

Commit

Permalink
reduce allocations:
Browse files Browse the repository at this point in the history
don't use variable_by_name, but store this in .ext Dict
  • Loading branch information
hdavid16 committed Nov 9, 2022
1 parent 1b1b49e commit 591d71c
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 8 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "DisjunctiveProgramming"
uuid = "0d27d021-0159-4c7d-b4a7-9ccb5d9366cf"
authors = ["hdavid16 <[email protected]>"]
version = "0.3.4"
version = "0.3.5"

[deps]
JuMP = "4076af6c-e467-56ae-b986-b466b2749572"
Expand Down
9 changes: 3 additions & 6 deletions src/hull.jl
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ function hull_reformulation!(constr::ConstraintRef{<:AbstractModel, MOI.Constrai
#replace each variable with its disaggregated version
for var_ref in get_constraint_variables(constr)
is_binary(var_ref) && continue #NOTE: binaries from nested disjunctions are not disaggregated and don't need to be swapped out
var_ref in m.ext[:disaggregated_variables] && continue #disaggregated variables are not touched
var_ref in values(m.ext[:disaggregated_variables]) && continue #disaggregated variables are not touched
#get disaggregated variable reference
var_name_i = name_disaggregated_variable(var_ref, bin_var, i)
var_i_ref = variable_by_name(m, var_name_i)
Expand Down Expand Up @@ -85,7 +85,7 @@ function disaggregate_variables(m::Model, disj, bin_var)
bounds_dict = :variable_bounds_dict in keys(obj_dict) ? obj_dict[:variable_bounds_dict] : Dict() #NOTE: should pass as an keyword argument
for var in var_refs
is_binary(var) && continue #NOTE: don't disaggregate binary variables from nested disjunctions
var in m.ext[:disaggregated_variables] && continue #skip already disaggregated variables
var in values(m.ext[:disaggregated_variables]) && continue #skip already disaggregated variables
#define UB and LB
LB, UB = get_bounds(var, bounds_dict)
#disaggregate variable and add bounding constraints
Expand All @@ -95,10 +95,7 @@ function disaggregate_variables(m::Model, disj, bin_var)
var_name_i = Symbol(var_name_i_str)
#create disaggregated variable
var_i = add_disaggregated_variable(m, var, LB, UB, var_name_i_str)
push!(
m.ext[:disaggregated_variables],
var_i
)
m.ext[:disaggregated_variables][var_name_i_str] = var_i
#apply bounding constraints on disaggregated variable
var_i_lb = "$(var_name_i)_lb"
var_i_ub = "$(var_name_i)_ub"
Expand Down
2 changes: 1 addition & 1 deletion src/reformulate.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ function reformulate_disjunction(m::Model, disj...; bin_var, reformulation, para
#run reformulation
if reformulation == :hull
if !in(:disaggregated_variables, keys(m.ext))
m.ext[:disaggregated_variables] = Set([]) #record disaggregated variables to avoid duplicating disaggregation (nested disjunctions)
m.ext[:disaggregated_variables] = Dict{String,VariableRef}() #record disaggregated variables to avoid duplicating disaggregation (nested disjunctions)
end
disaggregate_variables(m, disj, bin_var)
end
Expand Down

2 comments on commit 591d71c

@hdavid16
Copy link
Owner 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/71897

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.5 -m "<description of version>" 591d71c0020617125824d515fe1397562206193d
git push origin v0.3.5

Please sign in to comment.