0

I am trying to move a turtle towards it's offspring based on a shared variable. In my initial code I ask a male (breed) that has a display, but not a bower, to establish a bower (different breed) by hatching. I use create-link-with myself to so that the male and the bower share a variable called bird-id.

to establish-bower
  ask males[
   if has-bower? = false[
     if has-display? = true[
        hatch 1 [
        set breed bowers
        set color yellow
        set bower-id who
        set shape "house"
        create-link-with myself
        ]
           ]
      ]
    ]
end

In the second part of my code, I ask males that have a bower and a display to return-to-bower. If their distance to THEIR OWN bower is greater than 0, the male faces that bower and moves forward one. If the distance is 0, two other variables and the energy are adjusted and the male moves a bit randomly.

to return-to-bower
  ask males[
    if has-bower? = true[
      if has-display? = true[
        (ifelse
        distance one-of bowers with [bird-id = (bird-id of myself)] > 0[
          face one-of bowers with [bird-id = (bird-id of myself)]
          fd 1]
        distance one-of bowers with [bird-id = (bird-id of myself)] = 0[
          set has-display? false
          set nr-display nr-display + 1
          rt random 50
          lt random 50
          fd 1
          set energy energy - 3])
      ]
    ]
  ]
end

I have trouble coding the part that identifies a bower as belonging to the male that made it. So far I have

bowers with [bird-id = (bird-id of myself)]

This obviously doesn't work, but it conveys what I want to do.

How can I write this part?

desertnaut
  • 57,590
  • 26
  • 140
  • 166

1 Answers1

0

I think your bird-id is not being shared as expected between your males and bowers- in establish-bower you have the line set bower-id who, but the who there should be in the context of the bower instead of the context of the male. So, male 0 may ask bower 4 to set its bird id to 4, rather than 0. If you want to use that option (which is certainly doable), I think you'd need to alter to set bower-id [who] of myself- this mirrors what you did with the create-link-with myself line.

However, if you are already using links, you might like an alternative that makes use of the link already in place. The other-end the primitive returns the agent on the other end of the link owned by the asking turtle (better explained in that link). So whenever it makes sense to have the turtle figure out which bower is its own, have it query the associate link. For example:

breed [ males male ]
breed [ bowers bower ]

males-own [ has-bower? go-time? ]

to setup
  ca
  ask n-of 10 patches [
    sprout-males 1 [
      set go-time? true
      set has-bower? false
    ]
  ]
  ask males [
    establish-bower
  ]
  reset-ticks
end

to establish-bower
  if not has-bower? [
    hatch-bowers 1 [
      set shape "house"
      create-link-with myself
    ]    
    set has-bower? true
  ]  
end

to go 
  ask males with [has-bower?] [
    ifelse go-time? [
      rt random 60 - 30 
      fd 1
      if random-float 1 < 0.05 [
        set go-time? false
      ]
    ] [
      let my-bower [other-end] of one-of my-links
      ifelse distance my-bower > 1 [
        face my-bower
        fd 1
      ] [
        move-to my-bower
        if random-float 1 < 0.02 [
          set go-time? true
        ] 
      ]
    ]
  ]
  tick 
end

I use let in the above to have the bowers only be temporarily checked by the males- you could also simplify this by having a males-own variable like my-bower and assign the bower directly to that variable and then the males would permanently track it.

Luke C
  • 10,081
  • 1
  • 14
  • 21