1import pyomo.common.unittest as unittest 2from pyomo.common.tempfiles import TempfileManager 3import pyomo.environ as pe 4from pyomo.contrib import appsi 5from pyomo.contrib.appsi.cmodel import cmodel_available 6import os 7 8 9@unittest.skipUnless(cmodel_available, 'appsi extensions are not available') 10class TestNLWriter(unittest.TestCase): 11 def _write_and_check_header(self, m, correct_lines): 12 writer = appsi.writers.NLWriter() 13 with TempfileManager: 14 fname = TempfileManager.create_tempfile(suffix='.appsi.nl') 15 writer.write(m, fname) 16 with open(fname, 'r') as f: 17 for ndx, line in enumerate(list(f.readlines())[:10]): 18 self.assertTrue(line.startswith(correct_lines[ndx])) 19 20 def test_header_1(self): 21 m = pe.ConcreteModel() 22 m.x = pe.Var() 23 m.y = pe.Var() 24 m.obj = pe.Objective(expr=m.x + m.y) 25 m.c = pe.Constraint(expr=m.x + m.y == 1) 26 correct_lines = ['g3 1 1 0', 27 '2 1 1 0 1', 28 '0 0', 29 '0 0', 30 '0 0 0', 31 '0 0 0 1', 32 '0 0 0 0 0', 33 '2 2', 34 '0 0', 35 '0 0 0 0 0'] 36 self._write_and_check_header(m, correct_lines) 37 38 def test_header_2(self): 39 m = pe.ConcreteModel() 40 m.x = pe.Var() 41 m.y = pe.Var() 42 m.obj = pe.Objective(expr=m.x**2 + m.y) 43 m.c = pe.Constraint(expr=m.x + m.y == 1) 44 correct_lines = ['g3 1 1 0', 45 '2 1 1 0 1', 46 '0 1', 47 '0 0', 48 '0 1 0', 49 '0 0 0 1', 50 '0 0 0 0 0', 51 '2 2', 52 '0 0', 53 '0 0 0 0 0'] 54 self._write_and_check_header(m, correct_lines) 55 56 def test_header_3(self): 57 m = pe.ConcreteModel() 58 m.x = pe.Var() 59 m.y = pe.Var() 60 m.obj = pe.Objective(expr=m.x + m.y) 61 m.c = pe.Constraint(expr=m.x**2 + m.y == 1) 62 correct_lines = ['g3 1 1 0', 63 '2 1 1 0 1', 64 '1 0', 65 '0 0', 66 '1 0 0', 67 '0 0 0 1', 68 '0 0 0 0 0', 69 '2 2', 70 '0 0', 71 '0 0 0 0 0'] 72 self._write_and_check_header(m, correct_lines) 73 74 def test_header_4(self): 75 m = pe.ConcreteModel() 76 m.x = pe.Var() 77 m.y = pe.Var() 78 m.obj = pe.Objective(expr=m.x**2 + m.y) 79 m.c = pe.Constraint(expr=m.x**2 + m.y == 1) 80 correct_lines = ['g3 1 1 0', 81 '2 1 1 0 1', 82 '1 1', 83 '0 0', 84 '1 1 1', 85 '0 0 0 1', 86 '0 0 0 0 0', 87 '2 2', 88 '0 0', 89 '0 0 0 0 0'] 90 self._write_and_check_header(m, correct_lines) 91 92 def test_header_5(self): 93 m = pe.ConcreteModel() 94 m.x = pe.Var() 95 m.y = pe.Var() 96 m.obj = pe.Objective(expr=m.x**2 + m.y**2) 97 m.c = pe.Constraint(expr=m.x**2 + m.y == 1) 98 correct_lines = ['g3 1 1 0', 99 '2 1 1 0 1', 100 '1 1', 101 '0 0', 102 '1 2 1', 103 '0 0 0 1', 104 '0 0 0 0 0', 105 '2 2', 106 '0 0', 107 '0 0 0 0 0'] 108 self._write_and_check_header(m, correct_lines) 109 110 def test_header_6(self): 111 m = pe.ConcreteModel() 112 m.x = pe.Var() 113 m.y = pe.Var() 114 m.obj = pe.Objective(expr=m.x**2 + m.y) 115 m.c = pe.Constraint(expr=m.x**2 + m.y**2 == 1) 116 correct_lines = ['g3 1 1 0', 117 '2 1 1 0 1', 118 '1 1', 119 '0 0', 120 '2 1 1', 121 '0 0 0 1', 122 '0 0 0 0 0', 123 '2 2', 124 '0 0', 125 '0 0 0 0 0'] 126 self._write_and_check_header(m, correct_lines) 127 128 def test_header_7(self): 129 m = pe.ConcreteModel() 130 m.x = pe.Var() 131 m.y = pe.Var() 132 m.obj = pe.Objective(expr=m.x + m.y) 133 m.c = pe.Constraint(expr=m.x + m.y**2 == 1) 134 correct_lines = ['g3 1 1 0', 135 '2 1 1 0 1', 136 '1 0', 137 '0 0', 138 '1 0 0', 139 '0 0 0 1', 140 '0 0 0 0 0', 141 '2 2', 142 '0 0', 143 '0 0 0 0 0'] 144 self._write_and_check_header(m, correct_lines) 145 146 def test_header_8(self): 147 m = pe.ConcreteModel() 148 m.x = pe.Var() 149 m.y = pe.Var() 150 m.obj = pe.Objective(expr=m.x + m.y) 151 m.c = pe.Constraint(expr=m.x**2 + m.y**2 == 1) 152 correct_lines = ['g3 1 1 0', 153 '2 1 1 0 1', 154 '1 0', 155 '0 0', 156 '2 0 0', 157 '0 0 0 1', 158 '0 0 0 0 0', 159 '2 2', 160 '0 0', 161 '0 0 0 0 0'] 162 self._write_and_check_header(m, correct_lines) 163 164 def test_header_9(self): 165 m = pe.ConcreteModel() 166 m.x = pe.Var() 167 m.y = pe.Var() 168 m.obj = pe.Objective(expr=m.x + m.y**2) 169 m.c = pe.Constraint(expr=m.x + m.y == 1) 170 correct_lines = ['g3 1 1 0', 171 '2 1 1 0 1', 172 '0 1', 173 '0 0', 174 '0 1 0', 175 '0 0 0 1', 176 '0 0 0 0 0', 177 '2 2', 178 '0 0', 179 '0 0 0 0 0'] 180 self._write_and_check_header(m, correct_lines) 181 182 def test_header_10(self): 183 m = pe.ConcreteModel() 184 m.x = pe.Var() 185 m.y = pe.Var() 186 m.obj = pe.Objective(expr=m.x + m.y**2) 187 m.c = pe.Constraint(expr=m.x + m.y**2 == 1) 188 correct_lines = ['g3 1 1 0', 189 '2 1 1 0 1', 190 '1 1', 191 '0 0', 192 '1 1 1', 193 '0 0 0 1', 194 '0 0 0 0 0', 195 '2 2', 196 '0 0', 197 '0 0 0 0 0'] 198 self._write_and_check_header(m, correct_lines) 199 200 def test_header_11(self): 201 m = pe.ConcreteModel() 202 m.x = pe.Var() 203 m.y = pe.Var() 204 m.obj = pe.Objective(expr=m.x + m.y**2) 205 m.c = pe.Constraint(expr=m.x**2 + m.y == 1) 206 correct_lines = ['g3 1 1 0', 207 '2 1 1 0 1', 208 '1 1', 209 '0 0', 210 '1 2 0', 211 '0 0 0 1', 212 '0 0 0 0 0', 213 '2 2', 214 '0 0', 215 '0 0 0 0 0'] 216 self._write_and_check_header(m, correct_lines) 217 218 def test_header_12(self): 219 m = pe.ConcreteModel() 220 m.x = pe.Var() 221 m.y = pe.Var() 222 m.obj = pe.Objective(expr=m.x + m.y**2) 223 m.c = pe.Constraint(expr=m.x**2 + m.y**2 == 1) 224 correct_lines = ['g3 1 1 0', 225 '2 1 1 0 1', 226 '1 1', 227 '0 0', 228 '2 1 1', 229 '0 0 0 1', 230 '0 0 0 0 0', 231 '2 2', 232 '0 0', 233 '0 0 0 0 0'] 234 self._write_and_check_header(m, correct_lines) 235 236 def test_header_13(self): 237 m = pe.ConcreteModel() 238 m.x = pe.Var() 239 m.y = pe.Var() 240 m.obj = pe.Objective(expr=m.x**2 + m.y) 241 m.c = pe.Constraint(expr=m.x + m.y**2 == 1) 242 correct_lines = ['g3 1 1 0', 243 '2 1 1 0 1', 244 '1 1', 245 '0 0', 246 '1 2 0', 247 '0 0 0 1', 248 '0 0 0 0 0', 249 '2 2', 250 '0 0', 251 '0 0 0 0 0'] 252 self._write_and_check_header(m, correct_lines) 253 254 def test_header_14(self): 255 m = pe.ConcreteModel() 256 m.x = pe.Var() 257 m.y = pe.Var() 258 m.obj = pe.Objective(expr=m.x**2 + m.y**2) 259 m.c = pe.Constraint(expr=m.x + m.y == 1) 260 correct_lines = ['g3 1 1 0', 261 '2 1 1 0 1', 262 '0 1', 263 '0 0', 264 '0 2 0', 265 '0 0 0 1', 266 '0 0 0 0 0', 267 '2 2', 268 '0 0', 269 '0 0 0 0 0'] 270 self._write_and_check_header(m, correct_lines) 271 272 def test_header_15(self): 273 m = pe.ConcreteModel() 274 m.x = pe.Var() 275 m.y = pe.Var() 276 m.obj = pe.Objective(expr=m.x**2 + m.y**2) 277 m.c = pe.Constraint(expr=m.x + m.y**2 == 1) 278 correct_lines = ['g3 1 1 0', 279 '2 1 1 0 1', 280 '1 1', 281 '0 0', 282 '1 2 1', 283 '0 0 0 1', 284 '0 0 0 0 0', 285 '2 2', 286 '0 0', 287 '0 0 0 0 0'] 288 self._write_and_check_header(m, correct_lines) 289 290 def test_header_16(self): 291 m = pe.ConcreteModel() 292 m.x = pe.Var() 293 m.y = pe.Var() 294 m.obj = pe.Objective(expr=m.x**2 + m.y**2) 295 m.c = pe.Constraint(expr=m.x**2 + m.y**2 == 1) 296 correct_lines = ['g3 1 1 0', 297 '2 1 1 0 1', 298 '1 1', 299 '0 0', 300 '2 2 2', 301 '0 0 0 1', 302 '0 0 0 0 0', 303 '2 2', 304 '0 0', 305 '0 0 0 0 0'] 306 self._write_and_check_header(m, correct_lines) 307