|
9 | 9 | # This software is distributed under the 3-clause BSD License. |
10 | 10 | # ___________________________________________________________________________ |
11 | 11 |
|
12 | | -from pyomo.common.dependencies import pandas as pd, pandas_available |
| 12 | +from pyomo.common.dependencies import ( |
| 13 | + pandas as pd, |
| 14 | + pandas_available, |
| 15 | + numpy as np, |
| 16 | + numpy_available, |
| 17 | +) |
| 18 | + |
| 19 | +import os.path |
| 20 | +import json |
13 | 21 |
|
14 | 22 | import pyomo.environ as pyo |
| 23 | + |
| 24 | +from pyomo.common.fileutils import this_file_dir |
15 | 25 | import pyomo.common.unittest as unittest |
| 26 | + |
16 | 27 | import pyomo.contrib.parmest.parmest as parmest |
17 | 28 | from pyomo.opt import SolverFactory |
18 | 29 |
|
19 | | -ipopt_available = SolverFactory("ipopt").available() |
| 30 | +from pyomo.contrib.parmest.utils.model_utils import update_model_from_suffix |
| 31 | +from pyomo.contrib.doe.examples.reactor_example import ( |
| 32 | + ReactorExperiment as FullReactorExperiment, |
| 33 | +) |
| 34 | + |
| 35 | +currdir = this_file_dir() |
| 36 | +file_path = os.path.join(currdir, "..", "..", "doe", "examples", "result.json") |
| 37 | + |
| 38 | +with open(file_path) as f: |
| 39 | + data_ex = json.load(f) |
| 40 | +data_ex["control_points"] = {float(k): v for k, v in data_ex["control_points"].items()} |
| 41 | + |
| 42 | +ipopt_available = pyo.SolverFactory("ipopt").available() |
20 | 43 |
|
21 | 44 |
|
22 | 45 | @unittest.skipIf( |
@@ -60,6 +83,126 @@ def test_convert_param_to_var(self): |
60 | 83 | self.assertEqual(pyo.value(c), pyo.value(c_old)) |
61 | 84 | self.assertTrue(c in m_vars.unknown_parameters) |
62 | 85 |
|
| 86 | + def test_update_model_from_suffix_experiment_outputs(self): |
| 87 | + from pyomo.contrib.parmest.examples.reactor_design.reactor_design import ( |
| 88 | + ReactorDesignExperiment, |
| 89 | + ) |
| 90 | + |
| 91 | + data = pd.DataFrame( |
| 92 | + data=[ |
| 93 | + [1.05, 10000, 3458.4, 1060.8, 1683.9, 1898.5], |
| 94 | + [1.10, 10000, 3535.1, 1064.8, 1613.3, 1893.4], |
| 95 | + [1.15, 10000, 3609.1, 1067.8, 1547.5, 1887.8], |
| 96 | + ], |
| 97 | + columns=["sv", "caf", "ca", "cb", "cc", "cd"], |
| 98 | + ) |
| 99 | + experiment = ReactorDesignExperiment(data, 0) |
| 100 | + test_model = experiment.get_labeled_model() |
| 101 | + |
| 102 | + suffix_obj = test_model.experiment_outputs # a Suffix |
| 103 | + var_list = list(suffix_obj.keys()) # components |
| 104 | + orig_var_vals = np.array([pyo.value(v) for v in var_list]) |
| 105 | + orig_suffix_val = np.array([tag for _, tag in suffix_obj.items()]) |
| 106 | + new_vals = orig_var_vals + 0.5 |
| 107 | + # Update the model from the suffix |
| 108 | + update_model_from_suffix(suffix_obj, new_vals) |
| 109 | + # ── Check results ──────────────────────────────────────────────────── |
| 110 | + new_var_vals = np.array([pyo.value(v) for v in var_list]) |
| 111 | + new_suffix_val = np.array(list(suffix_obj.values())) |
| 112 | + # (1) Variables have been overwritten with `new_vals` |
| 113 | + self.assertTrue(np.allclose(new_var_vals, new_vals)) |
| 114 | + # (2) Suffix tags are unchanged |
| 115 | + self.assertTrue(np.array_equal(new_suffix_val, orig_suffix_val)) |
| 116 | + |
| 117 | + def test_update_model_from_suffix_measurement_error(self): |
| 118 | + experiment = FullReactorExperiment(data_ex, 10, 3) |
| 119 | + test_model = experiment.get_labeled_model() |
| 120 | + |
| 121 | + suffix_obj = test_model.measurement_error # a Suffix |
| 122 | + var_list = list(suffix_obj.keys()) # components |
| 123 | + orig_var_vals = np.array([suffix_obj[v] for v in var_list]) |
| 124 | + new_vals = orig_var_vals + 0.5 |
| 125 | + # Update the model from the suffix |
| 126 | + update_model_from_suffix(suffix_obj, new_vals) |
| 127 | + # ── Check results ──────────────────────────────────────────────────── |
| 128 | + new_var_vals = np.array([suffix_obj[v] for v in var_list]) |
| 129 | + # (1) Variables have been overwritten with `new_vals` |
| 130 | + self.assertTrue(np.allclose(new_var_vals, new_vals)) |
| 131 | + |
| 132 | + def test_update_model_from_suffix_length_mismatch(self): |
| 133 | + m = pyo.ConcreteModel() |
| 134 | + |
| 135 | + # Create a suffix with a Var component |
| 136 | + m.x = pyo.Var(initialize=0.0) |
| 137 | + m.unknown_parameters = pyo.Suffix(direction=pyo.Suffix.LOCAL) |
| 138 | + m.unknown_parameters[m.x] = 0.0 # tag a Var |
| 139 | + with self.assertRaisesRegex( |
| 140 | + ValueError, "values length does not match suffix length" |
| 141 | + ): |
| 142 | + # Attempt to update with a list of different length |
| 143 | + update_model_from_suffix(m.unknown_parameters, [42, 43, 44]) |
| 144 | + |
| 145 | + def test_update_model_from_suffix_not_numeric(self): |
| 146 | + m = pyo.ConcreteModel() |
| 147 | + |
| 148 | + # Create a suffix with a Var component |
| 149 | + m.x = pyo.Var(initialize=0.0) |
| 150 | + m.y = pyo.Var(initialize=1.0) |
| 151 | + bad_value = "not_a_number" |
| 152 | + m.unknown_parameters = pyo.Suffix(direction=pyo.Suffix.LOCAL) |
| 153 | + # Make multiple values |
| 154 | + m.unknown_parameters[m.x] = 0.0 # tag a Var |
| 155 | + m.unknown_parameters[m.y] = bad_value # tag a Var with a bad value |
| 156 | + # Attempt to update with a list of mixed types |
| 157 | + # This should raise an error because this utility only allows numeric values |
| 158 | + # in the model to be updated. |
| 159 | + |
| 160 | + with self.assertRaisesRegex( |
| 161 | + ValueError, f"could not convert string to float: '{bad_value}'" |
| 162 | + ): |
| 163 | + # Attempt to update with a non-numeric value |
| 164 | + update_model_from_suffix(m.unknown_parameters, [42, bad_value]) |
| 165 | + |
| 166 | + def test_update_model_from_suffix_wrong_component_type(self): |
| 167 | + m = pyo.ConcreteModel() |
| 168 | + |
| 169 | + # Create a suffix with a Var component |
| 170 | + m.x = pyo.Var(initialize=0.0) |
| 171 | + m.e = pyo.Expression(expr=m.x + 1) # not Var/Param |
| 172 | + m.unknown_parameters = pyo.Suffix(direction=pyo.Suffix.LOCAL) |
| 173 | + m.unknown_parameters[m.x] = 0.0 |
| 174 | + m.unknown_parameters[m.e] = 1.0 # tag an Expression |
| 175 | + # Attempt to update with a list of wrong component type |
| 176 | + with self.assertRaisesRegex( |
| 177 | + TypeError, |
| 178 | + f"Unsupported component type {type(m.e)}; expected VarData or ParamData.", |
| 179 | + ): |
| 180 | + update_model_from_suffix(m.unknown_parameters, [42, 43]) |
| 181 | + |
| 182 | + def test_update_model_from_suffix_unsupported_component(self): |
| 183 | + m = pyo.ConcreteModel() |
| 184 | + |
| 185 | + # Create a suffix with a ConstraintData component |
| 186 | + m.x = pyo.Var(initialize=0.0) |
| 187 | + m.c = pyo.Constraint(expr=m.x == 0) # not Var/Param! |
| 188 | + |
| 189 | + m.bad_suffix = pyo.Suffix(direction=pyo.Suffix.LOCAL) |
| 190 | + m.bad_suffix[m.c] = 0 # tag a Constraint |
| 191 | + |
| 192 | + with self.assertRaisesRegex( |
| 193 | + TypeError, r"Unsupported component type .*Constraint.*" |
| 194 | + ): |
| 195 | + update_model_from_suffix(m.bad_suffix, [1.0]) |
| 196 | + |
| 197 | + def test_update_model_from_suffix_empty(self): |
| 198 | + m = pyo.ConcreteModel() |
| 199 | + |
| 200 | + # Create an empty suffix |
| 201 | + m.empty_suffix = pyo.Suffix(direction=pyo.Suffix.LOCAL) |
| 202 | + |
| 203 | + # This should not raise an error |
| 204 | + update_model_from_suffix(m.empty_suffix, []) |
| 205 | + |
63 | 206 |
|
64 | 207 | if __name__ == "__main__": |
65 | 208 | unittest.main() |
0 commit comments