Expressions
===========
In this section, we use the word "expression" in two ways: first in the
general sense of the word and second to describe a class of Pyomo objects
that have the name ``Expression`` as described in the subsection on
expression objects.
Rules to Generate Expressions
-----------------------------
Both objectives and constraints make use of rules to generate
expressions. These are Python functions that return the appropriate
expression. These are first-class functions that can access
global data as well as data passed in, including the model object.
Operations on model elements results in expressions, which seems natural
in expressions like the constraints we have seen so far. It is also
possible to build up expressions. The following example illustrates
this, along with a reference to global Python data in the form of a
Python variable called ``switch``:
.. literalinclude:: /src/scripting/spy4Expressions_Buildup_expression_switch.spy
:language: python
In this example, the constraint that is generated depends on the value
of the Python variable called ``switch``. If the value is 2 or greater,
then the constraint is ``summation(model.c, model.x) - model.d >= 0.5``;
otherwise, the ``model.d`` term is not present.
.. warning::
Because model elements result in expressions, not values, the
following does not work as expected in an abstract model!
.. literalinclude:: /src/scripting/spy4Expressions_Abstract_wrong_usage.spy
:language: python
The trouble is that ``model.d >= 2`` results in an expression, not
its evaluated value. Instead use ``if value(model.d) >= 2``
.. note::
Pyomo supports non-linear expressions and can call non-linear solvers such as Ipopt.
.. _piecewise:
.. _abstract2piece.py:
Piecewise Linear Expressions
----------------------------
Pyomo has facilities to add piecewise constraints of the form y=f(x) for
a variety of forms of the function f.
The piecewise types other than SOS2, BIGM_SOS1, BIGM_BIN are implement
as described in the paper [VAN10]_.
There are two basic forms for the declaration of the constraint:
.. literalinclude:: /src/scripting/spy4Expressions_Declare_piecewise_constraints.spy
:language: python
where ``pwconst`` can be replaced by a name appropriate for the
application. The choice depends on whether the x and y variables are
indexed. If so, they must have the same index sets and these sets are
give as the first arguments.
Keywords:
*********
* **pw_pts={ },[ ],( )**
A dictionary of lists (where keys are the index set) or a single list
(for the non-indexed case or when an identical set of breakpoints is
used across all indices) defining the set of domain breakpoints for
the piecewise linear function.
.. note::
pw_pts is always required. These give the breakpoints for the
piecewise function and are expected to fully span the bounds for
the independent variable(s).
* **pw_repn=