# Design fitness function for polynomial approximation

I'm trying to apply a polynomial approximation for a given function (via Genetic Algorithms), and so far the results are not so good:

 # or any other GA package
require(gaoptim)

# for polyval
require(pracma)

# polynomial of degree 10
ndeg = 10
ndim = ndeg + 1

# search limits
search.low = rep(-1, ndim)
search.up = rep(1, ndim)

# no. of data points
m = 101
xi = seq(-1, 1, length = m)
yi = 1 / (1 + (5*xi)^2)

# fitness function
pfn = function(p) max(abs(polyval(c(p), xi) - yi))

# gaoptim perform maximization, so transform the fitness function
pfninv = function(p){ 1/(pfn(p) + 1) }

## set up the ga
ga = GAReal(pfninv, search.low, search.up, popSize = 500)
ga$evolve(100) y2 = polyval(ga$bestIndividual(), xi)

plot(xi, yi, ylim = range(c(yi, y2)), type = 'l', main = 'Runge function')
lines(xi, y2, col = 'red') Is there any strategy i can apply here, or this is a No-no approach? Maybe a better fitness function, or expand the search limits? Higher values of popSize doesn't seem to help too much.

Thanks for any insight!

1) Your target function is yi = 1 /(1 + (5*xi)^2), which is not a polynomial, so it's going to be hard to approximate with a polynomial: 2) If you invert the target function, yi = 1 + (5*xi)^2, increase your bounds, e.g. search.low = rep(-50, ndim); search.up= rep(50, ndim) 3) It's more common to take the root-mean-square error (RMSE) as the fitness function instead of max(abs(polyval(c(p), xi) - yi)).

• Sorry for the mistake, i edited the question. Oct 15, 2013 at 17:56
• The issue is that the search space of your GA, viz. the polynomials of degree 10, is too far away from your target function f(x) = 1 /(1 + (5*x)^2), hence the poor fitness value. Oct 15, 2013 at 18:22
• So theoretically, if i increase this search i should get better fits? Oct 15, 2013 at 18:23
• Yes but since your target function cannot be approximated by a polynomial, increasing the degree is useless. In evolutionary computation such task is called symbolic regression and we use genetic programming to optimize, not GA (GA typically requires to have a pretty good knowledge on the structure of your target function). Oct 15, 2013 at 18:30