Darwinbots Forum
Bots and Simulations => Bestiary => Interesting behaviour bots => Topic started by: rayz on April 03, 2009, 04:58:02 PM
-
Since I went through all of the trouble to make it a bot to test my algorithm, I thought I might post it here.
It's an interesting behavior bot. It approimates sine and cosine for use with direction setting. I think most of the stuff is explained in the comments.
I commented out a tracking and shooting section. If you uncomment it (except for the shooting part), and make a new version with more .up store commands, you can have the two species of bots just run straight at each other and go into complex little loops.
Anyway, here it is!
Works best in no costs environment, too inefficient to run otherwise.
'An attempt to make the bot move north no matter how it is turned.
'Lacks precision due to a integer-only stack.
'Attempt to correct that by waiting to divide bigs numbers till end.
'
'edit these to wherever--maybe pi should be epigenetic?
'sineIn and sineOut are input/output for Taylor series
'sineOut returns a number multiplied by factor2 (approx.)
'pi and factor are internal constants for sine calculation
'aimHeading is the absolute heading that the bot wants to travel
'magnitude is how approximately how fast (in Darwinbots units) the bot will travel
'turn is how much the bot itself turns each cycle--useful to sweep area around the bot
'aimTurn is how much the bot changes the heading each cycle.
def sineIn 50
def sineOut 51
def pi 52
def factor 53
def aimHeading 410
def magnitude 54
def turn 55
def aimTurn 56
def factor2 57
'set constants
cond
*.robage 1 =
start
31415 .pi store
10000 .factor store
32000 .factor2 store
100 .magnitude store
0 .aimHeading store
*.tiepres .deltie store
35 .turn store
5 .aimTurn store
'just to move them out of the way
1221 .eye1width store
1221 .eye2width store
1221 .eye3width store
1221 .eye4width store
'1221 .eye5width store
1221 .eye6width store
1221 .eye7width store
1221 .eye8width store
1221 .eye9width store
stop
'manage body
cond
*.body 10 <
*.nrg 300 >
start
100 .strbody store
stop
cond
*.nrg 500 <
*.body 10 >
start
10 .fdbody store
stop
cond
start
*.aim *.turn add .setaim store
*.aimHeading *.aimTurn add .aimHeading store
stop
'try to aim the eye and shoot
'cond
'*.refup *.myup !=
'*.eye5 0 !=
'start
'*.refxpos *.refypos angle .aimHeading store
'*.aimHeading *.aim sub *.turn sub .eye5dir store
'*.turn *.aim add *.aimHeading sub .aimshoot store
'-6 .shoot store
'*.nrg 2 div .shootval store
'stop
'
'cond
'*.waste 100 >
'start
'-4 .shoot store
'*.waste .shootval store
'stop
'begin movement section: it makes the bot move in the direction of aimHeading
'normalize aimheading
cond
*.aimHeading 1256 >=
start
*.aimHeading 1256 sub .aimHeading store
stop
cond
*.aimHeading 0 <
start
*.aimHeading 1256 add .aimHeading store
stop
'from here down it makes the bot move in the direction of *.aimheading
cond
start
*.aimHeading *.aim sub .sineIn store
stop
'begin sineIn normalization
cond
*.sineIn 0 <
start
*.sineIn 1256 add .sineIn store
stop
cond
*.sineIn 1256 >
start
*.sineIn 1256 sub .sineIn store
stop
'Taylor expansion of sine function
cond
start
628 *.sineIn sub *.pi mult 628 div *.factor div 1 pow *.factor2 mult 1 div 628 *.sineIn sub *.pi mult 628 div *.factor div 3 pow *.factor2 mult 6 div sub 628 *.sineIn sub *.pi mult 628 div *.factor div 5 pow *.factor2 mult 120 div add 628 *.sineIn sub *.pi mult 628 div *.factor div 7 pow *.factor2 mult 5040 div sub .sineOut store
stop
cond
start
*.sineOut *.magnitude mult *.factor2 div .sx store
stop
'calculate value for .up
cond
start
314 *.aimHeading *.aim sub sub .sineIn store
stop
cond
*.sineIn 0 <
start
*.sineIn 1256 add .sineIn store
stop
cond
*.sineIn 1256 >
start
*.sineIn 1256 sub .sineIn store
stop
'Taylor expansion of sine function
cond
start
628 *.sineIn sub *.pi mult 628 div *.factor div 1 pow *.factor2 mult 1 div 628 *.sineIn sub *.pi mult 628 div *.factor div 3 pow *.factor2 mult 6 div sub 628 *.sineIn sub *.pi mult 628 div *.factor div 5 pow *.factor2 mult 120 div add 628 *.sineIn sub *.pi mult 628 div *.factor div 7 pow *.factor2 mult 5040 div sub .sineOut store
stop
cond
start
*.sineOut *.magnitude mult *.factor2 div .up store
stop
'end movement section
'cond
'*.nrg 500 >
'*.body 1000 >=
'start
'50 .repro store
'stop
end
-
Very nice... hope you don't mind if I steal that.
-
Very nicely done . One minor thing I would change though is replace all that tedious wrapping with
*.sineIn 1256 mod .sineIn store
This modulus function replaces the four genes you used.
I look forward to new innovations using this algoritm...
-
There's actualy a slight advantage to wrapping manualy. Although modulus is a nifty operator and I personaly prefer to use it aswell, it is a bit heavy to execute.
So if you have a bot that runs realy slow and it has a lot of mod operators in it then it might actualy help a litle to replace them with logic. (Not sure how big of a difference it would make in DB... maybe if you have a high population or something)
Personaly I'll take readability over speed wherever possible. And the actual energy costs in DB are lower when using math over logic anyway...
-
Very nicely done . One minor thing I would change though is replace all that tedious wrapping with
*.sineIn 1256 mod .sineIn store
This modulus function replaces the four genes you used.
I look forward to new innovations using this algoritm...
one small problem that I found: at certain numbers, it reads numbers greater than 1 and less than -1, and at these values the value switches from something like 312... to -312...-- something a sine function does not do.
If somebody could take a look through it and see if there are any bugs, that would be great!
Thanks!
-Raymond
...School takes up too much time!
-
Very nicely done . One minor thing I would change though is replace all that tedious wrapping with
*.sineIn 1256 mod .sineIn store
This modulus function replaces the four genes you used.
I look forward to new innovations using this algoritm...
one small problem that I found: at certain numbers, it reads numbers greater than 1 and less than -1, and at these values the value switches from something like 312... to -312...-- something a sine function does not do.
If somebody could take a look through it and see if there are any bugs, that would be great!
Thanks!
-Raymond
...School takes up too much time!
Interesting point. Perhaps you could say you re-invented the sine, and leave it at that But honestly, it is probably integer rounding trouble, it is something we must all accept at some point... or you could fix the source code, but accepting it is much easier.
-
rayz, you will be happy to know that sin and cos are now native. :)