Skip to content

Commit

Permalink
return new NL constraints; change name split func
Browse files Browse the repository at this point in the history
  • Loading branch information
hdavid16 committed Jan 10, 2023
1 parent 4d0f337 commit 7c1bf50
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 13 deletions.
11 changes: 7 additions & 4 deletions src/constraint.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -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
17 changes: 8 additions & 9 deletions src/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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!(
Expand Down

0 comments on commit 7c1bf50

Please sign in to comment.