List Info

Thread: Data structures and Message passing between turtles




Data structures and Message passing between turtles
country flaguser name
United States
2007-07-30 11:16:10

Hi,

I have a breed of turtles, I want them to send a message to each other
to implement a sort of token ring (The token is to be a data structure
which expands over time). Any ideas how to do this data structure and
message passing.

Also, what kind of debugging can be done in NetLogo? Is it primarily
print or show- based or is there any tool available for debugging (aka
Watches/breakpoints etc.)

Take care,

Muaz

__._,_.___
.

__,_._,___
Re: Data structures and Message passing between turtles
country flaguser name
United States
2007-07-30 14:42:37

Hi, Muaz!

I would probably implement the token itself as a breed of turtle, too.
The content of the token would be in a -own'd variable. Also, any
other token information, such as the source, destination, etc, can be
-own'd by the token turtle.

The turtles that are passing the token will then require a variable to
refer to the token, such as "token".

Then, creating a token is a matter of creating a new token turtle,
then setting the token variable of the node to the newly created token
turtle. A helper reporter can be used to make this easy and clear.

Passing the token then becomes a matter of one node setting the token
variable of a another node to its own token variable, before setting
the variable to nobody:

This is only one approach--you may desire or require a different one,
such as having nodes never set each others variables, but rather,
addressing tokens to each other, then picking up tokens addressed for
themselves.

Example breed definitions:

breed [ tokens a-token ]
tokens-own [ data source destination ]

breed [ nodes a-node ]
nodes-own [ token ]

to pass-to [ #node ]
;; Procedure to pass the token to a given node.
;; ... this is run by a node,
;; ... passes token to the given node
set [ token ] of #node token
;; ... clear own reference to the token
set token nobody
end

reporter used to to create a token:

to-report new-token [ #data #destination ]
;; assumes this is always run by a node (or other turtle)
let me nobody
hatch-tokens 1
[ set me self
set source myself
set destination #destination
set data #data
]
report me
end

usage of the reporter:

;; this is run by a node when it is ready to create a token
set token new-token some-data some-destination

Hope this helps,

~~James
_________________________
http://www.turtlezero.com

On 7/30/07, Muaz Niazi < thhgttg%40gmail.com">thhgttggmail.com> wrote:
&gt; Hi,
>
> I have a breed of turtles, I want them to send a message to each other
&gt; to implement a sort of token ring (The token is to be a data structure
> which expands over time). Any ideas how to do this data structure and
> message passing.
>
>; Also, what kind of debugging can be done in NetLogo? Is it primarily
> print or show- based or is there any tool available for debugging (aka
>; Watches/breakpoints etc.)
>
>
> Take care,
&gt;
> Muaz

__._,_.___
.

__,_._,___
Re: Data structures and Message passing between turtles
country flaguser name
United States
2007-07-31 10:30:10
On Jul 30, 2007, at 12:16 PM, Muaz Niazi wrote:

>  I have a breed of turtles, I want them to send a
message to each other
>  to implement a sort of token ring (The token is to be
a data structure
>  which expands over time). Any ideas how to do this
data structure and
>  message passing. ... <snip>

Mauz,
Here is a demo with some more ideas for doing "sort of
a token ring". 
Since only one node can have the token, I just made the
token data and 
who-has-the-token global variables. I purposely didn't use
link turtles 
for this demo, but of course that would be a great way to do
it too, 
and opens up more possibilities for your model (like using
the layout 
procedures). Paste the code below into the Procedures of a
new 3.1 
model and make setup and go buttons to see it work. HTH,

Jim
--------------
; Demo Token Ring -- NetLogo 3.1 syntax

globals
[ token-data  ; the data of the token
   token-holder  ; agent ref of node with token
]

breed [ nodes node ]  nodes-own [ from-node to-node ]

to setup
   clear-all
   set-default-shape nodes "circle"

   ; make a 7-node ring
   create-custom-nodes 7 [ set color white  jump 4 ]
   let ring sort nodes
   (foreach ring rotate ring [ connect ?1 ?2 ])

   ; initialize token-data and start token at random node
   set token-data []
   ask one-of nodes [ pass-token ]
end

to pass-token  ; node -  pass token to next node
   set color white
   set token-holder to-node
   set color-of token-holder red
end

to connect [ #node1 #node2 ]
   set to-node-of #node1 #node2
   set from-node-of #node2 #node1  ; (not used in demo)
end

to-report rotate [ #list ]  ; rotate list forward
   report sentence (but-first #list) (first #list)
end

to go  ;;; demo
   if length token-data > 20 [ stop ]
   every 1
   [ ask token-holder
     [ ;;; do stuff to token
       if random 23 = 0 [ set token-data fput self
token-data ]
       pass-token
     ]
   ]
end

  Approximate file size 2447 bytes
[1-3]

about | contact  Other archives ( Real Estate discussion Medical topics )