How to run a genetic algorithm in GNU Octave, two ways
A genetic algorithm finds good answers by breeding guesses. Here are two real runs in GNU Octave on an awx computer: one we wrote and ran ourselves in the dashboard, and one Claude Code wrote and ran through the awx MCP server.
Every number and screenshot below comes from those two runs.
What a genetic algorithm does
A genetic algorithm searches for the best answer the way breeding works. It starts with a population of random guesses, scores each one with a fitness function, and builds the next generation from the better ones:
- Selection: pick parents, favouring the fitter guesses (here, the better of two picked at random: a tournament).
- Crossover: mix two parents into a child.
- Mutation: now and then, nudge a child at random, so the search can jump to places no parent was near.
- Elitism: copy the best guess unchanged, so the best answer never gets worse.
Repeat for some generations and the population gathers around the best answer. It is a common topic in optimisation, AI and engineering courses.
What you need
- An
awxcomputer with GNU Octave. If you have not installed it yet, follow Use GNU Octave on awanbox first (one line, under a minute). We used the Large computeroctave-labfrom that post. - For the second way: Claude Code on your laptop and an awx API key.
Way 1: write it yourself, in the dashboard
The problem: find the highest point of f(x) = x·sin(10πx) + 1 for x between −1 and 2. The curve has many peaks of different heights, so a search that only climbs uphill gets stuck on the first one it finds. The highest is about 2.85, near x = 1.85.
Save this as ga_peak.m on your laptop:
% ga_peak.m: a genetic algorithm finds the highest point of
% f(x) = x*sin(10*pi*x) + 1 for x between -1 and 2
rand("seed", 7); randn("seed", 7);
f = @(x) x .* sin(10*pi*x) + 1;
lo = -1; hi = 2;
N = 40; G = 50; pm = 0.3; % population, generations, mutation chance
pop = lo + (hi - lo) * rand(N, 1); % generation 0: random guesses
best = zeros(G, 1);
for g = 1:G
fit = f(pop);
[~, i] = max(fit); elite = pop(i);
best(g) = fit(i);
kids = zeros(N, 1); kids(1) = elite; % keep the best one
for k = 2:N
a = randi(N, 2, 1); b = randi(N, 2, 1); % two tournaments
[~, ia] = max(fit(a)); [~, ib] = max(fit(b));
w = rand; % crossover: a mix
kids(k) = w * pop(a(ia)) + (1 - w) * pop(b(ib));
if rand < pm % mutation: a nudge
kids(k) = kids(k) + 0.3 * randn;
end
end
pop = min(max(kids, lo), hi);
end
[fb, i] = max(f(pop));
printf("Best x = %.4f, f(x) = %.4f after %d generations\n", pop(i), fb, G);
figure();
subplot(1, 2, 1);
xs = linspace(lo, hi, 1000);
plot(xs, f(xs), pop, f(pop), "ro", "MarkerFaceColor", "r"); grid on;
title("The last generation (red)"); xlabel("x"); ylabel("f(x)");
subplot(1, 2, 2);
plot(1:G, best, "LineWidth", 2); grid on;
title("Best in each generation"); xlabel("generation"); ylabel("best f(x)");
print("-dpng", "-S900,400", "ga_peak.png");
Each line in the loop is one of the steps above: two tournaments pick the parents, w mixes them, a 30% chance adds a random nudge, and kids(1) = elite keeps the best.
Upload it on the Files tab
Open your computer, go to the Files tab, open the octave folder and press Upload:
~/octave on the Files tab.Run it in the Terminal tab
cd ~/octave
export DISPLAY=:0
time octave-cli ga_peak.m 2>/dev/null
x = 1.8505, f(x) = 2.8503.export DISPLAY=:0 lets Octave draw on the computer’s own desktop, which it needs to save a plot. 2>/dev/null hides a harmless font warning. The plot it saved, ga_peak.png, which you can download from the Files tab:
Try this: make it fail
Our first version used a mutation chance of 0.2 and a nudge of 0.1 * randn. With the same seed it stopped at 2.45, on a lower peak near x = 1.45: the nudges were too small to jump across to a higher peak. Raising both to 0.3 found the top (2.8503) with ten seeds out of ten. Change the two numbers and the seed, run it again, and watch the right-hand plot: this is the main thing to learn about tuning a genetic algorithm.
Way 2: ask Claude Code, through the awx MCP server
The awx MCP server lets an AI agent on your laptop use awx computers directly: make one, run commands, write files, take screenshots and delete it. We gave Claude Code a harder problem and let it do the whole job, including using Octave’s own ga function from the octave-ga package.
Connect Claude Code to awx
On box.awanbox.com, open Account, then API keys, and press Create key. The key is shown once. Then, in a terminal on your laptop, in an empty folder:
claude mcp add --transport http awx https://mcp.awanbox.com/mcp \
--header "Authorization: Bearer <your key>"
claude mcp list
claude mcp list should show awx ... Connected. The same command is on the API keys page, ready to copy.
The request
The problem is the Rastrigin function of two variables, a standard test for optimisers: a bowl covered in hundreds of small dips, with the lowest point, 0, at (0, 0). This was our exact request to Claude Code:
Using the awx tools, make a Small computer named octave-ga. On it, install GNU Octave and the Octave genetic algorithm package (the Ubuntu package octave-ga). In a folder ~/ga, write an Octave script rastrigin_ga.m that uses the ga function from that package to find the lowest point of the Rastrigin function of two variables, f(x, y) = 20 + x^2 + y^2 - 10*(cos(2*pi*x) + cos(2*pi*y)), with x and y between -5.12 and 5.12. Make the run repeatable with a fixed random seed. The script should print the best x, y and f(x, y), draw a contour plot of the function with the answer marked, and save it as rastrigin.png (plots need DISPLAY=:0). Run it. Then open rastrigin.png on the computer's desktop and take a screenshot so I can see it. When that is done, delete the computer octave-ga: I give you permission to delete that one computer, and nothing else. Finally, tell me what you did, the answer, and how long each step took.
We allowed it only the awx tools, so it could act on awx computers and nothing on the laptop.
What the agent did
It took 18 steps and 2 minutes 55 seconds:
- Made the Small computer
octave-ga(half a CPU, 1 GiB). - Installed Octave 8.4.0 and
octave-ga0.10.3 withapt-get, about 85 seconds. Its first try waited too long in one command and hit the 85-second limit for a single command; the install carried on, and it checked withpkg load ga. - Wrote
~/ga/rastrigin_ga.mwithwrite_file. - Ran it with
DISPLAY=:0: 26 seconds on the small computer, a population of 100 for 200 generations, plus a detailed contour plot. - Opened the plot on the desktop and took a screenshot (two bumps, below).
- Deleted
octave-ga, the only computer it had permission to delete.
The script it wrote:
% Minimise the 2-variable Rastrigin function with the Octave ga package.
pkg load ga;
% Fixed seeds make the run repeatable
rand("twister", 42);
randn("state", 42);
f = @(v) 20 + v(1)^2 + v(2)^2 - 10*(cos(2*pi*v(1)) + cos(2*pi*v(2)));
lb = [-5.12 -5.12];
ub = [ 5.12 5.12];
opts = gaoptimset("Generations", 200, "PopulationSize", 100);
[x, fval] = ga(f, 2, [], [], [], [], lb, ub, [], opts);
printf("Best x = %.6f\n", x(1));
printf("Best y = %.6f\n", x(2));
printf("f(x,y) = %.6g\n", fval);
% Contour plot with the answer marked
[X, Y] = meshgrid(linspace(-5.12, 5.12, 400));
Z = 20 + X.^2 + Y.^2 - 10*(cos(2*pi*X) + cos(2*pi*Y));
h = figure("visible", "on");
contourf(X, Y, Z, 30);
colorbar; hold on;
plot(x(1), x(2), "rp", "markersize", 14, "markerfacecolor", "r");
text(x(1)+0.2, x(2)+0.4, sprintf("best (%.4f, %.4f)\nf=%.3g", x(1), x(2), fval), ...
"color", "w", "fontweight", "bold");
xlabel("x"); ylabel("y");
title("Rastrigin function - GA minimum");
axis equal tight;
print(h, "rastrigin.png", "-dpng", "-r100");
drawnow;
It printed Best x = -0.000000, Best y = -0.000000, f(x,y) = 0: the true lowest point. The screenshot it took of the computer’s desktop:
Two bumps on the way
- Opening the picture. Opening a PNG on the computer starts Chrome. When the agent started Chrome with a fresh profile, Chrome asked it to accept Google’s terms. On an
awxcomputer, that is the agent’s job: Chrome comes with the computer, ready for the agent to use, so the agent may press Accept. Ours was more cautious than it needed to be: it pressed Cancel and showed the picture in an Octave window instead. - A command that stopped itself. One of its commands used
pkill -f "octave --gui", which matched its own shell and ended it. It noticed, said so, and tried again more carefully.
Its report also said, honestly, that it had not run the script twice to prove the seed gives the same answer, and that deleting the computer deleted the script and the plot. If you want to keep them, ask the agent to copy them out (for example with read_file, or to your file app) before it deletes the computer.
When we were done, we revoked the API key on the Account page. Do the same with any key you no longer use.
Which way to choose
- Learning how a genetic algorithm works: write it yourself (way 1). Changing the mutation and watching it fail teaches more than any answer.
- Getting a job done, such as comparing settings over many runs or trying a toolbox you have not used: let an agent do it through MCP (way 2), then read the script it wrote.
- Either way, check the answer. Here we knew the true best point for both problems; in your own work, try a few seeds and see that they agree.
Common questions
Does GNU Octave have a ga function like MATLAB?
Yes, through the Octave ga package. On an awx computer, install it with sudo apt-get install -y octave-ga and load it with pkg load ga. Its ga and gaoptimset work like MATLAB's for simple problems.
Why did my genetic algorithm stop at the wrong answer?
A genetic algorithm can settle on a lower peak when its mutation is too weak to jump to a better one. In our first run, a mutation chance of 0.2 and a nudge of 0.1 stopped at 2.45 instead of 2.85; a chance of 0.3 and a nudge of 0.3 found the top with ten different seeds out of ten.
How do I make a genetic algorithm give the same answer every run?
Fix the random seed at the top of the script, for example rand("seed", 7) and randn("seed", 7), or rand("twister", 42) and randn("state", 42) as the agent did.
What is the awx MCP server?
A way for an AI agent such as Claude Code to use awx computers directly: make one, run commands, write files, take screenshots and delete it. You add it to Claude Code with one command and an API key from the Account page.
Cautions
- Running time uses credit on the Free plan. Small is enough for these scripts; the agent’s run took 26 seconds on Small and the hand-written one under a second on Large.
- An agent acts with your key. Say exactly what it may delete, give it only the awx tools, and revoke the key when you are done.
- Check before you hand it in. Read and understand any code an agent writes for coursework, and follow your course’s rules on AI help.
- Keep your own copy of scripts and results.
awxis in early alpha.
Keep reading
- How to use GNU Octave on awanbox, in your browser
- How to let Claude Code use awx from your terminal
- For LLM agents: use awx from your terminal
- Small or Large: which size to choose
Getting started
Sign in with your Google account, make a computer, install Octave, and try way 1.
Awi, signing off.