StackGP.initializeGPModels#

generateRandomModel is a StackGP function that generates a random GP model.

The function expects 1 argument: variables

and has 4 optional arguments, ops, const, numberOfModels, and maxSize

The arguments are described below:

  • variables: The number of variables available to the models.

  • ops: The math operators to allow in the models. The default setting is ops=sgp.defaultOps().

  • const: The constants available to be used in the models. The default set is const=sgp.defaultConst(). The default allows \(\pi\), \(e\), random integers from -3 to 3, and random reals from -10 to 10.

  • numberOfModels: The total number of random models to generate.

  • maxSize: Sets the maximum stack length for the operator stack in the generated models.


First we need to load in the necessary packages

import StackGP as sgp

Overview#

Generate random models#

Here we generate a random model set of 10 models with up to 4 variables.

randomModel=sgp.initializeGPModels(4, numberOfModels=10)
[sgp.printGPModel(mod) for mod in randomModel]
[3.59591503370695,
 1/x3,
 9.86960440108936*(0.318309886183791*x0*exp(x2) - 1)**2,
 -5.821452394214521,
 0,
 exp(exp(15.1542622414793*exp(-x0 + exp(3.14159265358979/x0)))),
 x3,
 x3**2,
 0.367879441171442/x3,
 sqrt(x2 + 0.318309886183791/x0)]

We can display the random model below

sgp.printGPModel(randomModel)
\[\displaystyle - x_{0} + \left(x_{1} + 1\right)^{2}\]


Examples#

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


Controlling maxSize#

We could put a much higher cap on model size if we want. The model size generated will be from a uniform distribution from 1 to the maxSize.

randomModel=sgp.generateRandomModel(4, sgp.defaultOps(), sgp.defaultConst(), 100)
sgp.printGPModel(randomModel)
\[\displaystyle \frac{1}{\left(16.8029835121794 - \frac{1}{- \sqrt{x_{3}} \left(x_{3}^{4} - x_{3}^{2}\right) \left(1.64872127070013 \left(x_{0} + 3.14159265358979\right) \sqrt{1 - \frac{0.28650479686019}{\sqrt{1 + \frac{0.606530659712633}{x_{1}}}} + \frac{0.0293867713388586}{x_{3}}} + e^{2 x_{3}}\right) + 2}\right) \left(\sqrt{x_{2}^{2} - \frac{x_{1} + x_{3}}{x_{2}}} - 3.14159265358979\right)}\]

Controlling Number of Possible Variables#

Maybe we want up to 10 unique variables in the model. In this case we can set the first argument to 10.

randomModel=sgp.generateRandomModel(10, sgp.defaultOps(), sgp.defaultConst(), 20)
sgp.printGPModel(randomModel)
\[\displaystyle \frac{x_{5} x_{6}}{2 \left(- x_{5} + \frac{1}{- x_{2} + \frac{e^{x_{7}^{2}}}{x_{8} x_{9}}}\right)} - 2.71828182845905\]

Generating Linear Models#

We may want to restrict our operator set to just addition and subtraction so that we form linear models.

randomModel=sgp.generateRandomModel(4, [sgp.add, sgp.sub], sgp.defaultConst(), 100)
sgp.printGPModel(randomModel)
\[\displaystyle 3 x_{0} + 2 x_{1} - 4 x_{2} + x_{3} + 34.7940498274679\]

Integers and Linear Models#

If we only want integer constants and a linear model, we can do so by doing the following.

randomModel=sgp.generateRandomModel(4, [sgp.add, sgp.sub], [sgp.randomInt], 20)
sgp.printGPModel(randomModel)
\[\displaystyle 4 x_{0} - 2 x_{1} + 2 x_{2} + 4 x_{3} + 5\]

Specific Constants#

We can supply a list of specific constants if we know that a specific set of constants will be useful. Note, that other constants can appear via combinations of the constants in the supplied set.

randomModel=sgp.generateRandomModel(4, [sgp.add, sgp.sub], [1,5,10], 2)
sgp.printGPModel(randomModel)
\[\displaystyle x_{1} + 15\]