Quick Start

Quick Start#

Here, We introduce the basic usage of HyperGP

  1. Necessary Modules: Three type modules will be used in the quickstart to run:

    • basic components:
      • Population to initialize population

      • PrimitiveSet to set the primitives and terminals

      • executor to execute the expression

      • Tensor to store and compute datas

      • GpOptimizer a 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
  1. Generate the training data: We can use Tensor module to generate the array, or use to encapsulate the numpy.ndarray or the list

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])
  1. Build the primitive set: To run the program, we will need the PrimitiveSet module 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)])
  1. Initialize population: with the PrimitiveSet, we can use Population to 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)
  1. Initialize GpOptimizer workflow 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 )
  1. 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 )
  1. 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)