source: coopr.pyomo/stable/2.3/coopr/pyomo/tests/examples/test_pyomo.py @ 2360

Last change on this file since 2360 was 2360, checked in by wehart, 11 years ago

Merged revisions 2315-2359 via svnmerge from
https://software.sandia.gov/svn/public/coopr/coopr.pyomo/trunk

........

r2330 | wehart | 2010-02-14 13:47:44 -0700 (Sun, 14 Feb 2010) | 7 lines


Setting directory properties to ignore *.pyc files.


Reworking parser management for data command files, to
directly manage the generation of the parsetab.py file.
This file is not called parse_table_datacmds.py, and it is
committed to the coopr.pyomo repository.

........

r2344 | wehart | 2010-02-15 13:44:18 -0700 (Mon, 15 Feb 2010) | 2 lines


Extending definition of svn:ignore to include *.egg-info files.

........

r2350 | wehart | 2010-02-15 15:17:53 -0700 (Mon, 15 Feb 2010) | 3 lines


Unfortunately, this file is being regenerated with path information.
For now, I'm going to delete this from the subversion repository.

........

r2359 | wehart | 2010-02-15 20:26:44 -0700 (Mon, 15 Feb 2010) | 30 lines


  1. Renaming the 'presolve' phase in Pyomo to 'preprocess'. We'll

eventually want an explicit presolve phase, but that will be applied
as a transformation, right before the problem is written and sent
to the solver. Currently, this preprocess phase collects data about
the model and computes the canonical representations for expressions.


  1. Created the ability to support the construction of concrete models.

The concrete_mode() method for Model objects enables this mode.
When in the concrete mode, each component added to the model has
its construct() method called immediately after the component is
added to the model. The symbolic_mode() method for Model objects
can be called to switch back to a symbolic mode (which defers the
execution of construct().


Bad things might happen if you switch back and forth between these
modes. Notably, if you ever generate components in symbolic mode,
then you need to switch back to symbolic mode before calling create().


Note that the Model.create() method is still called when the model
construction is 'done', even when done completely in concrete mode.
This basically calls the preprocessing steps, and this method returns
the current model (i.e. no cloning is done when building a concrete
model).


  1. Created an example directory for concrete models. Note that these

can still be solved with the Pyomo command line. In this
instance, the command line simply executes the script and applies
a solver.

........

File size: 4.4 KB
Line 
1#
2# Test the Pyomo command-line interface
3#
4
5import unittest
6import os
7import sys
8from os.path import abspath, dirname
9currdir = dirname(abspath(__file__))+os.sep
10scriptdir = dirname(dirname(dirname(dirname(dirname(abspath(__file__))))))+os.sep
11
12from coopr.pyomo import *
13import pyutilib.services
14import pyutilib.subprocess
15import pyutilib.th
16import coopr.pyomo.scripting.pyomo as main
17import StringIO 
18from pyutilib.misc import setup_redirect, reset_redirect
19
20if os.path.exists(sys.exec_prefix+os.sep+'bin'+os.sep+'coverage'):
21    executable=sys.exec_prefix+os.sep+'bin'+os.sep+'coverage -x '
22else:
23    executable=sys.executable
24
25class Test(pyutilib.th.TestCase):
26
27    def run(self, result=None):
28        """
29        Run the tests if GLPK is available
30        """
31        if pyutilib.services.registered_executable("glpsol") is None:
32            return
33        pyutilib.th.TestCase.run(self, result)
34
35    def pyomo(self, cmd, **kwds):
36        args=re.split('[ ]+',cmd)
37        if 'file' in kwds:
38            OUTPUT=kwds['file']
39        else:
40            OUTPUT=StringIO.StringIO()
41        setup_redirect(OUTPUT)
42        os.chdir(currdir)
43        output = main.run(list(args))
44        reset_redirect()
45        if not 'file' in kwds:
46            return OUTPUT.getvalue()
47        return output
48       
49    def test1(self):
50        """Simple execution of 'pyomo'"""
51        self.pyomo('pmedian.py pmedian.dat', file=currdir+'test1.out')
52        self.failUnlessFileEqualsBaseline(currdir+"test1.out", currdir+"test1.txt")
53
54    def test2(self):
55        """Run pyomo with bad --model-name option value"""
56        output=self.pyomo('--model-name=dummy pmedian.py pmedian.dat')
57        self.failUnlessEqual(output.strip(),"Exiting pyomo: Neither 'dummy' nor 'create_model' are available in module pmedian.py")
58
59    def test3(self):
60        """Run pyomo with model that does not define model object"""
61        output=self.pyomo('pmedian1.py pmedian.dat')
62        self.failUnlessEqual(output.strip(),"Exiting pyomo: Neither 'model' nor 'create_model' are available in module pmedian1.py")
63
64    def test4(self):
65        """Run pyomo with good --model-name option value"""
66        self.pyomo('--model-name=MODEL pmedian1.py pmedian.dat', file=currdir+'test4.out')
67        self.failUnlessFileEqualsBaseline(currdir+"test4.out", currdir+"test1.txt")
68
69    def test5(self):
70        """Run pyomo with create_model function"""
71        self.pyomo('pmedian2.py pmedian.dat', file=currdir+'test5.out')
72        self.failUnlessFileEqualsBaseline(currdir+"test5.out", currdir+"test1.txt")
73
74    def test6(self):
75        """Run pyomo with help-components option"""
76        self.pyomo('--help-components', file=currdir+'test6.out')
77        self.failUnlessFileEqualsBaseline(currdir+"test6.out", currdir+"test6.txt")
78
79    def Xtest7(self):
80        """Run pyomo with help option"""
81        self.pyomo('--help', file=currdir+'test7.out')
82        self.failUnlessFileEqualsBaseline(currdir+"test7.out", currdir+"test7.txt")
83
84    def test8(self):
85        """Run pyomo with --instance-only option"""
86        output = self.pyomo('--instance-only pmedian.py pmedian.dat', file=currdir+'test8.out')
87        self.failUnlessEqual(type(output.instance), Model)
88        os.remove(currdir+'test8.out')
89
90    def test9(self):
91        """Run pyomo with --disable-gc option"""
92        output = self.pyomo('--disable-gc pmedian.py pmedian.dat', file=currdir+'test9.out')
93        self.failUnlessEqual(type(output.instance), Model)
94        os.remove(currdir+'test9.out')
95
96    def test10(self):
97        """Run pyomo with --verbose option"""
98        def filter(line):
99            #print "HERE",line
100            return line.startswith("Writing")
101        self.pyomo('-v pmedian.py pmedian.dat', file=currdir+'test10.out')
102        self.failUnlessFileEqualsBaseline(currdir+"test10.out", currdir+"test10.txt", filter)
103
104    def test11(self):
105        """Run pyomo with --debug=generate option"""
106        self.pyomo('--debug=generate pmedian.py pmedian.dat', file=currdir+'test11.out')
107        self.failUnlessFileEqualsBaseline(currdir+"test11.out", currdir+"test11.txt")
108
109    def test12(self):
110        """Run pyomo with --logfile option"""
111        self.pyomo('--debug=errors --logfile=%s pmedian.py pmedian.dat' % (currdir+'test12.log'), file=currdir+'test12.out')
112        self.failUnlessFileEqualsBaseline(currdir+"test12.log", currdir+"test12.txt")
113        os.remove(currdir+'test12.out')
114
115
116if __name__ == "__main__":
117   unittest.main()
Note: See TracBrowser for help on using the repository browser.