Skip to content

Numpy logic error in 5.5 #611

@qtothec

Description

@qtothec

Anna ran across a bug this morning.

In Pyomo 5.5, the following code will cause a model to be constructed with numpy.ndarray objects that results in an empty model (?) being sent to the solver. Even though IPOPT will report Optimal solution here, the initial model values will remain unchanged (c_1 = c_2 = 1 rather than the optimal value of c_1 = c_2 = 0.5). This is not due to a non-convexity.

Luckily, in the new expression system, an exception is raised because numpy.ndarray does not have an expected is_expression_type attribute during expression generation.

If we plan to release 5.5.1, we should address this issue.

Model

import numpy as np
from pyomo.environ import *


def run_model():
    samples = 10
    c1 = .5
    c2 = .5

    model = ConcreteModel()
    model.i = RangeSet(samples)

    def init_x(model, i):
        return np.random.rand(1)

    def init_y(model, i):
        return c1 * (model.x[i]**2) + c2 * model.x[i]

    model.x = Param(model.i, initialize=init_x)
    model.y = Param(model.i, initialize=init_y)
    model.c_1 = Var(within=Reals, initialize=1)
    model.c_2 = Var(within=Reals, initialize=1)

    model.error = Objective(
        # Sum squared error of quadratic fit
        expr=sum(((model.c_1 * (model.x[i]**2) + model.c_2 * model.x[i])
                  - model.y[i]
                  )**2
                 for i in model.i))

    SolverFactory('ipopt').solve(model, tee=True)
    model.display()


if __name__ == '__main__':
    run_model()

Pyomo 5.5 output

EXIT: Optimal Solution Found.
Model unknown

  Variables:
    c_1 : Size=1, Index=None
        Key  : Lower : Value : Upper : Fixed : Stale : Domain
        None :  None :   1.0 :  None : False : False :  Reals
    c_2 : Size=1, Index=None
        Key  : Lower : Value : Upper : Fixed : Stale : Domain
        None :  None :   1.0 :  None : False : False :  Reals

  Objectives:
    error : Size=1, Index=None, Active=True
        Key  : Active : Value
        None :   True : [3.47962527]

  Constraints:
    None

Pyomo master (commit ab7868d)

ERROR: Rule failed when generating expression for objective error:
    AttributeError: 'numpy.ndarray' object has no attribute
    'is_expression_type'
ERROR: Constructing component 'error' from data=None failed: AttributeError:
    'numpy.ndarray' object has no attribute 'is_expression_type'
Traceback (most recent call last):
  File "..\numpy_error_lists.py", line 25, in <module>
    model.error=Objective(rule=error_rule,sense=minimize)
  File "c:\cygwin64\home\qchen\git\pyomo\pyomo\core\base\block.py", line 538, in __setattr__
    self.add_component(name, val)
  File "c:\cygwin64\home\qchen\git\pyomo\pyomo\core\base\block.py", line 978, in add_component
    val.construct(data)
  File "c:\cygwin64\home\qchen\git\pyomo\pyomo\core\base\objective.py", line 340, in construct
    tmp = _init_rule(_self_parent)
  File "..\numpy_error_lists.py", line 24, in error_rule
    return sum(((model.c_1*(model.x[i]**2)+model.c_2*model.x[i])-model.y[i])**2 for i in model.i)
  File "..\numpy_error_lists.py", line 24, in <genexpr>
    return sum(((model.c_1*(model.x[i]**2)+model.c_2*model.x[i])-model.y[i])**2 for i in model.i)
  File "c:\cygwin64\home\qchen\git\pyomo\pyomo\core\expr\numvalue.py", line 770, in __mul__
    return _generate_mul_expression(_mul,self,other)
  File "c:\cygwin64\home\qchen\git\pyomo\pyomo\core\expr\expr_pyomo5.py", line 3060, in _generate_mul_expression
    if not (_other.__class__ in native_types or _other.is_expression_type()):
AttributeError: 'numpy.ndarray' object has no attribute 'is_expression_type'

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions