Changeset 2295
 Timestamp:
 02/09/10 16:23:37 (5 years ago)
 Location:
 coopr.pyomo/trunk/coopr/pyomo
 Files:

 1 added
 3 edited
Legend:
 Unmodified
 Added
 Removed

coopr.pyomo/trunk/coopr/pyomo/base/plugin.py
r2227 r2295 168 168 if name is None: 169 169 return map(lambda x:x.name, ep()) 170 return ep.service(name).create() 170 service = ep.service(name) 171 if service is None: 172 return None 173 return service.create() 171 174 172 175 
coopr.pyomo/trunk/coopr/pyomo/data/TableData.py
r2292 r2295 30 30 self.options = Options(**kwds) 31 31 32 def open(self , filename):32 def open(self): 33 33 """ 34 34 Open the table 
coopr.pyomo/trunk/coopr/pyomo/tests/unit/test_modeldata.py
r2292 r2295 11 11 from coopr.pyomo import * 12 12 import coopr 13 import pyutilib.common 13 14 14 15 currdir=dirname(abspath(__file__))+os.sep … … 37 38 38 39 def test_read_set(self): 39 td = TableData(filename=coopr_dir+"\\test\\acro\\unit\\Book1.xls",range="TheRange",set="X") 40 td = DataManagerFactory('xls') 41 td.initialize(currdir+"Book1.xls", range="TheRange", format='set', set="X") 40 42 try: 41 43 td.open() 42 44 td.read() 43 45 td.close() 44 self.failUnlessEqual( td. data(), ['set', 'X', ':=', 'A1', 2.0, 3.0, 4.0, 'A5', 6.0, 7.0, 8.0, 'A9', 10.0, 11.0, 12.0, 'A13', 14.0, 15.0, 16.0])45 except coopr.ApplicationError:46 self.failUnlessEqual( td._info, ['set', 'X', ':=', 'A1', 2.0, 3.0, 4.0, 'A5', 6.0, 7.0, 8.0, 'A9', 10.0, 11.0, 12.0, 'A13', 14.0, 15.0, 16.0]) 47 except pyutilib.common.ApplicationError: 46 48 pass 47 49 48 def test_read_set2(self):49 td = TableData(range="TheRange",set="X")50 try:51 td.open(filename=coopr_dir+"\\test\\acro\\unit\\Book1.xls")52 td.read()53 td.close()54 self.failUnlessEqual( td.data(), ['set', 'X', ':=', 'A1', 2.0, 3.0, 4.0, 'A5', 6.0, 7.0, 8.0, 'A9', 10.0, 11.0, 12.0, 'A13', 14.0, 15.0, 16.0])55 except coopr.ApplicationError:56 pass57 58 50 def test_read_param1(self): 59 td = TableData(filename=coopr_dir+"\\test\\acro\\unit\\Book1.xls",range="TheRange") 51 td = DataManagerFactory('xls') 52 td.initialize(currdir+"Book1.xls", range="TheRange", index=['aa'], param=['bb','cc','dd']) 60 53 try: 61 54 td.open() 62 55 td.read() 63 56 td.close() 64 self.failUnlessEqual( td. data(), ['param', ':', 'bb', 'cc', 'dd', ':=', 'A1', 2.0, 3.0, 4.0, 'A5', 6.0, 7.0, 8.0, 'A9', 10.0, 11.0, 12.0, 'A13', 14.0, 15.0, 16.0])65 except coopr.ApplicationError:57 self.failUnlessEqual( td._info, ['param', ':', 'bb', 'cc', 'dd', ':=', 'A1', 2.0, 3.0, 4.0, 'A5', 6.0, 7.0, 8.0, 'A9', 10.0, 11.0, 12.0, 'A13', 14.0, 15.0, 16.0]) 58 except pyutilib.common.ApplicationError: 66 59 pass 67 60 68 61 def test_read_param2(self): 69 td = TableData(filename=coopr_dir+"\\test\\acro\\unit\\Book1.xls",range="TheRange",index="X") 62 td = DataManagerFactory('xls') 63 td.initialize(currdir+"Book1.xls",range="TheRange", index_name="X", index=['aa'], param=['bb','cc','dd']) 70 64 try: 71 65 td.open() 72 66 td.read() 73 67 td.close() 74 self.failUnlessEqual( td. data(), ['param', ':', 'X', ':', 'bb', 'cc', 'dd', ':=', 'A1', 2.0, 3.0, 4.0, 'A5', 6.0, 7.0, 8.0, 'A9', 10.0, 11.0, 12.0, 'A13', 14.0, 15.0, 16.0])75 except coopr.ApplicationError:68 self.failUnlessEqual( td._info, ['param', ':', 'X', ':', 'bb', 'cc', 'dd', ':=', 'A1', 2.0, 3.0, 4.0, 'A5', 6.0, 7.0, 8.0, 'A9', 10.0, 11.0, 12.0, 'A13', 14.0, 15.0, 16.0]) 69 except pyutilib.common.ApplicationError: 76 70 pass 77 71 78 72 def test_read_param3(self): 79 td = TableData(filename=coopr_dir+"\\test\\acro\\unit\\Book1.xls",range="TheRange",index="X", param="a") 73 td = DataManagerFactory('xls') 74 td.initialize(currdir+"Book1.xls",range="TheRange", index_name="X", index=['aa','bb','cc'], param=["dd"], param_name={'dd':'a'}) 80 75 try: 81 76 td.open() 82 77 td.read() 83 78 td.close() 84 self.failUnlessEqual( td. data(), ['param', ':', 'X', ':', 'a', ':=', 'A1', 2.0, 3.0, 4.0, 'A5', 6.0, 7.0, 8.0, 'A9', 10.0, 11.0, 12.0, 'A13', 14.0, 15.0, 16.0])85 except coopr.ApplicationError:79 self.failUnlessEqual( td._info, ['param', ':', 'X', ':', 'a', ':=', 'A1', 2.0, 3.0, 4.0, 'A5', 6.0, 7.0, 8.0, 'A9', 10.0, 11.0, 12.0, 'A13', 14.0, 15.0, 16.0]) 80 except pyutilib.common.ApplicationError: 86 81 pass 87 82 88 83 def test_read_param4(self): 89 td = TableData(filename=coopr_dir+"\\test\\acro\\unit\\Book1.xls",range="TheRange",index="X", param=("a","b")) 84 td = DataManagerFactory('xls') 85 td.initialize(currdir+"Book1.xls",range="TheRange", index_name="X", index=['aa','bb'], param=['cc','dd'], param_name={'cc':'a', 'dd':'b'}) 90 86 try: 91 87 td.open() 92 88 td.read() 93 89 td.close() 94 self.failUnlessEqual( td. data(), ['param', ':', 'X', ':', 'a', 'b', ':=', 'A1', 2.0, 3.0, 4.0, 'A5', 6.0, 7.0, 8.0, 'A9', 10.0, 11.0, 12.0, 'A13', 14.0, 15.0, 16.0])95 except coopr.ApplicationError:90 self.failUnlessEqual( td._info, ['param', ':', 'X', ':', 'a', 'b', ':=', 'A1', 2.0, 3.0, 4.0, 'A5', 6.0, 7.0, 8.0, 'A9', 10.0, 11.0, 12.0, 'A13', 14.0, 15.0, 16.0]) 91 except pyutilib.common.ApplicationError: 96 92 pass 97 93 98 94 def test_read_array1(self): 99 td = TableData(filename=coopr_dir+"\\test\\acro\\unit\\Book1.xls",range="TheRange",param="X", format="array") 95 td = DataManagerFactory('xls') 96 td.initialize(currdir+"Book1.xls",range="TheRange", param="X", format="array") 100 97 try: 101 98 td.open() 102 99 td.read() 103 100 td.close() 104 self.failUnlessEqual( td. data(), ['param', 'X', ':', 'bb', 'cc', 'dd', ':=', 'A1', 2.0, 3.0, 4.0, 'A5', 6.0, 7.0, 8.0, 'A9', 10.0, 11.0, 12.0, 'A13', 14.0, 15.0, 16.0])105 except coopr.ApplicationError:101 self.failUnlessEqual( td._info, ['param', 'X', ':', 'bb', 'cc', 'dd', ':=', 'A1', 2.0, 3.0, 4.0, 'A5', 6.0, 7.0, 8.0, 'A9', 10.0, 11.0, 12.0, 'A13', 14.0, 15.0, 16.0]) 102 except pyutilib.common.ApplicationError: 106 103 pass 107 104 108 105 def test_read_array2(self): 109 td = TableData(filename=coopr_dir+"\\test\\acro\\unit\\Book1.xls",range="TheRange",param="X",format="transposed_array") 106 td = DataManagerFactory('xls') 107 td.initialize(currdir+"Book1.xls",range="TheRange",param="X",format="transposed_array") 110 108 try: 111 109 td.open() 112 110 td.read() 113 111 td.close() 114 self.failUnlessEqual( td. data(), ['param', 'X', '(tr)',':', 'bb', 'cc', 'dd', ':=', 'A1', 2.0, 3.0, 4.0, 'A5', 6.0, 7.0, 8.0, 'A9', 10.0, 11.0, 12.0, 'A13', 14.0, 15.0, 16.0])115 except coopr.ApplicationError:112 self.failUnlessEqual( td._info, ['param', 'X', '(tr)',':', 'bb', 'cc', 'dd', ':=', 'A1', 2.0, 3.0, 4.0, 'A5', 6.0, 7.0, 8.0, 'A9', 10.0, 11.0, 12.0, 'A13', 14.0, 15.0, 16.0]) 113 except pyutilib.common.ApplicationError: 116 114 pass 117 115 118 116 def test_error1(self): 119 td = TableData(filename="bad") 117 td = DataManagerFactory('xls') 118 td.initialize("bad") 120 119 try: 121 120 td.open() … … 125 124 126 125 def test_error2(self): 127 td = TableData()126 td = DataManagerFactory('xls') 128 127 try: 129 128 td.open() 130 129 self.fail("Expected IOError because no file specified") 131 except IOError:130 except (IOError,AttributeError): 132 131 pass 133 132 134 133 def test_error3(self): 135 td = TableData(filename=currdir+"display.txt") 136 try: 134 td = DataManagerFactory('txt') 135 try: 136 td.initialize(currdir+"display.txt") 137 137 td.open() 138 138 self.fail("Expected IOError because of bad file type") 139 except IOError:139 except (IOError, AttributeError): 140 140 pass 141 141 142 142 def test_error4(self): 143 td = TableData(filename=currdir+"dummy") 144 try: 143 td = DataManagerFactory('txt') 144 try: 145 td.initialize(filename=currdir+"dummy") 145 146 td.open() 146 147 self.fail("Expected IOError because of bad file type") 147 except IOError:148 except (IOError, AttributeError): 148 149 pass 149 150 150 151 def test_error5(self): 151 td = TableData(filename=example_dir+"D.tab", param="D", format="foo") 152 td = DataManagerFactory('tab') 153 td.initialize(example_dir+"D.tab", param="D", format="foo") 152 154 td.open() 153 155 try: … … 156 158 except ValueError: 157 159 pass 158 159 def test_error7(self):160 td = TableData(filename=example_dir+"D.tab", set="D", format="foo")161 td.open()162 try:163 td.read()164 self.fail("Expected IOError because of bad format")165 except ValueError:166 pass167 168 160 169 161 … … 502 494 503 495 504 class TestModelData(unittest.TestCase): 496 #class TestSpreadsheet(unittest.TestCase): 497 class TestSpreadsheet(object): 505 498 506 499 def test_tableA1(self): 507 500 """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 m d.read(model)513 instance = model.create( md)501 pyutilib.misc.setup_redirect(currdir+'importA1.dat') 502 print "import "+os.path.abspath(example_dir+'A.tab')+" format=set: A;" 503 pyutilib.misc.reset_redirect() 504 model=Model() 505 model.A = Set() 506 instance = model.create(currdir+'importA1.dat') 514 507 self.failUnlessEqual(instance.A.data(), set(['A1', 'A2', 'A3'])) 508 os.remove(currdir+'importA1.dat') 515 509 516 510 def test_tableA2(self): 517 511 """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)512 pyutilib.misc.setup_redirect(currdir+'importA2.dat') 513 print "import "+os.path.abspath(example_dir+'A.tab')+" ;" 514 pyutilib.misc.reset_redirect() 515 model=Model() 516 model.A = Set() 517 try: 518 instance = model.create(currdir+'importA2.dat') 525 519 self.fail("Should fail because no set name is specified") 526 520 except IOError: 527 521 pass 522 os.remove(currdir+'importA2.dat') 528 523 529 524 def test_tableA3(self): 530 525 """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)526 pyutilib.misc.setup_redirect(currdir+'importA3.dat') 527 print "import "+os.path.abspath(example_dir+'A.tab')+" : A ;" 528 pyutilib.misc.reset_redirect() 529 model=Model() 530 model.A = Set() 531 try: 532 instance = model.create(currdir+'importA3.dat') 538 533 self.fail("Should fail because no index is specified") 539 534 except IOError: 540 535 pass 536 os.remove(currdir+'importA3.dat') 541 537 542 538 def test_tableB(self): 543 539 """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 m d.read(model)549 instance = model.create( md)540 pyutilib.misc.setup_redirect(currdir+'importB.dat') 541 print "import "+os.path.abspath(example_dir+'B.tab')+" format=set:B;" 542 pyutilib.misc.reset_redirect() 543 model=Model() 544 model.B = Set() 545 instance = model.create(currdir+'importB.dat') 550 546 self.failUnlessEqual(instance.B.data(), set([1, 2, 3])) 547 os.remove(currdir+'importB.dat') 551 548 552 549 def test_tableC(self): 553 550 """Importing a multicolumn table, where all columns are 554 551 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') 552 pyutilib.misc.setup_redirect(currdir+'importC.dat') 553 print "import "+os.path.abspath(example_dir+'C.tab')+" format=set: C ;" 554 pyutilib.misc.reset_redirect() 557 555 model=Model() 558 556 model.C = Set(dimen=2) 559 md.read(model) 560 instance = model.create(md) 557 instance = model.create(currdir+'importC.dat') 561 558 self.failUnlessEqual(instance.C.data(), set([('A1',1), ('A1',2), ('A1',3), ('A2',1), ('A2',2), ('A2',3), ('A3',1), ('A3',2), ('A3',3)])) 559 os.remove(currdir+'importC.dat') 562 560 563 561 def test_tableD(self): 564 562 """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') 563 pyutilib.misc.setup_redirect(currdir+'importD.dat') 564 print "import "+os.path.abspath(example_dir+'D.tab')+" format=set_array: C ;" 565 pyutilib.misc.reset_redirect() 567 566 model=Model() 568 567 model.C = Set(dimen=2) 569 md.read(model) 570 instance = model.create(md) 568 instance = model.create(currdir+'importD.dat') 571 569 self.failUnlessEqual(instance.C.data(), set([('A1',1), ('A2',2), ('A3',3)])) 570 os.remove(currdir+'importD.dat') 572 571 573 572 def test_tableZ(self): 574 573 """Importing a single parameter""" 575 md = ModelData() 576 md.add(os.path.abspath(example_dir+'Z.tab'), format='param', param='Z') 574 pyutilib.misc.setup_redirect(currdir+'importZ.dat') 575 print "import "+os.path.abspath(example_dir+'Z.tab')+" format=param: Z ;" 576 pyutilib.misc.reset_redirect() 577 577 model=Model() 578 578 model.Z = Param(default=99.0) 579 md.read(model) 580 instance = model.create(md) 579 instance = model.create(currdir+'importZ.dat') 581 580 self.failUnlessEqual(instance.Z.value, 1.1) 581 os.remove(currdir+'importZ.dat') 582 582 583 583 def test_tableY(self): 584 584 """Same as tableXW.""" 585 md = ModelData() 586 md.add(os.path.abspath(example_dir+'Y.tab'), index=['A'], param=['Y']) 585 pyutilib.misc.setup_redirect(currdir+'importY.dat') 586 print "import "+os.path.abspath(example_dir+'Y.tab')+" : [A] Y;" 587 pyutilib.misc.reset_redirect() 587 588 model=Model() 588 589 model.A = Set(initialize=['A1','A2','A3','A4']) 589 590 model.Y = Param(model.A) 590 md.read(model) 591 instance = model.create(md) 591 instance = model.create(currdir+'importY.dat') 592 592 self.failUnlessEqual(instance.A.data(), set(['A1','A2','A3','A4'])) 593 593 self.failUnlessEqual(instance.Y.data(), {'A1':3.3,'A2':3.4,'A3':3.5}) 594 os.remove(currdir+'importY.dat') 594 595 595 596 def test_tableXW_1(self): … … 597 598 parameter columns. The first column is assumed to represent an 598 599 index column.""" 599 md = ModelData() 600 md.add(os.path.abspath(example_dir+'XW.tab'), index=['A'], param=['X','W']) 600 pyutilib.misc.setup_redirect(currdir+'importXW.dat') 601 print "import "+os.path.abspath(example_dir+'XW.tab')+": [A] X W;" 602 pyutilib.misc.reset_redirect() 601 603 model=Model() 602 604 model.A = Set(initialize=['A1','A2','A3','A4']) 603 605 model.X = Param(model.A) 604 606 model.W = Param(model.A) 605 md.read(model) 606 instance = model.create(md) 607 instance = model.create(currdir+'importXW.dat') 607 608 self.failUnlessEqual(instance.A.data(), set(['A1','A2','A3','A4'])) 608 609 self.failUnlessEqual(instance.X.data(), {'A1':3.3,'A2':3.4,'A3':3.5}) 609 610 self.failUnlessEqual(instance.W.data(), {'A1':4.3,'A2':4.4,'A3':4.5}) 611 os.remove(currdir+'importXW.dat') 610 612 611 613 def test_tableXW_2(self): 612 614 """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 pyutilib.misc.setup_redirect(currdir+'importXW.dat') 616 print "import "+os.path.abspath(example_dir+'XW.tab')+": [A] X W;" 617 pyutilib.misc.reset_redirect() 615 618 model=Model() 616 619 model.A = Set() 617 620 model.X = Param(model.A) 618 621 model.W = Param(model.A) 619 md.read(model) 620 instance = model.create(md) 622 instance = model.create(currdir+'importXW.dat') 621 623 self.failUnlessEqual(instance.A.data(), set()) 622 624 self.failUnlessEqual(instance.X.data(), {'A1':3.3,'A2':3.4,'A3':3.5}) 623 625 self.failUnlessEqual(instance.W.data(), {'A1':4.3,'A2':4.4,'A3':4.5}) 626 os.remove(currdir+'importXW.dat') 624 627 625 628 def test_tableXW_3(self): 626 629 """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']) 630 pyutilib.misc.setup_redirect(currdir+'importXW.dat') 631 print "import "+os.path.abspath(example_dir+'XW.tab')+": A=[A] X W;" 632 pyutilib.misc.reset_redirect() 629 633 model=Model() 630 634 model.A = Set() 631 635 model.X = Param(model.A) 632 636 model.W = Param(model.A) 633 md.read(model) 634 instance = model.create(md) 637 instance = model.create(currdir+'importXW.dat') 635 638 self.failUnlessEqual(instance.A.data(), set(['A1','A2','A3'])) 636 639 self.failUnlessEqual(instance.X.data(), {'A1':3.3,'A2':3.4,'A3':3.5}) 637 640 self.failUnlessEqual(instance.W.data(), {'A1':4.3,'A2':4.4,'A3':4.5}) 641 os.remove(currdir+'importXW.dat') 638 642 639 643 def test_tableXW_4(self): 640 644 """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'}) 645 pyutilib.misc.setup_redirect(currdir+'importXW.dat') 646 print "import "+os.path.abspath(example_dir+'XW.tab')+": B=[A] R=X S=W;" 647 pyutilib.misc.reset_redirect() 643 648 model=Model() 644 649 model.B = Set() 645 650 model.R = Param(model.B) 646 651 model.S = Param(model.B) 647 md.read(model) 648 instance = model.create(md) 652 instance = model.create(currdir+'importXW.dat') 649 653 self.failUnlessEqual(instance.B.data(), set(['A1','A2','A3'])) 650 654 self.failUnlessEqual(instance.R.data(), {'A1':3.3,'A2':3.4,'A3':3.5}) 651 655 self.failUnlessEqual(instance.S.data(), {'A1':4.3,'A2':4.4,'A3':4.5}) 656 os.remove(currdir+'importXW.dat') 652 657 653 658 def test_tableT(self): 654 659 """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') 660 pyutilib.misc.setup_redirect(currdir+'importT.dat') 661 print "import "+os.path.abspath(example_dir+'T.tab')+" format=transposed_array : T;" 662 pyutilib.misc.reset_redirect() 657 663 model=Model() 658 664 model.A = Set() 659 665 model.B = Set() 660 666 model.T = Param(model.A, model.B) 661 md.read(model) 662 instance = model.create(md) 667 instance = model.create(currdir+'importT.dat') 663 668 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}) 669 os.remove(currdir+'importT.dat') 664 670 665 671 def test_tableU(self): 666 672 """Importing a 2D array of parameters.""" 667 md = ModelData() 668 md.add(os.path.abspath(example_dir+'T.tab'), format='array', param='U') 673 pyutilib.misc.setup_redirect(currdir+'importU.dat') 674 print "import "+os.path.abspath(example_dir+'U.tab')+" format=array : U;" 675 pyutilib.misc.reset_redirect() 669 676 model=Model() 670 677 model.A = Set() 671 678 model.B = Set() 672 679 model.U = Param(model.A, model.B) 673 md.read(model) 674 instance = model.create(md) 680 instance = model.create(currdir+'importU.dat') 675 681 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}) 682 os.remove(currdir+'importU.dat') 676 683 677 684 def test_tableS(self): … … 679 686 parameter columns. The first column is assumed to represent an 680 687 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']) 688 pyutilib.misc.setup_redirect(currdir+'importS.dat') 689 print "import "+os.path.abspath(example_dir+'S.tab')+": [A] S ;" 690 pyutilib.misc.reset_redirect() 683 691 model=Model() 684 692 model.A = Set(initialize=['A1','A2','A3','A4']) 685 693 model.S = Param(model.A) 686 md.read(model) 687 instance = model.create(md) 694 instance = model.create(currdir+'importS.dat') 688 695 self.failUnlessEqual(instance.A.data(), set(['A1','A2','A3','A4'])) 689 696 self.failUnlessEqual(instance.S.data(), {'A1':3.3,'A3':3.5}) 697 os.remove(currdir+'importS.dat') 690 698 691 699 def test_tablePO(self): … … 694 702 print "import "+os.path.abspath(example_dir+'PO.tab')+" : J=[A,B] P O;" 695 703 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 704 model=Model() 699 705 model.J = Set(dimen=2) 700 706 model.P = Param(model.J) 701 707 model.O = Param(model.J) 702 md.read(model) 703 instance = model.create(md) 708 instance = model.create(currdir+'importPO.dat') 704 709 self.failUnlessEqual(instance.J.data(), set([('A3', 'B3'), ('A1', 'B1'), ('A2', 'B2')]) ) 705 710 self.failUnlessEqual(instance.P.data(), {('A3', 'B3'): 4.5, ('A1', 'B1'): 4.3, ('A2', 'B2'): 4.4} ) 706 711 self.failUnlessEqual(instance.O.data(), {('A3', 'B3'): 5.5, ('A1', 'B1'): 5.3, ('A2', 'B2'): 5.4}) 712 os.remove(currdir+'importPO.dat') 713 714 715 class TestModelData(unittest.TestCase): 716 717 def test_tableA1(self): 718 """Importing a single column of data""" 719 md = ModelData() 720 md.add(os.path.abspath(example_dir+'A.tab'), format='set', set='A') 721 model=Model() 722 model.A = Set() 723 md.read(model) 724 instance = model.create(md) 725 self.failUnlessEqual(instance.A.data(), set(['A1', 'A2', 'A3'])) 726 727 def test_tableA2(self): 728 """Importing a single column of data""" 729 md = ModelData() 730 md.add(os.path.abspath(example_dir+'A.tab')) 731 model=Model() 732 model.A = Set() 733 try: 734 md.read(model) 735 instance = model.create(md) 736 self.fail("Should fail because no set name is specified") 737 except IOError: 738 pass 739 740 def test_tableA3(self): 741 """Importing a single column of data""" 742 md = ModelData() 743 md.add(os.path.abspath(example_dir+'A.tab'), set='A') 744 model=Model() 745 model.A = Set() 746 try: 747 md.read(model) 748 instance = model.create(md) 749 self.fail("Should fail because no index is specified") 750 except IOError: 751 pass 752 753 def test_tableB(self): 754 """Same as test_tableA""" 755 md = ModelData() 756 md.add(os.path.abspath(example_dir+'B.tab'), format='set', set='B') 757 model=Model() 758 model.B = Set() 759 md.read(model) 760 instance = model.create(md) 761 self.failUnlessEqual(instance.B.data(), set([1, 2, 3])) 762 763 def test_tableC(self): 764 """Importing a multicolumn table, where all columns are 765 treated as values for a set with tuple values.""" 766 md = ModelData() 767 md.add(os.path.abspath(example_dir+'C.tab'), format='set', set='C') 768 model=Model() 769 model.C = Set(dimen=2) 770 md.read(model) 771 instance = model.create(md) 772 self.failUnlessEqual(instance.C.data(), set([('A1',1), ('A1',2), ('A1',3), ('A2',1), ('A2',2), ('A2',3), ('A3',1), ('A3',2), ('A3',3)])) 773 774 def test_tableD(self): 775 """Importing a 2D array of data as a set.""" 776 md = ModelData() 777 md.add(os.path.abspath(example_dir+'D.tab'), format='set_array', set='C') 778 model=Model() 779 model.C = Set(dimen=2) 780 md.read(model) 781 instance = model.create(md) 782 self.failUnlessEqual(instance.C.data(), set([('A1',1), ('A2',2), ('A3',3)])) 783 784 def test_tableZ(self): 785 """Importing a single parameter""" 786 md = ModelData() 787 md.add(os.path.abspath(example_dir+'Z.tab'), format='param', param='Z') 788 model=Model() 789 model.Z = Param(default=99.0) 790 md.read(model) 791 instance = model.create(md) 792 self.failUnlessEqual(instance.Z.value, 1.1) 793 794 def test_tableY(self): 795 """Same as tableXW.""" 796 md = ModelData() 797 md.add(os.path.abspath(example_dir+'Y.tab'), index=['A'], param=['Y']) 798 model=Model() 799 model.A = Set(initialize=['A1','A2','A3','A4']) 800 model.Y = Param(model.A) 801 md.read(model) 802 instance = model.create(md) 803 self.failUnlessEqual(instance.A.data(), set(['A1','A2','A3','A4'])) 804 self.failUnlessEqual(instance.Y.data(), {'A1':3.3,'A2':3.4,'A3':3.5}) 805 806 def test_tableXW_1(self): 807 """Importing a table, but only reporting the values for the nonindex 808 parameter columns. The first column is assumed to represent an 809 index column.""" 810 md = ModelData() 811 md.add(os.path.abspath(example_dir+'XW.tab'), index=['A'], param=['X','W']) 812 model=Model() 813 model.A = Set(initialize=['A1','A2','A3','A4']) 814 model.X = Param(model.A) 815 model.W = Param(model.A) 816 md.read(model) 817 instance = model.create(md) 818 self.failUnlessEqual(instance.A.data(), set(['A1','A2','A3','A4'])) 819 self.failUnlessEqual(instance.X.data(), {'A1':3.3,'A2':3.4,'A3':3.5}) 820 self.failUnlessEqual(instance.W.data(), {'A1':4.3,'A2':4.4,'A3':4.5}) 821 822 def test_tableXW_2(self): 823 """Like test_tableXW_1, except that set A is not defined.""" 824 md = ModelData() 825 md.add(os.path.abspath(example_dir+'XW.tab'), index=['A'], param=['X','W']) 826 model=Model() 827 model.A = Set() 828 model.X = Param(model.A) 829 model.W = Param(model.A) 830 md.read(model) 831 instance = model.create(md) 832 self.failUnlessEqual(instance.A.data(), set()) 833 self.failUnlessEqual(instance.X.data(), {'A1':3.3,'A2':3.4,'A3':3.5}) 834 self.failUnlessEqual(instance.W.data(), {'A1':4.3,'A2':4.4,'A3':4.5}) 835 836 def test_tableXW_3(self): 837 """Like test_tableXW_1, except that set A is defined in the import statment.""" 838 md = ModelData() 839 md.add(os.path.abspath(example_dir+'XW.tab'), index_name='A', index=['A'], param=['X','W']) 840 model=Model() 841 model.A = Set() 842 model.X = Param(model.A) 843 model.W = Param(model.A) 844 md.read(model) 845 instance = model.create(md) 846 self.failUnlessEqual(instance.A.data(), set(['A1','A2','A3'])) 847 self.failUnlessEqual(instance.X.data(), {'A1':3.3,'A2':3.4,'A3':3.5}) 848 self.failUnlessEqual(instance.W.data(), {'A1':4.3,'A2':4.4,'A3':4.5}) 849 850 def test_tableXW_4(self): 851 """Like test_tableXW_1, except that set A is defined in the import statment and all values are mapped.""" 852 md = ModelData() 853 md.add(os.path.abspath(example_dir+'XW.tab'), index_name='B', index=['A'], param=['X','W'], param_name={'X':'R', 'W':'S'}) 854 model=Model() 855 model.B = Set() 856 model.R = Param(model.B) 857 model.S = Param(model.B) 858 md.read(model) 859 instance = model.create(md) 860 self.failUnlessEqual(instance.B.data(), set(['A1','A2','A3'])) 861 self.failUnlessEqual(instance.R.data(), {'A1':3.3,'A2':3.4,'A3':3.5}) 862 self.failUnlessEqual(instance.S.data(), {'A1':4.3,'A2':4.4,'A3':4.5}) 863 864 def test_tableT(self): 865 """Importing a 2D array of parameters that are transposed.""" 866 md = ModelData() 867 md.add(os.path.abspath(example_dir+'T.tab'), format='transposed_array', param='T') 868 model=Model() 869 model.A = Set() 870 model.B = Set() 871 model.T = Param(model.A, model.B) 872 md.read(model) 873 instance = model.create(md) 874 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}) 875 876 def test_tableU(self): 877 """Importing a 2D array of parameters.""" 878 md = ModelData() 879 md.add(os.path.abspath(example_dir+'T.tab'), format='array', param='U') 880 model=Model() 881 model.A = Set() 882 model.B = Set() 883 model.U = Param(model.A, model.B) 884 md.read(model) 885 instance = model.create(md) 886 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}) 887 888 def test_tableS(self): 889 """Importing a table, but only reporting the values for the nonindex 890 parameter columns. The first column is assumed to represent an 891 index column. A missing value is represented in the column data.""" 892 md = ModelData() 893 md.add(os.path.abspath(example_dir+'S.tab'), index=['A'], param=['S']) 894 model=Model() 895 model.A = Set(initialize=['A1','A2','A3','A4']) 896 model.S = Param(model.A) 897 md.read(model) 898 instance = model.create(md) 899 self.failUnlessEqual(instance.A.data(), set(['A1','A2','A3','A4'])) 900 self.failUnlessEqual(instance.S.data(), {'A1':3.3,'A3':3.5}) 901 902 def test_tablePO(self): 903 """Importing a table that has multiple indexing columns""" 904 pyutilib.misc.setup_redirect(currdir+'importPO.dat') 905 print "import "+os.path.abspath(example_dir+'PO.tab')+" : J=[A,B] P O;" 906 pyutilib.misc.reset_redirect() 907 md = ModelData() 908 md.add(os.path.abspath(example_dir+'PO.tab'), index_name='J', index=['A','B'], param=['P','O']) 909 model=Model() 910 model.J = Set(dimen=2) 911 model.P = Param(model.J) 912 model.O = Param(model.J) 913 md.read(model) 914 instance = model.create(md) 915 self.failUnlessEqual(instance.J.data(), set([('A3', 'B3'), ('A1', 'B1'), ('A2', 'B2')]) ) 916 self.failUnlessEqual(instance.P.data(), {('A3', 'B3'): 4.5, ('A1', 'B1'): 4.3, ('A2', 'B2'): 4.4} ) 917 self.failUnlessEqual(instance.O.data(), {('A3', 'B3'): 5.5, ('A1', 'B1'): 5.3, ('A2', 'B2'): 5.4}) 707 918 708 919
Note: See TracChangeset
for help on using the changeset viewer.