Difference between revisions of "Stack"

From WikiManual
Jump to: navigation, search
(The Stack)
 
(10 intermediate revisions by 7 users not shown)
Line 1: Line 1:
==The Stack==
+
= Overview =
'''Overview'''
 
  
The basic principle is that everything is done passing through a last-in first-out stack. It's like a stack of dishes: you can take or put a dish only from the top of the stack, all others are inaccessible. The language's instructions work on the data that is on the top of the stack, removing them and putting the results in their place.
+
The principle is quite simple: values are placed in a temporary holding area that is last-in-first-out (LIFO): like a stack of dishes.  Almost all language commands make use of some stack or other, taking values off the stack and manipulating them, even occassionaly putting new values back on.
----
 
For example, to sum two numbers, you should put them in the stack, and then invoke the sum instruction. It will remove the two numbers, calculate the sum, and put the result on top of the stack.
 
  
It may seem complicated, but's not. For instance, where you usually write
+
== An example to illustrate: Subtraction ==
  
b+c=a
+
a - b = c
  
now you should write
+
Say you want to calculate c.  In the end we'll want c to be on the top of the stack so we can use it again.
  
b c add
+
With the stack, you must first place the two operands on.  Now, this is very important: Darwinbots uses ''reverse''-Polish notation.  Which means that in general the second number on the stack will be the first number used in the calculation.  This may seem complicated, but it means that the DNA is easier to read in a way we're accustomed to.  Anyway, here's what it'll look like:
  
The add instruction removed b and c from the stack and replaced it with the sum of the two, a.
+
a b '(note that if this were real code, you'd be more likely to see 5 6, or *.in1 *.in2
  
Just like a pile of dishes can get too big, when the stack fills up with numbers it will begin to overflow and will lose the oldest numbers. Ordinarily very few numbers remain in the stack for more than one gene or cycle, but sloppy or mutated DNA can cause this. It is easy to empty the stack by placing several add, sub, or mult commands in a row.
+
Next we'll want to invoke the subtraction command [[sub]], which we simply do by having it in the DNA.
 +
 
 +
a b sub
 +
 
 +
That's it.  Sub will take a and b from the stack, subtract b from a, and place the new answer on the top of the stack.  In essence, we're basically just moving the sub command from the middle of the two numbers to the end.
 +
 
 +
== Second Example: Conditionless Behavior ==
 +
 
 +
The stack can be used to create conditionless behavior.
 +
 
 +
start
 +
-6 *.refeye *.myeye sub sgn abs mult .shoot store
 +
stop
 +
 
 +
The above gene will fire a body shot at a robot if it is identified as an enemy.  Notice that it does not use any conditions, but rather uses math to determine when *.refeye and *.myeye are equal.  Notice that the -6 remains on the stack until it is used by the mult.
 +
 
 +
= Where it's used =
 +
A stack is used in the following bits of the progam:
 +
* conditions - A separate stack for conditions is kept for the logical operators to act on.  At the end of every condition block in the DNA, all values remaining in this stack are [[and]]-ed together and the result saved in an [[Execution Flag]].
 +
 
 +
* integers - basic, advanced, and bit commands, and any others I may have forgotten, use the integer stack, which is what is commonly meant by "the stack".
 +
 
 +
= Technical Details =
 +
 
 +
== Empty ==
 +
If a command tries to read from the stack when it's empty, it gets the appropriate default value.  For the integer stack, this is 0.  For the conditions stack, this is TRUE.
 +
 
 +
== Full ==
 +
In the same way that a pile of dishes can get too large, so can the stack.  After the stack begins to hold 20 numbers, it will begin to push older numbers on the bottom off.  This helps ensure that new DNA will always be operable even if it comes after sloppy DNA that doesn't clean up after itself and leaves a mess all over the kitchen floor, and won't pay their half of the rent, and stays in the bathroom too long...
 +
 
 +
Oh, right, Darwinbots.
 +
 
 +
It is easy to empty the stack by placing several add, sub, or mult commands in a row.
 +
 
 +
== Number Range ==
 +
As of 2.4, you are allowed to place numbers as large as +/-2000000000 (that's +/-2E9 or +/-2 billion) on the stack.  Note that the bots' memory can only hold integers as large as +/- 32000.  If a value greater in magnitude than this is stored in a memory location, your computer will explode.  (Okay, not really.  I think it's "mod"ed 32000.  I'd have to check --[[User:Numsgil|Numsgil]] 11:06, 19 Sep 2005 (MST)).
 +
 
 +
== At the end of the cycle ==
 +
As of 2.4, after a bot finishes executing its DNA for the cycle, its stack is cleared.  That means that a bot ''can't'' use the stack to store values between cycles.  For that, you have to use memory.
 +
 
 +
{{DNA}}

Latest revision as of 18:40, 13 February 2014

Overview

The principle is quite simple: values are placed in a temporary holding area that is last-in-first-out (LIFO): like a stack of dishes. Almost all language commands make use of some stack or other, taking values off the stack and manipulating them, even occassionaly putting new values back on.

An example to illustrate: Subtraction

a - b = c

Say you want to calculate c. In the end we'll want c to be on the top of the stack so we can use it again.

With the stack, you must first place the two operands on. Now, this is very important: Darwinbots uses reverse-Polish notation. Which means that in general the second number on the stack will be the first number used in the calculation. This may seem complicated, but it means that the DNA is easier to read in a way we're accustomed to. Anyway, here's what it'll look like:

a b '(note that if this were real code, you'd be more likely to see 5 6, or *.in1 *.in2 

Next we'll want to invoke the subtraction command sub, which we simply do by having it in the DNA.

a b sub

That's it. Sub will take a and b from the stack, subtract b from a, and place the new answer on the top of the stack. In essence, we're basically just moving the sub command from the middle of the two numbers to the end.

Second Example: Conditionless Behavior

The stack can be used to create conditionless behavior.

start
-6 *.refeye *.myeye sub sgn abs mult .shoot store
stop

The above gene will fire a body shot at a robot if it is identified as an enemy. Notice that it does not use any conditions, but rather uses math to determine when *.refeye and *.myeye are equal. Notice that the -6 remains on the stack until it is used by the mult.

Where it's used

A stack is used in the following bits of the progam:

  • conditions - A separate stack for conditions is kept for the logical operators to act on. At the end of every condition block in the DNA, all values remaining in this stack are and-ed together and the result saved in an Execution Flag.
  • integers - basic, advanced, and bit commands, and any others I may have forgotten, use the integer stack, which is what is commonly meant by "the stack".

Technical Details

Empty

If a command tries to read from the stack when it's empty, it gets the appropriate default value. For the integer stack, this is 0. For the conditions stack, this is TRUE.

Full

In the same way that a pile of dishes can get too large, so can the stack. After the stack begins to hold 20 numbers, it will begin to push older numbers on the bottom off. This helps ensure that new DNA will always be operable even if it comes after sloppy DNA that doesn't clean up after itself and leaves a mess all over the kitchen floor, and won't pay their half of the rent, and stays in the bathroom too long...

Oh, right, Darwinbots.

It is easy to empty the stack by placing several add, sub, or mult commands in a row.

Number Range

As of 2.4, you are allowed to place numbers as large as +/-2000000000 (that's +/-2E9 or +/-2 billion) on the stack. Note that the bots' memory can only hold integers as large as +/- 32000. If a value greater in magnitude than this is stored in a memory location, your computer will explode. (Okay, not really. I think it's "mod"ed 32000. I'd have to check --Numsgil 11:06, 19 Sep 2005 (MST)).

At the end of the cycle

As of 2.4, after a bot finishes executing its DNA for the cycle, its stack is cleared. That means that a bot can't use the stack to store values between cycles. For that, you have to use memory.

How the DNA works