source: coopr.pysp/stable/coopr/pysp/tests/unit/test_ph.py @ 3286

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

Merged revisions 3187-3285 via svnmerge from
https://software.sandia.gov/svn/public/coopr/coopr.pysp/trunk

........

r3201 | jwatson | 2010-10-29 13:18:17 -0600 (Fri, 29 Oct 2010) | 3 lines


Inverting order of .dat files in PySP when loading from a node representation - now root-to-leaf, instead of leaf-to-root. This allows for deeper-in-the-tree nodes to over-write parameter values defined higher in the tree, which is a more "expected" behavior than the converse. The real answer is to throw an exception if a parameter is re-defined, but we're not there yet.

........

r3217 | jwatson | 2010-11-05 11:29:42 -0600 (Fri, 05 Nov 2010) | 3 lines


Various updates to support heteogeneous index sets in PH for different nodes in the scenario tree - more work / testing remains.

........

r3218 | jwatson | 2010-11-05 12:35:51 -0600 (Fri, 05 Nov 2010) | 3 lines


More changes associated with generalizing the PySP index structures from per-stage to per-node.

........

r3220 | jwatson | 2010-11-05 20:28:29 -0600 (Fri, 05 Nov 2010) | 3 lines


Various fixes to the WW PH extension, bringing it in compliance to previous commit changes.

........

r3221 | jwatson | 2010-11-05 20:43:40 -0600 (Fri, 05 Nov 2010) | 1 line


Removing some older PySP test problems from the repository

........

r3222 | jwatson | 2010-11-05 20:55:15 -0600 (Fri, 05 Nov 2010) | 1 line


Moving PySP forestry example to local sandbox, to streamline distribution.

........

r3261 | jwatson | 2010-11-29 15:26:46 -0700 (Mon, 29 Nov 2010) | 9 lines


Adding two options to the runef and runph pysp scripts, to facilitate scenario downsampling - the case where you have a big tree, but you don't want to use it all.


The options are:
--scenario-tree-downsample-fraction=X
--scenario-tree-random-seed


The options are fairly self-explanatory - the only possible nuance is that the downsample fraction is the fraction of scenarios retained.

........

r3263 | jwatson | 2010-12-01 10:47:21 -0700 (Wed, 01 Dec 2010) | 3 lines


Corrected issue with cvar generation introduced a while back.

........

r3264 | jwatson | 2010-12-01 11:16:01 -0700 (Wed, 01 Dec 2010) | 3 lines


Adding PySP extensive form tests.

........

r3265 | wehart | 2010-12-01 13:19:56 -0700 (Wed, 01 Dec 2010) | 2 lines


Auxmenting the filter

........

r3266 | wehart | 2010-12-01 13:58:59 -0700 (Wed, 01 Dec 2010) | 2 lines


Adding further diagnostics to the filter.

........

r3267 | wehart | 2010-12-01 14:12:15 -0700 (Wed, 01 Dec 2010) | 2 lines


Another attempt to fix this filter...

........

r3271 | wehart | 2010-12-01 15:51:23 -0700 (Wed, 01 Dec 2010) | 4 lines


Simplifying filter.


The error was really a Python 2.5 portability issue in EF. :(

........

r3275 | jwatson | 2010-12-02 19:35:52 -0700 (Thu, 02 Dec 2010) | 3 lines


Fixing Python 2.5-related issue with string.translate in phutils.py - using None as the translation table is not allowed in Python 2.5.

........

File size: 7.6 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
26import coopr.pysp.ef_writer_script
27
28def filter_time(line):
29    return "seconds" in line or line.startswith("Data for")
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 unittest.TestCase class.
49#
50
51# @unittest.category('nightly')
52class TestPH(unittest.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    @unittest.skipIf(not cplex_available, "The 'cplex' executable is not available")
64    def test_quadratic_farmer(self):
65        pyutilib.misc.setup_redirect(current_directory+"farmer_quadratic.out")
66        farmer_examples_dir = pysp_examples_dir + "farmer"
67        model_dir = farmer_examples_dir + os.sep + "models"
68        instance_dir = farmer_examples_dir + os.sep + "scenariodata"       
69        argstring = "runph --solver=cplex --solver-manager=serial --model-directory="+model_dir+" --instance-directory="+instance_dir
70        args = string.split(argstring)
71        coopr.pysp.phinit.run(args=args)
72        pyutilib.misc.reset_redirect()
73        self.cleanup()       
74        self.failUnlessFileEqualsBaseline(current_directory+"farmer_quadratic.out",current_directory+"farmer_quadratic.baseline", filter=filter_time)
75       
76    @unittest.skipIf(not cplex_available, "The 'cplex' executable is not available")
77    def test_linearized_farmer(self):
78        if cplex_available:
79            solver_string="cplex"
80        pyutilib.misc.setup_redirect(current_directory+"farmer_linearized.out")
81        farmer_examples_dir = pysp_examples_dir + "farmer"
82        model_dir = farmer_examples_dir + os.sep + "models"
83        instance_dir = farmer_examples_dir + os.sep + "scenariodata"       
84        argstring = "runph --solver="+solver_string+" --solver-manager=serial --model-directory="+model_dir+" --instance-directory="+instance_dir+" --linearize-nonbinary-penalty-terms=10"
85        args = string.split(argstring)
86        coopr.pysp.phinit.run(args=args)       
87        pyutilib.misc.reset_redirect()
88        self.cleanup()               
89        self.failUnlessFileEqualsBaseline(current_directory+"farmer_linearized.out",current_directory+"farmer_linearized.baseline", filter=filter_time)
90
91    @unittest.skipIf(not cplex_available, "The 'cplex' executable is not available")
92    def test_linearized_farmer_nodedata(self):
93        if cplex_available:
94            solver_string="cplex"
95        pyutilib.misc.setup_redirect(current_directory+"farmer_linearized_nodedata.out")
96        farmer_examples_dir = pysp_examples_dir + "farmer"
97        model_dir = farmer_examples_dir + os.sep + "models"
98        instance_dir = farmer_examples_dir + os.sep + "nodedata"       
99        argstring = "runph --solver="+solver_string+" --solver-manager=serial --model-directory="+model_dir+" --instance-directory="+instance_dir+" --linearize-nonbinary-penalty-terms=10"
100        args = string.split(argstring)
101        coopr.pysp.phinit.run(args=args)       
102        pyutilib.misc.reset_redirect()
103        self.cleanup()               
104        self.failUnlessFileEqualsBaseline(current_directory+"farmer_linearized_nodedata.out",current_directory+"farmer_linearized_nodedata.baseline", filter=filter_time)       
105
106    @unittest.skipIf(not cplex_available, "The 'cplex' executable is not available")
107    def test_quadratic_sizes3(self):
108
109        # Ignore this for now
110        return
111       
112        pyutilib.misc.setup_redirect(current_directory+"sizes3_quadratic.out")
113
114        sizes_example_dir = pysp_examples_dir + "sizes"
115        model_dir = sizes_example_dir + os.sep + "models"
116        instance_dir = sizes_example_dir + os.sep + "SIZES3"       
117        argstring = "runph --solver=cplex --solver-manager=serial --model-directory="+model_dir+" --instance-directory="+instance_dir+ \
118                    " --max-iterations=200"+ \
119                    " --rho-cfgfile="+sizes_example_dir+os.sep+"config"+os.sep+"rhosetter.cfg"+ \
120                    " --scenario-solver-options=mip_tolerances_integrality=1e-7"+ \
121                    " --enable-ww-extensions"+ \
122                    " --ww-extension-cfgfile="+sizes_example_dir+os.sep+"config"+os.sep+"wwph.cfg"+ \
123                    " --ww-extension-suffixfile="+sizes_example_dir+os.sep+"config"+os.sep+"wwph.suffixes"
124        args = string.split(argstring)
125        coopr.pysp.phinit.run(args=args)               
126        pyutilib.misc.reset_redirect()
127        self.failUnlessFileEqualsBaseline(current_directory+"sizes3_quadratic.out",current_directory+"sizes3_quadratic.baseline", filter=filter_time)
128        self.cleanup()       
129
130    def test_farmer_ef(self):
131        pyutilib.misc.setup_redirect(current_directory+"farmer_ef.out")
132        farmer_examples_dir = pysp_examples_dir + "farmer"
133        model_dir = farmer_examples_dir + os.sep + "models"
134        instance_dir = farmer_examples_dir + os.sep + "scenariodata"       
135        argstring = "runef --verbose --output-file=farmer_ef.out --model-directory="+model_dir+" --instance-directory="+instance_dir
136        args = string.split(argstring)
137        coopr.pysp.ef_writer_script.run(args=args)       
138        pyutilib.misc.reset_redirect()
139        self.cleanup()               
140        self.failUnlessFileEqualsBaseline(current_directory+"farmer_ef.out",current_directory+"farmer_ef.baseline", filter=filter_time)
141
142    def test_farmer_ef_cvar(self):
143        pyutilib.misc.setup_redirect(current_directory+"farmer_ef_cvar.out")
144        farmer_examples_dir = pysp_examples_dir + "farmer"
145        model_dir = farmer_examples_dir + os.sep + "models"
146        instance_dir = farmer_examples_dir + os.sep + "scenariodata"       
147        argstring = "runef --verbose --output-file=farmer_ef_cvar.out --generate-weighted-cvar --risk-alpha=0.90 --cvar-weight=0.0 --model-directory="+model_dir+" --instance-directory="+instance_dir
148        args = string.split(argstring)
149        coopr.pysp.ef_writer_script.run(args=args)       
150        pyutilib.misc.reset_redirect()
151        self.cleanup()               
152        self.failUnlessFileEqualsBaseline(current_directory+"farmer_ef_cvar.out",current_directory+"farmer_ef_cvar.baseline", filter=filter_time)
153
154TestPH = unittest.category('nightly')(TestPH)
155
156
157if __name__ == "__main__":
158    unittest.main()
Note: See TracBrowser for help on using the repository browser.