I have defined an interpolation function in Python as:
rbfX = sp.interpolate.Rbf(X2, Y2, Wx2, function='multiquadric')
I want to use it inside of a Pyomo code, where m.lammda[i,1]
and m.phi[i,1]
are the positions of an airplane moving inside of a wind field represented by the previous interpolation. Note that m
is a concrete model and
type(m.lammda)
Out[7]: pyomo.core.base.var.IndexedVar
My current attempt at a solution is
def Wind_lammda_definition1(model, i):
abcdX=float(m.lammda[i,1])
abcdY=float(m.phi[i,1])
return m.Wind_lammda[i,1] ==rbfX(abcdX, abcdY)
m.Wind_lammda_const1 = Constraint(m.N, rule = Wind_lammda_definition1)
Since I want to know the windspeed at any point in the trajectory.
When I run the program, I get this error:
ERROR: Rule failed when generating expression for Constraint
Wind_lammda_const1 with index 0: TypeError: Implicit conversion of Pyomo
NumericValue type `lammda[0,1]' 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.
ERROR: Constructing component 'Wind_lammda_const1' from data=None failed:
TypeError: Implicit conversion of Pyomo NumericValue type `lammda[0,1]' 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.
Traceback (most recent call last):
File "D:\whatever\node1_test.py", line 530, in <module>
m.Wind_lammda_const1 = Constraint(m.N, rule = Wind_lammda_definition1)
TypeError: Implicit conversion of Pyomo NumericValue type `lammda[0,1]' 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.
The error still appears if I call m.lammda[i,1]
and m.phi[i,1]
as arguments instead of abcdX
and abcY
.
I need to know if I can somehow adapt this code to work. I have read the documentation. In pages 339 to 340 it talks about interpolation, but I am not familiar enough with Pyomo nor Python to understand it, so please, I would be extremely thankful if someone aided me with this.
CodePudding user response:
A lot of info from this and you another threads. I will try to handle all as I can.
The problem is not converting pyomo.IndexedVar
to float
, since when you create the variable in the pyomo
model you can do it as a NonNegativeReal
. This problem is rather the implicit convertion of not a number (In your code, m.lammda[i,1]
is a class, not exactly a number. Even though it has a float value) with the float
statement.
As you stated in your comment in another thread, when you try to use the pyomo.environ.IndexedVar
into the fitting (interpolation) function Rbf, you raise the TypeError
of error trying to convert the an IndexedVar
to float
(internally in the Rbf function). To evaluate the value you can use model.m.lammda[i,1]()
since calling the class returns you the value, but this still will not help, because another error will certainly raise
I recommend you to visit this post to get more information about the potential difficulty of using an external function as a pyomo
Constraint or Objective Function
Pyomo
got a Piecewise Function Library where you can model a linear (pyo.environ
layer also got it in the pyo.environ.Piecewise
) or multilinear interpolation, but you will need to work with the kernel layer, it use scipy to generate a Dlunay for the interpolation, you can check using help(pyomo.kernel.piecewise_nd)
. This surely may work to you, but it requires a little be of work.