def symbol_map_from_instance(instance): """ Create a symbol map from an instance using name-based labelers. """ from pyomo.core.base import Var, Constraint, Objective symbol_map = SymbolMap() labeler = TextLabeler() # # Recursively iterate over all variables # for varvalue in instance.component_data_objects(Var, active=True): symbol_map.getSymbol(varvalue, labeler) # # Recursively iterate over all constraints # for constraint_data in instance.component_data_objects(Constraint, active=True): con_symbol = symbol_map.getSymbol(constraint_data, labeler) if constraint_data.equality: label = 'c_e_%s_' % con_symbol symbol_map.alias(constraint_data, label) else: if constraint_data.lower is not None: if constraint_data.upper is not None: symbol_map.alias(constraint_data, 'r_l_%s_' % con_symbol) symbol_map.alias(constraint_data, 'r_u_%s_' % con_symbol) else: label = 'c_l_%s_' % con_symbol symbol_map.alias(constraint_data, label) elif constraint_data.upper is not None: label = 'c_u_%s_' % con_symbol symbol_map.alias(constraint_data, label) # # Recursively iterate over all objectives # first = True for objective_data in instance.component_data_objects(Objective, active=True): symbol_map.getSymbol(objective_data, labeler) if first: # The first objective is the default symbol_map.alias(objective_data, "__default_objective__") first = False # # Return the symbol map # return symbol_map
def test_existing_alias(self): s = SymbolMap() v1 = variable() s.alias(v1, "v") self.assertIs(s.aliases["v"](), v1) v2 = variable() with self.assertRaises(RuntimeError): s.alias(v2, "v") s.alias(v1, "A") self.assertIs(s.aliases["v"](), v1) self.assertIs(s.aliases["A"](), v1) s.alias(v1, "A") self.assertIs(s.aliases["v"](), v1) self.assertIs(s.aliases["A"](), v1)