Skip to content

Problems computing degree of expressions with numpy data #87

@whart222

Description

@whart222

The following script returns an error that indicates that the expression system things that the Objective constraint is a constant, when it clearly isn't. Digging around, it looks like there is some confusion generated by the NumPY data types.

import numpy as np
from pyomo.environ import *
opt = SolverFactory("glpk")
from pyomo.opt import SolverFactory

model = AbstractModel()

nsample = 500
nvariables = 20
X0 = np.ones([nsample,1])
model.X = np.random.uniform(0,10,([nsample,nvariables]))
X = np.concatenate([X0,model.X],axis = 1)

model.I = RangeSet(1,nsample) 
model.J = RangeSet(1,nvariables) 


error = np.random.normal(0,1,(nsample,1))
beta = np.random.randint(-5,5,size = ([nvariables+1,1]))
model.Y = np.dot(X,beta) + error

model.beta = Var(model.J)
model.beta0 = Var()

def obj_fun(model):
    
    return sum(abs(model.Y[i-1]-(model.beta0 + sum(model.X[i-1,j-1]*model.beta[j] for j in model.J) )) for i in model.I)

model.OBJ = Objective(rule = obj_fun, sense = minimize)



opt = SolverFactory('glpk')
instance = model.create_instance()
results = opt.solve(instance)
results.write()

This code generates the following error:

TypeError: Implicit conversion of Pyomo NumericValue type `<class 'pyomo.core.base.expr_coopr3._SumExpression'>' to a float is
disabled. This error is often the result of using Pyomo components as
arguments to one of the Python built-in math module functions when
defining expressions. Avoid this error by using Pyomo-provided math
functions.

And it generates the warning

WARNING: Constant objective detected, replacing with a placeholder to prevent solver failure.

In the cpxlp writer, the call

degree = canonical_degree(canonical_repn)

returns a value of zero for the degree!

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions