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

Last change on this file since 2931 was 2931, checked in by wehart, 10 years ago

Categorizing slow PySP tests as 'nightly'

File size: 6.0 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#
49
50# @unittest.category('nightly')
51class TestPH(unittest.TestCase):
52
53    def cleanup(self):
54
55        # IMPT: This step is key, as Python keys off the name of the module, not the location.
56        #       So, different reference models in different directories won't be detected.
57        #       If you don't do this, the symptom is a model that doesn't have the attributes
58        #       that the data file expects.
59        if "ReferenceModel" in sys.modules:
60           del sys.modules["ReferenceModel"]
61
62    @unittest.skipIf(not cplex_available, "The 'cplex' executable is not available")
63    def test_quadratic_farmer(self):
64        pyutilib.misc.setup_redirect(current_directory+"farmer_quadratic.out")
65        farmer_examples_dir = pysp_examples_dir + "farmer"
66        model_dir = farmer_examples_dir + os.sep + "models"
67        instance_dir = farmer_examples_dir + os.sep + "scenariodata"       
68        argstring = "runph --solver=cplex --solver-manager=serial --model-directory="+model_dir+" --instance-directory="+instance_dir
69        args = string.split(argstring)
70        coopr.pysp.phinit.run(args=args)
71        pyutilib.misc.reset_redirect()
72        self.cleanup()       
73        self.failUnlessFileEqualsBaseline(current_directory+"farmer_quadratic.out",current_directory+"farmer_quadratic.baseline", filter=filter_time)
74       
75    @unittest.skipIf(not cplex_available, "The 'cplex' executable is not available")
76    def test_linearized_farmer(self):
77        if cplex_available:
78            solver_string="cplex"
79        pyutilib.misc.setup_redirect(current_directory+"farmer_linearized.out")
80        farmer_examples_dir = pysp_examples_dir + "farmer"
81        model_dir = farmer_examples_dir + os.sep + "models"
82        instance_dir = farmer_examples_dir + os.sep + "scenariodata"       
83        argstring = "runph --solver="+solver_string+" --solver-manager=serial --model-directory="+model_dir+" --instance-directory="+instance_dir+" --linearize-nonbinary-penalty-terms=10"
84        args = string.split(argstring)
85        coopr.pysp.phinit.run(args=args)       
86        pyutilib.misc.reset_redirect()
87        self.cleanup()               
88        self.failUnlessFileEqualsBaseline(current_directory+"farmer_linearized.out",current_directory+"farmer_linearized.baseline", filter=filter_time)
89
90    @unittest.skipIf(not cplex_available, "The 'cplex' executable is not available")
91    def test_linearized_farmer_nodedata(self):
92        if cplex_available:
93            solver_string="cplex"
94        pyutilib.misc.setup_redirect(current_directory+"farmer_linearized_nodedata.out")
95        farmer_examples_dir = pysp_examples_dir + "farmer"
96        model_dir = farmer_examples_dir + os.sep + "models"
97        instance_dir = farmer_examples_dir + os.sep + "nodedata"       
98        argstring = "runph --solver="+solver_string+" --solver-manager=serial --model-directory="+model_dir+" --instance-directory="+instance_dir+" --linearize-nonbinary-penalty-terms=10"
99        args = string.split(argstring)
100        coopr.pysp.phinit.run(args=args)       
101        pyutilib.misc.reset_redirect()
102        self.cleanup()               
103        self.failUnlessFileEqualsBaseline(current_directory+"farmer_linearized_nodedata.out",current_directory+"farmer_linearized_nodedata.baseline", filter=filter_time)       
104
105    @unittest.skipIf(not cplex_available, "The 'cplex' executable is not available")
106    def test_quadratic_sizes3(self):
107
108        # Ignore this for now
109        return
110       
111        pyutilib.misc.setup_redirect(current_directory+"sizes3_quadratic.out")
112
113        sizes_example_dir = pysp_examples_dir + "sizes"
114        model_dir = sizes_example_dir + os.sep + "models"
115        instance_dir = sizes_example_dir + os.sep + "SIZES3"       
116        argstring = "runph --solver=cplex --solver-manager=serial --model-directory="+model_dir+" --instance-directory="+instance_dir+ \
117                    " --max-iterations=200"+ \
118                    " --rho-cfgfile="+sizes_example_dir+os.sep+"config"+os.sep+"rhosetter.cfg"+ \
119                    " --scenario-solver-options=mip_tolerances_integrality=1e-7"+ \
120                    " --enable-ww-extensions"+ \
121                    " --ww-extension-cfgfile="+sizes_example_dir+os.sep+"config"+os.sep+"wwph.cfg"+ \
122                    " --ww-extension-suffixfile="+sizes_example_dir+os.sep+"config"+os.sep+"wwph.suffixes"
123        args = string.split(argstring)
124        coopr.pysp.phinit.run(args=args)               
125        pyutilib.misc.reset_redirect()
126        self.failUnlessFileEqualsBaseline(current_directory+"sizes3_quadratic.out",current_directory+"sizes3_quadratic.baseline", filter=filter_time)
127        self.cleanup()       
128
129TestPH = unittest.category('nightly')(TestPH)
130
131
132if __name__ == "__main__":
133    unittest.main()
Note: See TracBrowser for help on using the repository browser.