- #1
Prof. 27
- 50
- 1
Homework Statement
So I'm currently modifying an example Symbolic Regression program that uses genetic programming, a type of evolutionary algorithm. I'm using the python deap library. Not sure if anyone on here is familiar with it, but I think that the error is more general to a misuse of python. I've "plotted" a series of values from the function x^2 to test the program on before working on more difficult problems in a list. I get a this error:
Traceback (most recent call last):
File "C:/Users/Anonymous/Downloads/WinPython-64bit-3.4.3.4/python-3.4.3.amd64/Lib/idlelib/Projects/GP SymbReg/Draft2.py", line 81, in <module>
main()
File "C:/Users/Anonymous/Downloads/WinPython-64bit-3.4.3.4/python-3.4.3.amd64/Lib/idlelib/Projects/GP SymbReg/Draft2.py", line 77, in main
halloffame=hof, verbose=True)
File "C:\Users\Anonymous\Downloads\WinPython-64bit-3.4.3.4\python-3.4.3.amd64\lib\site-packages\deap\algorithms.py", line 148, in eaSimple
for ind, fit in zip(invalid_ind, fitnesses):
File "C:/Users/Anonymous/Downloads/WinPython-64bit-3.4.3.4/python-3.4.3.amd64/Lib/idlelib/Projects/GP SymbReg/Draft2.py", line 47, in evalSymbReg
diff = ((func(testvals[index]) - values[index])**2)
IndexError: list index out of range
I assume this is exactly what it means, that the index variable achieves greater values than the index of one of my two lists. The trouble is, after an extensive review I am unable to find how this could be the case.
Here is the relevant code (as far as I'm aware, if you need the whole program, let me know.):
Python:
#Set data (x^2) and test vals.
values = [0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
L = []
testvals = L[0:len(values)-1:1]
#Evaluate Function
def evalSymbReg(individual):
index=0
# Transform the tree expression in a callable function
func = toolbox.compile(expr=individual)
# Evaluate the mean squared error between the function and the sample values.
diff = ((func(testvals[index]) - values[index])**2)
if index < len(values):
index+1
return diff,
else:
pass
Any ideas on why I'm getting the error?
Homework Equations
None
The Attempt at a Solution
A review of the material on stack overflow
Python Documentation on Lists, len()
DEAP Documentation