NetLogo - Have an agent identify one of two other types of agent closest to it and perform actions conditional on which agent type is identified

Viewed 41

Suppose I have 2 breeds of agents, sharks (stars and dots) and fish. The stars (large sharks) can eat dots (smaller sharks) and fish that are in-radius 0.5 of a star. A star should eat what is closest to it, either a dot or fish. For simplicity purposes, agents cannot move, and whether or not a star can feed depends on its spatial proximity to dots and fish during setup.

But i'm struggling to get the code to work. Specifically, I am encountering an "expected reporter" error message in my code and i cannot figure out how to solve this, although I don't know if fixing the code will achieve the aim. Any help is greatly appreciated.

Below is my code:

; Create the agents
breed [sharks shark]
breed [fishes fish]

sharks-own
[          
  energy                     
]

fishes-own
[
  x0                 ; Starting x-cor
  y0                 ; Starting y-cor
]


to setup

  ; Always start with this
  clear-all
  reset-ticks

  ask patches [set pcolor gray]                                     

  create-sharks 666
    [
      set color blue
      set energy 100               
                                   
                                    

      ifelse who >= 15
        [
          set shape "dot" set size 2.5                     
        ]
        [
          set shape "star" set size 3.5                
        ]
        
        setxy random-xcor random-ycor

    ] 

  create-fishes 300 
    [
      setxy random-xcor random-ycor                                     
      set x0 xcor
      set y0 ycor

           
      set shape "fish" 
      set size 2.5                                
                                
      set color white
    ] 
    
    
    ; this procedure gives an "expected reporter' error
    if shape = "star" 
    [
      ifelse min-one-of turtles with [shape = "dot"]
        [
          ifelse any? sharks with [shape = "dot"] in-radius 0.5      ; namely here         
            [                                                                                     
              fd 0
            ]
            [
              set energy (energy + 100)
              set color green
            ] ; End of 2nd ifelse
        ]
        [
          if min-one-of turtles with [shape = "fish"]
            [
              ifelse any? fishes with [shape = "fish"] in-radius 0.5
                [
                  fd 0
                ]
                [
                  set energy (energy + 1)
                  set color yellow
                ]
            ]
        ] ; End of 1st ifelse
    ] ; End of 1st if

end
2 Answers

You core issue is you are using ifelse min-one-of turtles with [shape = "dot"] as though min-one-of will give a true/false, but it will report a turtle. The error message NetLogo is giving is not great in this case. What I think you want to use any? in those cases (there are two of them that I see).

After those are resolved you have a context error, where you are checking if shape = "star" , but you aren't inside an ask [...] block where that check would have a turtle context to be valid. Maybe that's just a copy/paste issue in getting this question code ready, but I thought I'd note that, too.

Ok after many hours of blood, sweat and tears I finally arrived at the solution. In case the issue/objective is of interest to anyone else, the working code is as follows:

; Create the agents
breed [sharks shark]
breed [fishes fish]

sharks-own
[          
  energy
                       
]

fishes-own
[
  x0                 ; Starting x-cor
  y0                 ; Starting y-cor
]


to setup

  ; Always start with this
  clear-all
  reset-ticks

  ask patches [set pcolor gray]                                     

  create-sharks 666
    [
      set color blue
      set energy 100               
                                   
                                    

      ifelse who >= 10
        [
          set shape "dot" set size 2.5                     
        ]
        [
          set shape "star" set size 3.5                
        ]
        
        setxy random-xcor random-ycor

    ] 

  create-fishes 300 
    [
      setxy random-xcor random-ycor                                     
      set x0 xcor
      set y0 ycor

           
      set shape "fish" 
      set size 2.5                                
                                
      set color white
    ] 
    
    
    
    ask turtles [
      
      if shape = "star"
        [
          let turtleID min-one-of other turtles [distance myself]      ; Create a variable that identifies nearest turtle to apex
          
          ifelse [shape] of turtleID = "dot"                                    ; Evaluate whether the nearest turtle is a small meso
            [
              ifelse distance turtleID < 0.5     ; check if prey is still close enough
                [
                  ; do this if within radius 0.5
                  set color green
                ]
                [
                  ; do this if not within radius 0.5
                  set color yellow
                ]
            ]
            [
              if [shape] of turtleID = "fish"                                   ; Evaluate whether the nearest turtle is a fish
                [
                  ifelse distance turtleID < 0.5
                    [
                      ; do this if within radius 0.5
                      set color red
                    ]
                    [
                      ; otehrwise do this if not within radius 0.5
                      set color orange
                    ]
                ]
            ]
        ]
         

    
    ]

end
Related