Changeset 4158
 Timestamp:
 06/03/11 16:17:46 (4 years ago)
 File:

 1 edited
Legend:
 Unmodified
 Added
 Removed

coopr.pyomo/trunk/coopr/pyomo/io/ampl.py
r4146 r4158 29 29 import logging 30 30 logger = logging.getLogger('coopr.pyomo') 31 32 ampl_var_id = {} 31 33 32 34 class ProblemWriter_nl(AbstractProblemWriter): … … 167 169 168 170 169 # elif isinstance(exp, expr._SumExpression):170 # n = len(exp._args)171 # if exp._const != 0.0:172 # print >>OUTPUT, "o0 #+"173 # print >>OUTPUT, "n%s" %str(exp._const)174 #175 # for i in xrange(0,n1):176 # print >>OUTPUT, "o0 #+"177 # if exp._coef[i] != 1:178 # print >>OUTPUT, "o2 #*"179 # print >>OUTPUT, "n" + str(exp._coef[i])180 # self._print_nonlinear_terms_NL(OUTPUT, exp._args[i])181 #182 # if exp._coef[n1] != 1:183 # print >>OUTPUT, "o2 #*"184 # print >>OUTPUT, "n" + str(exp._coef[n1])185 # self._print_nonlinear_terms_NL(OUTPUT, exp._args[n1])186 #187 # elif isinstance(exp, list):188 # # this is an implied summation of expressions (we did not create a new sum expression for efficienc#y)189 # # this should be a list of tuples where [0] is the coeff and [1] is the expr to write190 # n = len(exp)191 # for i in xrange(0,n):192 # assert(isinstance(exp[i],tuple))193 # coef = exp[i][0]194 # child_exp = exp[i][1]195 # if i != n1:196 # # need the + op if it is not the last entry in the list197 # print >>OUTPUT, "o0 #+"198 #199 # if coef != 1.0:200 # print >>OUTPUT, "o2 #*"201 # print >>OUTPUT, "n" + str(coef)202 # self._print_nonlinear_terms_NL(OUTPUT, child_exp)203 204 171 elif isinstance(exp, expr._ProductExpression): 205 172 denom_exists = False … … 242 209 243 210 elif (isinstance(exp,var._VarValue) or isinstance(exp,var.Var)) and not exp.fixed_value(): 244 print >>OUTPUT, "v%s #%s" %( exp.ampl_var_id, exp.name)211 print >>OUTPUT, "v%s #%s" %(ampl_var_id[exp.name], exp.name) 245 212 246 213 elif isinstance(exp,param._ParamValue): … … 267 234 Con = model.active_components(Constraint) 268 235 Con.update( model.active_components(ConstraintList) ) 236 269 237 Vars = dict() # will be the entire list of all vars used in the problem 238 # nonlinear continuous variables in objectives and constraints 239 nlbv = dict() 240 # nonlinear integer variables in objectives and constraints 241 nlbvi = dict() 242 # nonlinear continuous variables in constraints 243 nlvc = dict() 244 # nonlinear integer variables in constraints 245 nlvci = dict() 246 # nonlinear continuous variables in objectives 247 nlvo = dict() 248 # nonlinear integer variables in objectives 249 nlvoi = dict() 250 # linear variables 251 lin_vars = dict() 270 252 # 271 253 # Count number of objectives and build the ampl_repns … … 285 267 Vars.update(ampl_repn._linear_terms_var) 286 268 Vars.update(ampl_repn._nonlinear_vars) 287 269 270 for key in ampl_repn._linear_terms_var: 271 if key not in ampl_repn._nonlinear_vars: 272 lin_vars[key] = ampl_repn._linear_terms_var[key] 273 for key in ampl_repn._nonlinear_vars: 274 if isinstance(ampl_repn._nonlinear_vars[key].domain, BooleanSet) or isinstance(ampl_repn._nonlinear_vars[key].domain, IntegerSet): 275 nlvoi[key] = ampl_repn._nonlinear_vars[key] 276 else: 277 nlvo[key] = ampl_repn._nonlinear_vars[key] 278 288 279 ObjVars.update(ampl_repn._linear_terms_var.keys()) 289 280 ObjVars.update(ampl_repn._nonlinear_vars.keys()) … … 321 312 ConVars.update(all_vars.keys()) 322 313 ConNonlinearVars.update(ampl_repn._nonlinear_vars.keys()) 314 315 for var_name in ampl_repn._linear_terms_var: 316 if var_name not in ampl_repn._nonlinear_vars: 317 lin_vars[var_name] = ampl_repn._linear_terms_var[var_name] 318 for var_name in ampl_repn._nonlinear_vars: 319 if isinstance(ampl_repn._nonlinear_vars[var_name].domain, BooleanSet) or isinstance(ampl_repn._nonlinear_vars[var_name].domain, IntegerSet): 320 nlvci[var_name] = ampl_repn._nonlinear_vars[var_name] 321 else: 322 nlvc[var_name] = ampl_repn._nonlinear_vars[var_name] 323 323 324 if ampl_repn.is_nonlinear(): 324 325 n_nonlinear_constraints += 1 325 326 326 327 L = None 327 328 U = None … … 350 351 Blist = [] 351 352 Vlist = [] 353 for key in nlvo: 354 if key in nlvc: 355 nlbv[key] = nlvo[key] 356 for key in nlbv: 357 nlvo.pop(key) 358 nlvc.pop(key) 359 360 for key in nlvoi: 361 if key in nlvci: 362 nlbvi[key] = nlvoi[key] 363 for key in nlbvi: 364 nlvoi.pop(key) 365 nlvci.pop(key) 366 keys = lin_vars.keys() 367 for ndx in keys: 368 if (ndx in nlvo.keys()) or (ndx in nlvc.keys()) or (ndx in nlbv.keys()) or (ndx in nlvci.keys()) or (ndx in nlvoi.keys()): 369 lin_vars.pop(ndx) 370 371 lin_int = [] 372 lin_bool = [] 373 keys = lin_vars.keys() 374 keys.sort() 375 for ndx in keys: 376 var = lin_vars[ndx].var 377 if isinstance(var.domain, IntegerSet): 378 lin_int.append(ndx) 379 lin_vars.pop(ndx) 380 elif isinstance(var.domain, BooleanSet): 381 L = Vars[ndx].lb 382 U = Vars[ndx].ub 383 if L is None or U is None: 384 raise ValueError, "Variable " + str(var.name) +\ 385 "is binary, but does not have lb and ub set" 386 elif value(L) == 0 and value(U) == 1: 387 lin_bool.append(ndx) 388 lin_vars.pop(ndx) 389 else: 390 lin_int.append(ndx) 391 lin_vars.pop(ndx) 392 352 393 keys = Vars.keys() 353 394 keys.sort() … … 374 415 # put the ampl variable id into the variable 375 416 var_id_ctr=0 376 for var_name in Vlist: 377 Vars[var_name].ampl_var_id = var_id_ctr 417 full_var_list = [] 418 full_var_list.extend(sorted(nlbv.keys())) 419 full_var_list.extend(sorted(nlbvi.keys())) 420 full_var_list.extend(sorted(nlvc.keys())) 421 full_var_list.extend(sorted(nlvci.keys())) 422 full_var_list.extend(sorted(nlvo.keys())) 423 full_var_list.extend(sorted(nlvoi.keys())) 424 full_var_list.extend(sorted(lin_vars.keys())) 425 full_var_list.extend(sorted(lin_bool)) 426 full_var_list.extend(sorted(lin_int)) 427 428 for var_name in full_var_list: 429 ampl_var_id[var_name] = var_id_ctr 378 430 var_id_ctr += 1 431 379 432 380 433 # … … 436 489 # 437 490 nc = 0 491 stack_lin = list() 492 stack_nonlin = list() 438 493 for key, C in Con.iteritems(): 439 494 if C.trivial: … … 443 498 if is_constant(ampl_repn): 444 499 continue 445 print >>OUTPUT, "C%s\t#%s[%s]" %(nc, key, ndx) 446 nc += 1 447 500 448 501 if ampl_repn.is_linear(): 449 print >>OUTPUT, "n0" 450 else: 502 stack_lin.append((key,ndx)) 503 else: 504 stack_nonlin.append((key,ndx)) 505 print >>OUTPUT, "C%s\t#%s[%s]" %(nc, key, ndx) 506 nc += 1 507 451 508 assert(not ampl_repn._nonlinear_expr is None) 452 509 self._print_nonlinear_terms_NL(OUTPUT, ampl_repn._nonlinear_expr) 453 510 511 512 for (key,ndx) in stack_lin: 513 print >>OUTPUT, "C%s\t#%s[%s]" %(nc,key,ndx) 514 nc += 1 515 print >>OUTPUT, "n0" 516 517 # nc = 0 518 # stack_ = list() 519 # for key, C in Con.iteritems(): 520 # if C.trivial: 521 # continue 522 # for ndx, constraint_data in C._data.iteritems(): 523 # ampl_repn = constraint_data.ampl_repn 524 # if is_constant(ampl_repn): 525 # continue 526 # print >>OUTPUT, "C%s\t#%s[%s]" %(nc, key, ndx) 527 # nc += 1 528 # 529 # if ampl_repn.is_linear(): 530 # print >>OUTPUT, "n0" 531 # else: 532 # assert(not ampl_repn._nonlinear_expr is None) 533 # self._print_nonlinear_terms_NL(OUTPUT, ampl_repn._nonlinear_expr) 454 534 # 455 535 # "O" lines … … 495 575 var = Vars[var_name] 496 576 if var.value is not None: 497 print >>OUTPUT, "%s %s # %s initial" %( var.ampl_var_id, var.value, var_name)577 print >>OUTPUT, "%s %s # %s initial" %(ampl_var_id[var_name], var.value, var_name) 498 578 499 579 # … … 501 581 # 502 582 print >>OUTPUT, "r" 503 nc=0 504 for key, C in Con.iteritems(): 505 506 if C.trivial: 507 continue 508 509 for ndx, constraint_data in C._data.iteritems(): 510 ampl_repn = constraint_data.ampl_repn 511 if ampl_repn.is_constant(): 512 continue 513 514 offset = ampl_repn._constant 515 516 if constraint_data._equality: 517 print >>OUTPUT, "4%s # c%s %s" %(self._get_bound(constraint_data.lower)offset, nc, constraint_data.name) 518 else: 519 if constraint_data.lower is None: 520 if constraint_data.upper is None: 521 print >>OUTPUT, "3 # c%s %s" %(nc, constraint_data.name) 522 else: 523 print >>OUTPUT, "1 %s # c%s %s" %(self._get_bound(constraint_data.upper)offset, nc, constraint_data.name) 583 nc = 0 584 585 for (key,ndx) in stack_nonlin: 586 constraint_data = Con[key][ndx] 587 ampl_repn = constraint_data.ampl_repn 588 589 offset = ampl_repn._constant 590 591 if constraint_data._equality: 592 print >>OUTPUT, "4 %s # c%s %s" %(self._get_bound(constraint_data.lower)offset, nc, constraint_data.name) 593 else: 594 if constraint_data.lower is None: 595 if constraint_data.upper is None: 596 print >>OUTPUT, "3 # c%s %s" %(nc, constraint_data.name) 524 597 else: 525 if constraint_data.upper is None: 526 print >>OUTPUT, "2 %s # c%s %s" %(self._get_bound(constraint_data.lower)offset, nc, constraint_data.name) 527 else: 528 print >>OUTPUT, "0 %s %s # c%s %s" %(self._get_bound(constraint_data.lower)offset, self._get_bound(constraint_data.upper)offset, nc, constraint_data.name) 529 symbol_map["c" + str(nc)] = constraint_data.name #str(key) + "[" + str(ndx) + "]" 530 nc += 1 598 print >>OUTPUT, "1 %s # c%s %s" %(self._get_bound(constraint_data.upper)offset, nc, constraint_data.name) 599 else: 600 if constraint_data.upper is None: 601 print >>OUTPUT, "2 %s # c%s %s" %(self._get_bound(constraint_data.lower)offset, nc, constraint_data.name) 602 else: 603 print >>OUTPUT, "0 %s %s # c%s %s" %(self._get_bound(constraint_data.lower)offset, self._get_bound(constraint_data.upper)offset, nc, constraint_data.name) 604 symbol_map["c" + str(nc)] = constraint_data.name #str(key) + "[" + str(ndx) + "]" 605 nc += 1 606 607 608 for (key,ndx) in stack_lin: 609 constraint_data = Con[key][ndx] 610 ampl_repn = constraint_data.ampl_repn 611 offset = ampl_repn._constant 612 613 if constraint_data._equality: 614 print >>OUTPUT, "4 %s # c%s %s" %(self._get_bound(constraint_data.lower)offset, nc, constraint_data.name) 615 else: 616 if constraint_data.lower is None: 617 if constraint_data.upper is None: 618 print >>OUTPUT, "3 # c%s %s" %(nc, constraint_data.name) 619 else: 620 print >>OUTPUT, "1 %s # c%s %s" %(self._get_bound(constraint_data.upper)offset, nc, constraint_data.name) 621 else: 622 if constraint_data.upper is None: 623 print >>OUTPUT, "2 %s # c%s %s" %(self._get_bound(constraint_data.lower)offset, nc, constraint_data.name) 624 else: 625 print >>OUTPUT, "0 %s %s # c%s %s" %(self._get_bound(constraint_data.lower)offset, self._get_bound(constraint_data.upper)offset, nc, constraint_data.name) 626 symbol_map["c" + str(nc)] = constraint_data.name #str(key) + "[" + str(ndx) + "]" 627 nc += 1 628 629 # for key, C in Con.iteritems(): 630 # 631 # if C.trivial: 632 # continue 633 # 634 # for ndx, constraint_data in C._data.iteritems(): 635 # ampl_repn = constraint_data.ampl_repn 636 # if ampl_repn.is_constant(): 637 # continue 638 # 639 # offset = ampl_repn._constant 640 # 641 # if constraint_data._equality: 642 # print >>OUTPUT, "4 %s # c%s %s" %(self._get_bound(constraint_data.lower)offset, nc, constraint_data.name) 643 # else: 644 # if constraint_data.lower is None: 645 # if constraint_data.upper is None: 646 # print >>OUTPUT, "3 # c%s %s" %(nc, constraint_data.name) 647 # else: 648 # print >>OUTPUT, "1 %s # c%s %s" %(self._get_bound(constraint_data.upper)offset, nc, constraint_data.name) 649 # else: 650 # if constraint_data.upper is None: 651 # print >>OUTPUT, "2 %s # c%s %s" %(self._get_bound(constraint_data.lower)offset, nc, constraint_data.name) 652 # else: 653 # print >>OUTPUT, "0 %s %s # c%s %s" %(self._get_bound(constraint_data.lower)offset, self._get_bound(constraint_data.upper)offset, nc, constraint_data.name) 654 # symbol_map["c" + str(nc)] = constraint_data.name #str(key) + "[" + str(ndx) + "]" 655 # nc += 1 531 656 # 532 657 # "b" lines 533 658 # 534 659 print >>OUTPUT, "b" 535 for ndx in Vlist:660 for ndx in full_var_list: 536 661 vv = Vars[ndx] 537 662 var = vv.var 538 663 if isinstance(var.domain, BooleanSet): 539 print >>OUTPUT, "0 0 1 # v%s %s" %( vv.ampl_var_id, vv.name)540 symbol_map["v"+str( vv.ampl_var_id)] = vv.name664 print >>OUTPUT, "0 0 1 # v%s %s" %(ampl_var_id[vv.name], vv.name) 665 symbol_map["v"+str(ampl_var_id[vv.name])] = vv.name 541 666 continue 542 667 L = vv.lb … … 547 672 Uv = str(value(U)) 548 673 if Lv == Uv: 549 print >>OUTPUT, "4 %s # v%s %s" %(Lv, vv.ampl_var_id, vv.name)674 print >>OUTPUT, "4 %s # v%s %s" %(Lv, ampl_var_id[vv.name], vv.name) 550 675 else: 551 print >>OUTPUT, "0 %s %s # v%s %s" %(Lv, Uv, vv.ampl_var_id, vv.name)552 else: 553 print >>OUTPUT, "2 %s # v%s %s" %(Lv, vv.ampl_var_id, vv.name)676 print >>OUTPUT, "0 %s %s # v%s %s" %(Lv, Uv, ampl_var_id[vv.name], vv.name) 677 else: 678 print >>OUTPUT, "2 %s # v%s %s" %(Lv, ampl_var_id[vv.name], vv.name) 554 679 elif U is not None: 555 print >>OUTPUT, "1 %s # v%s %s" %(value(U), vv.ampl_var_id, vv.name)680 print >>OUTPUT, "1 %s # v%s %s" %(value(U), ampl_var_id[vv.name], vv.name) 556 681 else: 557 print >>OUTPUT, "3 #v%s %s" %( vv.ampl_var_id, vv.name)558 symbol_map["v"+str( vv.ampl_var_id)] = vv.name682 print >>OUTPUT, "3 #v%s %s" %(ampl_var_id[vv.name], vv.name) 683 symbol_map["v"+str(ampl_var_id[vv.name])] = vv.name 559 684 # 560 685 # "k" lines 561 686 # 562 563 687 ktot = 0 564 688 cu = {} … … 566 690 cu[i] = 0 567 691 568 for key, C in Con.iteritems(): 569 if C.trivial: 570 continue 571 for cndx, constraint_data in C._data.iteritems(): 572 con_vars = set(constraint_data.ampl_repn._linear_terms_var.keys()) 573 con_vars.update(set(constraint_data.ampl_repn._nonlinear_vars.keys())) 574 for d in con_vars: 575 cu[Vars[d].ampl_var_id] += 1 692 for (key,ndx) in stack_nonlin: 693 constraint_data = Con[key][ndx] 694 con_vars = set(constraint_data.ampl_repn._linear_terms_var.keys()) 695 con_vars.update(set(constraint_data.ampl_repn._nonlinear_vars.keys())) 696 for d in con_vars: 697 cu[ampl_var_id[d]] += 1 698 699 700 for (key,ndx) in stack_lin: 701 constraint_data = Con[key][ndx] 702 con_vars = set(constraint_data.ampl_repn._linear_terms_var.keys()) 703 con_vars.update(set(constraint_data.ampl_repn._nonlinear_vars.keys())) 704 for d in con_vars: 705 cu[ampl_var_id[d]] += 1 706 707 708 # for key, C in Con.iteritems(): 709 # if C.trivial: 710 # continue 711 # for cndx, constraint_data in C._data.iteritems(): 712 # con_vars = set(constraint_data.ampl_repn._linear_terms_var.keys()) 713 # con_vars.update(set(constraint_data.ampl_repn._nonlinear_vars.keys())) 714 # for d in con_vars: 715 # cu[ampl_var_id[d]] += 1 576 716 577 717 n1 = len(Vars)  1 … … 586 726 # 587 727 nc = 0 588 for key, con in Con.iteritems(): 589 if con.trivial: 590 continue 591 for ndx, constraint_data in con._data.iteritems(): 592 ampl_repn = constraint_data.ampl_repn 593 # we should probably have this set already created in ampl_repn 594 con_vars = set(ampl_repn._linear_terms_var.keys()) 595 con_vars.update(set(ampl_repn._nonlinear_vars.keys())) 596 print >>OUTPUT, "J%s %s # %s" %(nc, len(con_vars), constraint_data.name) 597 nc += 1 598 grad_entries = {} 599 for con_var in con_vars: 600 if con_var in ampl_repn._linear_terms_var: 601 grad_entries[Vars[con_var].ampl_var_id] = ampl_repn._linear_terms_coef[con_var] 602 else: 603 assert(con_var in ampl_repn._nonlinear_vars) 604 grad_entries[Vars[con_var].ampl_var_id] = 0 605 606 sorted_keys = grad_entries.keys() 607 sorted_keys.sort() 608 for var_id in sorted_keys: 609 print >>OUTPUT, "%s %s" %(var_id, grad_entries[var_id]) 728 for (key,ndx) in stack_nonlin: 729 constraint_data = Con[key][ndx] 730 ampl_repn = constraint_data.ampl_repn 731 # we should probably have this set already created in ampl_repn 732 con_vars = set(ampl_repn._linear_terms_var.keys()) 733 con_vars.update(set(ampl_repn._nonlinear_vars.keys())) 734 print >>OUTPUT, "J%s %s # %s" %(nc, len(con_vars), constraint_data.name) 735 nc += 1 736 grad_entries = {} 737 for con_var in con_vars: 738 if con_var in ampl_repn._linear_terms_var: 739 grad_entries[ampl_var_id[con_var]] = ampl_repn._linear_terms_coef[con_var] 740 else: 741 assert(con_var in ampl_repn._nonlinear_vars) 742 grad_entries[ampl_var_id[con_var]] = 0 743 744 sorted_keys = grad_entries.keys() 745 sorted_keys.sort() 746 for var_id in sorted_keys: 747 print >>OUTPUT, "%s %s" %(var_id, grad_entries[var_id]) 748 749 for (key,ndx) in stack_lin: 750 constraint_data = Con[key][ndx] 751 ampl_repn = constraint_data.ampl_repn 752 # we should probably have this set already created in ampl_repn 753 con_vars = set(ampl_repn._linear_terms_var.keys()) 754 con_vars.update(set(ampl_repn._nonlinear_vars.keys())) 755 print >>OUTPUT, "J%s %s # %s" %(nc, len(con_vars), constraint_data.name) 756 nc += 1 757 grad_entries = {} 758 for con_var in con_vars: 759 if con_var in ampl_repn._linear_terms_var: 760 grad_entries[ampl_var_id[con_var]] = ampl_repn._linear_terms_coef[con_var] 761 else: 762 assert(con_var in ampl_repn._nonlinear_vars) 763 grad_entries[ampl_var_id[con_var]] = 0 764 765 sorted_keys = grad_entries.keys() 766 sorted_keys.sort() 767 for var_id in sorted_keys: 768 print >>OUTPUT, "%s %s" %(var_id, grad_entries[var_id]) 769 610 770 # 611 771 # "G" lines … … 629 789 for obj_var in obj_vars: 630 790 if obj_var in ampl_repn._linear_terms_var: 631 grad_entries[ Vars[obj_var].ampl_var_id] = ampl_repn._linear_terms_coef[obj_var]791 grad_entries[ampl_var_id[obj_var]] = ampl_repn._linear_terms_coef[obj_var] 632 792 else: 633 793 assert(obj_var in ampl_repn._nonlinear_vars) 634 grad_entries[ Vars[obj_var].ampl_var_id] = 0794 grad_entries[ampl_var_id[obj_var]] = 0 635 795 636 796 sorted_keys = grad_entries.keys()
Note: See TracChangeset
for help on using the changeset viewer.