imantha
imantha

Reputation: 3828

Netlogo, Finding the parent of the hatched breed

I am trying to find out when you use the hatch- function how to form link between the parent and the breed it creates. Or is there already a link that I am not aware of. For example, there are two breeds lets say yellow bees and red bees. The yellow bees travel a certain distance and when they come to a certain point (hive), they hatch x no of red-bees. I want to transfer the distance traveled by the yellow bees to the red bees it creates (from parent to its children). But there is no way of doing this since there is no link between yellow bees and red bees it creates.

i.e (yellow-bee 22 hatches red-bee 15 and red-bee 72) while yellow-bee 77 hatches red-bee 26). If yellow bee traveled 17m, I want red bee 22 & 15 to inherit that info. And red bee 26 to inherit the distance yellow-bee 77 has traveled (i.e 19m)

Note: ideally I am thinking its better not to use in-radius command to find the red-bees around the yellow-bees. Because if two yellow bees arrive at the hive at the same time and creates two sets of red bees. If they use in-radius to find the red bees around them, they might mix up

breed [yellow-bees yellow-bee]
breed [red-bees red-bee]
yellow-bees-own [distance-traveled no-of-red-bees]
red-bees-own [transfered-distance]

to go
....
ask yellow-bees[
if current-node node-at-hive[
hatch red-bees no-of-red-bees [set color red]
]
set transfered-distance [distance-traveled] of ....(;this is where the 
agent-set (red-bees) should be added
]

Upvotes: 1

Views: 173

Answers (1)

Nicolas Payette
Nicolas Payette

Reputation: 14972

The documentation of hatch says that:

Each new turtle inherits of all its variables, including its location, from its parent.

In other words, hatch already does what you want. You just have to make sure that the variable you want the child to inherit (in this case, distance-travelled) has the same name as in the parent's breed.

Here is an example:

breed [ yellow-bees yellow-bee ]
yellow-bees-own [ distance-travelled ]
breed [ red-bees red-bee ]
red-bees-own [ distance-travelled ]

to setup
  clear-all
  create-yellow-bees 1 [ set distance-travelled 10 ]
  create-yellow-bees 1 [ set distance-travelled 20 ]
  ask yellow-bees [ hatch-red-bees 1 ]
  ask turtles [ show distance-travelled ]
end

The output of which will be something like:

observer> setup
(red-bee 3): 20
(red-bee 2): 10
(yellow-bee 0): 10
(yellow-bee 1): 20

As you can see, the two red bees inherit their distance-travelled value from their parents.

Upvotes: 4

Related Questions