Genetic algorithm simulator: the stalagmite function, generation by generation
Watch a genetic algorithm search a landscape of 25 narrow peaks for the highest one. Pick bounds, population size, elites and crossover fraction, play the run generation by generation, then repeat it 50 times to see how often it really finds the global maximum. It opens on the stalagmite challenge of Skill-Lync's MATLAB course, with MATLAB ga's default settings, and checks the function value and the x, y, fval your own code printed. Free, no sign-up, runs in your browser.
Reproduced in Python and TypeScript, bit for bit, on 400 seeds per case · Updated 20 Nov 2026
MATLAB's defaults for 2 variables: population 50, 3 elites, crossover fraction 0.8, 200 generations, stop after 50 generations without progress. With no bounds the start population covers −10 to 10.
Runs the current settings 50 times (50 seeds, starting from the one in the Seed box), like the course's statistical study, and plots where every run ended: white found the maximum, orange did not.
Fill in what you have. The function value at (0.25, 0.25) identifies the typos we found in the reports; the x, y, fval tell which peak your run reached.
- Seed 1, bounds 0 to 1, population 50: 3 elites, 38 crossover and 9 mutation children per generation.
What is the stalagmite function challenge?
It is the genetic algorithm challenge of Skill-Lync's MATLAB course: write the stalagmite function, use MATLAB's ga to find its global maximum, and study how the answer changes over many runs, with and without bounds and with a bigger population. 2,229 public student reports answer it. We read all of them on 20 Nov 2026; 1,850 include ga code. 885 set a run count of 50, 893 use an upper bound of 1 and 709 one of 0.6, and the most common population sizes are 300 (307 reports), 170 (303 reports), 200 (271 reports).
This simulator runs the same study in your browser. It is not MATLAB: it uses MATLAB's documented default operators and settings, but its own random numbers, so your exact numbers will differ from MATLAB's. The pattern does not.
The stalagmite function and its peaks
The function is a product of the same curve in x and in y, so every peak of the surface sits above a peak of g(x) and a peak of g(y). The sine term, raised to the 6th power, makes sharp peaks where sin = ±1:
The Gaussian term is centred on 0.0667 and falls to half at 0.4 either side, so peaks further from the centre are shorter:
Between 0 and 1 there are 5 peaks in x: at 0.0668, 0.2618, 0.4568, 0.6518, 0.8468, with heights 1.000, 0.847, 0.515, 0.225, 0.071. So the square 0 to 1 holds 25 peaks. The global maximum is f = 1.0000 at x = y = 0.0668 (exactly 0.9999998; it is just under 1 because the sine peak at 0.0668 and the Gaussian centre at 0.0667 do not quite coincide). The two runners-up, at (0.0668, 0.2618) and its mirror, reach 0.847.
Why the unbounded run lands somewhere different every time
Without bounds MATLAB draws the start population from −10 to 10 in each variable. Every hill sits within about 1 of the centre 0.0667: further out the Gaussian factor is below 0.013. Only 0.18% of the 20 × 20 start square has f above 0.01, so a start population of 50 has a 92% chance that not one of its points has f above 0.01. Every individual scores about zero, rank scaling still picks "winners", and the search wanders until a mutation happens to land on a slope. It then climbs whichever peak that is.
In our simulator, 1 of 400 unbounded runs with the default population of 50 found the global maximum (best f above 0.999), 83 ended on its hill at all, and the median run ended at f = 0.512. Even the runs that find the right hill mostly stop on its slope: the mutation step starts 20 wide, far too coarse to climb a peak 0.1 across, and crossover alone refines slowly. A population of 300 finds the right hill more often, 236 of 400 runs, but only 15 climb to its top: more individuals help find the hill, they do not make the mutation step finer. That is the scatter the reports describe as "all over the place". It is the method working as designed on a bad search box, not a bug in your code.
Bounds and population size: 400 runs per setting
We ran every setting 400 times (seeds 1 to 400) with otherwise default options and asked two questions of each run. Did it end on the right hill, the global peak at (0.0668, 0.0668), and not one of the other peaks? And did it climb to the top, best f above 0.999 (the next peak is only 0.847)?
| Setting | On the global peak's hill, of 400 runs | Reached the top (f > 0.999) | Median best f | Median generations |
|---|---|---|---|---|
| No bounds, population 50 (MATLAB default) | 83 (21%) | 1 (0.3%) | 0.5124 | 110 |
| Bounds 0 to 0.6, population 50 | 399 (99.8%) | 315 (79%) | 0.9998 | 121 |
| Bounds 0 to 1, population 50 | 400 (100%) | 255 (64%) | 0.9996 | 127 |
| Bounds 0 to 1, population 170 | 400 (100%) | 381 (95%) | 0.9999 | 110 |
| Bounds 0 to 1, population 300 | 400 (100%) | 394 (99%) | 1.0000 | 102 |
| No bounds, population 300 | 236 (59%) | 15 (4%) | 0.8734 | 106 |
- Bounds solve the "wrong peak" problem. With x and y kept to 0 to 1, 100% of runs end on the global peak's hill, against 21% without bounds. The start population lands on the hills instead of flat ground, and the highest hill wins.
- What is left is precision. With bounds 0 to 1 and 50 individuals, 145 runs stopped on the right hill but below 0.999 (median 0.9972). The stall rule ends a run once 50 generations improve the best f by less than 10⁻⁶ on average, which can happen before the last digits are found. The median run stopped after 127 of 200 generations.
- A bigger population or a smaller box sharpens the answer. 170 individuals reached the top in 95% of runs and 300 in 99%, at 3.4 and 6 times the function calls per generation. The 0 to 0.6 box gets 79% with 50, because its mutation steps are smaller.
- Read the "top" column with care. How close a run gets to the top depends on the mutation step, which is where this simulator simplifies MATLAB (see below). The "right hill" column depends on the search box. If you need the last digits, finish with a local solver: MATLAB's 'HybridFcn', @fmincon option does this.
What MATLAB's ga does in one generation
With the default population of 50, every generation is built like this:
- Score and rank. Each individual gets f(x, y) (ga minimises −f). Rank scaling gives the best one the most expected offspring, whatever the size of the score gaps.
- Keep 3 elites. The 3 best (5% of 50, rounded up) pass to the next generation unchanged, so the best f never gets worse.
- 38 crossover children. 80% of the remaining 47 places. Parents are picked by stochastic uniform selection: one spin of a wheel with equally spaced pointers. Scattered crossover takes each of x and y from one parent or the other at random.
- 9 mutation children. The rest: a parent plus a random step. Without bounds the step is Gaussian, starting as wide as the start range (20) and shrinking to zero at the last generation. With bounds MATLAB uses an adaptive step that stays inside them.
The simulator does exactly this, with one simplification: with bounds it uses a Gaussian step starting at a quarter of the box width, shrinking the same way, and clipped to the bounds, instead of MATLAB's adaptive feasible mutation. Elite count and crossover fraction are editable above: set elites to 0 and the best of a generation can fall back below the one before.
Mistakes we found in 2,229 reports
Type the value your function returns at (0.25, 0.25) into "Check my numbers" and it names the typo. Correct: 0.57712.
- 0.667 for 0.0667 in the Gaussian (66 reports). It moves the centre tenfold, so ga's answer moves too: the maximum of the mistyped function is at x = y = 0.6551, not 0.0668. At (0.25, 0.25) it gives 0.17115.
- A bound written with a colon (91 reports, 81 of them [0:0]). In MATLAB 0:0 is the single number 0, so [0:0] is one bound for two variables. ga's documentation asks for one entry per variable. One report even says [0:0], [1:1] is the correct way. Write [0 0] or [0;0].
- log10(2) for log(2) (15 reports). In MATLAB log is the natural log, which the formula's ln 2 needs. The peaks stay where they are but the Gaussian gets wider, so the side peaks come out too tall: 0.68044 at (0.25, 0.25).
- 5*pi for 5.1*pi (6 reports) and +.05 for +0.5 (2). Both shift every peak: the global maximum moves to 0.0682 and 0.0948.
- 0.0677 for 0.0667 (9 reports). Harmless: the values change by about 0.3% and the peak does not move at four places.
- No rng call. Only 34 of 1,850 reports with code set the random seed, so most of the plots in the reports cannot be reproduced. rng(1) before the loop fixes that.
MATLAB code for the stalagmite study
The function file, returning −f because ga minimises:
function f = stalagmite(p)
% p = [x y]. ga MINIMISES, so return minus the function.
x = p(1); y = p(2);
g = @(t) sin(5.1*pi*t + 0.5).^6 .* exp(-4*log(2)*(t - 0.0667).^2 / 0.64);
f = -g(x) * g(y);
endThe study: bounds with one entry per variable, a population of 170, a fixed seed, and the answer turned back into a maximum. Swap lb, ub for [], [] to get the unbounded case.
lb = [0 0]; ub = [1 1]; % one entry per variable, not [0:0]
opts = optimoptions('ga', 'PopulationSize', 170);
n = 50; xopt = zeros(n,1); yopt = xopt; fopt = xopt;
rng(1) % same 50 runs every time you run the script
tic
for i = 1:n
[p, fval] = ga(@stalagmite, 2, [], [], [], [], lb, ub, [], opts);
xopt(i) = p(1); yopt(i) = p(2); fopt(i) = -fval; % back to the maximum
end
toc
fprintf('global peak found in %d of %d runs\n', sum(fopt > 0.999), n)What this simulator does not cover
- MATLAB's exact numbers. Same operators and defaults, different random number generator, and a simplified mutation with bounds. Compare success rates and patterns, not digits.
- Other ga options. Integer variables, linear and nonlinear constraints, hybrid functions (fminsearch after ga) and custom operators are not modelled.
- Other test functions. Stalagmite only, since that is the course problem. The same reasoning about search boxes holds for Rastrigin or Ackley.
The stalagmite study is one of the weekly challenges in Skill-Lync's MATLAB course for mechanical engineers, next to ODE solvers, curve fitting, NASA thermodynamic data and the 2R robot arm.
Frequently asked questions
What is the stalagmite function?
A test function with a grid of narrow peaks used in Skill-Lync's MATLAB course to practise optimisation: f(x, y) = g(x)·g(y), with g(x) = sin⁶(5.1πx + 0.5)·exp(−4 ln2 (x − 0.0667)²/0.64). The sine term makes a peak every 0.196 in x and in y; the Gaussian term makes the peaks shorter away from 0.0667. Between 0 and 1 there are 5 × 5 = 25 peaks.
What is the global maximum of the stalagmite function?
f = 1.0000 (1.0000 to four places) at x = y = 0.0668. The next highest peaks, at (0.0668, 0.2618) and the mirror point, reach 0.847. MATLAB's ga minimises, so with the usual f = −stalagmite the answer it prints is fval ≈ −1.0000.
Why does my MATLAB ga give a different answer every run?
Because ga is random: it draws a random start population and random parents, crossovers and mutations every generation. Without bounds the start population is spread over −10 to 10, where the function is almost exactly zero, so runs end on whichever peak they first stumble on. In our simulator with MATLAB's defaults, only 83 of 400 unbounded runs ended on the global peak's hill. Add bounds (0 to 1 puts 100% of runs there) and a bigger population, or call rng(1) before ga to make a run repeat.
What are MATLAB ga's default settings?
For 2 variables: PopulationSize 50, EliteCount 3 (5% of 50, rounded up), CrossoverFraction 0.8, MaxGenerations 200 (100 × the number of variables), MaxStallGenerations 50 and FunctionTolerance 1e-6. Without bounds the start population is drawn from −10 to 10. Selection is stochastic uniform, crossover is scattered, and mutation is Gaussian without bounds or adaptive feasible with bounds.
How do I give bounds to ga in MATLAB?
As the 7th and 8th arguments, one entry per variable: ga(@stalagmite, 2, [], [], [], [], [0 0], [1 1]). Write [0 0] or [0;0], not [0:0]: in MATLAB 0:0 is the single number 0, so [0:0] is one bound for two variables. 91 of the 1,850 reports with ga code write a bound with a colon.
Does a bigger population help a genetic algorithm?
Yes, at the cost of more function calls, but bounds matter more. In our simulator, with bounds 0 to 1, best f above 0.999 was reached in 64% of runs with 50 individuals, 95% with 170 and 99% with 300. Without bounds, 300 individuals put only 59% of runs on the right hill.
Related on Skill-Lync
- Curve fitting calculator (the next challenge in the same course)
- NASA polynomial calculator (the course's parsing project)
- Inverse kinematics calculator (the 2R robot arm challenge)
- Python project ideas for mechanical engineers
- Student project: week 4.1, genetic algorithm
- All free engineering tools
Sources
- MathWorks, ga and "Genetic Algorithm Options" documentation (R2026b, read 20 Nov 2026): default PopulationSize, EliteCount, CrossoverFraction, MaxGenerations, MaxStallGenerations, FunctionTolerance, InitialPopulationRange, selection, crossover, mutation and stopping rule.
- MathWorks, "How the Genetic Algorithm Works": elite, crossover and mutation children per generation.
- D. E. Goldberg, Genetic Algorithms in Search, Optimization and Machine Learning, Addison-Wesley, 1989: selection, crossover and mutation.
- Skill-Lync, all 2,229 public student reports of the genetic algorithm / stalagmite challenge, read 20 Nov 2026: bounds, population sizes, run counts, seeds and the function as coded.
- Skill-Lync, scripts/verify/ga-stalagmite.py: the peaks by golden-section search checked on a dense grid, the typo variants, and the GA run 400 times per setting with the same random numbers as this page. The page re-runs 40 seeds of every setting at build time and must match exactly.