DarwinBotsManual
Open the app EspañolES

Execution and costs

When the DNA runs within the cycle, in what order, what it sees of the senses, and how much energy each instruction costs.

A bot's DNA isn't a program that starts once and keeps running: it's a recipe that the world reads again in full, from top to bottom, every cycle. This page covers when in the cycle that happens, what the bot sees when it reads its senses, in what order the genes run, and how much energy each instruction costs.

The DNA runs at the start of the cycle

Every simulation cycle (you can see it in full in The cycle and the order of actions) begins, after some general bookkeeping, with the DNA phase: the engine goes through the living bots one by one and runs each one's DNA. Only once they've all finished do the shots move, the forces get applied, the positions get integrated, the eyes look and the actions get resolved (shooting, reproducing, making a shell…).

That has two consequences worth keeping in mind at all times:

Corpses don't run DNA. A bot born during cycle N runs its DNA for the first time in cycle N+1.

A one-cycle delay, verified

This bot pushes forward with .up and, before that, records at position 52 the velocity it reads from .vel:

' Pushes forward and records the velocity it reads
cond
start
  *.vel 52 store
  10 .up store
stop
end

Running it, position 52 is always one step behind the real velocity:

Cycle.vel at the end of the cycleWhat the DNA recorded in 52
170
2137
32013
42620

In cycle 1 the bot has already pushed, but when its DNA read .vel the thrust hadn't been applied yet. The same goes for .nrg: the bot sees the energy it ended the previous cycle with, not what it has left while its own DNA spends it. A bot freshly seeded into the world also reads its senses as 0 during its first cycle, because it hasn't been through any physical part yet.

All the DNA, every cycle, in order

Every cycle the interpreter starts at the first instruction and advances to the final end (or to the end of the file). There are no jumps or loops: genes don't call each other and nothing goes back. Each instruction is looked at at most once per cycle, and the genes are traversed in the order they're written.

A gene whose condition is false isn't skipped in one go: the interpreter keeps passing over its instructions, but ignores them one by one until the next marker (cond, start, else or stop). The details of those markers are in Genes: cond, start, else and stop.

The stacks (The integer and boolean stacks) are emptied when each bot's DNA starts: nothing stays on them from one cycle to the next, or passes from one bot to another. What does persist is memory, and stores write to it at that moment (Writing to memory). That's why a gene sees what an earlier gene wrote in the same cycle, but not what a later one is going to write:

' Gene 1 counts; gene 2 copies the counter
cond
start
  *50 1 add 50 store
stop

cond
start
  *50 51 store
stop
end

With this order, at the end of every cycle 50 and 51 hold the same value (1 and 1, 2 and 2, 3 and 3…). If you swap the two genes, the copy reads the old value and 51 is always one behind 50 (1 and 0, 2 and 1, 3 and 2…).

A bot's DNA touches only its own memory and its own energy; it doesn't read or write anything of other bots during this phase. That's why the order between bots doesn't change what each DNA computes. (The only shared thing is the random number generator: if you use rnd, the number you get depends on how many bots used it before you.)

What each instruction costs

Every instruction that runs deducts energy from the bot at that moment, according to its class. The prices are scenario parameters (the Parameters: Costs page) and all of them are multiplied by the Cost multiplier:

ClassExamplesParameter
Number10, .up (as an address)Number
Memory read*.eye5, *50*.number read
Basic operatoradd, dup, rndBasic command
Advanced operatorangle, dist, sqrAdvanced command
Bitwise operator&, <<Bitwise command
Condition>, =, %=Condition
Logicaland, not, dropboolLogic
Storestore, inc…Store (see below)
Gene markercond, start, else, stopFlow control

Some finer rules:

An example with numbers

' A gene whose condition is false
cond
  0 1 =
start
  1 50 store
  2 51 store
stop
end

With number, condition, store and markers at 1 each, this bot loses 6 energy per cycle: 2 numbers and a condition in the cond part, plus the three markers. The body isn't charged. If you change 0 1 = to 1 1 =, the body runs and the spend goes up to 12: 4 numbers and 2 stores get added.

Long DNA costs even when it doesn't run

On top of what each instruction that runs charges, each cycle's upkeep charges for the length of the DNA, whether it runs or not. The price is the DNA per cycle parameter, and it's paid once for each instruction in the genome, not counting the final end. A bot like cond start 1 50 store 2 51 store stop end has 9 instructions plus the end: with that cost at 1 it loses 9 per cycle, and it would lose the same with all its genes switched off. You can look up the length of your own DNA in .dnalen, which also counts the end (for that bot it's 10).

Length weighs in again at reproduction: the parent pays the DNA copy cost once for each instruction in the genome (this time counting the end) when copying it for the child (Reproduction). With these two costs switched on, every spare instruction is energy the bot isn't using for anything else.

Is there an instruction limit?

There's no cap on energy or on time: a bot runs all its DNA every cycle even if it runs out of energy along the way. The only limit is one of position: the interpreter looks at no more than the first 32000 instructions of the genome, and whatever lies beyond never runs. Since there are no loops, no DNA can hang the simulation.

Which values are used

Costs depend on the scenario. The F1 rules, the ones for competitions (in Experiment you apply them with “F1 settings”), charge 0.04 per store and 0.004 per condition and leave the other DNA costs at 0, including the length one. With those rules, ten stores per cycle cost a bot 0.4 energy per cycle; the genome length, nothing.

The Cost multiplier can move on its own if you turn on Dynamic cost adjustment (Parameters: Dynamic costs): the engine raises or lowers it to bring the population to a target, and all the prices on this page change with it. How all this fits with the rest of the bot's expenses is in Energy, body and waste.