def test_find_wildcard(self): cuid = ComponentUID('b:1,$2.c.a:*') comp = cuid.find_component_on(self.m) self.assertIs(comp.ctype, Param) cList = list(comp.values()) self.assertEqual(len(cList), 3) self.assertEqual(cList, list(self.m.b[1, '2'].c.a[:])) cuid = ComponentUID('b[*,*]') comp = cuid.find_component_on(self.m) self.assertIs(comp.ctype, Block) cList = list(comp.values()) self.assertEqual(len(cList), 9) self.assertEqual(cList, list(self.m.b.values()))
def test_pickle_index(self): m = ConcreteModel() m.b = Block(Any) idx = "|b'foo'" m.b[idx].x = Var() cuid = ComponentUID(m.b[idx].x) self.assertEqual(str(cuid), 'b["|b\'foo\'"].x') self.assertIs(cuid.find_component_on(m), m.b[idx].x) tmp = ComponentUID(str(cuid)) self.assertIsNot(cuid, tmp) self.assertEqual(cuid, tmp) self.assertIs(tmp.find_component_on(m), m.b[idx].x) tmp = pickle.loads(pickle.dumps(cuid)) self.assertIsNot(cuid, tmp) self.assertEqual(cuid, tmp) self.assertIs(tmp.find_component_on(m), m.b[idx].x) idx = _Foo(1, 'a') m.b[idx].x = Var() cuid = ComponentUID(m.b[idx].x) # Note that the pickle string for namedtuple changes between # Python 2, 3, and pypy, so we will just check the non-pickle # data part self.assertRegex(str(cuid), r"^b\[\|b?(['\"]).*\.\1\]\.x$") self.assertIs(cuid.find_component_on(m), m.b[idx].x) tmp = ComponentUID(str(cuid)) self.assertIsNot(cuid, tmp) self.assertEqual(cuid, tmp) self.assertIs(tmp.find_component_on(m), m.b[idx].x) tmp = pickle.loads(pickle.dumps(cuid)) self.assertIsNot(cuid, tmp) self.assertEqual(cuid, tmp) self.assertIs(tmp.find_component_on(m), m.b[idx].x) idx = datetime(1, 2, 3) m.b[idx].x = Var() cuid = ComponentUID(m.b[idx].x) # Note that the pickle string for namedtuple changes between # Python 2, 3, and pypy, so we will just check the non-pickle # data part self.assertRegex(str(cuid), r"^b\[\|b?(['\"]).*\.\1\]\.x$") self.assertIs(cuid.find_component_on(m), m.b[idx].x) tmp = ComponentUID(str(cuid)) self.assertIsNot(cuid, tmp) self.assertEqual(cuid, tmp) self.assertIs(tmp.find_component_on(m), m.b[idx].x) tmp = pickle.loads(pickle.dumps(cuid)) self.assertIsNot(cuid, tmp) self.assertEqual(cuid, tmp) self.assertIs(tmp.find_component_on(m), m.b[idx].x) self.assertEqual(len(m.b), 3)
def test_find_wildcard_partial_exists(self): # proper Reference: to ComponentData cuid = ComponentUID('b[*,*].c.a[**]') comp = cuid.find_component_on(self.m) self.assertIs(comp.ctype, Param) cList = list(comp.values()) self.assertEqual(len(cList), 3) self.assertEqual(cList, list(self.m.b[1, '2'].c.a[:])) # improper Reference: to IndexedComponent cuid = ComponentUID('b[*,*].c.a') comp = cuid.find_component_on(self.m) self.assertIs(comp.ctype, IndexedComponent) cList = list(comp.values()) self.assertEqual(len(cList), 1) self.assertIs(cList[0], self.m.b[1, '2'].c.a)
def _create_parmest_model(self, data): """ Modify the Pyomo model for parameter estimation """ from pyomo.core import Objective model = self.model_function(data) if (len(self.theta_names) == 1) and (self.theta_names[0] == 'parmest_dummy_var'): model.parmest_dummy_var = pyo.Var(initialize=1.0) for i, theta in enumerate(self.theta_names): # First, leverage the parser in ComponentUID to locate the # component. If that fails, fall back on the original # (insecure) use of 'eval' var_cuid = ComponentUID(theta) var_validate = var_cuid.find_component_on(model) if var_validate is None: logger.warning( "theta_name[%s] (%s) was not found on the model", (i, theta)) else: try: # If the component that was found is not a variable, # this will generate an exception (and the warning # in the 'except') var_validate.fixed = False # We want to standardize on the CUID string # representation (which is what PySP will use # internally) self.theta_names[i] = repr(var_cuid) except: logger.warning(theta + ' is not a variable') if self.obj_function: for obj in model.component_objects(Objective): obj.deactivate() def FirstStageCost_rule(model): return 0 model.FirstStageCost = pyo.Expression(rule=FirstStageCost_rule) model.SecondStageCost = pyo.Expression( rule=_SecondStateCostExpr(self.obj_function, data)) def TotalCost_rule(model): return model.FirstStageCost + model.SecondStageCost model.Total_Cost_Objective = pyo.Objective(rule=TotalCost_rule, sense=pyo.minimize) self.parmest_model = model return model
def get_dsdp(model, theta_names, theta, var_dic={}, tee=False, solver_options=None): """This function calculates gradient vector of the (decision variables, parameters) with respect to the paramerters (theta_names). e.g) min f: p1*x1+ p2*(x2^2) + p1*p2 s.t c1: x1 + x2 = p1 c2: x2 + x3 = p2 0 <= x1, x2, x3 <= 10 p1 = 10 p2 = 5 the function retuns dx/dp and dp/dp, and colum orders. The following terms are used to define the output dimensions: Ncon = number of constraints Nvar = number of variables (Nx + Ntheta) Nx = the numer of decision (primal) variables Ntheta = number of uncertain parameters. Parameters ---------- model: Pyomo ConcreteModel model should includes an objective function theta_names: list of strings List of Var names theta: dict Estimated parameters e.g) from parmest tee: bool, optional Indicates that ef solver output should be teed solver_options: dict, optional Provides options to the solver (also the name of an attribute) var_dic: dictionary If any original variable contains "'", need an auxiliary dictionary with keys theta_names without "'", values with "'". e.g) var_dic= {'fs.properties.tau[benzene,toluene]': "fs.properties.tau['benzene','toluene']", 'fs.properties.tau[toluene,benzene]': "fs.properties.tau['toluene','benzene']"} Returns ------- dsdp: scipy.sparse.csr.csr_matrix Ntheta by Nvar size sparse matrix. A Jacobian matrix of the (decision variables, parameters) with respect to paramerters (=theta_name). number of rows = len(theta_name), number of columns= len(col) col: list List of variable names """ m = model.clone() original_Param = [] perturbed_Param = [] m.extra = ConstraintList() kk = 0 if var_dic == {}: for i in theta_names: var_dic[i] = i ''' for v in theta_names: v_tmp = str(kk) original_param_object = Param(initialize=theta[v], mutable=True) perturbed_param_object = Param(initialize=theta[v]) m.add_component("original_"+v_tmp, original_param_object) m.add_component("perturbed_"+v_tmp, perturbed_param_object) m.extra.add(eval('m.'+var_dic[v]) - eval('m.original_'+v_tmp) == 0 ) original_Param.append(original_param_object) perturbed_Param.append(perturbed_param_object) kk = kk + 1 m_kaug_dsdp = sensitivity_calculation('kaug',m,original_Param, perturbed_Param, tee) ''' for i, name in enumerate(theta_names): orig_param = Param(initialize=theta[name], mutable=True) ptb_param = Param(initialize=theta[name]) m.add_component("original_%s" % i, orig_param) m.add_component("perturbed_%s" % i, ptb_param) cuid = ComponentUID(name) var = cuid.find_component_on(m) m.extra.add(var - orig_param == 0) original_Param.append(orig_param) perturbed_Param.append(ptb_param) m_kaug_dsdp = sensitivity_calculation('kaug', m, original_Param, perturbed_Param, tee) try: with open("./dsdp/col_row.col", "r") as myfile: col = myfile.read().splitlines() with open("./dsdp/col_row.row", "r") as myfile: row = myfile.read().splitlines() dsdp = np.loadtxt("./dsdp/dsdp_in_.in") except Exception as e: print('File not found.') dsdp = dsdp.reshape((len(theta_names), int(len(dsdp) / len(theta_names)))) dsdp = dsdp[:len(theta_names), :len(col)] try: shutil.rmtree('dsdp', ignore_errors=True) except OSError: pass col = [ i for i in col if SensitivityInterface.get_default_block_name() not in i ] dsdp_out = np.zeros((len(theta_names), len(col))) # e.g) k_aug dsdp returns -dx1/dx1 = -1.0 for i in range(len(theta_names)): for j in range(len(col)): if SensitivityInterface.get_default_block_name() not in col[j]: dsdp_out[i, j] = -dsdp[i, j] return sparse.csr_matrix(dsdp_out), col
def test_find_explicit_notExists_2(self): cuid = ComponentUID('b:$1,2.c.a:3') self.assertTrue(cuid.find_component_on(self.m) is None)
def test_find_implicit_notExists_1(self): cuid = ComponentUID('b:1,2.c.a:4') self.assertTrue(cuid.find_component_on(self.m) is None)
def test_find_wildcard_not_exists(self): cuid = ComponentUID('b[*,*].c.x') self.assertIsNone(cuid.find_component_on(self.m))
def test_find_component_exists_1(self): ref = self.m.b[1, '2'].c.a cuid = ComponentUID(ref) self.assertTrue(cuid.find_component_on(self.m) is ref)