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

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

Rework of unit tests to (a) import pyutilib.th as 'unittest' and
(b) employ test skipping.

File size: 5.9 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
7import string
8from os.path import abspath, dirname
9sys.path.insert(0, dirname(dirname(abspath(__file__)))+"/../..")
10
11current_directory = dirname(abspath(__file__))+os.sep
12
13pysp_examples_dir = dirname(dirname(dirname(dirname(dirname(abspath(__file__))))))+os.sep+"examples"+os.sep+"pysp"+os.sep
14
15coopr_bin_dir = dirname(dirname(dirname(dirname(dirname(dirname(dirname(abspath(__file__))))))))+os.sep+"bin"+os.sep
16
17#
18# Import the testing packages
19#
20import pyutilib.misc
21import pyutilib.th as unittest
22import pyutilib.subprocess
23import coopr.pysp
24import coopr.plugins.mip
25import coopr.pysp.phinit
26
27def filter_time(line):
28   return "seconds" in line
29
30cplex = None
31cplex_available = False
32try:
33    cplex = coopr.plugins.mip.CPLEX(keepFiles=True)
34    cplex_available = (not cplex.executable() is None) and cplex.available(False)
35except pyutilib.common.ApplicationError:
36    cplex_available=False
37
38glpk = None
39glpk_available = False
40try:
41    glpk = coopr.plugins.mip.GLPK(keepFiles=True)
42    glpk_available = (not glpk.executable() is None) and glpk.available(False)
43except pyutilib.common.ApplicationError:
44    glpk_available=False
45
46#
47# Define a testing class, using the unittest.TestCase class.
48#
49class TestPH(unittest.TestCase):
50
51    def cleanup(self):
52
53        # IMPT: This step is key, as Python keys off the name of the module, not the location.
54        #       So, different reference models in different directories won't be detected.
55        #       If you don't do this, the symptom is a model that doesn't have the attributes
56        #       that the data file expects.
57        if "ReferenceModel" in sys.modules:
58           del sys.modules["ReferenceModel"]
59
60    @unittest.skipIf(not cplex_available, "The 'cplex' executable is not available")
61    def test_quadratic_farmer(self):
62        pyutilib.misc.setup_redirect(current_directory+"farmer_quadratic.out")
63        farmer_examples_dir = pysp_examples_dir + "farmer"
64        model_dir = farmer_examples_dir + os.sep + "models"
65        instance_dir = farmer_examples_dir + os.sep + "scenariodata"       
66        argstring = "runph --solver=cplex --solver-manager=serial --model-directory="+model_dir+" --instance-directory="+instance_dir
67        args = string.split(argstring)
68        coopr.pysp.phinit.run(args=args)
69        pyutilib.misc.reset_redirect()
70        self.cleanup()       
71        self.failUnlessFileEqualsBaseline(current_directory+"farmer_quadratic.out",current_directory+"farmer_quadratic.baseline", filter=filter_time)
72       
73    @unittest.skipIf(not cplex_available, "The 'cplex' executable is not available")
74    def test_linearized_farmer(self):
75        if cplex_available:
76            solver_string="cplex"
77        pyutilib.misc.setup_redirect(current_directory+"farmer_linearized.out")
78        farmer_examples_dir = pysp_examples_dir + "farmer"
79        model_dir = farmer_examples_dir + os.sep + "models"
80        instance_dir = farmer_examples_dir + os.sep + "scenariodata"       
81        argstring = "runph --solver="+solver_string+" --solver-manager=serial --model-directory="+model_dir+" --instance-directory="+instance_dir+" --linearize-nonbinary-penalty-terms=10"
82        args = string.split(argstring)
83        coopr.pysp.phinit.run(args=args)       
84        pyutilib.misc.reset_redirect()
85        self.cleanup()               
86        self.failUnlessFileEqualsBaseline(current_directory+"farmer_linearized.out",current_directory+"farmer_linearized.baseline", filter=filter_time)
87
88    @unittest.skipIf(not cplex_available, "The 'cplex' executable is not available")
89    def test_linearized_farmer_nodedata(self):
90        if cplex_available:
91            solver_string="cplex"
92        pyutilib.misc.setup_redirect(current_directory+"farmer_linearized_nodedata.out")
93        farmer_examples_dir = pysp_examples_dir + "farmer"
94        model_dir = farmer_examples_dir + os.sep + "models"
95        instance_dir = farmer_examples_dir + os.sep + "nodedata"       
96        argstring = "runph --solver="+solver_string+" --solver-manager=serial --model-directory="+model_dir+" --instance-directory="+instance_dir+" --linearize-nonbinary-penalty-terms=10"
97        args = string.split(argstring)
98        coopr.pysp.phinit.run(args=args)       
99        pyutilib.misc.reset_redirect()
100        self.cleanup()               
101        self.failUnlessFileEqualsBaseline(current_directory+"farmer_linearized_nodedata.out",current_directory+"farmer_linearized_nodedata.baseline", filter=filter_time)       
102
103    @unittest.skipIf(not cplex_available, "The 'cplex' executable is not available")
104    def test_quadratic_sizes3(self):
105
106        # Ignore this for now
107        return
108       
109        pyutilib.misc.setup_redirect(current_directory+"sizes3_quadratic.out")
110
111        sizes_example_dir = pysp_examples_dir + "sizes"
112        model_dir = sizes_example_dir + os.sep + "models"
113        instance_dir = sizes_example_dir + os.sep + "SIZES3"       
114        argstring = "runph --solver=cplex --solver-manager=serial --model-directory="+model_dir+" --instance-directory="+instance_dir+ \
115                    " --max-iterations=200"+ \
116                    " --rho-cfgfile="+sizes_example_dir+os.sep+"config"+os.sep+"rhosetter.cfg"+ \
117                    " --scenario-solver-options=mip_tolerances_integrality=1e-7"+ \
118                    " --enable-ww-extensions"+ \
119                    " --ww-extension-cfgfile="+sizes_example_dir+os.sep+"config"+os.sep+"wwph.cfg"+ \
120                    " --ww-extension-suffixfile="+sizes_example_dir+os.sep+"config"+os.sep+"wwph.suffixes"
121        args = string.split(argstring)
122        coopr.pysp.phinit.run(args=args)               
123        pyutilib.misc.reset_redirect()
124        self.failUnlessFileEqualsBaseline(current_directory+"sizes3_quadratic.out",current_directory+"sizes3_quadratic.baseline", filter=filter_time)
125        self.cleanup()       
126
127if __name__ == "__main__":
128    unittest.main()
Note: See TracBrowser for help on using the repository browser.