Changeset 4092
 Timestamp:
 05/13/11 15:39:12 (4 years ago)
 File:

 1 edited
Legend:
 Unmodified
 Added
 Removed

coopr.pyomo/trunk/coopr/pyomo/io/cpxlp.py
r4077 r4092 308 308 return offset 309 309 310 # @deprecated 311 def _print_quadterm_arg(self, arg, is_minimizing, OUTPUT): 312 313 if isinstance(arg,expr._ProductExpression): 314 # NOTE: We need to handle quadratics defined with the 'pow' 315 # term here. 316 # WARNING: The following code is very specific to progressive 317 # hedging, with a very specific format assumed. We do need to 318 # handle more general expressions, but we'll worry about that 319 # at a latter time. 320 blend = arg._numerator[0]() 321 322 if blend is 1: 323 324 rho = arg._numerator[1]() 325 326 pow_expression = arg._numerator[2] 327 328 base = pow_expression._args[0] 329 exponent = pow_expression._args[1] 330 331 if not isinstance(base,expr._SumExpression): 332 msg = 'Quadratic term base must be a _SumExpression' 333 raise ValueError, msg 334 335 if not isinstance(exponent,NumericConstant): 336 msg = 'Quadratic term exponent must be a NumericConstant' 337 raise ValueError, msg 338 339 variable = base._args[0] 340 offset = base._args[1] 341 if variable.status is not VarStatus.unused: 342 343 sign = '' 344 if is_minimizing is True: 345 sign = '+' 346 print >>OUTPUT, '%s [ %s %s^2 ] / 2' % ( 347 sign, 348 str(rho), 349 variable.label 350 ) 351 352 sign = '' 353 if (is_minimizing is True) == (offset.value < 0): 354 sign = '+' 355 print >>OUTPUT, '%s %s %s' % ( 356 sign, 357 str(abs(rho*offset.value)), 358 variable.label 359 ) 360 361 objective_offset = (rho * offset.value*offset.value /2.0) 362 fmt = ' %s ONE_VAR_CONSTANT' 363 if is_minimizing is True: fmt = ' +%s ONE_VAR_CONSTANT' 364 print >>OUTPUT, fmt % str(objective_offset) 365 366 elif isinstance(arg,NumericConstant): 367 # this is the "0.0" element that forms the initial expression 368 #  the quadratic subexpressions aren't known to the presolve 369 # routines. ideally unnecessary  hacked in for now. 370 pass 371 372 else: 373 msg = '%s\nUnknown expression subtype found in quadratic ' \ 374 'objective expression' 375 raise ValueError, msg % `arg` 376 310 377 311 378 # @deprecated … … 314 381 # The LP format doesn't allow for expression of constant terms in the 315 382 # objective. A workaround involves tracking the sum of constant terms 316 # in the quadratic quadratic terms, and then writing that out with a317 # dummyvariable forced equal to one.383 # in the quadratic terms, and then writing that out with a dummy 384 # variable forced equal to one. 318 385 print >>OUTPUT, "" 319 for arg in x._args: 320 if isinstance(arg,expr._ProductExpression): 321 # NOTE: We need to handle quadratics defined with the 'pow' 322 # term here. 323 # WARNING: The following code is very specific to progressive 324 # hedging, with a very specific format assumed. We do need to 325 # handle more general expressions, but we'll worry about that 326 # at a latter time. 327 blend = arg._numerator[0]() 328 329 if blend is 1: 330 331 rho = arg._numerator[1]() 332 333 pow_expression = arg._numerator[2] 334 335 base = pow_expression._args[0] 336 exponent = pow_expression._args[1] 337 338 if not isinstance(base,expr._SumExpression): 339 msg = 'Quadratic term base must be a _SumExpression' 340 raise ValueError, msg 341 342 if not isinstance(exponent,NumericConstant): 343 msg = 'Quadratic term exponent must be a NumericConstant' 344 raise ValueError, msg 345 346 variable = base._args[0] 347 offset = base._args[1] 348 if variable.status is not VarStatus.unused: 349 350 sign = '' 351 if is_minimizing is True: 352 sign = '+' 353 print >>OUTPUT, '%s [ %s %s^2 ] / 2' % ( 354 sign, 355 str(rho), 356 variable.label 357 ) 358 359 sign = '' 360 if (is_minimizing is True) == (offset.value < 0): 361 sign = '+' 362 print >>OUTPUT, '%s %s %s' % ( 363 sign, 364 str(abs(rho*offset.value)), 365 variable.label 366 ) 367 368 objective_offset = (rho * offset.value*offset.value /2.0) 369 fmt = ' %s ONE_VAR_CONSTANT' 370 if is_minimizing is True: fmt = ' +%s ONE_VAR_CONSTANT' 371 print >>OUTPUT, fmt % str(objective_offset) 372 373 elif isinstance(arg,NumericConstant): 374 # this is the "0.0" element that forms the initial expression 375 #  the quadratic subexpressions aren't known to the presolve 376 # routines. ideally unnecessary  hacked in for now. 377 pass 378 379 else: 380 msg = '%s\nUnknown expression subtype found in quadratic ' \ 381 'objective expression' 382 raise ValueError, msg % `arg` 383 386 if isinstance(x, expr._SumExpression): # multiple terms means a sum expression 387 for arg in x._args: 388 self._print_quadterm_arg(arg, is_minimizing, OUTPUT) 389 elif isinstance(x, expr._ProductExpression): # if only a single term is involved, you have a product expression 390 self._print_quadterm_arg(x, is_minimizing, OUTPUT) 391 else: 392 msg = 'Unknown expression of type=%s encountered when printing quaratic term expression' 393 raise ValueError, msg % str(type(x)) 384 394 385 395 @staticmethod
Note: See TracChangeset
for help on using the changeset viewer.