Ignore:
Timestamp:
Feb 8, 2010 5:13:22 PM (10 years ago)
Author:
wehart
Message:

Setting up 'import' tests for the other simple examples.
These seem to work, but the internal code is too ugly. I'm
going to clean that up next to help explain what is going on...

File:
1 edited

Legend:

Unmodified
Added
Removed
  • coopr.pyomo/trunk/coopr/pyomo/tests/unit/test_modeldata.py

    r2285 r2289  
    320320        os.remove(currdir+'importC.dat')
    321321
    322     def Xtest_tableD(self):
     322    def test_tableD(self):
    323323        """Importing a 2D array of data as a set."""
    324324        pyutilib.misc.setup_redirect(currdir+'importD.dat')
     
    328328        model.C = Set(dimen=2)
    329329        instance = model.create(currdir+'importD.dat')
    330         self.failUnlessEqual(instance.C.data(), set([('A1',1), ('A1',2), ('A1',3), ('A2',1), ('A2',2), ('A2',3), ('A3',1), ('A3',2), ('A3',3)]))
    331         os.remove(currdir+'importC.dat')
    332 
    333     def test_tableS(self):
    334         """Importing a table, but only reporting the values for the non-index
    335         parameter columns.  The first column is assumed to represent an
    336         index column.  A missing value is represented in the column data."""
    337         pyutilib.misc.setup_redirect(currdir+'importS.dat')
    338         print "import "+os.path.abspath(example_dir+'S.tab')+" ;"
     330        self.failUnlessEqual(instance.C.data(), set([('A1',1), ('A2',2), ('A3',3)]))
     331        os.remove(currdir+'importD.dat')
     332
     333    def test_tableZ(self):
     334        """Importing a single parameter"""
     335        pyutilib.misc.setup_redirect(currdir+'importZ.dat')
     336        print "import "+os.path.abspath(example_dir+'Z.tab')+" format=param: Z ;"
     337        pyutilib.misc.reset_redirect()
     338        model=Model()
     339        model.Z = Param(default=99.0)
     340        instance = model.create(currdir+'importZ.dat')
     341        self.failUnlessEqual(instance.Z.value, 1.1)
     342        os.remove(currdir+'importZ.dat')
     343
     344    def test_tableY(self):
     345        """Same as tableXW."""
     346        pyutilib.misc.setup_redirect(currdir+'importY.dat')
     347        print "import "+os.path.abspath(example_dir+'Y.tab')+" ;"
    339348        pyutilib.misc.reset_redirect()
    340349        model=Model()
    341350        model.A = Set(initialize=['A1','A2','A3','A4'])
    342         model.S = Param(model.A)
    343         instance = model.create(currdir+'importS.dat')
     351        model.Y = Param(model.A)
     352        instance = model.create(currdir+'importY.dat')
    344353        self.failUnlessEqual(instance.A.data(), set(['A1','A2','A3','A4']))
    345         self.failUnlessEqual(instance.S.data(), {'A1':3.3,'A3':3.5})
    346         os.remove(currdir+'importS.dat')
     354        self.failUnlessEqual(instance.Y.data(), {'A1':3.3,'A2':3.4,'A3':3.5})
     355        os.remove(currdir+'importY.dat')
    347356
    348357    def test_tableXW_1(self):
     
    408417        os.remove(currdir+'importXW.dat')
    409418
    410     def test_tableY(self):
    411         """Same as tableXW."""
    412         pyutilib.misc.setup_redirect(currdir+'importY.dat')
    413         print "import "+os.path.abspath(example_dir+'Y.tab')+" ;"
     419    def test_tableT(self):
     420        """Importing a 2D array of parameters that are transposed."""
     421        pyutilib.misc.setup_redirect(currdir+'importT.dat')
     422        print "import "+os.path.abspath(example_dir+'T.tab')+" format=transposed_array : T;"
     423        pyutilib.misc.reset_redirect()
     424        model=Model()
     425        model.A = Set()
     426        model.B = Set()
     427        model.T = Param(model.A, model.B)
     428        instance = model.create(currdir+'importT.dat')
     429        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})
     430        os.remove(currdir+'importT.dat')
     431
     432    def test_tableU(self):
     433        """Importing a 2D array of parameters."""
     434        pyutilib.misc.setup_redirect(currdir+'importU.dat')
     435        print "import "+os.path.abspath(example_dir+'U.tab')+" format=array : U;"
     436        pyutilib.misc.reset_redirect()
     437        model=Model()
     438        model.A = Set()
     439        model.B = Set()
     440        model.U = Param(model.A, model.B)
     441        instance = model.create(currdir+'importU.dat')
     442        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})
     443        os.remove(currdir+'importU.dat')
     444
     445    def test_tableS(self):
     446        """Importing a table, but only reporting the values for the non-index
     447        parameter columns.  The first column is assumed to represent an
     448        index column.  A missing value is represented in the column data."""
     449        pyutilib.misc.setup_redirect(currdir+'importS.dat')
     450        print "import "+os.path.abspath(example_dir+'S.tab')+" ;"
    414451        pyutilib.misc.reset_redirect()
    415452        model=Model()
    416453        model.A = Set(initialize=['A1','A2','A3','A4'])
    417         model.Y = Param(model.A)
    418         instance = model.create(currdir+'importY.dat')
     454        model.S = Param(model.A)
     455        instance = model.create(currdir+'importS.dat')
    419456        self.failUnlessEqual(instance.A.data(), set(['A1','A2','A3','A4']))
    420         self.failUnlessEqual(instance.Y.data(), {'A1':3.3,'A2':3.4,'A3':3.5})
    421         os.remove(currdir+'importY.dat')
     457        self.failUnlessEqual(instance.S.data(), {'A1':3.3,'A3':3.5})
     458        os.remove(currdir+'importS.dat')
     459
     460    def test_tablePO(self):
     461        """Importing a table that has multiple indexing columns"""
     462        pyutilib.misc.setup_redirect(currdir+'importPO.dat')
     463        print "import "+os.path.abspath(example_dir+'PO.tab')+" : [A,B] J:A,B P O;"
     464        pyutilib.misc.reset_redirect()
     465        model=Model()
     466        model.J = Set(dimen=2)
     467        model.P = Param(model.J)
     468        model.O = Param(model.J)
     469        instance = model.create(currdir+'importPO.dat')
     470        self.failUnlessEqual(instance.J.data(), set([('A3', 'B3'), ('A1', 'B1'), ('A2', 'B2')]) )
     471        self.failUnlessEqual(instance.P.data(), {('A3', 'B3'): 4.5, ('A1', 'B1'): 4.3, ('A2', 'B2'): 4.4} )
     472        self.failUnlessEqual(instance.O.data(), {('A3', 'B3'): 5.5, ('A1', 'B1'): 5.3, ('A2', 'B2'): 5.4})
     473        os.remove(currdir+'importPO.dat')
    422474
    423475
Note: See TracChangeset for help on using the changeset viewer.