E W
E W

Reputation: 61

Changing Turtle Breed for 1 turtle at specific patch

I am trying to get the first turtle to arrive at a specific patch (food source 1) to become a new breed (leader). This is what I tried so far...

ask one-of followers
  if followers-at -41 -22
    set breed [leaders]]

That gives me the error: Ask expected 2 inputs, an agent or agentset and a command block.

If I add a bracket before if --> [if...], the error becomes IF expected two inputs, a true false and a command block.

Right now I have create-leaders 0 [set color red] in the to setup procedure, and a leader [leaders breed] at the beginning of the code, because I don't want any leaders to exist before the first turtle reaches food patch -41 -22. I'm still at a loss for how to determine which turtle is the first to arrive at that patch. Any suggestions for that?

I tried this to test whether I could get one follower at a specific patch to change:

to recruit 
  ask followers-at -41 -22
    [ask one-of followers
      [set color red]]
end

First, I counted 4 red followers during the course of the run, but the code specifies "one-of" which should only affect 1 follower. Then I modified it to:

to recruit 
  ask followers-at -41 -22
    [ask one-of followers]
      [set breed [ leaders ]
end

which doesn't seem like it has the right number of brackets, but is the only way that I don't get an "error no closing bracket" message. Instead, I get "Ask expected 2 inputs, an agent or agentset and a command block."

Upvotes: 0

Views: 158

Answers (1)

JenB
JenB

Reputation: 17678

This is the way the bracketing would work (note that the spacing is not important for syntax, but I structured it so you could see where the code blocks start and end). I also added an any?:

ask one-of followers
[ if any? followers-at -41 -22
  [ set breed leaders
  ]
]

So, the ask is asking one of the turtles of the breed named 'followers' to do everything in the outer code block. The randomly chosen turtle then checks if there are any 'followers' on the patch at coordinates -41 -22 from itself (relative, not absolute). If there are, then the randomly chosen turtle (not the ones on the specified patch) changes its breed to leaders.

What you probably want is to have one of the followers on the patch change breed. That would look like:

if any? followers-on patch -41 -22
[ ask one-of followers-on patch -41 -22
  [ set breed leaders
  ]
]

So I changed turtles-at (relative) to turtles-on (absolute) position, as well as going to the turtles on the relevant patch.

For efficiency (so set of turtles created only once instead of twice), debugging (so you don't accidentally have different coordinates in the two places), a better way of doing this same code is:

let potential-leaders followers-on patch -41 -22
if any? potential-leaders
[ ask one-of potential-leaders
  [ set breed leaders
  ]
]

As for finding out whether it's the first, all you need to do is check that there aren't any existing leaders as part of your conditions (eg not any? leaders)

Upvotes: 1

Related Questions