Pyomo "data = None" output error - python

Thanks so much for looking into my problem! I am new to pyomo and now trying to use it to solve a concrete model.
Here is part of my codes:
——— (edited)
def objective_rule(model):
ans = sum(model.DAEB[t] * model.DAEP[t] for t in model.t)
ans -= sum(model.DARUP[t] * model.RU[t] + model.DARDP[t] * model.RD[t] for t in model.t)
ans += sum(0.5 * (sum(model.penalty[t, w]) + sum(-model.RTEP[t] * (model.DAEB[t] - ((model.RTRD[t, w] * model.RU[t]) +
sum(model.veh_pwer_dem[t, v, w] for v in model.v))))) for t in model.t for w in model.w)
ans += 0.95 * (model.epslon + 1 / (0.5) * sum(0.01 * model.miu[w] for w in model.w))
return ans
t is defined as a set [0:23], time hour of a day;
w is defined as [0,1], it's different scenarios;
v is [0:29], 30 different samples.
Before defining the objective function, I have also defined a bunch of constraints relating with the above model variables.
Everything runs smoothly except till the last step, pyomo gives:
——— (edited)
Traceback (most recent call last):
line 61, in <module>
myresult = result.solve(project, pricefile, reg_dispatch, SOC=0, SOC_margin=0.05)
line 56, in solve
opti_model, result = self.Fr_optimal_bidding_optimization(self.vehicles, pricefile, reg_dispatch, SOC)
line 347, in Fr_optimal_bidding_optimization
model.objective = Objective(rule=objective_rule, sense=minimize, doc='minimize the total cost')
line 483, in __setattr__
self.add_component(name, val)
line 849, in add_component
val.construct(data)
line 307, in construct
tmp = _init_rule(_self_parent)
line 337, in objective_rule
sum(model.veh_pwer_dem[t, v, w] for v in model.v))))) for t in model.t for w in model.w)
ERROR: Rule failed when generating expression for objective objective:
line 337, in <genexpr>
TypeError: '_GeneralVarData' object is not iterable
sum(model.veh_pwer_dem[t, v, w] for v in model.v))))) for t in model.t for w in model.w)
ERROR: Constructing component 'objective' from data=None failed:
TypeError: '_GeneralVarData' object is not iterable
TypeError: '_GeneralVarData' object is not iterable
First of all, I am really confused about the _GeneralVarData object and don't know what it is referring to and second, I can't see a problem in my objective function, so if anyone could provide any kind of advice will be greatly appreciated!!
(I am also new to "stackoverflow," apologize at front if my question is not clearly stated!)
Teng

The exception is triggered by the following code in your second to last line:
sum(model.veh_pwer_dem[t, v, w])
This attempts to treat the (scalar) model.veh_pwer_dem[t, v, w] as an iterable and sum over its keys (indices). There also appear to be other problems with the logical structure of your expression. I would highly recommend breaking the expression apart into smaller chunks that will help you to keep things clear. For example:
def objective_rule(model):
ans = sum(model.DAEB[t] * model.DAEP[t] for t in model.t)
ans -= sum(model.DARUP[t] * model.RU[t] + model.DARDP[t] * model.RD[t] for t in model.t)
ans += sum(0.5 * ( sum(model.penalty[t, w] for t in model.t for w in model.w) + # ...
#...
return ans
As to what a _GeneralVarData object is. _GeneralVarData objects are an internal Pyomo structure used to represent an actual optimization variable that is part of an indexed Var component (you may occasionally alse see _SimpleVar, which is the internal implementation used for non-indexed Var components). As a rule in Pyomo (and Python in general), methods, attributes, and classes beginning with an underscore (_) are "private" to the implementation.

Related

Importing a function into a file yields results of a function run with inputs other than those the func is called with

when i import a python file(let's call it function file 1 ) that contains a function that i need in another python file(let's call it file 2) and call the function from file 2 using different function inputs, i get the results that i expect from file 1( when calling the function according to the inputs of file 1 ) and then i get the results that i want( from running the function in file 2 with the new function inputs). What could be the reason for that?
file 2 should call the function jacobi from the file Jacobi_Iteration and give the two arrays A,f and x(guess array) as inputs and the function"jacobi" should return x and stor it in ans_s. What calling the function, however, does is to call jacobi and give it old inputs that were used for calling jacobi in the file Jacobi_Iterations and then call jacobi with inputs given in file 2. The end result is getting the results expected from the old inputs and then those of the new inputs after each other in the results pane. Of course, i tried not calling jacobi in Jacobi_Iterations and only calling it in file2, but in vain.
Remarks:
Both functions are present in the same directory
This is file 2:
import numpy
import numpy.linalg as nl
from Jacobi_Iteration import jacobi
A = numpy.array([[72.0,0.0,0.0,0.0,0.0],
[0.0,38.0,0.0,0.0,0.0],
[72.0,38.0,-160.0,0.0,0.0],
[0.0,0.0,160.0,-185.0,0.0],
[0.0,0.0,0.0,180.0,-215.0]])
f = numpy.array([180.0,810.0,-630.0,-2750.0,-3820.0])
# sol = nl.solve(A,f)
#print("sol=",sol)
# solving using the jacobi iteration
x = [15.0,15.0,15.0,15.0,15.0]
ans_2 = jacobi(A,f,x)
print(ans_2)
This is the function file"Jacobi_Iterations"
import numpy
import numpy.linalg as nl
def jacobi(A,f,x,maxIter=100,tol=0.0001):
# x is the guess vector
xnew = numpy.copy(x)
n = f.size
# Checking for the dimensions of the matrices
if (A.shape[0] != n) or (A.shape[1] != n):
print("Incorrect dimentions !")
return f
# Generating new guesses and seeing whether A.x will converge during the maxIter
for iter in range(maxIter):
# checking if the guess vector satisfies the tol:
res = f - numpy.dot(A,x)
if ( nl.norm(res,2) < tol ):
print("converged after",iter,"iterations!")
return x
# Getting a better guess vector x:
for i in range(n):
sum = 0.0
for j in range(n):
if (i != j):
sum+= A[i,j] * x[j]
xnew[i] = (f[i] - sum)/A[i,i]
x = numpy.copy(xnew)
return x
Python by default will look for packages in some pre-determined places. Working dir might be a candidate
It might not be the case for you but try to find out if there are conflicts with these.
I had this prob in the past
This is a nice link:
https://leemendelowitz.github.io/blog/how-does-python-find-packages.html

What is the reason for TypeError: 'Zero' object has no attribute '__getitem__'?

I have written the following code in python 2.7 in order to calculate an integration numerically and then use the result of this integration for further steps of the project.
import numpy as np
from scipy import linspace,logspace
from cosmicpy import *
Omega_Matter, Omega_DarkEnergy, A, b, rho_critical, m = 0.306, 0.694, 0.3222, 0.707, 2.77536627e+11, 1000000
def D(z):
a = 1/(1+z)
x = (Omega_DarkEnergy/Omega_Matter)**(1/3)*a
return (5/2)*(Omega_Matter/Omega_DarkEnergy)**(1/3)*x**(-3/2)*(1+x**3)**(1/2)* \
(x**2/(3*x**3 + 3) - np.log(x + 1)/9 + np.log(x**2 - x + 1)/18 \
+ np.sqrt(3)*np.arctan(2*np.sqrt(3)*x/3 \
- np.sqrt(3)/3)/9 + np.sqrt(3)*np.pi/54)
def delta(z):
return D(z)/D(0)
def W(k, M):
rho_m = rho_critical*Omega_Matter
R = (3*M/(4*np.pi*rho_m))**(1/3)
x = k*R
return (3/x)*(sin(x)-x*cos(x))
my_cosmology = cosmology(Omega_m=0.306, Omega_de=0.694, h=0.679, Omega_b=0.0483, n=0.968, tau=0.067, sigma8=0.815, w=-1)
k_array = np.logspace(-16,4,m)
P = my_cosmology.pk_lin(k_array)
def sigma_squared(z, M):
dk = (np.max(k_array)-np.min(k_array))/(m-1)
summation = []
for k in k_array:
Integral = 0
Integrand = k**2*P[k]*(W(k, M))**2
Integral += dk * np.sum(Integrand[k])
summation.append(Integral)
return ((delta(z))**2/(2*(np.pi)**2))*summation[-1]
print(summation)
sigma_squared(0.01, 1e+9)
As I write more of the code, I check my steps one by one by getting a print and see if the output is what I expect. However, I am unable to produce the final product of the last function which is supposed to be a value given the values for the variables z and M. In particular I am sure that something is wrong with the integration inside that function because I am not getting any thing for print(summation) which is supposed to be a big 1d array whose last element print(summation[-1])should give me the area under the curve (upto a pre-factor defined in the final return of the function). Here is the error message and I couldn't find any on-line source for the particular error message I am getting. Your help is greatly appreciated.
mycode.py:95: VisibleDeprecationWarning: using a non-integer number
instead of an integer will result in an error in the future
Integrand = k**2*P[k]*(W(k, M))**2 Traceback (most recent call last):
File "mycode.py", line 102, in
sigma_squared(0.01, 1e+9) File "mycode.py", line 96, in sigma_squared
Integral += dk * np.sum(Integrand[k]) TypeError: 'Zero' object has no attribute 'getitem'
Edited Code (which is too slow to know if it is working correctly):
k_array = np.logspace(-16,4,m)
my_cosmology = cosmology(Omega_m=0.306, Omega_de=0.694, h=0.679, Omega_b=0.0483, n=0.968, tau=0.067, sigma8=0.815, w=-1)
P = my_cosmology.pk_lin(k_array)
M_array = np.logspace(8,16,n)
def W(k, M):
rho_m = rho_critical*Omega_Matter
R = (3*M/(4*np.pi*rho_m))**(1/3)
y = k*R
return (3/y**3)*(sin(y)-y*cos(y))
def sigma_squared(z, M):
dk = (np.max(k_array)-np.min(k_array))/(m-1)
summation = []
for k in k_array:
for M in M_array:
Integral = 0
Integrand = k**2*P[k]*(W(k, M))**2
Integral += dk * np.sum(Integrand)
summation.append(Integral)
return ((delta(z))**2/(2*(np.pi)**2))*summation[-1]
print(sigma_squared(0, 1e+9))

Error when instantiating bessel functions with Sympy lambdify function

I am currently developping a python program to tranform a symbolic expression computed by sympy into a numpy array containing all the numerical values. I instantiate the symbolic expression with the sympy.lambdify function.
Some of the symbolic expressions contain Bessel functions, and I pass scipy.special.jv/jy etc. as parameters in the lambdify function. Here is the single code (appart from the program) :
m = Symbol('m')
expr= -1.06048319593874*(-(3.14159265358979*m*besselj(27.9937791601866*m, 4.46681007624482*sqrt(-I)) - 0.501286290793831*sqrt(-I)*besselj(27.9937791601866*m + 1, 4.46681007624482*sqrt(-I)))*bessely(27.9937791601866*m, 6.81776274795262*sqrt(-I))/(3.14159265358979*m*bessely(27.9937791601866*m, 4.46681007624482*sqrt(-I)) - 0.501286290793831*sqrt(-I)*bessely(27.9937791601866*m + 1, 4.46681007624482*sqrt(-I))) + besselj(27.9937791601866*m, 6.81776274795262*sqrt(-I)))*sin(0.942966379693359*m)*cos(1.5707963267949*m)/m
nm = 5
vm = arange(nm) +1
bessel = {'besselj':jv,'besselk':kv,'besseli':iv,'bessely':yv}
libraries = [bessel, "numpy"]
result = lambdify(m, expr, modules=libraries)(vm)
In[1] : result
Out[1]:
array([ -7.51212638e-030 -3.22606326e-030j,
4.81143544e-046 +1.04405860e-046j,
1.97977798e-097 +3.02047228e-098j,
3.84986092e-124 +4.73598141e-125j,
1.12934434e-181 +1.21145535e-182j])
The result is as I expected : a 5 rows 1-d array with each integer value of the symbol m.
The problem is when I try to implement it in the program. Here is the implementation :
expr = list_symbolic_expr[index]
vcm = arange(Dim_col[0]) +1 #Dim_col = list of integer range values to instantiate
m = Symbol(str(Var_col[0])) #Var_col = list of symbolic integer parameters
print expr, m
smat = lambdify(m, expr, modules=libraries)(vcm)
Here is the error when using scipy.special bessel functions :
In [2]: run Get_System_Num
Out [2]:
-1.06048319593874*(-(3.14159265358979*m*besselj(27.9937791601866*m, 4.46681007624482*sqrt(-I)) - 0.501286290793831*sqrt(-I)*besselj(27.9937791601866*m + 1, 4.46681007624482*sqrt(-I)))*bessely(27.9937791601866*m, 6.81776274795262*sqrt(-I))/(3.14159265358979*m*bessely(27.9937791601866*m, 4.46681007624482*sqrt(-I)) - 0.501286290793831*sqrt(-I)*bessely(27.9937791601866*m + 1, 4.46681007624482*sqrt(-I))) + besselj(27.9937791601866*m, 6.81776274795262*sqrt(-I)))*sin(0.942966379693359*m)*cos(1.5707963267949*m)/m m
File "Numeric\Get_System_Num.py", line 183, in get_Mat
smat = lambdify(m, expr, modules=libraries)(vcm)
File "<string>", line 1, in <lambda>
TypeError: ufunc 'jv' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''
Here is the error when using sympy.special bessel functions :
File "Numeric\Get_System_Num.py", line 183, in get_Mat
smat = lambdify(m, expr, modules=libraries)(vcm)
File "<string>", line 1, in <lambda>
File "C:\Users\Emile\Anaconda2\lib\site-packages\sympy\core\function.py", line 375, in __new__
result = super(Function, cls).__new__(cls, *args, **options)
File "C:\Users\Emile\Anaconda2\lib\site-packages\sympy\core\function.py", line 199, in __new__
evaluated = cls.eval(*args)
File "C:\Users\Emile\Anaconda2\lib\site-packages\sympy\functions\special\bessel.py", line 161, in eval
if nu.is_integer:
AttributeError: 'numpy.ndarray' object has no attribute 'is_integer'
It seems that lambdify cannot interprete properly the input value in the bessel function in the program code (wether it is with scipy or sympy bessel functions), as the input value seems to be an array object, while it is not a problem in the single code. Yet I do not see the difference between them.
Thank you for reading this, and I hope someone may help me on this topic. Please tell me if more information are needed to illustrate this problem.
--Edit 1--
I have kept on searching the problem, and even when I try to lambdify this simple expression : 17469169.5935065*sin(0.942966379693359*m)*cos(1.5707963267949*m)/m, with "numpy" as module argument, it gives : 'float' object has no attribute 'sin'
I have also tried the packaged module 'numexpr' without success, as it stops on this other expression : 0.058**(46.6321243523316*k), saying :
File "C:\Users\Emile\Anaconda2\lib\site-packages\numexpr\necompiler.py", line 756, in evaluate
signature = [(name, getType(arg)) for (name, arg) in zip(names, arguments)]
File "C:\Users\Emile\Anaconda2\lib\site-packages\numexpr\necompiler.py", line 654, in getType
raise ValueError("unknown type %s" % a.dtype.name)
ValueError: unknown type object
So I really don't know the nature of this problem, and I can't even track it as the exception raised comes from inside sympy and I don't know how to put flags in sympy functions.
--Edit 2--
Here is the body of the Get_System_Num class : first the imports, then I also import the data from the symbolic project, meaning : each symbolic coefficient that I want to instantiate (listed in Mat_Num) ; the symbols to instantiate (listed in "list_Var') for each coefficient and depending on the rows and columns ; the size and the position of the instantiated matrix in the real big matrix (listed in list_Dim). As those lists are symbolic at the beginning I need to substitute symbols with real values that I put in Pr_Num <=> numerical project, and I do it in the sub_system_num method.
from numpy import diag, zeros, concatenate, arange, meshgrid, array
from scipy.special import jv,kv,iv,yv
from sympy import srepr, Matrix, lambdify, Symbol
from Numeric.Sub_System_Num import Sub_System_Num
class Get_System_Num :
#Calling constructor
def __init__(self, Pr_Num) :
#Assigning inputs values to local values
self.Pr_Num = Pr_Num
self.Pr_Symb = Pr_Num.Pr_Symb
#Load data from Pr_Symb
self.Mat_Num_Index = self.Pr_Symb.Mat_Num_Index
#Subs symbols with numeric values
print "Substitute symbols with real values..."
self.Sub_System_Num = Sub_System_Num(self)
#Gathering the results
self.Mat_Num = self.Sub_System_Num.Mat_Num
self.Mat_Size = self.Sub_System_Num.Mat_Size
self.list_Index = self.Sub_System_Num.list_Index
self.list_Var_row = self.Sub_System_Num.list_Var.col(0)
self.list_Dim_row = self.Sub_System_Num.list_Dim.col(0)
self.list_Var_col = self.Sub_System_Num.list_Var.col(1)
self.list_Dim_col = self.Sub_System_Num.list_Dim.col(1)
self.count = 0
print "Compute numerical matrix..."
self.Mat = self.get_Mat()
Here is the method to fill the numerical Matrix. I only put the case where there is only one variable to instantiate for both row and column. It's one case among 9 (3²) possible, as each row and columns have either 0,1 or 2 variables to instantiate. About the if srepr(coeff).__contains__(srepr(Var_row[0])) : this checks if the variable is really contained in the expr, and if not I duplicate manually the coeff. As expressions are previousvly computed symbolically, sometimes sympy may simplify them so that the variable is not here anymore.
def get_Mat(self) :
bessel = {'besselj':jv,'besselk':kv,'besseli':iv,'bessely':yv}
libraries = [bessel, 'numpy']
Mat = zeros((self.Mat_Size[0], self.Mat_Size[0]), dtype=complex)
for index in range(0, self.Mat_Num_Index.__len__()) :
Nb_row = self.list_Index[self.Mat_Num_Index[index][0], 0]
Nb_col = self.list_Index[self.Mat_Num_Index[index][1], 1]
Pos_row = self.list_Index[self.Mat_Num_Index[index][0], 2]
Pos_col = self.list_Index[self.Mat_Num_Index[index][1], 3]
Var_row = self.list_Var_row[self.Mat_Num_Index[index][0]]
Var_col = self.list_Var_col[self.Mat_Num_Index[index][1]]
Dim_row = self.list_Dim_row[self.Mat_Num_Index[index][0]]
Dim_col = self.list_Dim_col[self.Mat_Num_Index[index][1]]
coeff = self.Mat_Num[index]
#M(K or Z, M or Z)
if Var_row.__len__() == 1 :
if Var_col.__len__() == 1 :
if Var_row[0] == Var_col[0] : #M(K,M=K) or M(Z,Z)
vrk = arange(Dim_row[0]) +1
k = Symbol(str(Var_row[0]))
smat = lambdify(k, coeff, modules=libraries)(vrk)
if srepr(coeff).__contains__(srepr(Var_row[0])) :
smat = diag(smat)
print 'coeff n°', index, ' Case 1/1 M(K,M=K) or M(Z,Z) : i dependent '
print coeff, Var_col, Var_row
else :
smat = diag([smat]*Dim_row[0])
print 'coeff n°', index, ' Case 1/1 M(K,M=K) or M(Z,Z) : i non dependent'
print coeff, Var_col, Var_row
else :
if srepr(coeff).__contains__(srepr(Var_col[0]) and srepr(Var_row[0])) : #M(K,Z) or M(Z,M) or M(K, M)
print 'coeff n°', index, ' Case 1/1 M(K,Z) or M(Z,M) : i dependent '
print coeff, Var_col, Var_row, index
vrk = arange(Dim_row[0]) +1
vcm = arange(Dim_col[0]) +1
mcm, mrk = meshgrid(vcm, vrk)
k = Symbol(str(Var_row[0]))
i = Symbol(str(Var_col[0]))
smat = lambdify((k, i), coeff, modules=libraries)(mrk, mcm)
elif not(srepr(coeff).__contains__(srepr(Var_row[0]))) : #M(Z,M)
print 'coeff n°', index, ' Case 1/1 M(Z,M) : i non dependent '
print coeff, Var_col, Var_row, index
vcm = arange(Dim_col[0]) +1
m = Symbol(str(Var_col[0]))
smat = lambdify(m, coeff, modules=libraries)(vcm)
smat = [smat]*Dim_row[0]
smat = concatenate(list(smat), axis=0)
elif not(srepr(coeff).__contains__(srepr(Var_col[0]))) : #M(K,Z)
print 'coeff n°', index, ' Case 1/1 M(K,Z) : i non dependent'
print coeff, Var_col, Var_row, index
vrk = arange(Dim_row[0]) +1
k = Symbol(str(Var_row[0]))
smat = lambdify(k, coeff, modules=libraries)(vrk)
smat = [smat]*Dim_col[0]
smat = concatenate(list(smat), axis=1)
self.count = self.count +1
Mat[Pos_row:Pos_row+Nb_row, Pos_col:Pos_col+Nb_col] = smat
return Mat
In the end I fill the matrix with the lambdified coeff, which is in this case a smaller matrix of size (Nb_row, Nb_col).
I will keep searching on my own, do not hesitate to ask for more details !
--Edit 3 --
I have found that if I do this :
m = Symbol(str(Var_col[0]))
coeff = lambdify(m, coeff, modules=libraries)
for nm in range(0, Dim_col[0]) :
smat.append(coeff(nm+1))
It works but it's far more time consuming (though far less than using subs and evalf). The error appears when I call the lambda function created by lambdify with an array object (whatever it is numpy or sympy array type).
I can reproduce your error. Based on these inputs:
m = Symbol('m')
expr = -1.06048319593874*(-(3.14159265358979*m*besselj(27.9937791601866*m, 4.46681007624482*sqrt(-I)) - 0.501286290793831*sqrt(-I)*besselj(27.9937791601866*m + 1, 4.46681007624482*sqrt(-I)))*bessely(27.9937791601866*m, 6.81776274795262*sqrt(-I))/(3.14159265358979*m*bessely(27.9937791601866*m, 4.46681007624482*sqrt(-I)) - 0.501286290793831*sqrt(-I)*bessely(27.9937791601866*m + 1, 4.46681007624482*sqrt(-I))) + besselj(27.9937791601866*m, 6.81776274795262*sqrt(-I)))*sin(0.942966379693359*m)*cos(1.5707963267949*m)/m
nm = 2
bessel = {'besselj': jv,'besselk':kv,'besseli':iv,'bessely':yv}
libraries = [bessel, "numpy"]
It works integers in vm:
vm = np.arange(nm, dtype=np.int) +1
result = lambdify(m, expr, modules=libraries)(vm)
But complex numbers produce the same error message as you got:
vm = np.arange(nm, dtype=np.complex) +1
result = lambdify(m, expr, modules=libraries)(vm)
The error message:
TypeError Traceback (most recent call last)
<ipython-input-30-f0e31009275a> in <module>()
1 vm = np.arange(nm, dtype=np.complex) +1
----> 2 result = lambdify(m, expr, modules=libraries)(vm)
/Users/mike/anaconda/envs/py34/lib/python3.4/site-packages/numpy/__init__.py in <lambda>(_Dummy_27)
TypeError: ufunc 'jv' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''
So, check what data type you got in vcm, i.e. what arange(Dim_col[0]) +1 returns.
You can take only the real part of complex numbers with the attribute real. For example, vm.real gives you the real part of vm. If this is what you want, you should get your result with:
result = lambdify(m, expr, modules=libraries).(vm.real)

Why does calling this function twice cause an error?

I am having a problem with calling a function twice. If I comment my last 3 lines and keep show(), I don't get any errors and things come as they are suppose to. However, if I don't comment them out calling the last function again gives me this error:
Traceback (most recent call last):
File "second_1.py", line 29, in <module>
domega=c_d(len(t),t,z)
File "second_1.py", line 25, in c_d
dy[1:-1]=(y[2:]-y[0:-2])/(x[2:]-x[0:-2])
TypeError: unsupported operand type(s) for -: 'list' and 'list'
Here is the function:
import numpy as np
from pylab import *
import time
t_initial=time.time()
clf()
t,hp,hn= np.loadtxt("Richardson.dat", usecols=(0,1,2),comments='#', unpack=True) # to select just a few columns
print(time.time()-t_initial)
def phi(y,x):
return(np.arctan(y/x))
phase=[0.0]*len(t)
phase=phi(hp[0:],hn[0:])
#plot(t,phase)
#show()
def c_d(order,x,y):
dy=[0.0]*order
dy[0]=(y[1]-y[0])/(x[1]-x[0])
dy[-1]=(y[-1]-y[-2])/(x[-1]-x[-2])
dy[1:-1]=(y[2:]-y[0:-2])/(x[2:]-x[0:-2])
return(dy);
z=c_d(len(t),t,phase);
plot(t,z)
print(len(z)-len(t))
domega=c_d(len(t),t,z)
plot(t,domega)
show()
The problem is very clearly explained in the error message:
The '-' operand is not applicable for the type list.
(y[2:]-y[0:-2])/(x[2:]-x[0:-2])
y[2:] slices a list and returns a list. y[0:-2] slices also a list and returns a list. So there you have 2 lists.
y[2:] (a list) -(your operator) y[0:-2] (a list)
And list - list is not defined (there is no syntax for: 'listObject' - 'listObject').
BUT: the + operator is defined for lists (example):
l = ["ja"]
m = ["nein"]
n = l + m
print n
# output: ['ja', 'nein']
Take a look here for these different kind of possible operators:
https://docs.python.org/2/library/stdtypes.html
As explained in the other answers, you can not subtract regular Python lists. So why does it work the first time, and fails the second? Let's take a look at the code.
t, hp, hn = np.loadtxt(...)
...
def c_d(order, x, y):
dy = [0.0] * order
dy[ 0] = (y[1] -y[0]) / (x[ 1]-x[0])
dy[-1] = (y[-1]-y[-2]) / (x[-1]-x[-2])
dy[1:-1] = (y[2:]-y[0:-2]) / (x[2:]-x[0:-2])
return dy
z = c_d(len(t), t, phase)
...
domega = c_d(len(t), t, z)
...
When you first call c_d, the parameters x and y seem to be numpy arrays (at least t and phase are results of numpy function calls), and for those, - is a legal operation. But inside c_d, you create and finally return a regular Python list, dy, so when you then call c_d again with the result of the first call as y, this part y[2:]-y[0:-2] will fail, as y now is a regular list.
Make sure your dy is a numpy array, too, i.e. dy = np.array([0.0] *order) or just dy = np.zeros(order), then it should work.
As stated by Cyber and ProgrammingIsAwsome the error is on line
(y[2:]-y[0:-2])/(x[2:]-x[0:-2])
where you actually try to substract lists.
You could write explicitely :
for i in range(1, order - 1):
dy[i]=(y[i+1]-y[i-1])/(x[i+1]-x[1-1])

Type error Python programming

I am trying to write some code for an assignment in python. What I am not finding anywhere is what is wrong and why it will not run. It is sense and move robotic localization function. I do not understand why this line will not work.
q.append(p[i] * (hit * sensor_right + (1 - hit) * (1-sensor_right)))
hit = a comparison between two strings. That evaluates to true or false which is 1 or 0, right?
sensor_right = 0.7
Traceback (most recent call last):
File "vm_main.py", line 26, in <module> import main
File "/tmp/sbdxfjuois/main.py", line 50, in <module> p = sense(p, measurements[k])
File "/tmp/sbdxfjuois/main.py", line 34, in sense q.append(p[i] * (hit * sensor_right + (1 - hit) * (1-sensor_right)))
TypeError: can't multiply sequence by non-int of type 'float'
Can you suggest anything for what I have here posted?
def sense(p, Z):
q = [ ]
for i in range(len(p)):
hit = (Z == colors[i])
q.append(p[i] * (hit * sensor_right + (1 - hit) * (1-sensor_right)))
s = sum(q)
for i in range(len(q)):
q[i] = q[i]/s
return q
As others have pointed out, this p variable is apparently a sequence of sequences. You can verify this by putting
print(type(p))
print(type(p[i]))
before the append statement. You'll probably see something like
tuple
tuple
If that's what you expected, then you'll need to loop over the other index of the array. Also, does your q need to be returned with the same shape? I suspect you want something more like this.
def sense(p, Z):
q = p[:]
for i in range(len(p)):
for j in range(len(p[i])):
hit = (Z == colors[i])
q[i][j] = (p[i][j] * (hit * sensor_right + (1 - hit) * (1-sensor_right)))
s = sum(q)
for i in range(len(q)):
q[i] = q[i]/s
return q
Note that you also might want to look into numpy arrays.
If you're correct about the other variables, it is telling you that p[i] is a sequence (most likely a list), which can't be multiplied by a float. Perhaps p[i] is not what you're expecting it to be?
Try printing p[i] before the line that throws an error.
The problem here is what others have said. You can only multiply a sequence by an int, not a float.
For example
>>> [1] * 3
[1, 1, 1]
>>> "f" * 6
ffffff
>>> [1] * 0.7
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: can't multiply sequence by non-int of type 'float'
Double check your data type for p, to make sure it is supposed to be a sequence. If that is truly what p should be, then make sure to convert the following to an int before multiplying
(hit * sensor_right + (1 - hit) * (1-sensor_right))
Related to what #Mike said, you could also do:
q = []
sum = 0
for i in p:
sub_q = []
for val in i:
computed_val = val * (hit * sensor_right + (1 - hit) * (1-sensor_right))
sum += computed_val
sub_q.append(computed_val)
q.append(sub_q)
I like that because it is more concise and doesn't have to build a bunch of ranges every time you call it, and it also reduces the number of times you iterate over the data, but to each their own.

Categories

Resources