Skip to content

Commit

Permalink
Clone with parameters (#124)
Browse files Browse the repository at this point in the history
* rm setperendata

* clone hides sete
  • Loading branch information
guimarqu authored Jun 12, 2019
1 parent 9df44d2 commit c36f21d
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 52 deletions.
68 changes: 31 additions & 37 deletions src/clone.jl
Original file line number Diff line number Diff line change
@@ -1,46 +1,40 @@
function clone_in_formulation!(dest::Formulation,
src::Formulation,
var::Variable,
duty::Type{<:AbstractDuty},
is_explicit::Bool = true)
data = deepcopy(getrecordeddata(var))
set_is_explicit!(data, is_explicit)
var_clone = Variable(
getid(var), getname(var), duty;
var_data = data
function clonevar!(dest::Formulation,
var::Variable,
duty::Type{<:AbstractDuty};
name::String = getname(var),
cost::Float64 = getperenecost(var),
lb::Float64 = getperenelb(var),
ub::Float64 = getpereneub(var),
kind::VarKind = getperenekind(var),
sense::VarSense = getperenesense(var),
inc_val::Float64 = getpereneincval(var),
is_active::Bool = get_init_is_active(var),
is_explicit::Bool = get_init_is_explicit(var))
return setvar!(
dest, name, duty; cost = cost, lb = lb, ub = ub, kind = kind,
sense = sense, inc_val = inc_val, is_active = is_active,
is_explicit = is_explicit, id = getid(var)
)
addvar!(dest, var_clone)
return var_clone
end

function clone_in_formulation!(dest::Formulation,
src::Formulation,
constr::Constraint,
duty::Type{<:AbstractDuty},
is_explicit::Bool = true)

data = deepcopy(getrecordeddata(constr))
set_is_explicit!(data, is_explicit)
constr_clone = Constraint(
getid(constr), getname(constr), duty; constr_data = data
function cloneconstr!(dest::Formulation,
constr::Constraint,
duty::Type{<:AbstractDuty};
name::String = getname(constr),
rhs::Float64 = getperenerhs(constr),
kind::ConstrKind = getperenekind(constr),
sense::ConstrSense = getperenesense(constr),
inc_val::Float64 = getpereneincval(constr),
is_active::Bool = get_init_is_active(constr),
is_explicit::Bool = get_init_is_explicit(constr))
return setconstr!(
dest, name, duty, rhs = rhs, kind = kind, sense = sense,
inc_val = inc_val, is_active = is_active, is_explicit = is_explicit,
id = getid(constr)
)
addconstr!(dest, constr_clone)
return constr_clone
end

function clone_in_formulation!(dest::Formulation,
src::Formulation,
vcs::VarConstrDict,
duty::Type{<:AbstractVarConstrDuty},
is_explicit::Bool = true
) where {VC<:AbstractVarConstr}
for (id, vc) in vcs
clone_in_formulation!(dest, src, vc, duty, is_explicit)
end
return
end

function clone_coefficients!(dest::Formulation,
function clonecoeffs!(dest::Formulation,
src::Formulation)
dest_matrix = getcoefmatrix(dest)
src_matrix = getcoefmatrix(src)
Expand Down
23 changes: 10 additions & 13 deletions src/decomposition.jl
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ function instantiate_orig_vars!(mast::Formulation{DwMaster}, orig_form, annotati
dectype = BD.getdecomposition(ann)
for (id, var) in vars
duty, explicit = varexpduty(DwMaster, formtype, dectype)
clone_in_formulation!(mast, orig_form, var, duty, explicit)
clonevar!(mast, var, duty, is_explicit = explicit)
end
end
return
Expand All @@ -98,7 +98,7 @@ function instantiate_orig_constrs!(mast::Formulation{DwMaster}, orig_form, annot
!haskey(annotations.constrs_per_ann, mast_ann) && return
constrs = annotations.constrs_per_ann[mast_ann]
for (id, constr) in constrs
clone_in_formulation!(mast, orig_form, constr, MasterMixedConstr)
cloneconstr!(mast, constr, MasterMixedConstr)
end
return
end
Expand All @@ -110,7 +110,7 @@ function create_side_vars_constrs!(mast::Formulation{DwMaster})
setupvars = filter(var -> getduty(var[2]) == DwSpSetupVar, getvars(sp))
@assert length(setupvars) == 1
setupvar = collect(values(setupvars))[1] # issue 106
clone_in_formulation!(mast, sp, setupvar, MasterRepPricingSetupVar, false)
clonevar!(mast, setupvar, MasterRepPricingSetupVar, is_explicit = false)
# create convexity constraint
name = "sp_lb_$spuid"
lb_conv_constr = setconstr!(
Expand Down Expand Up @@ -149,7 +149,7 @@ function instantiate_orig_vars!(sp::Formulation{DwSp}, orig_form, annotations, s
vars = annotations.vars_per_ann[sp_ann]
for (id, var) in vars
# An original variable annoted in a subproblem is a DwSpPureVar
clone_in_formulation!(sp, orig_form, var, DwSpPricingVar)
clonevar!(sp, var, DwSpPricingVar)
end
return
end
Expand All @@ -158,7 +158,7 @@ function instantiate_orig_constrs!(sp::Formulation{DwSp}, orig_form, annotations
!haskey(annotations.constrs_per_ann, sp_ann) && return
constrs = annotations.constrs_per_ann[sp_ann]
for (id, constr) in constrs
clone_in_formulation!(sp, orig_form, constr, DwSpPureConstr)
cloneconstr!(sp, constr, DwSpPureConstr)
end
return
end
Expand Down Expand Up @@ -191,7 +191,7 @@ function instantiate_orig_vars!(mast::Formulation{BendersMaster}, orig_form, ann
vars = annotations.vars_per_ann[mast_ann]
for (id, var) in vars
duty = dutyofbendmastvar(var, annotations, orig_form)
clone_in_formulation!(mast, orig_form, var, duty)
clonevar!(mast, var, duty)
end
return
end
Expand All @@ -200,7 +200,7 @@ function instantiate_orig_constrs!(mast::Formulation{BendersMaster}, orig_form,
!haskey(annotations.constrs_per_ann, mast_ann) && return
constrs = annotations.constrs_per_ann[mast_ann]
for (id, constr) in constrs
clone_in_formulation!(mast, orig_form, constr, MasterPureConstr)
cloneconstr!(mast, constr, MasterPureConstr)
end
return
end
Expand Down Expand Up @@ -244,14 +244,12 @@ end
#end

function instantiate_orig_vars!(sp::Formulation{BendersSp}, orig_form, annotations, sp_ann)

if haskey(annotations.vars_per_ann, sp_ann)
vars = annotations.vars_per_ann[sp_ann]
for (id, var) in vars
clone_in_formulation!(sp, orig_form, var, BendSpSepVar)
clonevar!(sp, var, BendSpSepVar, cost = 0.0)
end
end

mast_ann = getparent(annotations, sp_ann)
if haskey(annotations.vars_per_ann, mast_ann)
vars = annotations.vars_per_ann[mast_ann]
Expand Down Expand Up @@ -294,7 +292,7 @@ function instantiate_orig_constrs!(sp::Formulation{BendersSp}, orig_form, annota
constrs = annotations.constrs_per_ann[sp_ann]
for (id, constr) in constrs
duty = dutyofbendspconstr(constr, annotations, orig_form)
clone_in_formulation!(sp, orig_form, constr, duty)
cloneconstr!(sp, constr, duty)
end
return
end
Expand All @@ -315,15 +313,14 @@ function create_side_vars_constrs!(sp::Formulation{BendersSp})
sp_coef[getid(cost), getid(nu)] = 1.0
for (var_id, var) in filter(var -> getduty(var[2]) == BendSpSepVar, getvars(sp))
sp_coef[getid(cost), var_id] = - getperenecost(var)
setperenecost!(var, 0.0)
end
return
end

function assign_orig_vars_constrs!(form, orig_form, annotations, ann)
instantiate_orig_vars!(form, orig_form, annotations, ann)
instantiate_orig_constrs!(form, orig_form, annotations, ann)
clone_coefficients!(form, orig_form)
clonecoeffs!(form, orig_form)
end

function getoptbuilder(prob::Problem, ann)
Expand Down
4 changes: 2 additions & 2 deletions src/formulation.jl
Original file line number Diff line number Diff line change
Expand Up @@ -225,8 +225,8 @@ function setconstr!(f::Formulation,
is_active::Bool = true,
is_explicit::Bool = true,
moi_index::MoiConstrIndex = MoiConstrIndex(),
members = nothing)
id = generateconstrid(f)
members = nothing,
id = generateconstrid(f))
c_data = ConstrData(rhs, kind, sense, inc_val, is_active, is_explicit)
c = Constraint(id, name, duty; constr_data = c_data, moi_index = moi_index)
members != nothing && setmembers!(f, c, members)
Expand Down

0 comments on commit c36f21d

Please sign in to comment.