def handle_results(self, res): # sympy to pyomo converter self._results[Metrics.RMSE] = self.alamopy_results['rmse'] self._results[Metrics.SSE] = self.alamopy_results['ssr'] self._results[Metrics.Time] = self.alamopy_results['totaltime'] self._results[Metrics.MSE] = float(self.alamopy_results['rmse']) ** 2 self._results[Metrics.Order] = self.alamopy_results['nbas'] self._results[Metrics.R2] = self.alamopy_results['R2'] # Generate pyomo expression m = pyo.ConcreteModel() m.x = pyo.Var(range(len(self._rdata_in))) obj_map = PyomoSympyBimap() obj_map.sympy2pyomo = {} sympy_locals = {} i = 1 for label in res['xlabels']: sympy_locals[label] = sympy.Symbol(label) sympy_obj = sympy.Symbol(label) obj_map.sympy2pyomo[sympy_obj] = m.x[i] i += 1 model_string = "" if type(res['model']) is dict: key = list(res['model'].keys())[0] model_string = res['model'][key].split('=')[1] else: model_string = res['model'].split('=')[1] model_symp = parse_expr(model_string.replace("^", "**"), local_dict=sympy_locals) model_pyomo = sympy2pyomo_expression(model_symp, obj_map) self._model = model_pyomo
def test_errors(self): m = ConcreteModel() m.x = Var() self.assertRaisesRegexp( ValueError, "Must specify exactly one of wrt and wrt_list", differentiate, m.x, wrt=m.x, wrt_list=[m.x]) obj_map = PyomoSympyBimap() class bogus(object): def __init__(self): self._args = (obj_map.getSympySymbol(m.x),) self.assertRaisesRegexp( DeveloperError, "sympy expression .* not found in the operator map", sympy2pyomo_expression, bogus(), obj_map)