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