Example #1
0
def generate_ampl_repn(exp, idMap=None):
    if idMap is None:
        idMap = {}
    degree = exp.polynomial_degree()
    if (degree is None) or (degree > 1):
        repn = _generate_ampl_repn(exp)
        repn.compress()
    elif degree == 0:
        repn = AmplRepn()
        repn._constant = value(exp)
        # compress
        repn._linear_vars = tuple()
        repn._linear_terms_coef = tuple()
        repn._nonlinear_vars = tuple()
    else: # degree == 1
        repn = AmplRepn()
        # compress
        repn._linear_vars = tuple()
        repn._linear_terms_coef = tuple()
        repn._nonlinear_vars = tuple()
        coef, varmap = collect_linear_canonical_repn(exp, idMap=idMap)
        if None in coef:
            val = coef.pop(None)
            if val:
                repn._constant = val
        # the six module is inefficient in terms of wrapping iterkeys and itervalues,
        # in the context of Python 2.7. use the native dictionary methods where possible.
        if using_py3 is False:
            repn._linear_terms_coef = tuple(val for val in coef.itervalues() if val)
            repn._linear_vars = tuple((varmap[var_hash] for var_hash,val in coef.iteritems() if val))
        else:
            repn._linear_terms_coef = tuple(val for val in coef.values() if val)
            repn._linear_vars = tuple((varmap[var_hash] for var_hash,val in coef.items() if val))
    return repn
Example #2
0
def generate_ampl_repn(exp, idMap=None):
    # We need to do this not at the global scope in case someone changed
    # the mode after importing the environment.
    _using_pyomo4_trees = expr_common.mode == expr_common.Mode.pyomo4_trees

    if idMap is None:
        idMap = {}
    if exp is None:
        return AmplRepn()
    degree = exp.polynomial_degree()
    if (degree is None) or (degree > 1):
        repn = _generate_ampl_repn(exp)
        repn.compress()
    elif degree == 0:
        repn = AmplRepn()
        repn._constant = value(exp)
        # compress
        repn._linear_vars = tuple()
        repn._linear_terms_coef = tuple()
        repn._nonlinear_vars = tuple()
    else:  # degree == 1
        repn = AmplRepn()
        if _using_pyomo4_trees:
            canonical_repn = generate_canonical_repn(exp, idMap=idMap)
            # compress
            repn._nonlinear_vars = tuple()
            repn._constant = value(canonical_repn.constant)
            repn._linear_vars = tuple(canonical_repn.variables)
            repn._linear_terms_coef = tuple(
                value(_v) for _v in canonical_repn.linear)
        else:
            # compress
            repn._linear_vars = tuple()
            repn._linear_terms_coef = tuple()
            repn._nonlinear_vars = tuple()
            coef, varmap = collect_linear_canonical_repn(exp, idMap=idMap)
            if None in coef:
                val = coef.pop(None)
                if val:
                    repn._constant = val
            # the six module is inefficient in terms of wrapping
            # iterkeys and itervalues, in the context of Python
            # 2.7. use the native dictionary methods where
            # possible.
            if using_py3 is False:
                repn._linear_terms_coef = tuple(val
                                                for val in coef.itervalues()
                                                if val)
                repn._linear_vars = tuple(
                    (varmap[var_hash] for var_hash, val in coef.iteritems()
                     if val))
            else:
                repn._linear_terms_coef = tuple(val for val in coef.values()
                                                if val)
                repn._linear_vars = tuple((varmap[var_hash]
                                           for var_hash, val in coef.items()
                                           if val))
    return repn
Example #3
0
def generate_ampl_repn(exp, idMap=None):
    # We need to do this not at the global scope in case someone changed
    # the mode after importing the environment.
    _using_pyomo4_trees = expr_common.mode == expr_common.Mode.pyomo4_trees
    _using_pyomo5_trees = expr_common.mode == expr_common.Mode.pyomo5_trees

    if idMap is None:
        idMap = {}
    if _using_pyomo5_trees:
        from pyomo.repn.standard_repn import generate_standard_repn
        return generate_standard_repn(exp, quadratic=False)

    if exp is None:
        return AmplRepn()
    degree = exp.polynomial_degree()
    if (degree is None) or (degree > 1):
        repn = _generate_ampl_repn(exp)
        repn.compress()
    elif degree == 0:
        repn = AmplRepn()
        repn._constant = value(exp)
        # compress
        repn._linear_vars = tuple()
        repn._linear_terms_coef = tuple()
        repn._nonlinear_vars = tuple()
    else: # degree == 1
        repn = AmplRepn()
        if _using_pyomo4_trees:
            canonical_repn = generate_canonical_repn(exp, idMap=idMap)
            # compress
            repn._nonlinear_vars = tuple()
            repn._constant = value(canonical_repn.constant)
            repn._linear_vars = tuple(canonical_repn.variables)
            repn._linear_terms_coef = tuple(value(_v) for _v in canonical_repn.linear)
        else:
            # compress
            repn._linear_vars = tuple()
            repn._linear_terms_coef = tuple()
            repn._nonlinear_vars = tuple()
            coef, varmap = collect_linear_canonical_repn(exp, idMap=idMap)
            if None in coef:
                val = coef.pop(None)
                if val:
                    repn._constant = val
            # the six module is inefficient in terms of wrapping
            # iterkeys and itervalues, in the context of Python
            # 2.7. use the native dictionary methods where
            # possible.
            if using_py3 is False:
                repn._linear_terms_coef = tuple(val for val in coef.itervalues() if val)
                repn._linear_vars = tuple((varmap[var_hash]
                                           for var_hash,val in coef.iteritems() if val))
            else:
                repn._linear_terms_coef = tuple(val for val in coef.values() if val)
                repn._linear_vars = tuple((varmap[var_hash]
                                           for var_hash,val in coef.items() if val))
    return repn