Starlokk
Starlokk

Reputation: 21

im having a strange issue in netlogo with an ifelse statement

I'm asking about an issue on net logo as I'm doing a project in it. I'm making a flocking simulation but when i tried to impalement other behaviors using ifelse statements, but when i put the ifelse statement into it, they don't follow any behaviors, rather they just move.

Here's the code:

breed [Birds Bird] breed [Hawks Hawk]

to Setup   clear-all   reset-ticks   create-Birds Number_of_Birds[
    setxy random-xcor random-ycor]   create-Hawks Number_of_Hawks[
    setxy random-xcor random-ycor] end

 to Start   ask Birds[
    set color white
    ifelse (Hawks in-radius Reaction_Distance = 0)
    [
      set heading Migration_Direction
      let closest-Birds max-n-of Target_Group_Size (other Birds) [distance myself]
      let Group_Heading mean [heading] of closest-Birds
      let centroidx mean [xcor] of closest-Birds
      let centroidy mean [ycor] of closest-Birds
      set heading (Migration_Direction +( attraction * (Group_Heading)))
      fd 1
      set heading ( attraction * (towardsxy centroidx centroidy) )
      fd 1
    ]
    [
      let Closest_Hawks max-n-of 1 (Hawks) [distance myself]
      set heading (mean [heading] of Closest_Hawks + 180)
      fd 1
    ]   ] end

Upvotes: 1

Views: 66

Answers (1)

Nicolas Payette
Nicolas Payette

Reputation: 14972

Let's take a look at the definition of in-radius in the NetLogo dictionary:

Reports an agentset that includes only those agents from the original agentset whose distance from the caller is less than or equal to number.

It says that in-radius reports an agentset.

Now let's take a look at your ifelse condition:

ifelse (Hawks in-radius Reaction_Distance = 0)

The definition tells us that the Hawks in-radius Reaction_Distance part reports an agentset (i.e., all the hawks that are in the radius). The = sign then compares that agentset to the number 0. But an agentset is not an number! It can never be equal to zero.

I suppose that what you want is to compare the number of hawks in the radius to the number 0.

One way to do that would be to use the count primitive, which reports the number of agents in an agentset:

ifelse (count Hawks in-radius Reaction_Distance = 0)

That would work, but I would not write it like that. NetLogo has the much nicer any? primitive, which you can use like this:

ifelse (not any? Hawks in-radius Reaction_Distance)

This expresses your intent in a much clearer way.

You could also invert the order of your ifelse clauses to avoid the not:

ifelse (any? Hawks in-radius Reaction_Distance)
[
  ; get away from hawks...
]
[
  ; flock normally...
] 

Upvotes: 3

Related Questions