Ignore:
Timestamp:
Nov 20, 2009 5:27:31 PM (10 years ago)
Author:
wehart
Message:

Rework of the SolverResults? object, to provide for a more explicit
definition of solver results, and to support loading of
solver results from YAML-formated data.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • coopr.opt/trunk/coopr/opt/tests/base/test_soln.py

    r1768 r1918  
    2020import xml
    2121
    22 class OptSolnDebug(pyutilib.th.TestCase):
     22class Test(pyutilib.th.TestCase):
    2323
    2424    def setUp(self):
    2525        pyutilib.services.TempfileManager.tempdir = currdir
    2626        self.results = coopr.opt.SolverResults()
    27         self.soln = self.results.solution.create()
    28 
    29     def tmp(self):
     27        self.soln = self.results.solution.add()
    3028        self.soln.variable[0]=0.0
    3129        self.soln.variable[1]=0.0
     
    5048        self.failUnlessFileEqualsBaseline(currdir+"write_solution2.txt", currdir+"test2_soln.txt")
    5149
    52     def test_delete_solution(self):
     50    def test_read_solution1(self):
     51        """ Read a SolverResults Object"""
     52        self.results = coopr.opt.SolverResults()
     53        self.results.read(filename=currdir+"test4_sol.txt")
     54        self.results.write(filename=currdir+"read_solution1.out")
     55        if not os.path.exists(currdir+"read_solution1.out"):
     56           self.fail("test_read_solution1 - failed to write read_solution1.out")
     57        self.failUnlessFileEqualsBaseline(currdir+"read_solution1.out", currdir+"test4_sol.txt")
     58
     59    #
     60    # deleting is not supported right now
     61    #
     62    def Xtest_delete_solution(self):
    5363        """ Delete a solution from a SolverResults object """
    5464        self.results.solution.delete(0)
     
    7181           pass
    7282
    73     def test_set_solution_attr_error(self):
     83    #
     84    # This is currently allowed, although soln.variable = True is equivalent to
     85    #   soln.variable.value = True
     86    #
     87    def Xtest_set_solution_attr_error(self):
    7488        """ Create an error with a solution suffix """
    7589        try:
    7690           self.soln.variable = True
    77            self.fail("Expected attribute error failure for 'var'")
     91           self.fail("Expected attribute error failure for 'variable'")
    7892        except AttributeError:
    7993           pass
     
    8195    def test_soln_pprint1(self):
    8296        """ Write a solution with only zero values, using the results 'write()' method """
    83         self.soln.variable.add(0,0.0)
    84         self.soln.variable.add(1,0.0)
    85         self.soln.variable.add(3,0.0)
     97        self.soln.variable[0]=0.0
     98        self.soln.variable[1]=0.0
     99        self.soln.variable[3]=0.0
    86100        self.results.write(filename=currdir+"soln_pprint.txt")
    87101        if not os.path.exists(currdir+"soln_pprint.txt"):
     
    91105    def test_soln_pprint2(self):
    92106        """ Write a solution with only zero values, using the Solution.pprint() method """
    93         self.soln.variable.add(0,0.0)
    94         self.soln.variable.add(1,0.0)
    95         self.soln.variable.add(3,0.0)
     107        self.soln.variable[0]=0.0
     108        self.soln.variable[1]=0.0
     109        self.soln.variable[3]=0.0
    96110        pyutilib.misc.setup_redirect(currdir+"soln_pprint2.out")
    97         self.soln.pprint()
    98         self.soln._variable.pprint()
     111        print self.soln
     112        print self.soln.variable
    99113        pyutilib.misc.reset_redirect()
    100114        self.failUnlessFileEqualsBaseline(currdir+"soln_pprint2.out", currdir+"soln_pprint2.txt")
     
    102116    def test_soln_suffix_iter(self):
    103117        """ Test a suffix iterator """
    104         self.soln.variable.add(0,0.0)
    105         self.soln.variable.add(1,0.1)
    106         self.soln.variable.add(3,0.3)
     118        self.soln.variable[0]=0.0
     119        self.soln.variable[1]=0.1
     120        self.soln.variable[3]=0.3
    107121        i=0
    108         for val in self.soln.variable:
     122        for key in self.soln.variable:
    109123          i=i+1
    110           self.failUnlessEqual(val[1], val[0]/10.0)
     124          self.failUnlessEqual(self.soln.variable[key].value, self.soln.variable[key].id/10.0)
    111125        self.failUnlessEqual(i,len(self.soln.variable))
    112126
    113127    def test_soln_suffix_getiter(self):
    114         self.soln.variable.add("x0",0.0)
    115         self.soln.variable.add(1,0.1)
    116         self.soln.variable.add("x3",0.3)
    117         #print "HERE", self.soln.variable._index.keys()
    118         self.failUnlessEqual(self.soln.variable["x3"],0.3)
    119         self.failUnlessEqual(self.soln.variable[1],0.1)
     128        self.soln.variable["x0"]=0.0
     129        self.soln.variable[1]=0.1
     130        self.soln.variable["x3"]=0.3
     131        self.failUnlessEqual(self.soln.variable["x3"].value,0.3)
     132        #print "HERE",self.soln.variable._names
     133        self.failUnlessEqual(self.soln.variable[1].value,0.1)
    120134
    121135    def test_soln_suffix_setattr(self):
    122         self.soln.variable.add("x(0)",0.0)
    123         self.soln.variable.add("x3",0.3)
    124         self.soln.variable.add("y[1,ab]",0.5)
     136        self.soln.variable["x(0)"] = 0.0
     137        self.soln.variable["x3"]=0.3
     138        self.soln.variable["x3"].slack=0.4
     139        self.soln.variable["y[1,ab]"]=0.5
     140        #self.soln.variable.z[0,'a']=2.0
     141        #print "HERE",self.soln.variable._names
     142        #print "HERE",self.soln.variable.__dict__.keys()
     143        #print "HERE",self.soln.variable.keys()
     144        #print "HERE",self.soln.variable.__getitem__('y[1,ab]')
     145        #print "HERE",repr(self.soln.variable.x)
     146        #print "HERE",repr(self.soln.variable.x[0])
     147        #print "HERE",repr(self.soln.variable.y)
     148        #print "HERE",repr(self.soln.variable.y[1,'ab'])
     149        #print "HERE",repr(self.soln.variable.z)
     150        #print "HERE",repr(self.soln.variable.z[0,'a'])
     151        #print "HERE",repr(self.soln.variable.foo('x(0)'))
    125152        #self.soln.pprint()
    126153        #print "HERE", self.soln.variable._index.keys()
    127154        self.failUnlessEqual(self.soln.variable.x.keys(),[0])
    128         self.failUnlessEqual(self.soln.variable.x[0],0.0)
    129         self.failUnlessEqual(self.soln.variable.x3,0.3)
     155        self.failUnlessEqual(self.soln.variable.x[0].value,0.0)
     156        self.failUnlessEqual(self.soln.variable.x3.value,0.3)
     157        self.failUnlessEqual(self.soln.variable.x3.slack,0.4)
    130158        self.failUnlessEqual(self.soln.variable.y.keys(),[(1,'ab')])
    131         self.failUnlessEqual(self.soln.variable.y[1,'ab'],0.5)
    132 
     159        self.failUnlessEqual(self.soln.variable.y[1,'ab'].value,0.5)
    133160
    134161
    135162if __name__ == "__main__":
    136    unittest.main()
     163    import pyutilib.misc
     164    #sys.settrace(pyutilib.misc.traceit)
     165    unittest.main()
Note: See TracChangeset for help on using the changeset viewer.