how to call for separate procedures using the ifelse any? function in netlogo?

Viewed 73

In brief below is the code I wrote to show whether there is a turtle on the neighbor patches

to play-the-game
  ifelse any? turtles-on neighbors4
  [show "turtles-found"]
  [show "turtles-not-found"]
end 

I need to change it to perform the procedures I have already written out; if they are the same breed they 'gain-energy' different breed 'fight-opponent'

I am not sure as to how to change the first part to carry out the other procedures.

2 Answers

If I understand correctly, you need to check the breed of both the asking turtle and the 'opponent' turtle. You're certainly on the right track by checking if there are any neighbors present, the next step is to make the breed check and then the turtles can choose what action to take. For example, look at this toy model where wolves and cows can identify whether they've landed on a patch next to the same breed or not:

breed [ cows cow ]
breed [ wolves wolf ]

to setup
  ca
  ask n-of 10 patches [ 
    sprout-cows 1 [ 
      set shape "cow" 
      set color white
    ]
  ]
  ask n-of 10 patches with [ not any? turtles-here ] [
    sprout-wolves 1 [ 
      set shape "wolf" 
      set color red - 1
    ]
  ]
  reset-ticks
end

to go 
  ask turtles [
    face one-of neighbors
    fd 1
    play-the-game
  ]
  tick
end

to play-the-game
  if any? turtles-on neighbors4 [
    let current-neighbor-turtle one-of turtles-on neighbors4
    ifelse [breed] of current-neighbor-turtle = breed [
      show "I see one of my own breed!"
    ] [
      show "I see an opponent!!!"
    ]      
  ]
end

If that's not quite what you had in mind, please edit your question above to provide more detail.

I think you should consider dropping ifelse. In your example, the two possible outcomes of the any? turtles-on neighbors4 condition are mutually exclusive: either there are turtles, or there are not.

However what you want to achieve in your model is a bit different: (I imagine that) on the neighboring cells there can be turtles of the same breed AND turtles of a different breed at the same time. In this case, the two scenarios are not mutually exclusive and using ifelse (where only one or the other of the two command blocks will be executed) would potentially overlook one of the two cases, depending on how you specify the condition.

I think two if statements would be more appropriate:

to play-the-game
  if (any-friends-nearby?) [gain-energy]
  if (any-opponents-nearby?) [fight]
end

to-report any-friends-nearby?
  report (any? (turtles-on neighbors4) with [breed = [breed] of myself])
end

to-report any-opponents-nearby?
  report (any? (turtles-on neighbors4) with [breed != [breed] of myself])
end

It is still relevant for you to choose which condition will be checked first (and therefore which action, between gain-energy and fight, will be executed first in case both conditions are satisfied)

Related