Skip to content

Commit d7e5795

Browse files
committed
move disjunction name to a kwarg
this specifies the name of the binary variable for the disjunction (if missing, a name is generated)
1 parent 6333f15 commit d7e5795

File tree

3 files changed

+8
-18
lines changed

3 files changed

+8
-18
lines changed

examples/ex1.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,4 @@ m = Model()
99
@constraint(m, con3, x<=9)
1010
@constraint(m, con4, 5<=x)
1111

12-
s=@disjunction(m,:y,(con1,con2),con3,con4,reformulation=:BMR)
12+
@disjunction(m,(con1,con2),con3,con4,reformulation=:CHR,name=:y)

src/disjunction.jl

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,10 @@
1-
function add_disjunction(m::Model,args...;reformulation=:BMR,M=missing,eps=1e-6)
1+
function add_disjunction(m::Model,disj...;reformulation=:BMR,M=missing,eps=1e-6,name=missing)
22
@assert m isa Model "A valid JuMP Model must be provided."
33
@assert reformulation in [:BMR, :CHR] "Invalid reformulation method passed to keyword argument `:reformulation`. Valid options are :BMR (Big-M Reformulation) and :CHR (Convex-Hull Reormulation)."
44

55
#create binary indicator variables for each disjunction
6-
if args[1] isa Symbol #name for disjunction can be the first argument
7-
@assert isnothing(variable_by_name(m,string(args[1]))) "Name for disjunction binary cannot be the same as an existing variable."
8-
disj_name = args[1]
9-
disj = args[2:end]
10-
else #if name is not passed, then generate it
11-
disj_name = Symbol("disj",gensym())
12-
disj = args
13-
end
6+
disj_name = ismissing(name) ? Symbol("disj",gensym()) : name
7+
@assert isnothing(variable_by_name(m,string(disj_name))) "Name for disjunction binary cannot be the same as an existing variable."
148
eval(:(@variable($m, $disj_name[i = 1:length($disj)], Bin)))
159
#enforce exclussive OR
1610
eval(:(@constraint($m,sum($disj_name[i] for i = 1:length($disj)) == 1)))

src/macro.jl

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,13 @@ macro disjunction(args...)
1212
M = !isempty(M) ? esc(M[1].args[2]) : :(missing)
1313
eps = filter(i -> i.args[1] == :eps, kw_args)
1414
eps = !isempty(eps) ? esc(eps[1].args[2]) : :(1e-6)
15+
name = filter(i -> i.args[1] == :name, kw_args)
16+
name = !isempty(name) ? esc(name[1].args[2]) : :(missing)
1517

1618
#get args
1719
m = esc(pos_args[1])
18-
disj_name = pos_args[2]
19-
if disj_name isa QuoteNode
20-
disj = [esc(a) for a in pos_args[3:end]]
21-
elseif isa(disj_name, Symbol) || Meta.isexpr(disj_name, (:tuple,:vect))
22-
disj_name = :(Symbol("disj",gensym()))
23-
disj = [esc(a) for a in pos_args[2:end]]
24-
end
20+
disj = [esc(a) for a in pos_args[2:end]]
2521

2622
#build disjunction
27-
:(add_disjunction($m, $disj_name, $(disj...), reformulation = $reformulation, M = $M, eps = $eps))
23+
:(add_disjunction($m, $(disj...), reformulation = $reformulation, M = $M, eps = $eps, name = $name))
2824
end

0 commit comments

Comments
 (0)