Skip to content

Commit 4e6c0d2

Browse files
authored
Use set_expr_bound (#1077)
2 parents 25153eb + 9e9861c commit 4e6c0d2

File tree

1 file changed

+13
-63
lines changed

1 file changed

+13
-63
lines changed

src/variables/variable_common.jl

Lines changed: 13 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -157,10 +157,12 @@ function _finalize_expressions!(m, name, args...)
157157
inds, exprs, bins, ints, lbs, ubs, internal_fix_values, fix_values = _collect_info(m, args...)
158158
_set_binary.(exprs, bins)
159159
_set_integer.(exprs, ints)
160-
_set_lower_bound.(exprs, lbs, Symbol(name, :_lb), inds)
161-
_set_upper_bound.(exprs, ubs, Symbol(name, :_ub), inds)
162-
_fix.(exprs, internal_fix_values, Symbol(name, :_fix_value), inds)
163-
_fix.(exprs, fix_values, Symbol(name, :_internal_fix_value), inds)
160+
m.ext[:spineopt].constraints[Symbol(name, :_lb)] = Dict(zip(inds, set_expr_bound.(exprs, >=, lbs)))
161+
m.ext[:spineopt].constraints[Symbol(name, :_ub)] = Dict(zip(inds, set_expr_bound.(exprs, <=, ubs)))
162+
m.ext[:spineopt].constraints[Symbol(name, :_internal_fix)] = Dict(
163+
zip(inds, set_expr_bound.(exprs, ==, internal_fix_values))
164+
)
165+
m.ext[:spineopt].constraints[Symbol(name, :_fix)] = Dict(zip(inds, set_expr_bound.(exprs, ==, fix_values)))
164166
end
165167

166168
function _collect_info(m, d, bin, int, lb, ub, fix_value, internal_fix_value)
@@ -195,75 +197,23 @@ _set_binary(expr::GenericAffExpr, bin) = bin && set_binary.(keys(expr.terms))
195197
_set_integer(var::VariableRef, int) = int && set_integer(var)
196198
_set_integer(expr::GenericAffExpr, int) = int && set_integer.(keys(expr.terms))
197199

198-
_set_lower_bound(::VariableRef, ::Nothing) = nothing
200+
_set_lower_bound(_var, ::Nothing) = nothing
199201
_set_lower_bound(var::VariableRef, bound::Call) = set_lower_bound(var, bound)
200202
_set_lower_bound(var::VariableRef, bound::Number) = isfinite(bound) && set_lower_bound(var, bound)
201-
_set_lower_bound(expr::GenericAffExpr, bound, name, ind) = _set_bound(expr, >=, bound, name, ind)
202203

203-
_set_upper_bound(::VariableRef, ::Nothing) = nothing
204+
_set_upper_bound(_var, ::Nothing) = nothing
204205
_set_upper_bound(var::VariableRef, bound::Call) = set_upper_bound(var, bound)
205206
_set_upper_bound(var::VariableRef, bound::Number) = isfinite(bound) && set_upper_bound(var, bound)
206-
_set_upper_bound(expr::GenericAffExpr, bound, name, ind) = _set_bound(expr, <=, bound, name, ind)
207207

208-
_fix(::VariableRef, ::Nothing) = nothing
209-
_fix(var::VariableRef, x::Call) = fix(var, x)
210-
function _fix(var::VariableRef, x::Number)
211-
if !isnan(x)
212-
fix(var, x; force=true)
208+
_fix(_var, ::Nothing) = nothing
209+
_fix(var::VariableRef, value::Call) = fix(var, value)
210+
function _fix(var::VariableRef, value::Number)
211+
if !isnan(value)
212+
fix(var, value; force=true)
213213
elseif is_fixed(var)
214214
unfix(var)
215215
end
216216
end
217-
_fix(expr::GenericAffExpr, x, name, ind) = _set_bound(expr, ==, x, name, ind)
218-
219-
_set_bound(_expr, _sense, _bound::Nothing, _name, _ind) = nothing
220-
function _set_bound(expr, sense, bound::Call, name, ind)
221-
upd = _ExpressionBoundUpdate(expr, sense, bound, name, ind)
222-
upd()
223-
end
224-
function _set_bound(expr, sense, bound::Number, name, ind)
225-
m = owner_model(expr)
226-
m === nothing && return
227-
bounds = get!(m.ext[:spineopt].constraints, name, Dict())
228-
existing_constraint = get(bounds, ind, nothing)
229-
if existing_constraint !== nothing
230-
_update_bound_constraint(existing_constraint, expr, sense, bound)
231-
elseif isfinite(bound)
232-
new_constraint = build_sense_constraint(expr, sense, bound)
233-
bounds[ind] = add_constraint(m, new_constraint)
234-
end
235-
end
236-
237-
function _update_bound_constraint(existing_constraint, expr, ::typeof(==), bound)
238-
if isnan(bound)
239-
for var in keys(expr.terms)
240-
set_normalized_coefficient(existing_constraint, var, 0)
241-
end
242-
set_normalized_rhs(existing_constraint, 0)
243-
else
244-
for (var, coeff) in expr.terms
245-
set_normalized_coefficient(existing_constraint, var, realize(coeff))
246-
end
247-
set_normalized_rhs(existing_constraint, bound - realize(expr.constant))
248-
end
249-
end
250-
function _update_bound_constraint(existing_constraint, expr, _sense, bound)
251-
if isfinite(bound)
252-
set_normalized_rhs(existing_constraint, bound - realize(expr.constant))
253-
end
254-
end
255-
256-
struct _ExpressionBoundUpdate
257-
expr
258-
sense
259-
bound
260-
name
261-
ind
262-
end
263-
264-
function (upd::_ExpressionBoundUpdate)()
265-
_set_bound(upd.expr, upd.sense, realize(upd.bound, upd), upd.name, upd.ind)
266-
end
267217

268218
"""
269219
_representative_index(ind)

0 commit comments

Comments
 (0)