Changeset 2267


Ignore:
Timestamp:
Feb 5, 2010 5:09:02 PM (10 years ago)
Author:
jwatson
Message:

Initial PySP unit tests!!!

Location:
coopr.pysp/trunk/coopr/pysp/tests/unit
Files:
2 added
1 edited

Legend:

Unmodified
Added
Removed
  • coopr.pysp/trunk/coopr/pysp/tests/unit/test_ph.py

    r1768 r2267  
    88sys.path.insert(0, dirname(dirname(abspath(__file__)))+"/../..")
    99currdir = dirname(abspath(__file__))+os.sep
     10
     11pysp_examples_dir = dirname(dirname(dirname(dirname(dirname(abspath(__file__))))))+os.sep+"examples"+os.sep+"pysp"+os.sep
     12
    1013#
    1114# Import the testing packages
     
    1417import pyutilib.misc
    1518import pyutilib.th
     19import pyutilib.subprocess
     20import coopr.pysp
     21import coopr.plugins.mip
     22
     23def filter_time(line):
     24   return "seconds" in line
     25
     26cplex = None
     27cplex_available = False
     28try:
     29    cplex = coopr.plugins.mip.CPLEX(keepFiles=True)
     30    cplex_available = (not cplex.executable() is None) and cplex.available(False)
     31except pyutilib.common.ApplicationError:
     32    cplex_available=False
     33
     34glpk = None
     35glpk_available = False
     36try:
     37    glpk = coopr.plugins.mip.GLPK(keepFiles=True)
     38    glpk_available = (not glpk.executable() is None) and glpk.available(False)
     39except pyutilib.common.ApplicationError:
     40    glpk_available=False
    1641
    1742#
     
    2247class TestPH(pyutilib.th.TestCase):
    2348
    24     #
    25     # A test which redirects IO to a file, prints stuff, resets the
    26     # redirection, and then compares with a baseline file.
    27     #
    28     def test_dummy(self):
    29         pyutilib.misc.setup_redirect(currdir+"dummy.out")
    30         print "HELLO WORLD"
     49    def test_quadratic_farmer(self):
     50
     51        if cplex_available is False:
     52           return
     53       
     54        pyutilib.misc.setup_redirect(currdir+"test_quadratic_farmer.out")
     55        print "RUNNING QUADRATIC FARMER TEST"
    3156        pyutilib.misc.reset_redirect()
    32         self.failUnlessFileEqualsBaseline(currdir+"dummy.out",currdir+"dummy.txt")
     57        farmer_examples_dir = pysp_examples_dir + os.sep + "farmer"
     58        model_dir = farmer_examples_dir + os.sep + "models"
     59        instance_dir = farmer_examples_dir + os.sep + "scenariodata"       
     60        pyutilib.subprocess.run("runph --solver=cplex --solver-manager=serial --model-directory="+model_dir+" --instance-directory="+instance_dir,outfile="farmer_quadratic.out")
     61        # if the test passes, the first argument is deleted.
     62        self.failUnlessFileEqualsBaseline(currdir+"farmer_quadratic.out",currdir+"farmer_quadratic.baseline", filter=filter_time)
     63
     64    def test_linearized_farmer(self):
     65
     66        if cplex_available is False:
     67           return       
     68
     69        pyutilib.misc.setup_redirect(currdir+"test_linearized_farmer.out")
     70        print "RUNNING LINEARIZED FARMER TEST"
     71        pyutilib.misc.reset_redirect()
     72        farmer_examples_dir = pysp_examples_dir + os.sep + "farmer"
     73        model_dir = farmer_examples_dir + os.sep + "models"
     74        instance_dir = farmer_examples_dir + os.sep + "scenariodata"       
     75        pyutilib.subprocess.run("runph --solver=cplex --solver-manager=serial --model-directory="+model_dir+" --instance-directory="+instance_dir+" --linearize-nonbinary-penalty-terms=10",outfile="farmer_linearized.out")
     76        # if the test passes, the first argument is deleted.
     77        self.failUnlessFileEqualsBaseline(currdir+"farmer_linearized.out",currdir+"farmer_linearized.baseline", filter=filter_time)
     78
     79    def run(self, result=None):
     80       global cplex_available
     81       if not cplex_available:
     82          return
     83       pyutilib.th.TestCase.run(self,result)
    3384
    3485if __name__ == "__main__":
Note: See TracChangeset for help on using the changeset viewer.