simulationnetlogocellular-automata

Error: "expected command in 'choose-exit'" evacuation simulation


I'm getting an error message saying expected command in 'choose-exit' and the issue seems to be in the line "choose-exit patch-here" within the "initialize-passengers" procedure. I've checked the code and everything seems correct, so I'm not sure what could be causing the problem.

breed [  passengers    passenger]
breed [  fire-spots  a-fire-spot]
breed [ smoke-spots a-smoke-spot]

globals [        exit1            ;;   pID
                 exit2            ;;   pID
                 exits-list       ;; [ pID ... ]
                 passenger_count  ;;   integer
  
]

passengers-own [ in-seat?         ;; boolean
                 safe?            ;; boolean
                 dead?            ;; boolean
                 panic?           ;; boolean
                 current-heading  ;; turtleHDG
                 target-exit      ;;   pID
                 my-exits-list    ;; [ pID ... ]
                 item?            ;; boolean
]

patches-own [    accessible?      ;; boolean
                 fire?            ;; boolean
]

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; FUNZIONI SETUP ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
to setup
   
   __clear-all-and-reset-ticks
     ;;;;;; DEBUG
            type "----------------------------[SETUP]\n"
     ;;;;;; DEBUG
     initialize-globals
     initialize-train
     initialize-exits
     initialize-passengers
   ; initialize-fire
     reset-ticks
     
end


to initialize-globals
   
   set exit1           patch 64 -13
   set exit2           patch 13 -54
   set exits-list    ( list exit1 exit2 )
   
end


to initialize-train
   import-pcolors "C:/Users/palli/OneDrive/Desktop/Modello_base/images/ambiente.png"
   ask patches [
     if pcolor = 86.6 [set pcolor cyan]
     if pcolor =  0   [set pcolor black]
     if pcolor = 64.3 [set pcolor green]
     if pcolor =  9.9 [set pcolor white]
       
     set fire? false
     set accessible? false ; Imposta tutte le patch come inaccessibili di default
   ]
   
   ask patches with [pcolor = white] [
     set accessible? true ; Contrassegna le patch verdi come accessibili
   ]
end


to initialize-exits
  set exit1 patch 64 -13
  set exit2 patch 13 -54
  set exits-list (list exit1 exit2)
end


to initialize-passengers
  create-passengers passenger-count [
    set shape "person business"
    set size 2
    set color yellow
    set in-seat? false  ; Cambia da true a false
    set safe? false
    set dead? false
    set panic? true
    set current-heading 0
    set my-exits-list [] ; SET here, it must assign a []-list to this property BEFORE next call to choose-exit
    set target-exit nobody ; SET this as the initial target-exit
    
    ; Aggiungi il seguente blocco di codice per posizionare casualmente i passeggeri in patch accessibili
    let target one-of patches with [accessible?]
    move-to target
    set target-exit choose-exit patch-here ; Aggiorna il target-exit dopo esserti spostato sulla patch iniziale
  ]
end



to-report choose-exit [current-position]  ;; still ignore the passed parameter-value

   let possible-exits        []
   let shortest-distance 10000
   let nearest-exit          nobody

   foreach exits-list [ exitUnderTest ->

           let exit-coords (list [pxcor] of exitUnderTest [pycor] of exitUnderTest) ;; Get exit coordinates as a list
           let exit-distance distance exitUnderTest

           if  exit-distance < shortest-distance [
               set shortest-distance exit-distance
               set nearest-exit      exitUnderTest
               set possible-exits    []
           ]

           if  exit-distance = shortest-distance [
              
               set possible-exits lput exit-coords possible-exits ;; Use exit coordinates instead of the patch object
                      
           ]
   ]

   if length possible-exits = 0 [
       print "No exits found."
       report nobody
   ]

   let lucky-exit-coords    one-of     possible-exits
   let lucky-exit           patch-at ( item 0 lucky-exit-coords )
                                     ( item 1 lucky-exit-coords ) ;; Get the object using the coordinates
   set target-exit          lucky-exit
   
   
   set my-exits-list   lput lucky-exit my-exits-list
         
   report lucky-exit
end


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; FUNZIONI GO ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
to go
   ;;;;;; DEBUG
          type "----------------------------[GO.tick]\n"
   ;;;;;; DEBUG
     
   ask passengers [
       if panic? [
          let  target-to-exit choose-exit patch-here
          face target-to-exit
       ]
       move
   ]
   tick
end
;;;go()

to move
   
   if panic? [
      move-to target-exit
      if  patch-here = target-exit [
          set safe?    true
          set in-seat? false
      ]
   ]
end
;;;move()

I have solved the previous 2 problems, This problem occurred to me, the passengers move but do not go towards the exit in a smart way and remain in the centre moving around, they also move out of the edge of the room. How can I solve it?


Solution

  • enter image description here

    The MCVE syntax needed a few touches, feel free to improve it further ( other components like initialize-fire remained still missing in the MCVE ) :

    breed [  passengers    passenger]
    breed [  fire-spots  a-fire-spot]
    breed [ smoke-spots a-smoke-spot]
    
    globals [        exit1            ;;   pID
                     exit2            ;;   pID
                     exits-list       ;; [ pID ... ]
                     passenger_count  ;;   integer
    ]
    
    passengers-own [ in-seat?         ;; boolean
                     safe?            ;; boolean
                     dead?            ;; boolean
                     panic?           ;; boolean
                     current-heading  ;; turtleHDG
                     target-exit      ;;   pID
                     my-exits-list    ;; [ pID ... ]
                     item?            ;; boolean
    ]
    
    patches-own [    accessible?      ;; boolean
                     fire?            ;; boolean
    ]
    
    ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; FUNZIONI SETUP ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
    to setup
       
       __clear-all-and-reset-ticks
         ;;;;;; DEBUG
                type "----------------------------[SETUP]\n"
         ;;;;;; DEBUG
         initialize-globals
         initialize-train
         initialize-exits
         initialize-passengers
       ; initialize-fire
         reset-ticks
         
    end
    
    
    to initialize-globals
       
       set exit1           patch 64 -13
       set exit2           patch 13 -54
       set exits-list    ( list exit1 exit2 )
       set passenger_count 2
    end
    
    
    to initialize-train
       
     ; import-pcolors "C:/Users/palli/OneDrive/Desktop/Modello_base/images/ambiente.png"
       ask patches [
           if pcolor = 86.6 [set pcolor cyan]
           if pcolor =  0   [set pcolor black]
           if pcolor = 64.3 [set pcolor green]
           if pcolor =  9.9 [set pcolor white]
           
           set fire? false
       ]
       ask patches [
           set accessible? false
       ]
       ask patches with [ pxcor >   5 and
                          pxcor <  64 and
                          pycor >  -5 and
                          pycor < -54 ] [
           set accessible? true
       ]
    end
    ;;;initialise-train()
    
    to initialize-exits
       
       set exit1        patch 64 -13
       set exit2        patch 13 -54
       set exits-list ( list exit1 exit2 )
    end
    ;;;initialize-exits()
    
    to initialize-passengers
       
       create-passengers passenger_count [
            ; set shape          "person business"
              set size            2
              set color           yellow
              set in-seat?        true
              set safe?           false
              set dead?           false
              set panic?          false
              set current-heading 0
              set my-exits-list   []                     ; SET here as SET target-exit calls choose-exit()-method, where this attribute must have been already assigned a list-value
              set target-exit     choose-exit patch-here ; SET this as the initial target-exit
      ]
      ask passengers [
          let     target one-of patches with [accessible?]
        ; HANDLE a case resulting in an empty agentSet { patches with ... }
          ;;;;;;; DEBUG                     face target
                  type word who "-will TRY: face target\n"
                  type word "AFTER:" word patches with [accessible?] "IN patches with [accessible?]\n"
                  type word "WITH:" word target  "IN target\n"
          face    target
                  type "EXEC'd...PASS\n\n"
          ;;;;;; DEBUG ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 
          move-to target
      ]
    end
    ;;;initialize-passengers()
    
    to-report choose-exit [current-position]  ;; you still ignore the passed parameter-value
    
       let possible-exits        []
       let shortest-distance 10000
       let nearest-exit          nobody
    
       foreach exits-list [ exitUnderTest ->
    
               let exit-coords (list [pxcor] of exitUnderTest [pycor] of exitUnderTest) ;; Get exit coordinates as a list
               let exit-distance distance exitUnderTest
    
               if  exit-distance < shortest-distance [
                   set shortest-distance exit-distance
                   set nearest-exit      exitUnderTest
                   set possible-exits    []
               ]
    
               if  exit-distance = shortest-distance [
                   ;;;;;; DEBUG                     set possible-exits lput exit-coords possible-exits
                          type word who "-will TRY: set possible-exits lput exit-coords possible-exits\n"
                          type word "WITH:" word exit-coords          "IN exit-coords\n"
                          type word "WITH:" word possible-exits       "IN possible-exits\n"
                   set possible-exits lput exit-coords possible-exits ;; Use exit coordinates instead of the patch object
                          type "EXEC'd...PASS\n\n"
                   ;;;;;; DEBUG ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 
               ]
       ]
    
       if length possible-exits = 0 [
           print "No exits found."
           report nobody
       ]
    
       let lucky-exit-coords    one-of     possible-exits
       let lucky-exit           patch-at ( item 0 lucky-exit-coords )
                                         ( item 1 lucky-exit-coords ) ;; Get the object using the coordinates
       set target-exit          lucky-exit
       
       ;;;;;; DEBUG                     set my-exits-list lput lucky-exit my-exits-list
              type word who "-will TRY: set my-exits-list lput lucky-exit my-exits-list\n"
              type word "WITH:" word lucky-exit     "IN lucky-exit\n"
              type word "WITH:" word my-exits-list  "IN my-exits-list\n"
       set my-exits-list   lput lucky-exit my-exits-list
              type "EXEC'd...PASS\n\n"
       ;;;;;; DEBUG ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 
       report lucky-exit
    end
    ;;;report choose-exit()
    
    ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; FUNZIONI GO ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
    to go
       ;;;;;; DEBUG
              type "----------------------------[GO.tick]\n"
       ;;;;;; DEBUG
         
       ask passengers [
           if panic? [
              let  target-to-exit choose-exit patch-here
              face target-to-exit
           ]
           move
       ]
       tick
    end
    ;;;go()
    
    to move
       
       if panic? [
          move-to target-exit
          if  patch-here = target-exit [
              set safe?    true
              set in-seat? false
          ]
       ]
    end
    ;;;move()
    

    This works with respect to failing to meet the lput-syntax with a list-instance ( the call to-initialize-passengers asked a not-yet assigned passenger-property [my-exits-list], which was until its later 1st assignment pre-initialised with a system default integer value of 0 )

    Q/A-PASS-ed

    A next problem is straightforward in the debug-report :
    Q/A-FAIL-s

    ----------------------------[SETUP]
    0-will TRY: set possible-exits lput exit-coords possible-exits
    WITH:         [-2 -13] IN exit-coords
    WITH:               [] IN possible-exits
    EXEC'd...PASS
    
    0-will TRY: set my-exits-list lput lucky-exit my-exits-list
    WITH:   (patch -2 -13) IN lucky-exit
    WITH:               [] IN my-exits-list
    EXEC'd...PASS
    
    1-will TRY: set possible-exits lput exit-coords possible-exits
    WITH:         [-2 -13] IN exit-coords
    WITH:               [] IN possible-exits
    EXEC'd...PASS
    
    1-will TRY: set my-exits-list lput lucky-exit my-exits-list
    WITH:   (patch -2 -13) IN lucky-exit
    WITH:               [] IN my-exits-list
    EXEC'd...PASS
    
    1-will TRY: face target
    AFTER:   (agentset, 0 patches) IN patches with [accessible?]
    WITH:                   nobody IN target
    

    A good, robust code has to assume corner-cases, where empty-[]-instances might crash an otherwise reasonably meant code.

    Go solve these cases, with delivering some emergency-case value for those cases, where a []-instance resulted in being empty.

    As per comment below - MCVE-code, without having the variable passenger-count defined (was perhaps "injected" from a GUI-interface design, likely via a field or a slider) at least as a mock-up fix, was not complete ( if you have added some other code-sections later, not published in the MCVE above, there you go and now have to remove the mock-up fix or solve the new errors introduced by/with the new code-section added as appropriate ) :

    enter image description here

    "People are not born in the environment I created but in the top left-hand corner and do not move. Why?"

    Because the create-passengers passenger_count [...]-code does not specify any particular randomisation of the passenger-instances initial locations. Code it there and you can have whatever behaviour upon "birth" w.r.t. environment ( as per loaded picture ?)

    Why the passengers do not move? Check your code and contents of variables, if you want other than the so far coded behaviour. The current code has a global named panic? which was pre-assigned a false, i.e. the if panic? [...]-clause will never execute the commands there, unless something somewhere at some unknown time switch the boolean panic? into a true-state.