Pavlo
Pavlo

Reputation: 47

Switch to pyomo 4 expressions by default

Why Pyomo uses by default expression types from expr_coopr3.py? I found the way how it could be changed but I have doubts if it's the right way.

In the file expr_common.py

try:
  from sys import getrefcount
  _getrefcount_available = False # changed!
except ImportError:
    logger = logging.getLogger('pyomo.core')
    logger.warning(
    "This python interpreter does not support sys.getrefcount()\n"
    "Pyomo cannot automatically guarantee that expressions do not become\n"
    "entangled (multiple expressions that share common subexpressions).\n")
     getrefcount = None
     _getrefcount_available = False

class Mode(object):
    coopr3_trees = (1,)
    pyomo4_trees = (2,)
if _getrefcount_available:
    mode = _default_mode = Mode.coopr3_trees
else:
    mode = _default_mode = Mode.pyomo4_trees

the variable _getrefcount_available could be assigned to False and after everything works with pyomo expressions 4. Is there any other way to do that?

I am using Pyomo 5.2

Upvotes: 0

Views: 82

Answers (1)

jsiirola
jsiirola

Reputation: 2634

You can switch expression tree systems with:

import pyomo.core.base.expr as EXPR
EXPR.set_expression_tree_format(EXPR.common.Mode.pyomo4_trees)

Pyomo4 expressions have been a (long-running) development activity to simplify the expression tree system, improve performance, and most importantly to provide support for pypy. It has not been made the default mostly because while I think it works correctly, it generates slightly different expression trees from what the coopr3 system generated. This impacts a large number of the Pyomo tests and I haven't had the time to resolve all the (2000+) test differences to make sure that the new results are correct.

Also, I should point out that [as of Dec 2017] there is a Pyomo branch with a revised version of the pyomo4 expression system (currently dubbed pyomo5). This system has an improved way to ensure expression correctness in the absence of getrefcount() (needed for pypy). Unfortunately, the development of pyomo5 expressions hass diverged from the coopr3/pyomo4 systems sufficiently such that they do not cleanly coexist. When that branch is merged (likely January 2018), both coopr3 and pyomo4 expression systems are expected to be removed from master.

Upvotes: 1

Related Questions