Skip to content

Commit 7c1bf50

Browse files
committed
return new NL constraints; change name split func
1 parent 4d0f337 commit 7c1bf50

File tree

2 files changed

+15
-13
lines changed

2 files changed

+15
-13
lines changed

src/constraint.jl

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,8 @@ function _split_constraint(m::Model, constr::ConstraintRef, lb::Float64, ub::Flo
116116
if isempty(constr_name)
117117
constr_name = "[$constr]"
118118
end
119-
lb_name = name_split_constraint(constr_name, :lb)
120-
ub_name = name_split_constraint(constr_name, :ub)
119+
lb_name = name_split(constr_name; new_index = :lb)
120+
ub_name = name_split(constr_name; new_index = :ub)
121121
func = constraint_object(constr).func
122122
return [
123123
@constraint(m, lb <= func, base_name = lb_name),
@@ -154,7 +154,10 @@ function add_reformulated_constraint(constr::ConstraintRef, bin_var::Symbol, sym
154154
m = constr.model
155155
replace_JuMPvars!(expr, m)
156156
replace_operators!(expr)
157-
#replace constraint with prespective function
158-
push!(m.ext[bin_var], add_nonlinear_constraint(m, expr))
157+
#add new constraint and delete old one
158+
new_constr = add_nonlinear_constraint(m, expr)
159+
push!(m.ext[bin_var], new_constr)
159160
delete(m, constr)
161+
162+
return new_constr
160163
end

src/utils.jl

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -160,19 +160,18 @@ end
160160

161161
function name_disaggregated_variable(var_ref, bin_var, i)
162162
var_name = name(var_ref)
163-
var_name_i = "$(var_name)_$(bin_var)[$i]"
164-
165-
return var_name_i
163+
return name_split(var_name; post = string(bin_var), new_index = i)
166164
end
167165

168-
function name_split_constraint(con_name, side)
169-
#get disaggregated variable reference
170-
if occursin("[", string(con_name))
171-
con_name = replace(string(con_name), "]" => ",$side]")
166+
function name_split(str; post="", new_index)
167+
#add side as the last index of str (variable or constraint name)
168+
if occursin("[", str)
169+
new_str = replace(str, "]" => ",$new_index]")
170+
new_str_spl = split(new_str, "[") #NOTE: assumes that [ only occurs once in the string
171+
return string(new_str_spl[1],post,"[",new_str_spl[end])
172172
else
173-
con_name = "$(con_name)[$side]"
173+
return "$(str)$(post)[$new_index]"
174174
end
175-
return con_name
176175
end
177176

178177
function constraint_variables!(

0 commit comments

Comments
 (0)