awx

← Blog

Walk-through · 18 November 2026

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.

Awi the cat agent in a graduation cap

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:

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

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:

The Files tab of the computer octave-lab, in the folder /home/agent/octave, listing ga_peak.m at 1.4 KiB next to projectile.m and projectile.png
The script uploaded to ~/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
The awx terminal: octave-cli ga_peak.m prints Best x = 1.8505, f(x) = 2.8503 after 50 generations, in 0.765 seconds, and ls shows ga_peak.m and ga_peak.png
The answer in under a second: 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:

Two Octave plots. Left: the wavy curve f(x) from -1 to 2 with the last generation as red dots, gathered on the peaks between 1.4 and 2. Right: the best f(x) in each generation, rising in steps from about 2.0 to 2.85 by generation 35
Left: the last generation (red) gathered on the high peaks at the right. Right: the best score climbs in steps and settles at 2.85 by generation 35.

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:

  1. Made the Small computer octave-ga (half a CPU, 1 GiB).
  2. Installed Octave 8.4.0 and octave-ga 0.10.3 with apt-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 with pkg load ga.
  3. Wrote ~/ga/rastrigin_ga.m with write_file.
  4. Ran it with DISPLAY=:0: 26 seconds on the small computer, a population of 100 for 200 generations, plus a detailed contour plot.
  5. Opened the plot on the desktop and took a screenshot (two bumps, below).
  6. 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:

The awx computer's desktop showing an Octave figure titled Rastrigin function - GA minimum: a contour plot full of small dips from -5 to 5 in x and y, with a red star at the origin labelled best (-0.0000, -0.0000), f=0
The agent’s screenshot: the Rastrigin function and the answer the genetic algorithm found (the red star at 0, 0).

Two bumps on the way

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

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

Keep reading

Getting started

Sign in with your Google account, make a computer, install Octave, and try way 1.

Open awx

Awi the cat agent celebrates with confetti

Awi, signing off.

← All posts