Changeset 3248
 Timestamp:
 Nov 17, 2010 3:51:47 PM (10 years ago)
 File:

 1 edited
Legend:
 Unmodified
 Added
 Removed

coopr.pyomo/trunk/coopr/pyomo/io/cpxlp.py
r3166 r3248 238 238 # 239 239 if 2 in x: 240 quadterms = list() 241 keys = sorted( x[2].keys() ) 242 for id in keys: 243 coef = x[2][id] 244 240 241 print >>OUTPUT, "+ [" 242 243 for id in sorted(x[2].keys()): 244 245 coefficient = x[2][id] 245 246 sign = '+' 246 if coef < 0: sign = '' 247 if coefficient < 0: 248 sign = '' 249 coefficient = math.fabs(coefficient) 250 247 251 if is_objective: 248 coef *= 2252 coefficient *= 2 249 253 # times 2 because LP format requires /2 for all the quadratic 250 254 # terms /of the objective only/. Discovered the last bit thru … … 252 256 # Ref: ILog CPlex 8.0 User's Manual, p197. 253 257 254 coef = str( math.fabs(coef) ) + ' '255 256 quad = list()258 print >>OUTPUT, sign, coefficient, 259 260 term_variables = [] 257 261 for var in id: 258 262 label = x[1][var].label 259 263 if not label: 260 self._no_label_error( x[1][var] ) 261 262 name = convert_name( label ) # like X(3,1), 263 quad.append( name ) 264 265 term = ' ^2' # same var (e.g. X**2) ... 266 if ( 2 == len(quad) ): # ... unless we've 2 vars 267 term = ' * ' + quad[1] # different vars (e.g. X*Y) 268 term = sign + coef + quad[0] + term 269 quadterms.append( term ) 270 271 quad_output = '+ [ %s ]' % ' '.join( quadterms ) 264 self._no_label_error(x[1][var]) 265 266 name = convert_name(label) # like X(3,1), 267 term_variables.append(name) 268 269 if len(term_variables) == 2: 270 print >>OUTPUT, term_variables[1],"*",term_variables[1], 271 else: 272 print >>OUTPUT, term_variables[0],"^ 2", 273 274 print >>OUTPUT, "" 275 276 print >>OUTPUT, "]", 272 277 if is_objective: 273 quad_output += ' / 2' 274 # divide by 2 because LP format requires /2 for all the quadratic 275 # terms. Weird. Ref: ILog CPlex 8.0 User's Manual, p197 276 277 output.append( quad_output ) 278 print >>OUTPUT, ' / 2' 279 # divide by 2 because LP format requires /2 for all the quadratic 280 # terms. Weird. Ref: ILog CPlex 8.0 User's Manual, p197 281 else: 282 print >>OUTPUT, "" 283 278 284 279 285 #
Note: See TracChangeset
for help on using the changeset viewer.