natapc
natapc

Reputation: 11

Agents leaving white patches in NetLogo

I'm working on a fire escape simulation in NetLogo and I have a problem with agents leaving the white area. The main idea is that I have a number of agents and they spawn randomly on white area and then, they face one of the green exit and they're supposed to go there but I can't find the right solution to make it work.

this is my code

globals [
  evacuated-count
  evacuation-time
  exits
]

turtles-own [
  speed
  target
]

to setup
  clear-all
  setup-environment
  create-pedestrians
  set evacuated-count 0
  reset-ticks
end

to setup-environment
  ;; Rysowanie korytarzy (poziome)
  

  ;; Rysowanie czarnych cienkich ścian (ramki) wokół pokoi i korytarzy
  draw-wall -15 8 10 6 black   ;; Górny lewy pokój
  draw-wall -5 8 10 6 black    ;; Górny środkowy pokój
  draw-wall 5 8 10 6 black     ;; Górny prawy pokój
  
  draw-wall -15 -2 10 6 black  ;; Dolny lewy pokój
  draw-wall -5 -2 10 6 black   ;; Dolny środkowy pokój
  draw-wall 5 -2 10 6 black    ;; Dolny prawy pokój

  draw-rectangle -15 10 30 3 white  ;; Górny korytarz
  draw-rectangle -15 0 30 3 white   ;; Środkowy korytarz
  draw-rectangle -15 10 3 25 white

  ;; Tworzenie pokojów (kolory: żółty i pomarańczowy)
  draw-rectangle -11 6 8 5 white    ;; Górny lewy pokój
  draw-rectangle -2 6 8 5 white     ;; Górny środkowy pokój
  draw-rectangle 7 6 8 5 white      ;; Górny prawy pokój

  draw-rectangle -11 -4 8 5 white   ;; Dolny lewy pokój
  draw-rectangle -2 -4 8 5 white    ;; Dolny środkowy pokój
  draw-rectangle 7 -4 8 5 white     ;; Dolny prawy pokój

  ;; Rysowanie wyjść zgodnie z oznaczeniami na planie
  ;; Wyjścia z pokojów na korytarze ()
  ask patches with [
    (pxcor = -7 and pycor = 1) or (pxcor = 0 and pycor = 1) or (pxcor = 10 and pycor = 1) or
    (pxcor = -7 and pycor = -3) or (pxcor = 0 and pycor = -3) or (pxcor = 10 and pycor = -3)
  ] [
    set pcolor white  ;; czerwone wyjścia z pokojów
  ]

  ;; Główne wyjścia z budynku (zielone kółka)
  ask patches with [
    (pxcor = 14 and pycor = 9) or
    (pxcor = -14 and pycor = -14)
  ] [
    set pcolor green  ;; wyjścia z budynku
  ]
  
  ;; Zbierz wszystkie wyjścia do zmiennej `exits`
  set exits patches with [pcolor = green]
end

to draw-rectangle [x y width height room-color]
  ask patches with [
    pxcor >= x and pxcor < (x + width) and
    pycor <= y and pycor > (y - height)
  ] [
    set pcolor room-color
  ]
end

to draw-wall [x y width height wall-color]
  ask patches with [
    pxcor >= x and pxcor < (x + width) and
    pycor <= y and pycor > (y - height)
  ] [
    set pcolor wall-color
  ]
end

to create-pedestrians
  create-turtles num-pedestrians [
    ;; Losowe rozmieszczenie pieszych w pokojach i korytarzach
    let spawn-patch one-of patches with [
      (pcolor = white or pcolor = yellow or pcolor = orange or pcolor = red) and not any? turtles-here
    ]
    if spawn-patch != nobody [
      move-to spawn-patch
      set color blue
      set size 1.5
      set speed random-float-between 0.3 0.5
      set target one-of exits ;; Ustaw cel jako jedno z wyjść
    ]
  ]
end

to move-pedestrians
  ask turtles [
    ;; Sprawdź, czy agent dotarł do wyjścia
    if target != nobody and distance target < 0.5 [
      set evacuated-count evacuated-count + 1
      die
    ]

    ;; Jeśli `target` jest nieprawidłowy, przypisz nowe wyjście
    if target = nobody or not member? target exits [
      set target min-one-of exits [distance myself]
    ]

    ;; Sprawdzenie, czy patch przed agentem jest przeszkodą (szary lub czarny kolor)
    let next-patch patch-ahead 1
    ifelse ([pcolor] of next-patch = white or [pcolor] of next-patch = red) [
      face target
      fd speed  ;; Poruszaj się w kierunku celu
    ] [
      rt random 90
      lt random 90  ;; Obrót w losową stronę, aby znaleźć wolny patch
    ]
  ]
end



to go
  move-pedestrians
  if not any? turtles [
    set evacuation-time ticks
    print (word "Evacuation completed in " evacuation-time " ticks.")
    stop
  ]
  tick
end

to-report random-float-between [#min #max]
  report #min + random-float (#max - #min)
end

Not sure what to do with it. Please send help

I tried a hundred different versions but I just can't seem to make it work. Any tips?

Upvotes: 0

Views: 41

Answers (1)

Bart de Bruin
Bart de Bruin

Reputation: 61

The problem is in the ordering of heading and movement. Because you change your heading (face target) after you decided to move forwards (check whether the color of the next patch is white or red) agents end up on a black patch, where they get stuck.

Something like this would fix it:

let my-heading heading
face target
let next-patch patch-ahead 1
if [pcolor] of next-patch = white or [pcolor] of next-patch = green [
  fd speed  ;; Poruszaj się w kierunku celu
  stop
]
set heading my-heading
set next-patch patch-ahead 1
if [pcolor] of next-patch = white or [pcolor] of next-patch = green [
  fd speed  ;; Poruszaj się w kierunku celu
  stop
]
  rt random 90
  lt random 90  ;; Obrót w losową stronę, aby znaleźć wolny patch

This still causes agents to be stuck in the white rooms, but they don't end up on the black colored patches.

Upvotes: 2

Related Questions