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)
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 test_iis_no_variable_values(self): with pyomo.opt.ReaderFactory("sol") as reader: if reader is None: raise IOError("Reader 'sol' is not registered") result = reader(currdir+"iis_no_variable_values.sol", suffixes=["iis"]) soln = result.solution(0) self.assertEqual(len(list(soln.variable['v0'].keys())), 1) self.assertEqual(soln.variable['v0']['iis'], 1) self.assertEqual(len(list(soln.variable['v1'].keys())), 1) self.assertEqual(soln.variable['v1']['iis'], 1) self.assertEqual(len(list(soln.constraint['c0'].keys())), 1) self.assertEqual(soln.constraint['c0']['Iis'], 4) import pyomo.kernel as pmo m = pmo.block() m.v0 = pmo.variable() m.v1 = pmo.variable() m.c0 = pmo.constraint() m.iis = pmo.suffix(direction=pmo.suffix.IMPORT) from pyomo.core.expr.symbol_map import SymbolMap soln.symbol_map = SymbolMap() soln.symbol_map.addSymbol(m.v0, 'v0') soln.symbol_map.addSymbol(m.v1, 'v1') soln.symbol_map.addSymbol(m.c0, 'c0') m.load_solution(soln) pmo.pprint(m.iis) self.assertEqual(m.iis[m.v0], 1) self.assertEqual(m.iis[m.v1], 1) self.assertEqual(m.iis[m.c0], 4)
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)
def test_expr_xfrm(self): from pyomo.repn.plugins.gams_writer import (expression_to_string, StorageTreeChecker) from pyomo.core.expr.symbol_map import SymbolMap M = ConcreteModel() M.abc = Var() smap = SymbolMap() tc = StorageTreeChecker(M) expr = M.abc**2.0 self.assertEqual(str(expr), "abc**2.0") self.assertEqual(expression_to_string(expr, tc, smap=smap), "power(abc, 2.0)") expr = log(M.abc**2.0) self.assertEqual(str(expr), "log(abc**2.0)") self.assertEqual(expression_to_string(expr, tc, smap=smap), "log(power(abc, 2.0))") expr = log(M.abc**2.0) + 5 self.assertEqual(str(expr), "log(abc**2.0) + 5") self.assertEqual(expression_to_string(expr, tc, smap=smap), "log(power(abc, 2.0)) + 5") expr = exp(M.abc**2.0) + 5 self.assertEqual(str(expr), "exp(abc**2.0) + 5") self.assertEqual(expression_to_string(expr, tc, smap=smap), "exp(power(abc, 2.0)) + 5") expr = log(M.abc**2.0)**4 self.assertEqual(str(expr), "log(abc**2.0)**4") self.assertEqual(expression_to_string(expr, tc, smap=smap), "power(log(power(abc, 2.0)), 4)") expr = log(M.abc**2.0)**4.5 self.assertEqual(str(expr), "log(abc**2.0)**4.5") self.assertEqual(expression_to_string(expr, tc, smap=smap), "log(power(abc, 2.0)) ** 4.5")
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))
def test_no_labeler(self): s = SymbolMap() v = variable() self.assertEquals(str(v), s.getSymbol(v))