source: coopr.pyomo/trunk/coopr/pyomo/presolve/compute_canonical_repn.py @ 2170

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

A rework of the NL/LP file writers. This now uses the
canonical expression representation. I'm pretty sure that the LP writer
is OK, but much less sure about the NL writer. Also, it's clear that we don't
have adequate tests of the writers to ensure that all different types of
models are written correctly.

File size: 3.3 KB
Line 
1#  _________________________________________________________________________
2#
3#  Coopr: A COmmon Optimization Python Repository
4#  Copyright (c) 2008 Sandia Corporation.
5#  This software is distributed under the BSD License.
6#  Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
7#  the U.S. Government retains certain rights in this software.
8#  For more information, see the FAST README.txt file.
9#  _________________________________________________________________________
10
11#
12# Note: this leaves the trivial constraints in the model
13#
14
15import pyutilib.plugin.core
16from coopr.pyomo.base import Constraint, Objective
17from coopr.pyomo.base import IPyomoPresolver, IPyomoPresolveAction
18import coopr.pyomo.expr 
19
20
21class ComputeCanonicalRepn(pyutilib.plugin.core.SingletonPlugin):
22    """
23    This plugin computes the canonical representation for
24    all objectives and constraints
25    linear terms.
26    """
27
28    pyutilib.plugin.core.implements(IPyomoPresolveAction)
29
30    def __init__(self, **kwds):
31        kwds['name'] = "compute_canonical_repn"
32        pyutilib.plugin.core.Plugin.__init__(self, **kwds)
33
34    def rank(self):
35        return 10000
36
37    def presolve(self, model):
38        """
39        The main routine to perform the presolve
40        """
41        #
42        # Objectives
43        #
44        Obj = model.active_components(Objective)
45        for key in Obj.keys():
46            obj = Obj[key]
47            # number of objective indicies with non-trivial expressions
48            nt = 0
49            for ondx in obj:
50                if not obj._data[ondx].active:
51                    continue
52                if obj._data[ondx].expr is None:
53                    raise ValueError, "No expression has been defined for objective %s" % str(key)
54                try:
55                    obj._data[ondx].repn = coopr.pyomo.expr.generate_canonical_repn(obj._data[ondx].expr, model)
56                except Exception, e:
57                    print "Error generating a canonical representation for objective %s (index %s)" % (str(key), str(ondx))
58                    raise e
59                if not coopr.pyomo.expr.is_constant(obj._data[ondx].repn):
60                    nt += 1
61            if nt == 0:
62                obj.trivial = True
63        #
64        # Constraints
65        #
66        Con = model.active_components(Constraint)
67        for key in Con.keys():
68            con = Con[key]
69            # number of constraint indicies with non-trivial bodies
70            nt = 0 
71            for cndx in con:
72                if not con._data[cndx].active:
73                    continue
74                if con._data[cndx].body is None:
75                    raise ValueError, "No expression has been defined for the body of constraint %s" % str(key)
76                try:                 
77                    con._data[cndx].repn = coopr.pyomo.expr.generate_canonical_repn(con._data[cndx].body, model)
78                except Exception, e:
79                    print "Error generating a canonical representation for constraint %s (index %s)" % (str(key), str(cndx))
80                    raise e
81                if not coopr.pyomo.expr.is_constant(con._data[cndx].repn):
82                    nt += 1
83            if nt == 0:
84                con.trivial = True
85        #
86        # Return modified instance
87        #
88        return model
89
Note: See TracBrowser for help on using the repository browser.