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

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

Making unit tests for PySP compatible with coverage utilities. Farmer examples work, SIZES3 doesn't for some reason.

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        sys.argv = args
66        coopr.pysp.phinit.run()
67        pyutilib.misc.reset_redirect()       
68        self.failUnlessFileEqualsBaseline(curr_dir+"farmer_quadratic.out",curr_dir+"farmer_quadratic.baseline", filter=filter_time)
69
70    def test_linearized_farmer(self):
71
72        if cplex_available is False:
73           return       
74
75        pyutilib.misc.setup_redirect(curr_dir+"farmer_linearized.out")
76        farmer_examples_dir = pysp_examples_dir + os.sep + "farmer"
77        model_dir = farmer_examples_dir + os.sep + "models"
78        instance_dir = farmer_examples_dir + os.sep + "scenariodata"       
79        argstring = "runph --solver=cplex --solver-manager=serial --model-directory="+model_dir+" --instance-directory="+instance_dir+" --linearize-nonbinary-penalty-terms=10"
80        args = string.split(argstring)
81        sys.argv = args
82        coopr.pysp.phinit.run()       
83        pyutilib.misc.reset_redirect()       
84        self.failUnlessFileEqualsBaseline(curr_dir+"farmer_linearized.out",curr_dir+"farmer_linearized.baseline", filter=filter_time)
85
86    def test_quadratic_sizes3(self):
87
88        if cplex_available is False:
89           return
90       
91        pyutilib.misc.setup_redirect(curr_dir+"sizes3_quadratic.out")
92
93        sizes_example_dir = pysp_examples_dir + os.sep + "sizes"
94        model_dir = sizes_example_dir + os.sep + "models"
95        instance_dir = sizes_example_dir + os.sep + "SIZES3"       
96        argstring = "runph --solver=cplex --solver-manager=serial --model-directory="+model_dir+" --instance-directory="+instance_dir+ \
97                    " --max-iterations=200"+ \
98                    " --rho-cfgfile="+sizes_example_dir+os.sep+"config"+os.sep+"rhosetter.cfg"+ \
99                    " --scenario-solver-options=\"mip_tolerances_integrality=1e-7\""+ \
100                    " --enable-ww-extensions"+ \
101                    " --ww-extension-cfgfile="+sizes_example_dir+os.sep+"config"+os.sep+"wwph.cfg"+ \
102                    " --ww-extension-suffixfile="+sizes_example_dir+os.sep+"config"+os.sep+"wwph.suffixes"
103        print "ARGSTRING=",argstring
104        args = string.split(argstring)
105        print "ARGS=",args
106        sys.argv = args
107        coopr.pysp.phinit.run()               
108        pyutilib.misc.reset_redirect()
109        print args
110        self.failUnlessFileEqualsBaseline(curr_dir+"sizes3_quadratic.out",curr_dir+"sizes3_quadratic.baseline", filter=filter_time)       
111
112    def run(self, result=None):
113       global cplex_available
114       if not cplex_available:
115          return
116       pyutilib.th.TestCase.run(self,result)
117
118if __name__ == "__main__":
119   unittest.main()
Note: See TracBrowser for help on using the repository browser.