From 7c1bf502982f12794627b71d03ba5466c2b8ca83 Mon Sep 17 00:00:00 2001 From: hdavid16 Date: Tue, 10 Jan 2023 17:34:23 -0500 Subject: [PATCH] return new NL constraints; change name split func --- src/constraint.jl | 11 +++++++---- src/utils.jl | 17 ++++++++--------- 2 files changed, 15 insertions(+), 13 deletions(-) diff --git a/src/constraint.jl b/src/constraint.jl index ff49fda..40534cc 100644 --- a/src/constraint.jl +++ b/src/constraint.jl @@ -116,8 +116,8 @@ function _split_constraint(m::Model, constr::ConstraintRef, lb::Float64, ub::Flo if isempty(constr_name) constr_name = "[$constr]" end - lb_name = name_split_constraint(constr_name, :lb) - ub_name = name_split_constraint(constr_name, :ub) + lb_name = name_split(constr_name; new_index = :lb) + ub_name = name_split(constr_name; new_index = :ub) func = constraint_object(constr).func return [ @constraint(m, lb <= func, base_name = lb_name), @@ -154,7 +154,10 @@ function add_reformulated_constraint(constr::ConstraintRef, bin_var::Symbol, sym m = constr.model replace_JuMPvars!(expr, m) replace_operators!(expr) - #replace constraint with prespective function - push!(m.ext[bin_var], add_nonlinear_constraint(m, expr)) + #add new constraint and delete old one + new_constr = add_nonlinear_constraint(m, expr) + push!(m.ext[bin_var], new_constr) delete(m, constr) + + return new_constr end \ No newline at end of file diff --git a/src/utils.jl b/src/utils.jl index ee78559..938f8e6 100644 --- a/src/utils.jl +++ b/src/utils.jl @@ -160,19 +160,18 @@ end function name_disaggregated_variable(var_ref, bin_var, i) var_name = name(var_ref) - var_name_i = "$(var_name)_$(bin_var)[$i]" - - return var_name_i + return name_split(var_name; post = string(bin_var), new_index = i) end -function name_split_constraint(con_name, side) - #get disaggregated variable reference - if occursin("[", string(con_name)) - con_name = replace(string(con_name), "]" => ",$side]") +function name_split(str; post="", new_index) + #add side as the last index of str (variable or constraint name) + if occursin("[", str) + new_str = replace(str, "]" => ",$new_index]") + new_str_spl = split(new_str, "[") #NOTE: assumes that [ only occurs once in the string + return string(new_str_spl[1],post,"[",new_str_spl[end]) else - con_name = "$(con_name)[$side]" + return "$(str)$(post)[$new_index]" end - return con_name end function constraint_variables!(