Skip to content

Commit ad9b19b

Browse files
committed
add choose!
don't add XOR with reformulation
1 parent 0c4bf3c commit ad9b19b

File tree

4 files changed

+31
-10
lines changed

4 files changed

+31
-10
lines changed

src/DisjunctiveProgramming.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ using JuMP, IntervalArithmetic, Symbolics, Suppressor
44

55
export add_disjunction!, add_proposition!, reformulate_disjunction
66
export @disjunction, @proposition
7+
export choose!
78

89
include("constraint.jl")
910
include("logic.jl")

src/logic.jl

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,28 @@
11
"""
2+
choose!(m::Model, n::Int, vars::VariableRef...; mode)
23
4+
Add constraint to select n elements from the list of variables. Options for mode
5+
are `:at_least`, `:at_most`, `:exactly`.
36
"""
4-
function select()
5-
7+
function choose!(m::Model, n::Int, vars::VariableRef...; mode=:exactly)
8+
@assert length(vars) >= n "Not enough variables passed."
9+
@assert all(is_valid.(m, vars)) "Invalid VariableRefs passed."
10+
add_selection!(m, n, vars...; mode)
11+
end
12+
function choose!(m::Model, vars::VariableRef...; mode=:exactly)
13+
@assert all(is_valid.(m, vars)) "Invalid VariableRefs passed."
14+
n = vars[1] #first variable is the n
15+
add_selection!(m, n, vars...; mode)
16+
end
17+
function add_selection!(m::Model, n, vars::VariableRef...; mode::Symbol)
18+
display(n)
19+
if mode == :exactly
20+
display(@constraint(m, sum(vars) == n))
21+
elseif mode == :at_least
22+
@constraint(m, sum(vars) n)
23+
elseif mode == :at_most
24+
@constraint(m, sum(vars) n)
25+
end
626
end
727

828
"""

src/macros.jl

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,14 @@ macro disjunction(m, args...)
5050
end
5151
end
5252

53-
#XOR constraint name
54-
xor_con = Symbol("XOR($disj_name)")
53+
# #XOR constraint name
54+
# xor_con = Symbol("XOR($disj_name)")
5555

5656
#build disjunction
5757
code = quote
5858
@assert !in($name, keys(object_dictionary($m))) "The disjunction name $name is already registered in the model. Specify new name."
5959
$m[$name] = @variable($m, [eachindex($disjunction)], Bin, base_name = string($name), lower_bound = 0, upper_bound = 1)
60-
@constraint($m, $xor_con, sum($m[$name]) == 1)
60+
# @constraint($m, $xor_con, sum($m[$name]) == 1)
6161
reformulate_disjunction($m, $(disjunction...); bin_var = $name, reformulation = $reformulation, param = $param)
6262
end
6363

@@ -114,15 +114,15 @@ function add_disjunction!(m::Model,disj...;reformulation::Symbol,M=missing,ϵ=1e
114114
bin_var = ismissing(name) ? Symbol("disj",gensym()) : name
115115
disj_name = ismissing(name) ? bin_var : Symbol("disj_",name)
116116

117-
#XOR constraint name
118-
xor_con = "XOR($disj_name)"
117+
# #XOR constraint name
118+
# xor_con = "XOR($disj_name)"
119119

120120
#apply reformulation
121121
@assert !in(bin_var, keys(object_dictionary(m))) "The disjunction name $bin_var is already registered in the model. Specify new name."
122122
#create indicator variable
123123
m[bin_var] = @variable(m, [eachindex(disj)], Bin, base_name = string(bin_var), lower_bound = 0, upper_bound = 1)
124-
#add xor constraint on binary variable
125-
m[Symbol(xor_con)] = @constraint(m, sum(m[bin_var]) == 1, base_name = xor_con)
124+
# #add xor constraint on binary variable
125+
# m[Symbol(xor_con)] = @constraint(m, sum(m[bin_var]) == 1, base_name = xor_con)
126126
#reformulate disjunction
127127
reformulate_disjunction(m, disj...; bin_var, reformulation, param)
128128
end

src/reformulate.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ function reformulate_disjunction(m::Model, disj...; bin_var, reformulation, para
3030
new_constraints = Dict{Symbol,Any}(
3131
Symbol(bin_var,"[$i]") => disj[i] for i in eachindex(disj)
3232
)
33-
new_constraints[Symbol(bin_var,"_XOR")] = constraint_by_name(m, "XOR(disj_$bin_var)")
33+
# new_constraints[Symbol(bin_var,"_XOR")] = constraint_by_name(m, "XOR(disj_$bin_var)")
3434
if reformulation == :hull
3535
for var in m[:gdp_variable_refs]
3636
agg_con_name = "$(var)_$(bin_var)_aggregation"

0 commit comments

Comments
 (0)