 Timestamp:
 02/09/10 12:21:24 (6 years ago)
 File:

 1 edited
Legend:
 Unmodified
 Added
 Removed

coopr.pyomo/trunk/coopr/pyomo/tests/unit/test_modeldata.py
r2289 r2292 286 286 class TestImport(unittest.TestCase): 287 287 288 def test_tableA (self):288 def test_tableA1(self): 289 289 """Importing a single column of data""" 290 pyutilib.misc.setup_redirect(currdir+'importA.dat') 290 pyutilib.misc.setup_redirect(currdir+'importA1.dat') 291 print "import "+os.path.abspath(example_dir+'A.tab')+" format=set: A;" 292 pyutilib.misc.reset_redirect() 293 model=Model() 294 model.A = Set() 295 instance = model.create(currdir+'importA1.dat') 296 self.failUnlessEqual(instance.A.data(), set(['A1', 'A2', 'A3'])) 297 os.remove(currdir+'importA1.dat') 298 299 def test_tableA2(self): 300 """Importing a single column of data""" 301 pyutilib.misc.setup_redirect(currdir+'importA2.dat') 291 302 print "import "+os.path.abspath(example_dir+'A.tab')+" ;" 292 303 pyutilib.misc.reset_redirect() 293 304 model=Model() 294 305 model.A = Set() 295 instance = model.create(currdir+'importA.dat') 296 self.failUnlessEqual(instance.A.data(), set(['A1', 'A2', 'A3'])) 297 os.remove(currdir+'importA.dat') 306 try: 307 instance = model.create(currdir+'importA2.dat') 308 self.fail("Should fail because no set name is specified") 309 except IOError: 310 pass 311 os.remove(currdir+'importA2.dat') 312 313 def test_tableA3(self): 314 """Importing a single column of data""" 315 pyutilib.misc.setup_redirect(currdir+'importA3.dat') 316 print "import "+os.path.abspath(example_dir+'A.tab')+" : A ;" 317 pyutilib.misc.reset_redirect() 318 model=Model() 319 model.A = Set() 320 try: 321 instance = model.create(currdir+'importA3.dat') 322 self.fail("Should fail because no index is specified") 323 except IOError: 324 pass 325 os.remove(currdir+'importA3.dat') 298 326 299 327 def test_tableB(self): 300 328 """Same as test_tableA""" 301 329 pyutilib.misc.setup_redirect(currdir+'importB.dat') 302 print "import "+os.path.abspath(example_dir+'B.tab')+" ;"330 print "import "+os.path.abspath(example_dir+'B.tab')+" format=set:B;" 303 331 pyutilib.misc.reset_redirect() 304 332 model=Model() … … 312 340 treated as values for a set with tuple values.""" 313 341 pyutilib.misc.setup_redirect(currdir+'importC.dat') 314 print "import "+os.path.abspath(example_dir+'C.tab')+" : C ;"342 print "import "+os.path.abspath(example_dir+'C.tab')+" format=set: C ;" 315 343 pyutilib.misc.reset_redirect() 316 344 model=Model() … … 345 373 """Same as tableXW.""" 346 374 pyutilib.misc.setup_redirect(currdir+'importY.dat') 347 print "import "+os.path.abspath(example_dir+'Y.tab')+" ;"375 print "import "+os.path.abspath(example_dir+'Y.tab')+" : [A] Y;" 348 376 pyutilib.misc.reset_redirect() 349 377 model=Model() … … 360 388 index column.""" 361 389 pyutilib.misc.setup_redirect(currdir+'importXW.dat') 362 print "import "+os.path.abspath(example_dir+'XW.tab')+" 390 print "import "+os.path.abspath(example_dir+'XW.tab')+": [A] X W;" 363 391 pyutilib.misc.reset_redirect() 364 392 model=Model() … … 375 403 """Like test_tableXW_1, except that set A is not defined.""" 376 404 pyutilib.misc.setup_redirect(currdir+'importXW.dat') 377 print "import "+os.path.abspath(example_dir+'XW.tab')+" 405 print "import "+os.path.abspath(example_dir+'XW.tab')+": [A] X W;" 378 406 pyutilib.misc.reset_redirect() 379 407 model=Model() … … 390 418 """Like test_tableXW_1, except that set A is defined in the import statment.""" 391 419 pyutilib.misc.setup_redirect(currdir+'importXW.dat') 392 print "import "+os.path.abspath(example_dir+'XW.tab')+" : [A] AX W;"420 print "import "+os.path.abspath(example_dir+'XW.tab')+": A=[A] X W;" 393 421 pyutilib.misc.reset_redirect() 394 422 model=Model() … … 405 433 """Like test_tableXW_1, except that set A is defined in the import statment and all values are mapped.""" 406 434 pyutilib.misc.setup_redirect(currdir+'importXW.dat') 407 print "import "+os.path.abspath(example_dir+'XW.tab')+" : [A] B:A R:X S:W;"435 print "import "+os.path.abspath(example_dir+'XW.tab')+": B=[A] R=X S=W;" 408 436 pyutilib.misc.reset_redirect() 409 437 model=Model() … … 448 476 index column. A missing value is represented in the column data.""" 449 477 pyutilib.misc.setup_redirect(currdir+'importS.dat') 450 print "import "+os.path.abspath(example_dir+'S.tab')+" ;"478 print "import "+os.path.abspath(example_dir+'S.tab')+": [A] S ;" 451 479 pyutilib.misc.reset_redirect() 452 480 model=Model() … … 461 489 """Importing a table that has multiple indexing columns""" 462 490 pyutilib.misc.setup_redirect(currdir+'importPO.dat') 463 print "import "+os.path.abspath(example_dir+'PO.tab')+" : [A,B] J:A,BP O;"491 print "import "+os.path.abspath(example_dir+'PO.tab')+" : J=[A,B] P O;" 464 492 pyutilib.misc.reset_redirect() 465 493 model=Model() … … 474 502 475 503 504 class TestModelData(unittest.TestCase): 505 506 def test_tableA1(self): 507 """Importing a single column of data""" 508 md = ModelData() 509 md.add(os.path.abspath(example_dir+'A.tab'), format='set', set='A') 510 model=Model() 511 model.A = Set() 512 md.read(model) 513 instance = model.create(md) 514 self.failUnlessEqual(instance.A.data(), set(['A1', 'A2', 'A3'])) 515 516 def test_tableA2(self): 517 """Importing a single column of data""" 518 md = ModelData() 519 md.add(os.path.abspath(example_dir+'A.tab')) 520 model=Model() 521 model.A = Set() 522 try: 523 md.read(model) 524 instance = model.create(md) 525 self.fail("Should fail because no set name is specified") 526 except IOError: 527 pass 528 529 def test_tableA3(self): 530 """Importing a single column of data""" 531 md = ModelData() 532 md.add(os.path.abspath(example_dir+'A.tab'), set='A') 533 model=Model() 534 model.A = Set() 535 try: 536 md.read(model) 537 instance = model.create(md) 538 self.fail("Should fail because no index is specified") 539 except IOError: 540 pass 541 542 def test_tableB(self): 543 """Same as test_tableA""" 544 md = ModelData() 545 md.add(os.path.abspath(example_dir+'B.tab'), format='set', set='B') 546 model=Model() 547 model.B = Set() 548 md.read(model) 549 instance = model.create(md) 550 self.failUnlessEqual(instance.B.data(), set([1, 2, 3])) 551 552 def test_tableC(self): 553 """Importing a multicolumn table, where all columns are 554 treated as values for a set with tuple values.""" 555 md = ModelData() 556 md.add(os.path.abspath(example_dir+'C.tab'), format='set', set='C') 557 model=Model() 558 model.C = Set(dimen=2) 559 md.read(model) 560 instance = model.create(md) 561 self.failUnlessEqual(instance.C.data(), set([('A1',1), ('A1',2), ('A1',3), ('A2',1), ('A2',2), ('A2',3), ('A3',1), ('A3',2), ('A3',3)])) 562 563 def test_tableD(self): 564 """Importing a 2D array of data as a set.""" 565 md = ModelData() 566 md.add(os.path.abspath(example_dir+'D.tab'), format='set_array', set='C') 567 model=Model() 568 model.C = Set(dimen=2) 569 md.read(model) 570 instance = model.create(md) 571 self.failUnlessEqual(instance.C.data(), set([('A1',1), ('A2',2), ('A3',3)])) 572 573 def test_tableZ(self): 574 """Importing a single parameter""" 575 md = ModelData() 576 md.add(os.path.abspath(example_dir+'Z.tab'), format='param', param='Z') 577 model=Model() 578 model.Z = Param(default=99.0) 579 md.read(model) 580 instance = model.create(md) 581 self.failUnlessEqual(instance.Z.value, 1.1) 582 583 def test_tableY(self): 584 """Same as tableXW.""" 585 md = ModelData() 586 md.add(os.path.abspath(example_dir+'Y.tab'), index=['A'], param=['Y']) 587 model=Model() 588 model.A = Set(initialize=['A1','A2','A3','A4']) 589 model.Y = Param(model.A) 590 md.read(model) 591 instance = model.create(md) 592 self.failUnlessEqual(instance.A.data(), set(['A1','A2','A3','A4'])) 593 self.failUnlessEqual(instance.Y.data(), {'A1':3.3,'A2':3.4,'A3':3.5}) 594 595 def test_tableXW_1(self): 596 """Importing a table, but only reporting the values for the nonindex 597 parameter columns. The first column is assumed to represent an 598 index column.""" 599 md = ModelData() 600 md.add(os.path.abspath(example_dir+'XW.tab'), index=['A'], param=['X','W']) 601 model=Model() 602 model.A = Set(initialize=['A1','A2','A3','A4']) 603 model.X = Param(model.A) 604 model.W = Param(model.A) 605 md.read(model) 606 instance = model.create(md) 607 self.failUnlessEqual(instance.A.data(), set(['A1','A2','A3','A4'])) 608 self.failUnlessEqual(instance.X.data(), {'A1':3.3,'A2':3.4,'A3':3.5}) 609 self.failUnlessEqual(instance.W.data(), {'A1':4.3,'A2':4.4,'A3':4.5}) 610 611 def test_tableXW_2(self): 612 """Like test_tableXW_1, except that set A is not defined.""" 613 md = ModelData() 614 md.add(os.path.abspath(example_dir+'XW.tab'), index=['A'], param=['X','W']) 615 model=Model() 616 model.A = Set() 617 model.X = Param(model.A) 618 model.W = Param(model.A) 619 md.read(model) 620 instance = model.create(md) 621 self.failUnlessEqual(instance.A.data(), set()) 622 self.failUnlessEqual(instance.X.data(), {'A1':3.3,'A2':3.4,'A3':3.5}) 623 self.failUnlessEqual(instance.W.data(), {'A1':4.3,'A2':4.4,'A3':4.5}) 624 625 def test_tableXW_3(self): 626 """Like test_tableXW_1, except that set A is defined in the import statment.""" 627 md = ModelData() 628 md.add(os.path.abspath(example_dir+'XW.tab'), index_name='A', index=['A'], param=['X','W']) 629 model=Model() 630 model.A = Set() 631 model.X = Param(model.A) 632 model.W = Param(model.A) 633 md.read(model) 634 instance = model.create(md) 635 self.failUnlessEqual(instance.A.data(), set(['A1','A2','A3'])) 636 self.failUnlessEqual(instance.X.data(), {'A1':3.3,'A2':3.4,'A3':3.5}) 637 self.failUnlessEqual(instance.W.data(), {'A1':4.3,'A2':4.4,'A3':4.5}) 638 639 def test_tableXW_4(self): 640 """Like test_tableXW_1, except that set A is defined in the import statment and all values are mapped.""" 641 md = ModelData() 642 md.add(os.path.abspath(example_dir+'XW.tab'), index_name='B', index=['A'], param=['X','W'], param_name={'X':'R', 'W':'S'}) 643 model=Model() 644 model.B = Set() 645 model.R = Param(model.B) 646 model.S = Param(model.B) 647 md.read(model) 648 instance = model.create(md) 649 self.failUnlessEqual(instance.B.data(), set(['A1','A2','A3'])) 650 self.failUnlessEqual(instance.R.data(), {'A1':3.3,'A2':3.4,'A3':3.5}) 651 self.failUnlessEqual(instance.S.data(), {'A1':4.3,'A2':4.4,'A3':4.5}) 652 653 def test_tableT(self): 654 """Importing a 2D array of parameters that are transposed.""" 655 md = ModelData() 656 md.add(os.path.abspath(example_dir+'T.tab'), format='transposed_array', param='T') 657 model=Model() 658 model.A = Set() 659 model.B = Set() 660 model.T = Param(model.A, model.B) 661 md.read(model) 662 instance = model.create(md) 663 self.failUnlessEqual(instance.T.data(), {('A2', 'I1'): 2.3, ('A1', 'I2'): 1.4, ('A1', 'I3'): 1.5, ('A1', 'I4'): 1.6, ('A1', 'I1'): 1.3, ('A3', 'I4'): 3.6, ('A2', 'I4'): 2.6, ('A3', 'I1'): 3.3, ('A2', 'I3'): 2.5, ('A3', 'I2'): 3.4, ('A2', 'I2'): 2.4, ('A3', 'I3'): 3.5}) 664 665 def test_tableU(self): 666 """Importing a 2D array of parameters.""" 667 md = ModelData() 668 md.add(os.path.abspath(example_dir+'T.tab'), format='array', param='U') 669 model=Model() 670 model.A = Set() 671 model.B = Set() 672 model.U = Param(model.A, model.B) 673 md.read(model) 674 instance = model.create(md) 675 self.failUnlessEqual(instance.U.data(), {('I2', 'A1'): 1.4, ('I3', 'A1'): 1.5, ('I3', 'A2'): 2.5, ('I4', 'A1'): 1.6, ('I3', 'A3'): 3.5, ('I1', 'A2'): 2.3, ('I4', 'A3'): 3.6, ('I1', 'A3'): 3.3, ('I4', 'A2'): 2.6, ('I2', 'A3'): 3.4, ('I1', 'A1'): 1.3, ('I2', 'A2'): 2.4}) 676 677 def test_tableS(self): 678 """Importing a table, but only reporting the values for the nonindex 679 parameter columns. The first column is assumed to represent an 680 index column. A missing value is represented in the column data.""" 681 md = ModelData() 682 md.add(os.path.abspath(example_dir+'S.tab'), index=['A'], param=['S']) 683 model=Model() 684 model.A = Set(initialize=['A1','A2','A3','A4']) 685 model.S = Param(model.A) 686 md.read(model) 687 instance = model.create(md) 688 self.failUnlessEqual(instance.A.data(), set(['A1','A2','A3','A4'])) 689 self.failUnlessEqual(instance.S.data(), {'A1':3.3,'A3':3.5}) 690 691 def test_tablePO(self): 692 """Importing a table that has multiple indexing columns""" 693 pyutilib.misc.setup_redirect(currdir+'importPO.dat') 694 print "import "+os.path.abspath(example_dir+'PO.tab')+" : J=[A,B] P O;" 695 pyutilib.misc.reset_redirect() 696 md = ModelData() 697 md.add(os.path.abspath(example_dir+'PO.tab'), index_name='J', index=['A','B'], param=['P','O']) 698 model=Model() 699 model.J = Set(dimen=2) 700 model.P = Param(model.J) 701 model.O = Param(model.J) 702 md.read(model) 703 instance = model.create(md) 704 self.failUnlessEqual(instance.J.data(), set([('A3', 'B3'), ('A1', 'B1'), ('A2', 'B2')]) ) 705 self.failUnlessEqual(instance.P.data(), {('A3', 'B3'): 4.5, ('A1', 'B1'): 4.3, ('A2', 'B2'): 4.4} ) 706 self.failUnlessEqual(instance.O.data(), {('A3', 'B3'): 5.5, ('A1', 'B1'): 5.3, ('A2', 'B2'): 5.4}) 707 708 476 709 if __name__ == "__main__": 477 710 unittest.main()
Note: See TracChangeset
for help on using the changeset viewer.