StackGP.printGPModel#

StackGP.printGPModel(model,inputData=symbols([“x”+str(i) for i in range(100)]))

printGPModel is a StackGP function that evaluates a GP model with the supplied data.

The function expects 1 arguments: model

There is 1 optional argument: inputData

The arguments are described below:

  • model: A StackGP model.

  • inputData: a list of SymPy symbols to use as the variable names.


First we need to load in the necessary packages

import StackGP as sgp
import numpy as np

Overview#

Display a model#

Here we generate a random model with up to 4 variables, the default operator set, the default constant set, and a maxSize of 10.

randomModel=sgp.generateRandomModel(4, sgp.defaultOps(), sgp.defaultConst(), 10)

We can display the random model below

sgp.printGPModel(randomModel)
\[\displaystyle e^{2 \sqrt{x_{3}}}\]


Options#

This section showcases how each of the different option settings can be used with the printGPModel function.


inputData#

inputData can be used to change the symbols used when displaying the model. The default is to use x1, x2, …, x100

First lets generate some random data to fit a model to.

#Define a challenging function to generate data
def demoFunc(x,y):
    return np.sin(x) + y
#Generate data
inputData=np.array([np.random.randint(1,10,10),np.random.randint(1,10,10)])
response=demoFunc(inputData[0],inputData[1])

Now lets try fitting with the default settings. We will turn on tracking so we can see the progress.

#Generate models
models=sgp.evolve(inputData,response,liveTracking=True, generations=100, popSize=100, ops=sgp.allOps())
../_images/d9eb0e9f9d6b672757bce6dddafd68557c8755afc282f6e0c1614dfa84f875c9.png

Now we can display the best model evolved.

#View best model
sgp.printGPModel(models[0])
\[\displaystyle 0.999999999999999 x_{1} + 0.999999999999999 \sin{\left(x_{0} \right)} + 5.61733354972272 \cdot 10^{-16}\]

Now maybe we don’t want x and y as the variable names and instead want something like var1 and var2. We can do this using the inputData argument like below.

from sympy import symbols
sgp.printGPModel(models[0], inputData=symbols("var1,var2"))
\[\displaystyle 0.999999999999999 var_{2} + 0.999999999999999 \sin{\left(var_{1} \right)} + 5.61733354972272 \cdot 10^{-16}\]


Examples#

This section showcases how each of the different arguments can be used with the printGPModel function.


Checking the form of a random model using specific variable names#

randomModel = sgp.generateRandomModel(4, sgp.defaultOps(), sgp.defaultConst(), 10)
sgp.printGPModel(randomModel, inputData=symbols("temperature , pressure, humidity, windSpeed"))
\[\displaystyle humidity + \frac{e^{2}}{pressure}\]

Checking the forms of models from an evolved Pareto front#

First we generate some random data and evolve a model population.

inputData, responseData, model = sgp.generateRandomBenchmark()
models = sgp.evolve(inputData,responseData,liveTracking=True, generations=100, popSize=100, ops=sgp.allOps())
../_images/78e63173eff84c5b04f83b6a3ac442d4512be3c7c50030d4e31cb50a2c3a64dd.png

Now we can grab the Pareto front of the models

pFront = sgp.selectModels(models, selectionSize=2)
for i, model in zip(range(len(pFront)), pFront):
    print(i, sgp.printGPModel(model, inputData=symbols("position, velocity, acceleration, time, mass")))
0 -3.5527136788005e-16 + 1.0/velocity
1 7.35939390871629 - 8.29633610621588*velocity