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

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

Various fixes to PySP unit tests. Changing name of testphextension to examplephextension - with the test prefix, coverage tests import the module, which causes all kinds of issues.

File size: 4.5 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__)))+"/../..")
10curr_dir = dirname(abspath(__file__))+os.sep
11
12pysp_examples_dir = dirname(dirname(dirname(dirname(dirname(abspath(__file__))))))+os.sep+"examples"+os.sep+"pysp"+os.sep
13
14coopr_bin_dir = dirname(dirname(dirname(dirname(dirname(dirname(dirname(abspath(__file__))))))))+os.sep+"bin"+os.sep
15
16#
17# Import the testing packages
18#
19import unittest
20import pyutilib.misc
21import pyutilib.th
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 pyutilib.th.TestCase class.  This is
48# an extension of the unittest.TestCase class that adds additional testing
49# functions.
50#
51class TestPH(pyutilib.th.TestCase):
52
53    def test_quadratic_farmer(self):
54
55        if cplex_available is False:
56           return
57       
58        pyutilib.misc.setup_redirect(curr_dir+"farmer_quadratic.out")
59
60        farmer_examples_dir = pysp_examples_dir + os.sep + "farmer"
61        model_dir = farmer_examples_dir + os.sep + "models"
62        instance_dir = farmer_examples_dir + os.sep + "scenariodata"       
63        argstring = "runph --solver=cplex --solver-manager=serial --model-directory="+model_dir+" --instance-directory="+instance_dir
64        args = string.split(argstring)
65        coopr.pysp.phinit.run(args=args)
66        pyutilib.misc.reset_redirect()       
67        self.failUnlessFileEqualsBaseline(curr_dir+"farmer_quadratic.out",curr_dir+"farmer_quadratic.baseline", filter=filter_time)
68
69    def test_linearized_farmer(self):
70
71        if cplex_available is False:
72           return       
73
74        pyutilib.misc.setup_redirect(curr_dir+"farmer_linearized.out")
75        farmer_examples_dir = pysp_examples_dir + os.sep + "farmer"
76        model_dir = farmer_examples_dir + os.sep + "models"
77        instance_dir = farmer_examples_dir + os.sep + "scenariodata"       
78        argstring = "runph --solver=cplex --solver-manager=serial --model-directory="+model_dir+" --instance-directory="+instance_dir+" --linearize-nonbinary-penalty-terms=10"
79        args = string.split(argstring)
80        coopr.pysp.phinit.run(args=args)       
81        pyutilib.misc.reset_redirect()       
82        self.failUnlessFileEqualsBaseline(curr_dir+"farmer_linearized.out",curr_dir+"farmer_linearized.baseline", filter=filter_time)
83
84    def test_quadratic_sizes3(self):
85
86        if cplex_available is False:
87           return
88       
89        pyutilib.misc.setup_redirect(curr_dir+"sizes3_quadratic.out")
90
91        sizes_example_dir = pysp_examples_dir + os.sep + "sizes"
92        model_dir = sizes_example_dir + os.sep + "models"
93        instance_dir = sizes_example_dir + os.sep + "SIZES3"       
94        argstring = "runph --solver=cplex --solver-manager=serial --model-directory="+model_dir+" --instance-directory="+instance_dir+ \
95                    " --max-iterations=200"+ \
96                    " --rho-cfgfile="+sizes_example_dir+os.sep+"config"+os.sep+"rhosetter.cfg"+ \
97                    " --scenario-solver-options=\"mip_tolerances_integrality=1e-7\""+ \
98                    " --enable-ww-extensions"+ \
99                    " --ww-extension-cfgfile="+sizes_example_dir+os.sep+"config"+os.sep+"wwph.cfg"+ \
100                    " --ww-extension-suffixfile="+sizes_example_dir+os.sep+"config"+os.sep+"wwph.suffixes"
101        print "ARGSTRING=",argstring
102        args = string.split(argstring)
103        print "ARGS=",args
104        coopr.pysp.phinit.run(args=args)               
105        pyutilib.misc.reset_redirect()
106        print args
107        self.failUnlessFileEqualsBaseline(curr_dir+"sizes3_quadratic.out",curr_dir+"sizes3_quadratic.baseline", filter=filter_time)       
108
109    def run(self, result=None):
110       global cplex_available
111       if not cplex_available:
112          return
113       pyutilib.th.TestCase.run(self,result)
114
115if __name__ == "__main__":
116   unittest.main()
Note: See TracBrowser for help on using the repository browser.