source: coopr.pysp/trunk/coopr/pysp/tests/unit/test_ph.py @ 2268

Last change on this file since 2268 was 2268, checked in by wehart, 9 years ago

A fix to the tests.

File size: 3.1 KB
Line 
1#
2# Get the directory where this script is defined, and where the baseline
3# files are located.
4#
5import os
6import sys
7from os.path import abspath, dirname
8sys.path.insert(0, dirname(dirname(abspath(__file__)))+"/../..")
9currdir = 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
13#
14# Import the testing packages
15#
16import unittest
17import pyutilib.misc
18import 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
41
42#
43# Define a testing class, using the pyutilib.th.TestCase class.  This is
44# an extension of the unittest.TestCase class that adds additional testing
45# functions.
46#
47class TestPH(pyutilib.th.TestCase):
48
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"
56        pyutilib.misc.reset_redirect()
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("lbin 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("lbin 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)
84
85if __name__ == "__main__":
86   unittest.main()
Note: See TracBrowser for help on using the repository browser.