1# ___________________________________________________________________________ 2# 3# Pyomo: Python Optimization Modeling Objects 4# Copyright 2017 National Technology and Engineering Solutions of Sandia, LLC 5# Under the terms of Contract DE-NA0003525 with National Technology and 6# Engineering Solutions of Sandia, LLC, the U.S. Government retains certain 7# rights in this software. 8# This software is distributed under the 3-clause BSD License. 9# ___________________________________________________________________________ 10 11from pyomo.core.expr.symbol_map import SymbolMap 12 13from pyomo.core.base.label import TextLabeler 14def symbol_map_from_instance(instance): 15 """ 16 Create a symbol map from an instance using name-based labelers. 17 """ 18 from pyomo.core.base import Var, Constraint, Objective 19 20 symbol_map = SymbolMap() 21 labeler = TextLabeler() 22 # 23 # Recursively iterate over all variables 24 # 25 for varvalue in instance.component_data_objects(Var, active=True): 26 symbol_map.getSymbol(varvalue, labeler) 27 # 28 # Recursively iterate over all constraints 29 # 30 for constraint_data in instance.component_data_objects(Constraint, active=True): 31 con_symbol = symbol_map.getSymbol(constraint_data, labeler) 32 if constraint_data.equality: 33 label = 'c_e_%s_' % con_symbol 34 symbol_map.alias(constraint_data, label) 35 else: 36 if constraint_data.lower is not None: 37 if constraint_data.upper is not None: 38 symbol_map.alias(constraint_data, 'r_l_%s_' % con_symbol) 39 symbol_map.alias(constraint_data, 'r_u_%s_' % con_symbol) 40 else: 41 label = 'c_l_%s_' % con_symbol 42 symbol_map.alias(constraint_data, label) 43 elif constraint_data.upper is not None: 44 label = 'c_u_%s_' % con_symbol 45 symbol_map.alias(constraint_data, label) 46 # 47 # Recursively iterate over all objectives 48 # 49 first = True 50 for objective_data in instance.component_data_objects(Objective, active=True): 51 symbol_map.getSymbol(objective_data, labeler) 52 if first: 53 # The first objective is the default 54 symbol_map.alias(objective_data, "__default_objective__") 55 first = False 56 # 57 # Return the symbol map 58 # 59 return symbol_map 60