Example for differential evolutionary regressionΒΆ

Example demonstrating the use of Cartesian genetic programming for a regression task that involves numeric constants. Local gradient-based search is used to determine numeric leaf values of the graph.

References:

  • Topchy, A., & Punch, W. F. (2001). Faster genetic programming based on local gradient search of numeric leaf values. In Proceedings of the genetic and evolutionary computation conference (GECCO-2001) (Vol. 155162). Morgan Kaufmann San Francisco, CA, USA.

  • Izzo, D., Biscani, F., & Mereta, A. (2017). Differentiable genetic programming. In European Conference on Genetic Programming (pp. 35-51). Springer, Cham.

# The docopt str is added explicitly to ensure compatibility with
# sphinx-gallery.
docopt_str = """
  Usage:
    example_differential_evo_regression.py [--max-generations=<N>]

  Options:
    -h --help
    --max-generations=<N>  Maximum number of generations [default: 2000]

"""

import functools

import matplotlib.pyplot as plt
import numpy as np
import scipy.constants
import torch
from docopt import docopt

import cgp

args = docopt(docopt_str)

We first define the target function. Note that this function contains numeric values which are initially not available as constants to the search.

def f_target(x):
    return x[:, 0] ** 2 + 1.0 + np.pi

Then we define the differentiable(!) objective function for the evolution. It consists of an inner objective which accepts a torch tensor as input variable and uses mean-squared error between the expression represented by a given individual and the target function evaluated on a set of random points. This inner objective is then used by actual objective function to update the fitness of the individual.

def inner_objective(f, seed):
    """Return a differentiable loss of the differentiable graph f. Used
    for calculating the fitness of each individual and for the local
    search of numeric leaf values.

    """

    torch.manual_seed(seed)
    batch_size = 500
    x = torch.DoubleTensor(batch_size, 1).uniform_(-5, 5)
    y = f(x)
    return torch.nn.MSELoss()(f_target(x), y[:, 0])


def objective(individual, seed):
    """Objective function of the regression task."""

    if not individual.fitness_is_None():
        return individual

    f = individual.to_torch()
    loss = inner_objective(f, seed)
    individual.fitness = -loss.item()

    return individual

Next, we define the parameters for the population, the genome of individuals, the evolutionary algorithm, and the local search.

population_params = {"n_parents": 1, "seed": 818821}

genome_params = {
    "n_inputs": 1,
    "n_outputs": 1,
    "n_columns": 20,
    "n_rows": 1,
    "levels_back": None,
    "primitives": (cgp.Add, cgp.Sub, cgp.Mul, cgp.Parameter),
}

ea_params = {
    "n_offsprings": 4,
    "tournament_size": 1,
    "mutation_rate": 0.05,
    "n_processes": 1,
    "k_local_search": 2,
}

evolve_params = {"max_generations": int(args["--max-generations"]), "termination_fitness": 0.0}

# use an uneven number of gradient steps so they can not easily
# average out for clipped values
local_search_params = {"lr": 1e-3, "gradient_steps": 9}

We then create a Population instance and instantiate the local search and evolutionary algorithm.

pop = cgp.Population(**population_params, genome_params=genome_params)

# define the function for local search; parameters such as the
# learning rate and number of gradient steps are fixed via the use
# of `partial`; the `local_search` function should only receive a
# population of individuals as input
local_search = functools.partial(
    cgp.local_search.gradient_based,
    objective=functools.partial(inner_objective, seed=population_params["seed"]),
    **local_search_params,
)

ea = cgp.ea.MuPlusLambda(**ea_params, local_search=local_search)

We define a recording callback closure for bookkeeping of the progression of the evolution.

history = {}
history["champion"] = []
history["fitness_parents"] = []


def recording_callback(pop):
    history["champion"].append(pop.champion)
    history["fitness_parents"].append(pop.fitness_parents())


obj = functools.partial(objective, seed=population_params["seed"])

Finally, we call the evolve method to perform the evolutionary search.

cgp.evolve(pop, obj, ea, **evolve_params, print_progress=True, callback=recording_callback)

Out:

[2/2000] max fitness: -171.47786981872468
[3/2000] max fitness: -167.26491103858154
[4/2000] max fitness: -163.2010677408287
[5/2000] max fitness: -159.2810620606094
[6/2000] max fitness: -155.49980294034543
[7/2000] max fitness: -151.85237951779072
[8/2000] max fitness: -148.33405474811138
[9/2000] max fitness: -100.81326218785846
[10/2000] max fitness: -94.30280108884027
[11/2000] max fitness: -88.66873616760508
[12/2000] max fitness: -83.79309263730596
[13/2000] max fitness: -79.57377671409922
[14/2000] max fitness: -75.92243781920395
[15/2000] max fitness: -72.76261855749713
[16/2000] max fitness: -70.02815373402858
[17/2000] max fitness: -67.6617848845812
[18/2000] max fitness: -52.479091199098995
[19/2000] max fitness: -52.47074731440681
[20/2000] max fitness: -52.46473038281477
[21/2000] max fitness: -52.460391460703725
[22/2000] max fitness: -52.45726258267045
[23/2000] max fitness: -52.455006290094474
[24/2000] max fitness: -52.45337923524147
[25/2000] max fitness: -52.45220593550063
[26/2000] max fitness: -52.45135984707325
[27/2000] max fitness: -52.45074971685496
[28/2000] max fitness: -52.450309740522506
[29/2000] max fitness: -52.44999246534523
[30/2000] max fitness: -52.4497636722701
[31/2000] max fitness: -43.97969325975194
[32/2000] max fitness: -42.34382519791927
[33/2000] max fitness: -1.6945892457041014
[34/2000] max fitness: -1.5766399889918228
[35/2000] max fitness: -1.466900407393588
[36/2000] max fitness: -1.3647990792035125
[37/2000] max fitness: -1.2698043556374683
[38/2000] max fitness: -1.1814215925005425
[39/2000] max fitness: -1.0991905745399781
[40/2000] max fitness: -1.0226831190720527
[41/2000] max fitness: -0.9515008464047797
[42/2000] max fitness: -0.8852731054468744
[43/2000] max fitness: -0.8236550437015104
[44/2000] max fitness: -0.7663258115951534
[45/2000] max fitness: -0.7129868917913014
[46/2000] max fitness: -0.6633605447897681
[47/2000] max fitness: -0.6171883627175898
[48/2000] max fitness: -0.0044074907345378325
[49/2000] max fitness: -0.004251489894911988
[50/2000] max fitness: -0.00410101062377719
[51/2000] max fitness: -0.003955857488091476
[52/2000] max fitness: -0.00381584197206412
[53/2000] max fitness: -0.0036807822323223173
[54/2000] max fitness: -0.0035505028617448266
[55/2000] max fitness: -0.0034248346616540743
[56/2000] max fitness: -0.003303614422071895
[57/2000] max fitness: -0.003186684709751792
[58/2000] max fitness: -0.003073893663715416
[59/2000] max fitness: -0.00296509479802461
[60/2000] max fitness: -0.0028601468115347518
[61/2000] max fitness: -0.002758913404381729
[62/2000] max fitness: -0.002661263100964073
[63/2000] max fitness: -0.0025670690791906705
[64/2000] max fitness: -0.002476209005772322
[65/2000] max fitness: -0.0023885648773429103
[66/2000] max fitness: -0.0023040228672040635
[67/2000] max fitness: -0.0022224731774942255
[68/2000] max fitness: -0.0021438098965896914
[69/2000] max fitness: -0.0020679308615539475
[70/2000] max fitness: -0.0019947375254538297
[71/2000] max fitness: -0.0019241348293743115
[72/2000] max fitness: -0.0018560310789605707
[73/2000] max fitness: -0.0017903378253320357
[74/2000] max fitness: -0.0017269697502099038
[75/2000] max fitness: -0.0016658445551119543
[76/2000] max fitness: -0.0016068828544674144
[77/2000] max fitness: -0.0015500080725165128
[78/2000] max fitness: -0.0014951463438588556
[79/2000] max fitness: -0.0014422264175212244
[80/2000] max fitness: -0.0013911795644216203
[81/2000] max fitness: -0.0013419394881080523
[82/2000] max fitness: -0.0012944422386569898
[83/2000] max fitness: -0.0012486261296188838
[84/2000] max fitness: -0.0012044316579044275
[85/2000] max fitness: -0.0011618014265048302
[86/2000] max fitness: -0.0011206800699485604
[87/2000] max fitness: -0.0010810141823962055
[88/2000] max fitness: -0.0010427522482802684
[89/2000] max fitness: -0.001005844575400015
[90/2000] max fitness: -0.0009702432303839517
[91/2000] max fitness: -0.0009359019764375898
[92/2000] max fitness: -0.0009027762132935137
[93/2000] max fitness: -0.0008708229192878325
[94/2000] max fitness: -0.0008400005954858004
[95/2000] max fitness: -0.0008102692117860012
[96/2000] max fitness: -0.0007815901549314825
[97/2000] max fitness: -0.0007539261783615094
[98/2000] max fitness: -0.0007272413538379993
[99/2000] max fitness: -0.0007015010247840883
[100/2000] max fitness: -0.0006766717612742488
[101/2000] max fitness: -0.0006527213166182617
[102/2000] max fitness: -0.0006296185854801173
[103/2000] max fitness: -0.0006073335634812071
[104/2000] max fitness: -0.0005858373082324481
[105/2000] max fitness: -0.0005651019017454806
[106/2000] max fitness: -0.0005451004141743991
[107/2000] max fitness: -0.0005258068688414415
[108/2000] max fitness: -0.0005071962084995705
[109/2000] max fitness: -0.000489244262790172
[110/2000] max fitness: -0.0004719277168518964
[111/2000] max fitness: -0.000455224081040556
[112/2000] max fitness: -0.00043911166172141145
[113/2000] max fitness: -0.0004235695330945358
[114/2000] max fitness: -0.00040857751001782287
[115/2000] max fitness: -0.00039411612179172473
[116/2000] max fitness: -0.0003801665868720182
[117/2000] max fitness: -0.00036671078847748195
[118/2000] max fitness: -0.0003537312510608356
[119/2000] max fitness: -0.00034121111761279885
[120/2000] max fitness: -0.0003291341277690685
[121/2000] max fitness: -0.00031748459669253163
[122/2000] max fitness: -0.0003062473947027537
[123/2000] max fitness: -0.0002954079276262724
[124/2000] max fitness: -0.0002849521178429621
[125/2000] max fitness: -0.00027486638600276856
[126/2000] max fitness: -0.0002651376333895683
[127/2000] max fitness: -0.0002557532249094495
[128/2000] max fitness: -0.000246700972681148
[129/2000] max fitness: -0.0002379691202070591
[130/2000] max fitness: -0.00022954632710474448
[131/2000] max fitness: -0.00022142165437870624
[132/2000] max fitness: -0.00021358455021334668
[133/2000] max fitness: -0.00020602483626918275
[134/2000] max fitness: -0.00019873269446387323
[135/2000] max fitness: -0.0001916986542208697
[136/2000] max fitness: -0.00018491358016970065
[137/2000] max fitness: -0.00017836866028170823
[138/2000] max fitness: -0.0001720553944253103
[139/2000] max fitness: -0.00016596558332664706
[140/2000] max fitness: -0.00016009131792093048
[141/2000] max fitness: -0.00015442496908058223
[142/2000] max fitness: -0.0001489591777070152
[143/2000] max fitness: -0.0001436868451731719
[144/2000] max fitness: -0.00013860112410414352
[145/2000] max fitness: -0.0001336954094842604
[146/2000] max fitness: -0.0001289633300790108
[147/2000] max fitness: -0.00012439874016038772
[148/2000] max fitness: -0.00011999571152522122
[149/2000] max fitness: -0.0001157485257960073
[150/2000] max fitness: -0.00011165166699420546
[151/2000] max fitness: -0.0001076998143765427
[152/2000] max fitness: -0.00010388783552458623
[153/2000] max fitness: -0.00010021077967923942
[154/2000] max fitness: -9.666387131095686e-05
[155/2000] max fitness: -9.324250391750824e-05
[156/2000] max fitness: -8.994223404151546e-05
[157/2000] max fitness: -8.675877549940173e-05
[158/2000] max fitness: -8.368799381475065e-05
[159/2000] max fitness: -8.072590084893348e-05
[160/2000] max fitness: -7.78686496213376e-05
[161/2000] max fitness: -7.511252931316888e-05
[162/2000] max fitness: -7.245396044821348e-05
[163/2000] max fitness: -6.988949024399114e-05
[164/2000] max fitness: -6.741578812736107e-05
[165/2000] max fitness: -6.502964140912483e-05
[166/2000] max fitness: -6.272795111153977e-05
[167/2000] max fitness: -6.050772794358825e-05
[168/2000] max fitness: -5.836608841861961e-05
[169/2000] max fitness: -5.630025110950117e-05
[170/2000] max fitness: -5.4307533036291044e-05
[171/2000] max fitness: -5.238534618170205e-05
[172/2000] max fitness: -5.053119412996946e-05
[173/2000] max fitness: -4.8742668824674905e-05
[174/2000] max fitness: -4.7017447441296546e-05
[175/2000] max fitness: -4.535328937048877e-05
[176/2000] max fitness: -4.374803330810897e-05
[177/2000] max fitness: -4.219959444819059e-05
[178/2000] max fitness: -4.070596177547299e-05
[179/2000] max fitness: -3.9265195453488976e-05
[180/2000] max fitness: -3.787542430528739e-05
[181/2000] max fitness: -3.653484338323382e-05
[182/2000] max fitness: -3.5241711624895794e-05
[183/2000] max fitness: -3.3994349591844995e-05
[184/2000] max fitness: -3.2791137288466834e-05
[185/2000] max fitness: -3.16305120580581e-05
[186/2000] max fitness: -3.051096655335317e-05
[187/2000] max fitness: -2.9431046778859086e-05
[188/2000] max fitness: -2.8389350202474457e-05
[189/2000] max fitness: -2.7384523934045824e-05
[190/2000] max fitness: -2.6415262968167007e-05
[191/2000] max fitness: -2.5480308489497646e-05
[192/2000] max fitness: -2.457844623777724e-05
[193/2000] max fitness: -2.3708504930873735e-05
[194/2000] max fitness: -2.2869354743545314e-05
[195/2000] max fitness: -2.2059905840168662e-05
[196/2000] max fitness: -2.1279106959264007e-05
[197/2000] max fitness: -2.0525944048201067e-05
[198/2000] max fitness: -1.9799438946207708e-05
[199/2000] max fitness: -1.90986481140154e-05
[200/2000] max fitness: -1.8422661408431612e-05
[201/2000] max fitness: -1.7770600900322872e-05
[202/2000] max fitness: -1.714161973438214e-05
[203/2000] max fitness: -1.6534901029317158e-05
[204/2000] max fitness: -1.5949656816894407e-05
[205/2000] max fitness: -1.5385127018641667e-05
[206/2000] max fitness: -1.4840578458662996e-05
[207/2000] max fitness: -1.4315303911416352e-05
[208/2000] max fitness: -1.3808621183258584e-05
[209/2000] max fitness: -1.3319872226450057e-05
[210/2000] max fitness: -1.2848422284486582e-05
[211/2000] max fitness: -1.2393659067730234e-05
[212/2000] max fitness: -1.195499195824013e-05
[213/2000] max fitness: -1.15318512426874e-05
[214/2000] max fitness: -1.1123687372440872e-05
[215/2000] max fitness: -1.072997024985652e-05
[216/2000] max fitness: -1.0350188539812629e-05
[217/2000] max fitness: -9.983849005659054e-06
[218/2000] max fitness: -9.63047586856904e-06
[219/2000] max fitness: -9.289610189666946e-06
[220/2000] max fitness: -8.960809273972843e-06
[221/2000] max fitness: -8.643646095502621e-06
[222/2000] max fitness: -8.33770874258489e-06
[223/2000] max fitness: -8.0425998829672e-06
[224/2000] max fitness: -7.757936247785078e-06
[225/2000] max fitness: -7.483348133753249e-06
[226/2000] max fitness: -7.218478923040567e-06
[227/2000] max fitness: -6.962984620127221e-06
[228/2000] max fitness: -6.716533405034193e-06
[229/2000] max fitness: -6.4788052023767685e-06
[230/2000] max fitness: -6.249491265664885e-06
[231/2000] max fitness: -6.028293776342745e-06
[232/2000] max fitness: -5.814925456987636e-06
[233/2000] max fitness: -5.609109198186654e-06
[234/2000] max fitness: -5.410577698711631e-06
[235/2000] max fitness: -5.219073118285661e-06
[236/2000] max fitness: -5.0343467427580095e-06
[237/2000] max fitness: -4.856158661112954e-06
[238/2000] max fitness: -4.6842774538454656e-06
[239/2000] max fitness: -4.5184798924075765e-06
[240/2000] max fitness: -4.358550649321811e-06
[241/2000] max fitness: -4.204282018526142e-06
[242/2000] max fitness: -4.055473645599688e-06
[243/2000] max fitness: -3.911932267542971e-06
[244/2000] max fitness: -3.7734714618221773e-06
[245/2000] max fitness: -3.6399114042341436e-06
[246/2000] max fitness: -3.511078635332248e-06
[247/2000] max fitness: -3.3868058352063127e-06
[248/2000] max fitness: -3.2669316061324506e-06
[249/2000] max fitness: -3.151300262983619e-06
[250/2000] max fitness: -3.039761631017949e-06
[251/2000] max fitness: -2.932170850853546e-06
[252/2000] max fitness: -2.828388190332224e-06
[253/2000] max fitness: -2.7282788630389024e-06
[254/2000] max fitness: -2.631712853257048e-06
[255/2000] max fitness: -2.538564747113976e-06
[256/2000] max fitness: -2.448713569687122e-06
[257/2000] max fitness: -2.362042627902047e-06
[258/2000] max fitness: -2.278439358969847e-06
[259/2000] max fitness: -2.1977951842111707e-06
[260/2000] max fitness: -2.1200053680266502e-06
[261/2000] max fitness: -2.044968881889211e-06
[262/2000] max fitness: -1.9725882731025127e-06
[263/2000] max fitness: -1.9027695382788158e-06
[264/2000] max fitness: -1.8354220012155787e-06
[265/2000] max fitness: -1.7704581951655042e-06
[266/2000] max fitness: -1.7077937492037767e-06
[267/2000] max fitness: -1.6473472786765609e-06
[268/2000] max fitness: -1.5890402794956215e-06
[269/2000] max fitness: -1.5327970261952635e-06
[270/2000] max fitness: -1.478544473557647e-06
[271/2000] max fitness: -1.4262121617722177e-06
[272/2000] max fitness: -1.3757321249191958e-06
[273/2000] max fitness: -1.3270388026813504e-06
[274/2000] max fitness: -1.2800689552310394e-06
[275/2000] max fitness: -1.2347615810755779e-06
[276/2000] max fitness: -1.191057837838716e-06
[277/2000] max fitness: -1.1489009658364399e-06
[278/2000] max fitness: -1.1082362143686246e-06
[279/2000] max fitness: -1.0690107706057994e-06
[280/2000] max fitness: -1.031173691001136e-06
[281/2000] max fitness: -9.946758351258694e-07
[282/2000] max fitness: -9.594698018562787e-07
[283/2000] max fitness: -9.25509867801454e-07
[284/2000] max fitness: -8.927519279315567e-07
[285/2000] max fitness: -8.611534382871476e-07
[286/2000] max fitness: -8.306733607296162e-07
[287/2000] max fitness: -8.012721096481824e-07
[288/2000] max fitness: -7.72911500540179e-07
[289/2000] max fitness: -7.455547004212404e-07
[290/2000] max fitness: -7.191661800012643e-07
[291/2000] max fitness: -6.937116675200297e-07
[292/2000] max fitness: -6.691581042553279e-07
[293/2000] max fitness: -6.45473601576692e-07
[294/2000] max fitness: -6.226273995365602e-07
[295/2000] max fitness: -6.005898269260926e-07
[296/2000] max fitness: -5.793322627208394e-07
[297/2000] max fitness: -5.588270989330532e-07
[298/2000] max fitness: -5.390477047406136e-07
[299/2000] max fitness: -5.199683919062825e-07
[300/2000] max fitness: -5.015643814149625e-07
[301/2000] max fitness: -4.838117712925954e-07
[302/2000] max fitness: -4.6668750556238303e-07
[303/2000] max fitness: -4.5016934430265124e-07
[304/2000] max fitness: -4.342358347581552e-07
[305/2000] max fitness: -4.188662834888211e-07
[306/2000] max fitness: -4.040407294816282e-07
[307/2000] max fitness: -3.8973991824187816e-07
[308/2000] max fitness: -3.7594527676688673e-07
[309/2000] max fitness: -3.6263888944559313e-07
[310/2000] max fitness: -3.498034747755452e-07
[311/2000] max fitness: -3.3742236292315755e-07
[312/2000] max fitness: -3.2547947407822844e-07
[313/2000] max fitness: -3.139592975641878e-07
[314/2000] max fitness: -3.0284687170222914e-07
[315/2000] max fitness: -2.92127764366001e-07
[316/2000] max fitness: -2.8178805425322915e-07
[317/2000] max fitness: -2.7181431279592125e-07
[318/2000] max fitness: -2.6219358672600055e-07
[319/2000] max fitness: -2.529133812443132e-07
[320/2000] max fitness: -2.4396164380365e-07
[321/2000] max fitness: -2.35326748448513e-07
[322/2000] max fitness: -2.26997480720008e-07
[323/2000] max fitness: -2.1896302308431152e-07
[324/2000] max fitness: -2.1121294089376325e-07
[325/2000] max fitness: -2.0373716882522507e-07
[326/2000] max fitness: -1.965259978177547e-07
[327/2000] max fitness: -1.8957006245294474e-07
[328/2000] max fitness: -1.8286032879963543e-07
[329/2000] max fitness: -1.7638808267525137e-07
[330/2000] max fitness: -1.7014491833199339e-07
[331/2000] max fitness: -1.6412272754003706e-07
[332/2000] max fitness: -1.5831368905566765e-07
[333/2000] max fitness: -1.5271025846333947e-07
[334/2000] max fitness: -1.473051583788038e-07
[335/2000] max fitness: -1.420913689983436e-07
[336/2000] max fitness: -1.370621189776944e-07
[337/2000] max fitness: -1.3221087664240087e-07
[338/2000] max fitness: -1.2753134150452421e-07
[339/2000] max fitness: -1.2301743607606487e-07
[340/2000] max fitness: -1.186632979787163e-07
[341/2000] max fitness: -1.1446327233373472e-07
[342/2000] max fitness: -1.1041190440963595e-07
[343/2000] max fitness: -1.0650393254405723e-07
[344/2000] max fitness: -1.0273428130642886e-07
[345/2000] max fitness: -9.909805491247213e-08
[346/2000] max fitness: -9.559053085824871e-08
[347/2000] max fitness: -9.220715379137703e-08
[348/2000] max fitness: -8.89435295944308e-08
[349/2000] max fitness: -8.579541967585278e-08
[350/2000] max fitness: -8.275873546956476e-08
[351/2000] max fitness: -7.982953311917296e-08
[352/2000] max fitness: -7.700400836150401e-08
[353/2000] max fitness: -7.427849158087947e-08
[354/2000] max fitness: -7.164944304788966e-08
[355/2000] max fitness: -6.911344831822495e-08
[356/2000] max fitness: -6.666721380181088e-08
[357/2000] max fitness: -6.430756248200645e-08
[358/2000] max fitness: -6.20314297921591e-08
[359/2000] max fitness: -5.983585963377841e-08
[360/2000] max fitness: -5.7718000537897236e-08
[361/2000] max fitness: -5.567510196176085e-08
[362/2000] max fitness: -5.370451071748896e-08
[363/2000] max fitness: -5.180366752387463e-08
[364/2000] max fitness: -4.997010368529521e-08
[365/2000] max fitness: -4.8201437884713605e-08
[366/2000] max fitness: -4.6495373088674334e-08
[367/2000] max fitness: -4.48496935675358e-08
[368/2000] max fitness: -4.3262262016229374e-08
[369/2000] max fitness: -4.1731016778293805e-08
[370/2000] max fitness: -4.0253969169037175e-08
[371/2000] max fitness: -3.8829200890151206e-08
[372/2000] max fitness: -3.7454861542842e-08
[373/2000] max fitness: -1.6691693147125976e-08
[374/2000] max fitness: -1.625275614390718e-08
[375/2000] max fitness: -1.5989836491795227e-08
[376/2000] max fitness: -1.5732574774806953e-08
[377/2000] max fitness: -1.547946403497131e-08
[378/2000] max fitness: -1.523042552290294e-08
[379/2000] max fitness: -1.498539362202537e-08
[380/2000] max fitness: -1.4744303871817323e-08
[381/2000] max fitness: -1.45070928494566e-08
[382/2000] max fitness: -1.4273698153136346e-08
[383/2000] max fitness: -1.4044058384567943e-08
[384/2000] max fitness: -1.3818113133156008e-08
[385/2000] max fitness: -1.3595802960229045e-08
[386/2000] max fitness: -1.3377069383920294e-08
[387/2000] max fitness: -1.3161854862325997e-08
[388/2000] max fitness: -1.2950102779911614e-08
[389/2000] max fitness: -1.274175743200611e-08
[390/2000] max fitness: -1.2536764010004577e-08
[391/2000] max fitness: -1.233506858703702e-08
[392/2000] max fitness: -1.2136618103678079e-08
[393/2000] max fitness: -1.1941360354387309e-08
[394/2000] max fitness: -1.1749243973400464e-08
[395/2000] max fitness: -1.1560218421612713e-08
[396/2000] max fitness: -1.1374233972912588e-08
[397/2000] max fitness: -1.1191241700595891e-08
[398/2000] max fitness: -1.1011193465857509e-08
[399/2000] max fitness: -1.0834041904044162e-08
[400/2000] max fitness: -1.0659740412701253e-08
[401/2000] max fitness: -1.0488243138666934e-08
[402/2000] max fitness: -1.0319504966940762e-08
[403/2000] max fitness: -1.0153481508515973e-08
[404/2000] max fitness: -9.9901290881651e-09
[405/2000] max fitness: -9.829404732972476e-09
[406/2000] max fitness: -9.671266161979988e-09
[407/2000] max fitness: -9.515671774456158e-09
[408/2000] max fitness: -9.362580638374263e-09
[409/2000] max fitness: -9.211952481037743e-09
[410/2000] max fitness: -9.063747677070932e-09
[411/2000] max fitness: -8.917927238901982e-09
[412/2000] max fitness: -8.774452806031646e-09
[413/2000] max fitness: -8.633286635158083e-09
[414/2000] max fitness: -8.494391590292859e-09
[415/2000] max fitness: -8.357731132705495e-09
[416/2000] max fitness: -8.223269311849164e-09
[417/2000] max fitness: -8.090970755429702e-09
[418/2000] max fitness: -7.960800659812577e-09
[419/2000] max fitness: -7.832724781959455e-09
[420/2000] max fitness: -7.706709429356803e-09
[421/2000] max fitness: -7.582721451480445e-09
[422/2000] max fitness: -7.46072823165958e-09
[423/2000] max fitness: -7.340697677032532e-09
[424/2000] max fitness: -7.2225982120593765e-09
[425/2000] max fitness: -7.106398768541265e-09
[426/2000] max fitness: -6.992068778502524e-09
[427/2000] max fitness: -6.879578165471779e-09
[428/2000] max fitness: -6.768897336872407e-09
[429/2000] max fitness: -6.659997176404416e-09
[430/2000] max fitness: -6.552849036150805e-09
[431/2000] max fitness: -6.447424729245053e-09
[432/2000] max fitness: -6.343696521687218e-09
[433/2000] max fitness: -6.24163712643932e-09
[434/2000] max fitness: -6.141219695029289e-09
[435/2000] max fitness: -6.042417811138361e-09
[436/2000] max fitness: -5.945205482935335e-09
[437/2000] max fitness: -5.849557137631079e-09
[438/2000] max fitness: -5.7554476130058175e-09
[439/2000] max fitness: -5.6628521522275494e-09
[440/2000] max fitness: -5.571746396487368e-09
[441/2000] max fitness: -5.482106378859338e-09
[442/2000] max fitness: -5.3939085182381405e-09
[443/2000] max fitness: -5.307129612600098e-09
[444/2000] max fitness: -5.221746833489565e-09
[445/2000] max fitness: -5.137737719427379e-09
[446/2000] max fitness: -5.0550801705540985e-09
[447/2000] max fitness: -4.973752442537899e-09
[448/2000] max fitness: -4.8937331407498816e-09
[449/2000] max fitness: -4.815001214771312e-09
[450/2000] max fitness: -4.737535953030211e-09
[451/2000] max fitness: -4.661316976930218e-09
[452/2000] max fitness: -4.586324235696143e-09
[453/2000] max fitness: -4.5125380013813325e-09
[454/2000] max fitness: -4.439938863473767e-09
[455/2000] max fitness: -4.368507723426386e-09
[456/2000] max fitness: -4.298225790033011e-09
[457/2000] max fitness: -4.2290745745759115e-09
[458/2000] max fitness: -4.161035885799362e-09
[459/2000] max fitness: -4.0940918248713635e-09
[460/2000] max fitness: -4.028224781303933e-09
[461/2000] max fitness: -3.963417427604301e-09
[462/2000] max fitness: -3.899652714947072e-09
[463/2000] max fitness: -3.836913869221072e-09
[464/2000] max fitness: -3.775184385794517e-09
[465/2000] max fitness: -3.71444802592785e-09
[466/2000] max fitness: -3.654688811800248e-09
[467/2000] max fitness: -3.595891022732023e-09
[468/2000] max fitness: -3.5380391910969327e-09
[469/2000] max fitness: -3.481118097957549e-09
[470/2000] max fitness: -3.4251127692095538e-09
[471/2000] max fitness: -3.3700084721559028e-09
[472/2000] max fitness: -3.3157907103039982e-09
[473/2000] max fitness: -3.2624452210134384e-09
[474/2000] max fitness: -3.2099579709054813e-09
[475/2000] max fitness: -3.158315152165346e-09
[476/2000] max fitness: -3.1075031794643525e-09
[477/2000] max fitness: -3.0575086859038987e-09
[478/2000] max fitness: -3.0083185194153266e-09
[479/2000] max fitness: -2.9599197400204536e-09
[480/2000] max fitness: -2.9122996154743236e-09
[481/2000] max fitness: -2.865445618450185e-09
[482/2000] max fitness: -2.819345423459024e-09
[483/2000] max fitness: -2.773986902917393e-09
[484/2000] max fitness: -2.729358124690831e-09
[485/2000] max fitness: -2.685447348236784e-09
[486/2000] max fitness: -2.642243022305178e-09
[487/2000] max fitness: -2.5997337812345104e-09
[488/2000] max fitness: -2.5579084422587345e-09
[489/2000] max fitness: -2.5167560025731218e-09
[490/2000] max fitness: -2.4762656362675388e-09
[491/2000] max fitness: -2.4364266917761875e-09
[492/2000] max fitness: -2.397228688951228e-09
[493/2000] max fitness: -2.3586613159513975e-09
[494/2000] max fitness: -2.320714426954067e-09
[495/2000] max fitness: -2.2833780395758635e-09
[496/2000] max fitness: -2.2466423318332715e-09
[497/2000] max fitness: -2.210497639645818e-09
[498/2000] max fitness: -2.1749344547731967e-09
[499/2000] max fitness: -2.1399434215716244e-09
[500/2000] max fitness: -2.1055153352424594e-09
[501/2000] max fitness: -2.0716411389078588e-09
[502/2000] max fitness: -2.0383119211748223e-09
[503/2000] max fitness: -2.005518914491199e-09
[504/2000] max fitness: -1.9732534921539953e-09
[505/2000] max fitness: -1.9415071661575954e-09
[506/2000] max fitness: -1.9102715850195973e-09
[507/2000] max fitness: -1.8795385317515225e-09
[508/2000] max fitness: -1.849299921547426e-09
[509/2000] max fitness: -1.8195477997668847e-09
[510/2000] max fitness: -1.79027433949349e-09
[511/2000] max fitness: -1.7614718399022004e-09
[512/2000] max fitness: -1.73313272401909e-09
[513/2000] max fitness: -1.7052495367462794e-09
[514/2000] max fitness: -1.677814943077986e-09
[515/2000] max fitness: -1.6508217258806947e-09
[516/2000] max fitness: -1.6242627840335822e-09
[517/2000] max fitness: -1.5981311308737732e-09
[518/2000] max fitness: -1.572419891973697e-09
[519/2000] max fitness: -1.5471223036503454e-09
[520/2000] max fitness: -1.5222317109040215e-09
[521/2000] max fitness: -1.4977415659300303e-09
[522/2000] max fitness: -1.4736454259523138e-09
[523/2000] max fitness: -1.4499369524769937e-09
[524/2000] max fitness: -1.4266099084223682e-09
[525/2000] max fitness: -1.4036581572252023e-09
[526/2000] max fitness: -1.3810756609748384e-09
[527/2000] max fitness: -1.3588564789984338e-09
[528/2000] max fitness: -1.3369947663677034e-09
[529/2000] max fitness: -1.3154847718688517e-09
[530/2000] max fitness: -1.2943208369514355e-09
[531/2000] max fitness: -1.2734973940760493e-09
[532/2000] max fitness: -1.2530089654229185e-09
[533/2000] max fitness: -1.2328501610226143e-09
[534/2000] max fitness: -1.2130156778765891e-09
[535/2000] max fitness: -1.193500298123723e-09
[536/2000] max fitness: -1.1742988880035214e-09
[537/2000] max fitness: -1.1554063963234818e-09
[538/2000] max fitness: -1.136817852902419e-09
[539/2000] max fitness: -1.118528367878699e-09
[540/2000] max fitness: -1.1005331298803043e-09
[541/2000] max fitness: -1.0828274049278086e-09
[542/2000] max fitness: -1.0654065352370475e-09
[543/2000] max fitness: -1.0482659380540415e-09
[544/2000] max fitness: -1.0314011042155286e-09
[545/2000] max fitness: -1.0148075971007388e-09
[546/2000] max fitness: -9.984810516597952e-10
[547/2000] max fitness: -9.82417172830671e-10
[548/2000] max fitness: -9.666117347450632e-10
[549/2000] max fitness: -9.510605795456356e-10
[550/2000] max fitness: -9.357596162133732e-10
[551/2000] max fitness: -9.207048196771598e-10
[552/2000] max fitness: -9.058922294064902e-10
[553/2000] max fitness: -8.913179488415379e-10
[554/2000] max fitness: -8.769781438969066e-10
[555/2000] max fitness: -8.62869042244757e-10
[556/2000] max fitness: -8.48986932322007e-10
[557/2000] max fitness: -8.3532816213376e-10
[558/2000] max fitness: -8.21889138569885e-10
[559/2000] max fitness: -8.086663263281516e-10
[560/2000] max fitness: -7.956562467770255e-10
[561/2000] max fitness: -7.828554775030776e-10
[562/2000] max fitness: -7.702606510782971e-10
[563/2000] max fitness: -7.578684542134871e-10
[564/2000] max fitness: -7.456756269415859e-10
[565/2000] max fitness: -7.336789617254456e-10
[566/2000] max fitness: -7.218753025932323e-10
[567/2000] max fitness: -7.102615444754831e-10
[568/2000] max fitness: -6.988346321785057e-10
[569/2000] max fitness: -6.875915596446977e-10
[570/2000] max fitness: -6.765293692029319e-10
[571/2000] max fitness: -6.656451507940306e-10
[572/2000] max fitness: -6.54936041132038e-10
[573/2000] max fitness: -6.443992230593293e-10
[574/2000] max fitness: -6.340319246068179e-10
[575/2000] max fitness: -6.23831418576784e-10
[576/2000] max fitness: -6.137950215103696e-10
[577/2000] max fitness: -6.039200931582604e-10
[578/2000] max fitness: -5.94204035773774e-10
[579/2000] max fitness: -5.84644293384042e-10
[580/2000] max fitness: -5.752383511498428e-10
[581/2000] max fitness: -5.659837347547347e-10
[582/2000] max fitness: -5.568780095034443e-10
[583/2000] max fitness: -5.47918780025669e-10
[584/2000] max fitness: -5.391036894463954e-10
[585/2000] max fitness: -5.304304188550883e-10
[586/2000] max fitness: -5.218966865954501e-10
[587/2000] max fitness: -5.13500247696686e-10
[588/2000] max fitness: -5.052388933310334e-10
[589/2000] max fitness: -4.971104503121546e-10
[590/2000] max fitness: -4.891127802370185e-10
[591/2000] max fitness: -4.812437791820063e-10
[592/2000] max fitness: -4.735013770855136e-10
[593/2000] max fitness: -4.658835371953597e-10
[594/2000] max fitness: -4.583882555566997e-10
[595/2000] max fitness: -4.510135603434303e-10
[596/2000] max fitness: -4.437575116178432e-10
[597/2000] max fitness: -4.3661820047796914e-10
[598/2000] max fitness: -4.29593748806338e-10
[599/2000] max fitness: -4.226823087976939e-10
[600/2000] max fitness: -4.158820621994119e-10
[601/2000] max fitness: -4.091912201180289e-10
[602/2000] max fitness: -4.0260802238362535e-10
[603/2000] max fitness: -3.961307372188651e-10
[604/2000] max fitness: -3.8975766068101684e-10
[605/2000] max fitness: -3.8348711621832305e-10
[606/2000] max fitness: -3.7731745423622907e-10
[607/2000] max fitness: -3.7124705173998794e-10
[608/2000] max fitness: -3.652743118104861e-10
[609/2000] max fitness: -3.593976632133577e-10
[610/2000] max fitness: -3.536155600121326e-10
[611/2000] max fitness: -3.4792648109135087e-10
[612/2000] max fitness: -3.423289298574463e-10
[613/2000] max fitness: -3.3682143380750863e-10
[614/2000] max fitness: -3.314025440977303e-10
[615/2000] max fitness: -3.2607083518895673e-10
[616/2000] max fitness: -3.208249044732012e-10
[617/2000] max fitness: -3.156633719568518e-10
[618/2000] max fitness: -3.105848798397484e-10
[619/2000] max fitness: -3.055880920826457e-10
[620/2000] max fitness: -3.006716942103424e-10
[621/2000] max fitness: -2.958343929553578e-10
[622/2000] max fitness: -2.910749157034731e-10
[623/2000] max fitness: -2.8639201045612775e-10
[624/2000] max fitness: -2.817844452603621e-10
[625/2000] max fitness: -2.772510079925149e-10
[626/2000] max fitness: -2.727905061238534e-10
[627/2000] max fitness: -2.6840176621749507e-10
[628/2000] max fitness: -2.6408363374087487e-10
[629/2000] max fitness: -2.598349727653297e-10
[630/2000] max fitness: -2.556546655860272e-10
[631/2000] max fitness: -2.5154161249639136e-10
[632/2000] max fitness: -2.474947315341221e-10
[633/2000] max fitness: -2.435129580462551e-10
[634/2000] max fitness: -2.395952446055638e-10
[635/2000] max fitness: -2.3574056055440107e-10
[636/2000] max fitness: -2.3194789187242663e-10
[637/2000] max fitness: -2.282162408504499e-10
[638/2000] max fitness: -2.245446257984218e-10
[639/2000] max fitness: -2.209320808906968e-10
[640/2000] max fitness: -2.173776556999241e-10
[641/2000] max fitness: -2.13880415266346e-10
[642/2000] max fitness: -2.1043943951289726e-10
[643/2000] max fitness: -2.0705382326344675e-10
[644/2000] max fitness: -2.037226759022755e-10
[645/2000] max fitness: -2.0044512108493895e-10
[646/2000] max fitness: -1.972202965661826e-10
[647/2000] max fitness: -1.9404735406418813e-10
[648/2000] max fitness: -1.9092545888619794e-10
[649/2000] max fitness: -1.8785378974528085e-10
[650/2000] max fitness: -1.8483153858438846e-10
[651/2000] max fitness: -1.8185791035491208e-10
[652/2000] max fitness: -1.789321228076525e-10
[653/2000] max fitness: -1.7605340624243192e-10
[654/2000] max fitness: -1.7322100339122778e-10
[655/2000] max fitness: -1.7043416912492994e-10
[656/2000] max fitness: -1.676921703260828e-10
[657/2000] max fitness: -1.649942856313875e-10
[658/2000] max fitness: -1.6233980541696137e-10
[659/2000] max fitness: -1.5972803129080065e-10
[660/2000] max fitness: -1.5715827625034982e-10
[661/2000] max fitness: -1.546298641958225e-10
[662/2000] max fitness: -1.5214213006243568e-10
[663/2000] max fitness: -1.4969441936874993e-10
[664/2000] max fitness: -1.4728608821984088e-10
[665/2000] max fitness: -1.4491650307104668e-10
[666/2000] max fitness: -1.4258504055038678e-10
[667/2000] max fitness: -1.40291087355453e-10
[668/2000] max fitness: -1.380340399909784e-10
[669/2000] max fitness: -1.3581330472319847e-10
[670/2000] max fitness: -1.3362829733242112e-10
[671/2000] max fitness: -1.3147844305111633e-10
[672/2000] max fitness: -1.2936317629758252e-10
[673/2000] max fitness: -1.272819406061958e-10
[674/2000] max fitness: -1.252341885061376e-10
[675/2000] max fitness: -1.2321938130627622e-10
[676/2000] max fitness: -1.212369889582592e-10
[677/2000] max fitness: -1.1928648997044865e-10
[678/2000] max fitness: -1.1736737120365566e-10
[679/2000] max fitness: -1.1547912783011874e-10
[680/2000] max fitness: -1.1362126311661775e-10
[681/2000] max fitness: -1.1179328831013425e-10
[682/2000] max fitness: -1.0999472256108432e-10
[683/2000] max fitness: -1.0822509269563585e-10
[684/2000] max fitness: -1.0648393318462806e-10
[685/2000] max fitness: -1.0477078599038801e-10
[686/2000] max fitness: -1.0308520044321419e-10
[687/2000] max fitness: -1.0142673315538603e-10
[688/2000] max fitness: -9.979494780444712e-11

After finishing the evolution, we plot the result and log the final evolved expression.

width = 9.0
fig = plt.figure(figsize=(width, width / scipy.constants.golden))

ax_fitness = fig.add_subplot(121)
ax_fitness.set_xlabel("Generation")
ax_fitness.set_ylabel("Fitness")
ax_fitness.set_yscale("symlog")

ax_function = fig.add_subplot(122)
ax_function.set_ylabel(r"$f(x)$")
ax_function.set_xlabel(r"$x$")


print(f"Final expression {pop.champion.to_sympy()} with fitness {pop.champion.fitness}")

history_fitness = np.array(history["fitness_parents"])
ax_fitness.plot(np.max(history_fitness, axis=1), label="Champion")
ax_fitness.plot(np.mean(history_fitness, axis=1), label="Population mean")

x = np.linspace(-5.0, 5, 100).reshape(-1, 1)
f = pop.champion.to_func()
y = [f(xi) for xi in x]
ax_function.plot(x, f_target(x), lw=2, label="Target")
ax_function.plot(x, y, lw=1, label="Target", marker="x")

plt.savefig("example_differential_evo_regression.pdf", dpi=300)
example differential evo regression

Out:

Final expression 1.0000010246551405*x_0**2 + 4.141577802503509 with fitness -9.979494780444712e-11

Total running time of the script: ( 0 minutes 25.363 seconds)

Gallery generated by Sphinx-Gallery