Skip to content

Commit 2010800

Browse files
committed
Made _copy_variable function
1 parent 9830edb commit 2010800

3 files changed

Lines changed: 44 additions & 32 deletions

File tree

src/constraints.jl

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,32 @@ _vec_to_scalar_set(::_MOIExactly) = _MOI.EqualTo
2323
_vec_to_scalar_set(::_MOIAtLeast) = _MOI.GreaterThan
2424
_vec_to_scalar_set(::_MOIAtMost) = _MOI.LessThan
2525

26+
#helper function to create variables
27+
# Helper function to copy variable properties from an existing variable
28+
function _copy_variable(
29+
target_model::JuMP.AbstractModel,
30+
original_var::JuMP.AbstractVariableRef,
31+
)
32+
# Create new variable
33+
new_var = JuMP.@variable(target_model, base_name = JuMP.name(original_var))
34+
35+
# Copy all properties from original variable
36+
JuMP.has_lower_bound(original_var) && JuMP.set_lower_bound(new_var, JuMP.lower_bound(original_var))
37+
JuMP.has_upper_bound(original_var) && JuMP.set_upper_bound(new_var, JuMP.upper_bound(original_var))
38+
JuMP.has_start_value(original_var) && JuMP.set_start_value(new_var, JuMP.start_value(original_var))
39+
JuMP.is_integer(original_var) && JuMP.set_integer(new_var)
40+
JuMP.is_binary(original_var) && JuMP.set_binary(new_var)
41+
42+
# Handle fixed values with force=true (as in original MBM code)
43+
if JuMP.is_fixed(original_var)
44+
JuMP.fix(new_var, JuMP.fix_value(original_var); force=true)
45+
end
46+
47+
return new_var
48+
end
49+
50+
51+
2652
################################################################################
2753
# BOILERPLATE EXTENSION METHODS
2854
################################################################################

src/mbm.jl

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ function reformulate_disjunct_constraint(
150150
::Dict{LogicalVariableRef,Float64},
151151
::MBM
152152
) where {F}
153-
error("Constraint type $(typeof(con)) is not supported by the Multiple Big-M reformulation method.")
153+
error("Constraint type $(typeof(F)) is not supported by the Multiple Big-M reformulation method.")
154154
end
155155

156156
################################################################################
@@ -227,22 +227,7 @@ function _mini_model(
227227
sub_model = JuMP.Model()
228228
new_vars = Dict{JuMP.AbstractVariableRef, JuMP.AbstractVariableRef}()
229229
for var in JuMP.all_variables(model)
230-
new_vars[var] = JuMP.@variable(sub_model, base_name= "sub_model_$(JuMP.name(var))")
231-
if JuMP.is_fixed(var)
232-
JuMP.fix(new_vars[var], JuMP.fix_value(var); force=true)
233-
end
234-
if JuMP.is_integer(var)
235-
JuMP.set_integer(new_vars[var])
236-
end
237-
if JuMP.has_upper_bound(var)
238-
JuMP.set_upper_bound(new_vars[var], JuMP.upper_bound(var))
239-
end
240-
if JuMP.has_lower_bound(var)
241-
JuMP.set_lower_bound(new_vars[var], JuMP.lower_bound(var))
242-
end
243-
if JuMP.has_start_value(var)
244-
JuMP.set_start_value(new_vars[var], JuMP.start_value(var))
245-
end
230+
new_vars[var] = _copy_variable(sub_model, var)
246231
end
247232
for con in [JuMP.constraint_object(con) for con in constraints]
248233
expr = replace_variables_in_constraint(con.func, new_vars)

test/runtests.jl

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,23 @@
11
import DisjunctiveProgramming as DP
22
using DisjunctiveProgramming
33
using Test
4+
using Revise
45
include("utilities.jl")
56

67
# RUN ALL THE TESTS
7-
include("aqua.jl")
8-
include("model.jl")
9-
include("jump.jl")
10-
include("variables/query.jl")
11-
include("variables/logical.jl")
12-
include("constraints/selector.jl")
13-
include("constraints/proposition.jl")
14-
include("constraints/disjunct.jl")
15-
include("constraints/indicator.jl")
16-
include("constraints/bigm.jl")
17-
include("constraints/hull.jl")
18-
include("constraints/fallback.jl")
19-
include("constraints/disjunction.jl")
20-
include("print.jl")
21-
include("solve.jl")
8+
# include("aqua.jl")
9+
# include("model.jl")
10+
# include("jump.jl")
11+
# include("variables/query.jl")
12+
# include("variables/logical.jl")
13+
# include("constraints/selector.jl")
14+
# include("constraints/proposition.jl")
15+
# include("constraints/disjunct.jl")
16+
# include("constraints/indicator.jl")
17+
# include("constraints/bigm.jl")
18+
# include("constraints/hull.jl")
19+
# include("constraints/fallback.jl")
20+
# include("constraints/disjunction.jl")
21+
# include("print.jl")
22+
# include("solve.jl")
2223
include("constraints/mbm.jl")

0 commit comments

Comments
 (0)