StackGP.generateRandomModel#

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

The function expects 4 arguments: variables, ops, const, and maxSize

The arguments are described below:

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

  • ops: The math operators to allow in the model. A good standard set can be used by setting ops=sgp.defaultOps().

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

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


First we need to load in the necessary packages

import StackGP as sgp
import numpy as np

Overview#

Generate random models#

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 - x_{0} + \left(x_{1} + 1\right)^{2}\]


Examples#

This section showcases how each of the different arguments can be used with the generateRandomModel 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\]