Search code examples
netlogopatch

How to simplify ifelse argument Netlogo


I have this code and im getting the error "expected command" I know it most certainly its a brackets problem but I ve looked and cant figure out where is it even when netlogo is showing it.

the second If else is the one highlighted by the error, the one with distancia agua. I think a fresher and more keen set of eyes can spot it in no time. Thanks in advance.

ifelse ambiente_natural[move-to one-of patches with [not any? other turtles in-radius 6 and pcolor = 55 and pycor < 80]ask patches in-radius (1 + random 2) [if pcolor != blue [set pcolor 35]]ask patches with [pycor > 80] [set pcolor 68]]
    [
      move-to one-of patches with 
      [not any? other turtles in-radius 6 and pcolor = 55]
      [ifelse distancia_agua <= 15 
        [ask patches in-radius (2 + random 2) 
          [if pcolor != blue 
            [set pcolor 35]
        ]
        ]
      
        [ifelse distancia_agua > 15 and distancia_agua <= 35 
          [ask patches in-radius (1 + random 2) [if pcolor != blue 
            [set pcolor 35]
          ]
          ]
        
          [if distancia_agua > 35 and <= 60 
            [ask patches in-radius 1 [if pcolor != blue 
              [set pcolor 35]
    ]
    ]
    ]
    ]
    ]
    ]

Solution

  • For ease of debugging, you may find it helpful to have a very consistent structure- then you can highlight all your code and hit "Tab" to auto-format and hopefully identify the problem area. Here is one version of that- I commented the two places that seem to be the problems:

    globals [ ambiente_natural distancia_agua ]
    
    to test
      ifelse ambiente_natural 
      ; Ifelse 1-1
      [
        move-to one-of patches with [not any? other turtles in-radius 6 and pcolor = 55 and pycor < 80] 
        ask patches in-radius (1 + random 2) [ 
          if pcolor != blue [set pcolor 35] 
        ]
        ask patches with [pycor > 80] [
          set pcolor 68
        ]
      ]
      ; Ifelse 1-2
      [
        move-to one-of patches with [not any? other turtles in-radius 6 and pcolor = 55]
        ; There was an extra brace here
        ifelse distancia_agua <= 15 
        ; Ifelse 2-1
        [ 
          ask patches in-radius (2 + random 2) [ 
            if pcolor != blue [
              set pcolor 35
            ]
          ]
        ]
        ; Ifelse 2-2
        [ 
          ifelse distancia_agua > 15 and distancia_agua <= 35 
          ; Ifelse 3-1
          [
            ask patches in-radius (1 + random 2) [
              if pcolor != blue [
                set pcolor 35
              ]
            ]
          ]
          ; Ifelse 3-2
          [   
            if distancia_agua > 35 and distancia_agua <= 60 [ ; This wasmissing the second "distancia_agua"
              ask patches in-radius 1 [
                if pcolor != blue [
                  set pcolor 35
                ]
              ]
            ]
          ]
        ]
      ]
    end