Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def total_equalities_set(block):
"""
Method to return a ComponentSet of all equality Constraint components in a
model.
Args:
block : model to be studied
Returns:
A ComponentSet including all equality Constraint components in block
"""
return ComponentSet(total_equalities_generator(block))
def __init__(self, subproblem_solver):
self._subproblem_solver = pe.SolverFactory(subproblem_solver)
if isinstance(self._subproblem_solver, PersistentSolver):
self._using_persistent_solver = True
else:
self._using_persistent_solver = False
self._relaxations = ComponentSet()
self._relaxations_not_tracking_solver = ComponentSet()
self._relaxations_with_added_cuts = ComponentSet()
self._pyomo_model = None
self.options = ConfigBlock()
self.options.declare('feasibility_tol', ConfigValue(default=1e-6, domain=NonNegativeFloat,
doc='Tolerance below which cuts will not be added'))
self.options.declare('max_iter', ConfigValue(default=30, domain=NonNegativeInt,
doc='Maximum number of iterations'))
self.options.declare('keep_cuts', ConfigValue(default=False, domain=In([True, False]),
doc='Whether or not to keep the cuts generated after the solve'))
self.subproblem_solver_options = ConfigBlock(implicit=True)
def build_model_size_report(model):
"""Build a model size report object."""
report = ModelSizeReport()
activated_disjunctions = ComponentSet()
activated_disjuncts = ComponentSet()
fixed_true_disjuncts = ComponentSet()
activated_constraints = ComponentSet()
activated_vars = ComponentSet()
new_containers = (model,)
while new_containers:
new_activated_disjunctions = ComponentSet()
new_activated_disjuncts = ComponentSet()
new_fixed_true_disjuncts = ComponentSet()
new_activated_constraints = ComponentSet()
for container in new_containers:
(next_activated_disjunctions,
next_fixed_true_disjuncts,
next_activated_disjuncts,
next_activated_constraints
def build_model_size_report(model):
"""Build a model size report object."""
report = ModelSizeReport()
activated_disjunctions = ComponentSet()
activated_disjuncts = ComponentSet()
fixed_true_disjuncts = ComponentSet()
activated_constraints = ComponentSet()
activated_vars = ComponentSet()
new_containers = (model,)
while new_containers:
new_activated_disjunctions = ComponentSet()
new_activated_disjuncts = ComponentSet()
new_fixed_true_disjuncts = ComponentSet()
new_activated_constraints = ComponentSet()
for container in new_containers:
(next_activated_disjunctions,
next_fixed_true_disjuncts,
next_activated_disjuncts,
next_activated_constraints
) = _process_activated_container(container)
new_activated_disjunctions.update(next_activated_disjunctions)
new_activated_disjuncts.update(next_activated_disjuncts)
new_fixed_true_disjuncts.update(next_fixed_true_disjuncts)
new_activated_constraints.update(next_activated_constraints)
new_containers = ((new_activated_disjuncts - activated_disjuncts) |
(new_fixed_true_disjuncts - fixed_true_disjuncts))
if v.ub is None:
var_ubs[v] = math.inf
else:
var_ubs[v] = value(v.ub)
var_to_con_map[v].append(c)
n_cons += 1
for _v in m.component_data_objects(ctype=Var, active=True, descend_into=True, sort=True):
if _v.is_fixed():
_v.setlb(_v.value)
_v.setub(_v.value)
new_var_bounds[_v] = (_v.value, _v.value)
n_fbbt = 0
improved_vars = ComponentSet()
for c in m.component_data_objects(ctype=Constraint, active=True,
descend_into=True, sort=True):
_new_var_bounds = _fbbt_con(c, config)
n_fbbt += 1
new_var_bounds.update(_new_var_bounds)
for v, bnds in _new_var_bounds.items():
vlb, vub = bnds
if vlb is not None:
if vlb > var_lbs[v] + config.improvement_tol:
improved_vars.add(v)
var_lbs[v] = vlb
if vub is not None:
if vub < var_ubs[v] - config.improvement_tol:
improved_vars.add(v)
var_ubs[v] = vub
equality. Mapping takes place using id(). That is, if you have x = y, then
you would have id(x) -> ComponentSet([x, y]) and id(y) -> ComponentSet([x,
y]) in the mapping.
"""
# Map of variables to their equality set (ComponentSet)
eq_var_map = ComponentMap()
# Loop through all the active constraints in the model
for constraint in model.component_data_objects(
ctype=Constraint, active=True, descend_into=True):
eq_linked_vars = _get_equality_linked_variables(constraint)
if not eq_linked_vars:
continue # if we get an empty tuple, skip to next constraint.
v1, v2 = eq_linked_vars
set1 = eq_var_map.get(v1, ComponentSet((v1, v2)))
set2 = eq_var_map.get(v2, (v2,))
# if set1 and set2 are equivalent, skip to next constraint.
if set1 is set2:
continue
# add all elements of set2 to set 1
set1.update(set2)
# Update all elements to point to set 1
for v in set1:
eq_var_map[v] = set1
return eq_var_map
def total_blocks_set(block):
"""
Method to return a ComponentSet of all Block components in a model.
Args:
block : model to be studied
Returns:
A ComponentSet including all Block components in block (including block
itself)
"""
total_blocks_set = ComponentSet(
block.component_data_objects(
ctype=Block, active=None, descend_into=True))
total_blocks_set.add(block)
return total_blocks_set
def _process_activated_container(blk):
"""Process a container object, returning the new components found."""
new_fixed_true_disjuncts = ComponentSet(
disj for disj in blk.component_data_objects(Disjunct, active=True)
if disj.indicator_var.value == 1 and disj.indicator_var.fixed)
new_activated_disjunctions = ComponentSet(
blk.component_data_objects(Disjunction, active=True))
new_activated_disjuncts = ComponentSet(
disj for disjtn in new_activated_disjunctions
for disj in _activated_disjuncts_in_disjunction(disjtn))
new_activated_constraints = ComponentSet(
blk.component_data_objects(Constraint, active=True))
return (
new_activated_disjunctions,
new_fixed_true_disjuncts,
new_activated_disjuncts,
new_activated_constraints
)
mosek_expr = None
referenced_vars = None
cone_type = None
cone_param = 0
cone_members = None
if con._linear_canonical_form:
mosek_expr, referenced_vars = self._get_expr_from_pyomo_repn(
con.canonical_form(),
self._max_constraint_degree)
elif isinstance(con, _ConicBase):
cone_type, cone_param, cone_members = \
self._get_cone_data(con)
if cone_type is not None:
assert cone_members is not None
referenced_vars = ComponentSet(cone_members)
else:
logger.warning("Cone %s was not recognized by Mosek"
% (str(con)))
# the cone was not recognized, treat
# it like a standard constraint, which
# will in all likelihood lead to Mosek
# reporting a helpful error message
assert mosek_expr is None
if (mosek_expr is None) and (cone_type is None):
mosek_expr, referenced_vars = \
self._get_expr_from_pyomo_expr(
con.body,
self._max_constraint_degree)
assert referenced_vars is not None
if mosek_expr is not None:
def pass_tear_direct(self, G, tears):
"""Pass values across all tears in the given tear set"""
fixed_outputs = ComponentSet()
edge_list = self.idx_to_edge(G)
for tear in tears:
# fix everything then call pass values
arc = G.edges[edge_list[tear]]["arc"]
for var in arc.src.iter_vars(expr_vars=True, fixed=False):
fixed_outputs.add(var)
var.fix()
self.pass_values(arc, fixed_inputs=self.fixed_inputs())
for var in fixed_outputs:
var.free()
fixed_outputs.clear()