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

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

Disabling tests that would rely on glpk, since that solver creates diffs
in PH output.

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 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        solver_string = ""
82        if cplex_available:
83            solver_string="cplex"
84        #elif glpk_available:
85            #solver_string="glpk"
86        else:
87           return
88
89        pyutilib.misc.setup_redirect(current_directory+"farmer_linearized.out")
90        farmer_examples_dir = pysp_examples_dir + "farmer"
91        model_dir = farmer_examples_dir + os.sep + "models"
92        instance_dir = farmer_examples_dir + os.sep + "scenariodata"       
93        argstring = "runph --solver="+solver_string+" --solver-manager=serial --model-directory="+model_dir+" --instance-directory="+instance_dir+" --linearize-nonbinary-penalty-terms=10"
94        args = string.split(argstring)
95        coopr.pysp.phinit.run(args=args)       
96        pyutilib.misc.reset_redirect()
97        self.cleanup()               
98        self.failUnlessFileEqualsBaseline(current_directory+"farmer_linearized.out",current_directory+"farmer_linearized.baseline", filter=filter_time)
99
100    def test_linearized_farmer_nodedata(self):
101
102        solver_string = ""
103        if cplex_available:
104            solver_string="cplex"
105        #elif glpk_available:
106            #solver_string="glpk"
107        else:
108           return
109
110        pyutilib.misc.setup_redirect(current_directory+"farmer_linearized_nodedata.out")
111        farmer_examples_dir = pysp_examples_dir + "farmer"
112        model_dir = farmer_examples_dir + os.sep + "models"
113        instance_dir = farmer_examples_dir + os.sep + "nodedata"       
114        argstring = "runph --solver="+solver_string+" --solver-manager=serial --model-directory="+model_dir+" --instance-directory="+instance_dir+" --linearize-nonbinary-penalty-terms=10"
115        args = string.split(argstring)
116        coopr.pysp.phinit.run(args=args)       
117        pyutilib.misc.reset_redirect()
118        self.cleanup()               
119        self.failUnlessFileEqualsBaseline(current_directory+"farmer_linearized_nodedata.out",current_directory+"farmer_linearized_nodedata.baseline", filter=filter_time)       
120
121    def test_quadratic_sizes3(self):
122
123        # Ignore this for now
124        return
125
126        if cplex_available is False:
127           return
128       
129        pyutilib.misc.setup_redirect(current_directory+"sizes3_quadratic.out")
130
131        sizes_example_dir = pysp_examples_dir + "sizes"
132        model_dir = sizes_example_dir + os.sep + "models"
133        instance_dir = sizes_example_dir + os.sep + "SIZES3"       
134        argstring = "runph --solver=cplex --solver-manager=serial --model-directory="+model_dir+" --instance-directory="+instance_dir+ \
135                    " --max-iterations=200"+ \
136                    " --rho-cfgfile="+sizes_example_dir+os.sep+"config"+os.sep+"rhosetter.cfg"+ \
137                    " --scenario-solver-options=mip_tolerances_integrality=1e-7"+ \
138                    " --enable-ww-extensions"+ \
139                    " --ww-extension-cfgfile="+sizes_example_dir+os.sep+"config"+os.sep+"wwph.cfg"+ \
140                    " --ww-extension-suffixfile="+sizes_example_dir+os.sep+"config"+os.sep+"wwph.suffixes"
141        args = string.split(argstring)
142        coopr.pysp.phinit.run(args=args)               
143        pyutilib.misc.reset_redirect()
144        self.failUnlessFileEqualsBaseline(current_directory+"sizes3_quadratic.out",current_directory+"sizes3_quadratic.baseline", filter=filter_time)
145        self.cleanup()       
146
147if __name__ == "__main__":
148   unittest.main()
Note: See TracBrowser for help on using the repository browser.