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)
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)
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)
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)
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)
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)