Symbolic regression solver based on genetic programming (GAP)
Source code name: "symb_regression_gap.py"
Programming language: Python
Topic: Artificial Intelligence/genetic programming
DMelt Version 1. Last modified: 05/09/2015. License: Pro
https://datamelt.org/code/cache/symb_regression_gap_5419.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.



from java.awt import Color,Font
from jhplot  import * 
from jhpro.sregression import *

# make a configuration file to do a symbolic regression 
data="""presentation: P(4) x^4 + x^3 + x^2 - x (the JGAP example)
num_input_variables: 1
variable_names: x y
functions: Add,Subtract,Multiply,Divide,Pow,Log,Sine
terminal_range: -10 10
max_init_depth: 4
population_size: 1000
max_crossover_depth: 8
num_evolutions: 800
max_nodes: 20
stop_criteria_fitness: 0.1
data
-2.378099   26.567495
4.153756   382.45743
2.6789956   75.23481
5.336802   986.33777
2.4132318   51.379707
-1.7993588   9.693933
3.9202332   307.8775
2.9227705   103.56364
-0.1422224   0.159982
4.9111285   719.39545
1.2542424   4.76668
1.5987749   11.577456
4.7125554   615.356
-1.1101999   2.493538
-1.7379236   8.631802
3.8303614   282.29697
5.158349   866.7222
3.6650343   239.42934
0.3196721   -0.17437163
-2.3650131   26.014963
"""
file = open("test.conf", "w")
file.write(data)
file.close()
js=SymRegression("test.conf")

#dir=SystemDir+fSep+"macros"+fSep+"examples"+fSep+"data"+fSep
#js=SymRegression(dir+"test.conf")

js.run()

# create a tree as image
# IView("/home/sergei/a.png")
print "Best solution=",js.getBestSolution()
# js.createTree("/home/sergei/a.png")


data=js.getDataPND()
print data.toString()

#val=js.getValidationPND()
#print val.toString()

c1 = HPlot("Canvas")
c1.setGTitle("Symbolic regression")
c1.visible(1)
c1.setAutoRange()
f1=F1D(js.getBestSolution(),-5,6)
f1.setTitle("best analytic solution")
c1.draw(f1)

r12=data.getP1D(0,1)
r12.setColor(Color.blue)
c1.draw(r12)


You see the box below because you did not login.