source: coopr.pyomo/stable/2.3/coopr/pyomo/tests/unit/test_concrete.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: 1.6 KB
Line 
1#
2# Test behavior of concrete classes.
3#
4
5import unittest
6import os
7import sys
8from os.path import abspath, dirname
9sys.path.insert(0, dirname(dirname(abspath(__file__)))+os.sep+".."+os.sep+"..")
10currdir = dirname(abspath(__file__))+os.sep
11
12from coopr.pyomo import *
13from coopr.opt import *
14import pyutilib.th
15import pyutilib.services
16
17class Test(pyutilib.th.TestCase):
18
19    def test_blending(self):
20        """ The blending example from the PuLP documentation """
21        if not pyutilib.services.registered_executable("glpsol"):
22            return
23        model = Model()
24        model.concrete_mode()
25
26        model.x1 = Var(bounds=(0,None), doc="ChickenPercent")
27        model.x2 = Var(bounds=(0,None), doc="BeefPercent")
28
29        model.obj = Objective(expr=0.013*model.x1 + 0.008*model.x2, doc="Total Cost of Ingredients per can")
30
31        model.c0 = Constraint(expr=model.x1+model.x2 == 100.0, doc="Percentage Sum")
32        model.c1 = Constraint(expr=0.100*model.x1 + 0.200*model.x2 >= 8.0, doc="Protein Requirement")
33        model.c2 = Constraint(expr=0.080*model.x1 + 0.100*model.x2 >= 6.0, doc="Fat Requirement")
34        model.c3 = Constraint(expr=0.001*model.x1 + 0.005*model.x2 <= 2.0, doc="Fiber Requirement")
35        model.c4 = Constraint(expr=0.002*model.x1 + 0.005*model.x2 <= 0.4, doc="Salt Requirement")
36
37        instance = model.create()
38        opt = SolverFactory('glpk')
39        solutions = opt.solve(instance, keepFiles=True)
40        instance.load(solutions)
41        solutions.write(filename=currdir+"blend.out")
42        self.failUnlessFileEqualsBaseline(currdir+"blend.out",currdir+"blend.txt")
43
44
45if __name__ == "__main__":
46   unittest.main()
47
Note: See TracBrowser for help on using the repository browser.