Curve Fitting using Genetic.js

From a plot of vertices, this genetic algorithm optimizes a continious function to best fit the data (aka least squared error). Genetic.js automatically runs the algorithm in the background with web workers so that the UI experience isn’t compromised.

Demo

Given the population parameters below, the simulation will run and interpolate color, transparency, and line-thickness to show how solutions evolve over time. The best-fit solutions are narrow and red. The least fit are large, purple, and semi-transparent.

Generation:

Error (Avg):

Error (Stdev):

Error:

VertError (Avg):

Solution: Press 'Compute' Button

Dataset - Choose - Clear Parabola Linear (with noise) Sinusoidal Sinusoidal (with noise) Iterations 50 100 300 500 1000 2000 Degree 0 (constant) 1 (line) 2 (parabola) 3 (polynomial) 4 (polynomial) Mutation 0.0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0 with Genetic.Select1.Tournament2 Genetic.Select1.Tournament3 Genetic.Select1.Fittest Genetic.Select1.Random Genetic.Select1.RandomLinearRank Genetic.Select1.Sequential Crossover 0.0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0 with Genetic.Select2.Tournament2 Genetic.Select2.Tournament3 Genetic.Select2.Random Genetic.Select2.RandomLinearRank Genetic.Select2.Sequential Genetic.Select2.FittestRandom

Compute

How it Works

This genetic algorithm optimizes the the coefficients of a polynomial to fit a plot of data. Given a polynomial such as: ax4 + bx3 + cx2 + dx + e. The coefficients a, b, c, d, e are randomly seeded and then iteratively selected, mutated, and crossed over a number of generations.

A random stepping drift is applied to entities (represented as an array of coefficients) when selected for mutation. Crossover uses single point interpolation between two parents.

Crossover can be disabled by setting it to 0% (0.0 from the dropdown), which causes the simulation to run as an evolutionary algorithm.

— Sean