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

Last change on this file since 2275 was 2275, checked in by jwatson, 10 years ago

Testing improvements. From lpython, the tests run individually just fine. In aggregate, only the first run passes - independent of what test that might be! Something to stare at later....

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