Symbolic regression solver based on genetic programming
Source code name: "symb_regression.py"
Programming language: Python
Topic: Artificial Intelligence/genetic programming
DMelt Version 1. Last modified: 07/24/2016. License: Pro
https://datamelt.org/code/cache/symb_regression_2890.py
To run this script using the DMelt IDE, copy the above URL link to the menu [File]→[Read script from URL] of the DMelt IDE.



# Example: find a target function f(x) = x^2 + 10*x using input data

from com.lagodiuk.gp.symbolic import *
from com.lagodiuk.gp.symbolic.interpreter import *

# build x and y data
x=[0,1,2,3,4,5,6]
y=[0,11,24,39,56,75,96]
data=[]

for i in range( len(x) ):
    data.append(Target().when("x", x[i]).targetIs(y[i]))

# build fitness function
fitness=TabulatedFunctionFitness(data)
engine =SymbolicRegressionEngine(fitness,["x"],[Functions.ADD, Functions.SUB, Functions.MUL, Functions.VARIABLE, Functions.CONSTANT])
engine.evolve(200)

# get answer
bestSyntaxTree = engine.getBestSyntaxTree()
currFitValue = engine.fitness(bestSyntaxTree)
print "Iterations=",engine.getIteration()
print "Fit value=",currFitValue
print "Function=",bestSyntaxTree.print()



You see the box below because you did not login.