简体   繁体   中英

is it possible to write a nonlinear piecewise function inside the objective function in Pyomo?

I want to use Pyomo to solve a nonlinear optimization using Ipopt. In this regard I devised a function as an objective function in my code which itself uses another function inside it. the inner function is nonlinear and piecewise and is defined as follows.

def U3(x,omega,U_alpha3):
       if x<=omega/U_alpha3:
              return omega*x-U_alpha3/2*x**2
       else:
              return 0.5*omega**2/U_alpha3

But this function leads to this error:

TypeError: '<=' not supported between instances of 'IndexedComponent_slice' and 'numpy.float64'

considering that my decision variable goes inside x (which is 'IndexedComponent_slice' type) and omega and U_alpha3 are fixed parameters(which are 'numpy.float64' type)

Yes. The Pyomo NL-file interface to Ipopt supports this via the Expr_If expression class, given the standard caveats about the function you are defining being twice-continuously differentiable, etc. (which we do not verify for you).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM