Quick Start#
Here, We introduce the basic usage of HyperGP
Necessary Modules: Three type modules will be used in the quickstart to run:
- basic components:
Populationto initialize populationPrimitiveSetto set the primitives and terminalsexecutorto execute the expressionTensorto store and compute datasGpOptimizera workflow manager, to iter overall process
- operators:
such as:
RandTrCrv,RandTrMut
- states:
such as
ProgBuildStates,ParaStates
1import random, HyperGP, numpy as np
2from HyperGP.states import ProgBuildStates, ParaStates
Generate the training data: We can use
Tensormodule to generate the array, or use to encapsulate thenumpy.ndarrayor thelist
1 # Generate training set
2 input_array = HyperGP.tensor.uniform(0, 10, size=(2, input_size))
3 target = (input_array[0] * 3 + input_array[0] * input_array[1] * 2) * (input_array[0])
Build the primitive set: To run the program, we will need the
PrimitiveSetmodule to define the used primitives and terminals
1 # Generate primitive set
2 pset = HyperGP.PrimitiveSet(input_arity=2, primitive_set=[('add', HyperGP.tensor.add, 2),('sub', HyperGP.tensor.sub, 2),('mul', HyperGP.tensor.mul, 2),('div', HyperGP.tensor.div, 2),('sin', HyperGP.tensor.sin, 1),('cos', HyperGP.tensor.cos, 1)])
Initialize population: with the
PrimitiveSet, we can usePopulationto initialize the population
1 # Init population
2 pop = HyperGP.Population()
3 pop.initPop(pop_size=pop_size, prog_paras=ProgBuildStates(pset=pset, depth_rg=[2, 6], len_limit=100000))
4 output, _ = HyperGP.executor(pop.states['progs'].indivs, input=input_array, pset=pset)
5 pop.states['progs'].fitness = HyperGP.ops.rmse(output, target)
Initialize
GpOptimizerworkflow module: To run a workflow, we should first initialize it and set the states we use to the GpOptimizer.
1 # Init workflow
2 optimizer = HyperGP.GpOptimizer()
3
4 # Register relevant states
5 optimizer.status_init(
6 childs_list=pop.states['progs'].indivs, target=target,
7 input=input_array,pset=pset,output=None,
8 cfit_list = pop.states['progs'].fitness,
9 parent_list=[ind.copy() for ind in pop.states['progs'].indivs], pfit_list=pop.states['progs'].fitness.copy(),
10 )
Add the components user want to iteratively run
1 optimizer.iter_component(
2 ParaStates(func=HyperGP.ops.RandTrCrv(), source=["parent_list", "parent_list", 0.8], to=["parent_list", "parent_list"],
3 mask=[lambda x=int(pop_size / 2):random.sample(range(pop_size), x), lambda x=int(pop_size / 2):random.sample(range(pop_size), x), 1]),
4 ParaStates(func=HyperGP.ops.RandTrMut(), source=["parent_list", ProgBuildStates(pset=pset, depth_rg=[1, 3], len_limit=pop_size), 0.2, True], to=["parent_list"],
5 mask=[lambda x=pop_size:random.sample(range(pop_size), x), 1, 1, 1]),
6 ParaStates(func=HyperGP.executor, source=["parent_list", "input", "pset"], to=["output", None],
7 mask=[1, 1, 1]),
8 ParaStates(func=HyperGP.ops.rmse, source=["output", "target"], to=["cfit_list"]),
9 ParaStates(func=HyperGP.ops.tournament, source=["parent_list", "parent_list", "cfit_list", "pfit_list"], to=["parent_list", "parent_list", "cfit_list", "pfit_list"],
10 mask=[1, 1, 1, 1]),
11 )
Run the optimizer and monitor the states wanted to print
1optimizer.monitor(HyperGP.monitors.statistics_record, "cfit_list")
2optimizer.run(100, stop_criteria=lambda: HyperGP.tensor.min(optimizer.workflowstates.cfit_list) == 0.0, tqdm_diable=False)