Darwinbots Forum
Bots and Simulations => DNA - General => Topic started by: bacillus on March 28, 2008, 01:47:07 AM
-
When I made my first 1-gene bot last year, it took me about two weeks to fully understand how they worked. So in case you might have those same problems, I have made a quick guide on how to make them, including an explanation.
Every command can be compressed into the basic formats:
CONDITION VALUE mult LOCATION store
or
VALUE LOCATION CONTIDION mult store
The Condition
The condition is a number, either 1(true) or 0(false). In the first format, the value is multiplied with this, so false returns 0 and true returns 1. Use this if you need to overwrite a certain memory location Easy enough?
The second one is more efficient; it multiplies the location instead of the value; so true stores in the correct memory location, false in memory location 0, where a store is useless, but free (whereas storing 0 in the desired location costs nrg).
Calculating the Condition from numbers A and B
Negation
A -- abs
If A is 1(true), it turns into 0(false)
If A is 0(false), it turns into -1, then 1(true)
Equals
A B sub sgn abs -- abs
If A equals B, the sub command will return 0; otherwise, it will return something else. The sgn command turns this something else into 1 or -1, depending if it's negative or positive. The abs makes sure its 1 in either case. So:
A B != => 1
A B = 0
next, we negate the value:
A B != => 0
A B = => 1
There you go!
Comparing
A B sub sgn 1 add sgn
A and B are subtracted and sgn value taken:
A < B => -1
A = B => 0
A > B => 1
1 is added:
A < B => 0
A = B => 1
A > B => 2
sgn value taken again
A < B => 0
A = B => 1
A > B => 1
A >= B => 1
To get A <= B, flip a and b in the formula. To get A > B, negate A <= B. To get A < B, negate A >= B.
Operators
AND
ConditionA conditionB mult
If both are 1, the result will be 1*1=1.
NOT
ConditionA conditionB mult -- abs
AND value negated.
OR
Calculating the Condition from numbers A and B
Equals
A B sub sgn abs -- abs
If A equals B, the sub command will return 0; otherwise, it will return something else. The sgn command turns this something else into 1 or -1, depending if it's negative or positive. The abs makes sure its 1 in either case. So:
A B != => 1
A B = 0
next, we negate the value:
A B != => 0
A B = => 1
There you go!
Comparing
A B sub sgn 1 add sgn
A and B are subtracted and sgn value taken:
A < B => -1
A = B => 0
A > B => 1
1 is added:
A < B => 0
A = B => 1
A > B => 2
sgn value taken again
A < B => 0
A = B => 1
A > B => 1
A >= B => 1
To get A <= B, flip a and b in the formula. To get A > B, negate A <= B. To get A < B, negate A >= B.
Operators
AND
ConditionA conditionB mult
If both are 1, the result will be 1*1=1.
OR
ConditionA conditionB add sgn
if both are 0, it will add to 0 and sgn will return 0. If one of them is 1, 1 will be returned, and if both are 1, 2 will be returned. Either way, sgn reduces it to 1.
XOR
ConditionA conditionB add -- abs -- abs
A=0, B=0 => 0 => -1 => 1 => 0
A=1, B=1 => 2 => 1 => 1 => 0
One is 1, other is 0 => 1 => 0 => 0 => -1 => 1
EDIT-Better comparators
Greater or equal
ConditionA conditionB sub sgn 0 floor ++ abs is the same as A B >=
ConditionA conditionB sub sgn - 0 floor ++ abs is the same as A B <=
I hope I have made this clear enough for you to get the basic idea.
-
You have a few "1 add"s - you can replace them with "++"
Also, your NOT operator is actually OR. Put your original "negation" in as NOT
-
I got the first part, but the comparator part was writen a little too hastely maybe. I just didnt understand how you were explaining the operators/comparators.
-
But why would you make especially a SG-bot. As far I know they don't have a particulary advantage anymore.
Just build them modular, I want to understand them too.
-
AND
ConditionA conditionB mult
If both are 1, the result will be 1*1=1.
If both conditions are either 0 or 1, conditionA conditionB & will work the same, but faster than mult. You do still need to multiply times your location or value at the end, though.
OR
Calculating the Condition from numbers A and B
Equals
You seem to have included the same or similar block of text in your post twice, starting with Equals and continuing through comparing.
NOT
ConditionA conditionB add sgn
if both are 0, it will add to 0 and sgn will return 0. If one of them is 1, 1 will be returned, and if both are 1, 2 will be returned. Either way, sgn reduces it to 1.
I guess you mislabelled that - From your description, that's an OR. But you don't need to do that at all - DBII has an | operator which is bitwise or, and it will work just fine for this, since your numbers are always 0 or 1.
XOR
ConditionA conditionB add -- abs -- abs
You can do this one with ^, which is the bitwise xor operator.
These ones aren't very important, but:
Negation
A -- abs
If you see - ++ in a bot's code, that's another way to write negation/not.
Equals
A B sub sgn abs -- abs
If you see sub sgn abs - ++ in a bot's DNA, that's another way to write this too.
What I use for comparison operators:
'>' is 'sub sgn 0 floor'
'<' is 'sub sgn - 0 floor'
'>=' is 'sub 1 add sgn 0 floor'
'<=' is 'sub 1 sub sgn - 0 floor'
'==' is 'sub sgn abs - ++'
'!=' is 'sub sgn abs'
-
These modules were posted a while back, before I realized you can use floor and ceil for < and >, and before you could put boolean logic into the gene itself. OK, so there are some fundamental errors in the code. You don't need it anymore, you said so yourself!