def store_to(self, results, cuid=False, skip_stale_vars=False): """ Return a Solution() object that is populated with the values in the model. """ instance = self._instance() results.solution.clear() results._smap_id = None for soln_ in self.solutions: soln = Solution() soln._cuid = cuid for key, val in soln_._metadata.items(): setattr(soln, key, val) if cuid: labeler = CuidLabeler() else: labeler = CNameLabeler() sm = SymbolMap() entry = soln_._entry['objective'] for obj in instance.component_data_objects(Objective, active=True): vals = entry.get(id(obj), None) if vals is None: vals = {} else: vals = vals[1] vals['Value'] = value(obj) soln.objective[sm.getSymbol(obj, labeler)] = vals entry = soln_._entry['variable'] for obj in instance.component_data_objects(Var, active=True): if obj.stale and skip_stale_vars: continue vals = entry.get(id(obj), None) if vals is None: vals = {} else: vals = vals[1] vals['Value'] = value(obj) soln.variable[sm.getSymbol(obj, labeler)] = vals entry = soln_._entry['constraint'] for obj in instance.component_data_objects(Constraint, active=True): vals = entry.get(id(obj), None) if vals is None: continue else: vals = vals[1] soln.constraint[sm.getSymbol(obj, labeler)] = vals results.solution.insert(soln)
def store_to(self, results, cuid=False): """ Return a Solution() object that is populated with the values in the model. """ instance = self._instance() results.solution.clear() results._smap_id = None for soln_ in self.solutions: soln = Solution() soln._cuid = cuid for key, val in iteritems(soln_._metadata): setattr(soln, key, val) if cuid: labeler = CuidLabeler() else: labeler = CNameLabeler() sm = SymbolMap() entry = soln_._entry['objective'] for obj in instance.component_data_objects(Objective, active=True): vals = entry.get(id(obj), None) if vals is None: vals = {} else: vals = vals[1] vals['Value'] = value(obj) soln.objective[ sm.getSymbol(obj, labeler) ] = vals entry = soln_._entry['variable'] for obj in instance.component_data_objects(Var, active=True): if obj.stale: continue vals = entry.get(id(obj), None) if vals is None: vals = {} else: vals = vals[1] vals['Value'] = value(obj) soln.variable[ sm.getSymbol(obj, labeler) ] = vals entry = soln_._entry['constraint'] for obj in instance.component_data_objects(Constraint, active=True): vals = entry.get(id(obj), None) if vals is None: continue else: vals = vals[1] soln.constraint[ sm.getSymbol(obj, labeler) ] = vals results.solution.insert( soln )
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_no_labeler(self): s = SymbolMap() v = variable() self.assertEquals(str(v), s.getSymbol(v))