Skip to content

Commit

Permalink
compact add_disjunction!
Browse files Browse the repository at this point in the history
  • Loading branch information
hdavid16 committed Sep 11, 2022
1 parent 2502562 commit 7a8aebf
Showing 1 changed file with 5 additions and 8 deletions.
13 changes: 5 additions & 8 deletions src/macros.jl
Original file line number Diff line number Diff line change
Expand Up @@ -86,21 +86,18 @@ function add_disjunction!(m::Model,disj...;reformulation::Symbol,M=missing,ϵ=1e
#run checks
@assert reformulation in [:big_m, :hull] "Invalid reformulation method passed to keyword argument `:reformulation`. Valid options are :big_m (Big-M Reformulation) and :hull (Hull Reformulation)."
@assert length(disj) > 1 "At least 2 disjuncts must be included. If there is an empty disjunct, use `nothing`."

#get kwargs and set defaults if missing
param = reformulation == :big_m ? M : ϵ
bin_var = ismissing(name) ? Symbol("disj",gensym()) : name

#apply reformulation
#create indicator variable
bin_var = ismissing(name) ? Symbol("disj_",gensym()) : name
if bin_var in keys(object_dictionary(m))
@assert m[bin_var] isa Vector{VariableRef} "The binary variable $bin_var is already registered in the model and is not of type Vector{VariableRef}. Specify a new name for the disjunction."
@assert length(disj) <= length(m[bin_var]) "The binary variable $bin_var is already registered in the model and its size is smaller than the number of disjunts. Specify a new name for the disjunction."
@assert m[bin_var] isa AbstractArray{VariableRef} "The binary variable $bin_var is already registered in the model and is not of type AbstractArray{VariableRef}. Specify a new name for the disjunction."
@assert length(disj) <= prod(size(m[bin_var])) "The binary variable $bin_var is already registered in the model and its size is smaller than the number of disjunts. Specify a new name for the disjunction."
else
#create indicator variable
m[bin_var] = @variable(m, [eachindex(disj)], Bin, base_name = string(bin_var))
end

#reformulate disjunction
param = reformulation == :big_m ? M : ϵ
reformulate_disjunction(m, disj...; bin_var, reformulation, param)
end

Expand Down

2 comments on commit 7a8aebf

@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/68069

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>" 7a8aebfd772d0c4728c8a139be855d45a975c247
git push origin v0.3.0

Please sign in to comment.