Final Demo

No preview image

1 collaborator

Default-person Tanner Poling (Author)

Tags

(This model has yet to be categorized with any tags)
Visible to everyone | Changeable by the author
Model was written in NetLogo 6.1.1 • Viewed 67 times • Downloaded 9 times • Run 0 times
Download the 'Final Demo' modelDownload this modelEmbed this model

Do you have questions or comments about this model? Ask them here! (You'll first need to log in.)


Comments and Questions

Please start the discussion about this model! (You'll first need to log in.)

Click to Run Model

; to-do

; add threat communication amongst turtles
;   - any turtle that "sees" a threat (assuming field of view (fov) is a cone in front of it)
;     should broadcast a threat message about it
;   - this message should contain the threat level and the ID of the turtle sending it
;     (ID of a turtle is accessed via "who" keyword)

; add moving towards current highest threat
;   need to still maintain separation while pursuing threat

turtles-own [
  vx ; y-velocity
  vy ; x-velocity
  threat-active? ; true if currently pursuing threat
  threat-x
  threat-y
  threat-high?
  new-info?
]

patches-own [
  threat
]

; user-specified variables:
;   velocity-base: base speed
;   velocity-variance: how much to randomly change speed by
;   velocity-adjust: how much to speed up or slow down by
;   search-fov: field of view when searching for neighbors
;   search-radius: outward limit when searching for neighbors
;   vertical-dist-thresh: difference in y-coordinates that causes turtle to adjust speed


; global variables:
globals [
  num-turtles
  turtle-spacing ; how much distance turtles keep between themselves in line
  threat-speed-low
  threat-speed-high

  turtle-detect-radius
  turtle-comms-radius
  turtle-comms-speed
  turtle-default-color
  turtle-with-info-color

  avoidance-radius

  obj-x
  obj-y

]


; starting positions of turtles is pre-defined (using origin at bottom-left corner)

to setup
  clear-all
  reset-ticks

  set num-turtles 5
  set turtle-spacing 40

  set turtle-default-color green
  set turtle-with-info-color red
  set turtle-detect-radius 70
  set turtle-comms-radius 180
  set turtle-comms-speed 1

  set avoidance-radius 25

  set threat-speed-low 8
  set threat-speed-high 14

  create-turtles num-turtles
  ask turtles [
    set color turtle-default-color
    setxy (125 + who * turtle-spacing) 50
    set size 10
    set heading 0

    set vx 0
    set vy velocity-base
    set threat-active? false
    set threat-high? false
    set new-info? false
  ]

  if obstacles? [ spawn-threats ]
end 

to spawn-threats
  spawn-obstacle 40 110 90 30 green
  spawn-obstacle 30 220 70 20 orange
  spawn-obstacle 260 330 70 20 red
end 


; draws a rectangular obstacle
; x,y = coords of upper left corner
; clr = color

to spawn-obstacle [ x y width height clr ]
  ask patches with
    [ pxcor >= x and pxcor <= x + width
      and
      pycor >= y and pycor <= y + height ] [ set pcolor clr ]
end 

to go

  ask turtles [

    ; new-info? restricts turtles that just received info from transmitting in the same tick
    ; this creates a cascading effect
    set new-info? false

    ; if this turtle is near an obstacle, analyze obstacle
    if near-obstacle [
      ; load object data
      let obstacle detect-obj

      ; determine whether to focus on this new threat
      if compare-detected-threat obstacle [ ; returns true if detected obstacle is higher threat than previous
        set color [ pcolor ] of obstacle
        if color = orange or color = red [ set threat-active? true ]
        ifelse color = red
        [ set threat-high? true ]
        [ set threat-high? false ]
        set threat-x ([pxcor] of obstacle)
        set threat-y ([pycor] of obstacle)
      ]

    ]

    ; see what other turtles have found
    communicate-neighbors

    ifelse threat-active?
    [ towards-threat threat-x threat-y ]
    [ check-neighbors ]
    move

  ]

  tick
end 

to-report compare-detected-threat [ obstacle ]

  ; compare new threat to last seen threat, return true if higher than last seen threat
  (ifelse
    color = green
    [ ifelse [ pcolor ] of obstacle = orange or [ pcolor ] of obstacle = red
      [ report true ]
      [ report false] ]
    color = orange
    [ ifelse [ pcolor ] of obstacle = red
      [ report true ]
      [ report false ] ]
    color = red [ report false ]
    [ report false ])
end 

to-report compare-neighbor-threat [ transmitting-turtle ]
  ; compare new threat to last seen threat, return true if higher than last seen threat
  (ifelse
    color = green
    [ ifelse [ color ] of transmitting-turtle = orange or [ color ] of transmitting-turtle = red
      [ report true ]
      [ report false] ]
    color = orange
    [ ifelse [ color ] of transmitting-turtle = red
      [ report true ]
      [ report false ] ]
    color = red [ report false ]
    [ report false ])
end 

; look at neighbors and see if they have found any threats

to communicate-neighbors
  if (ticks mod turtle-comms-speed = 0) and (near-turtle-with-info) [

    ; pick nearby turtle with useful info
    let transmitting-turtle get-turtle-with-info

    if compare-neighbor-threat transmitting-turtle [  ; recieved message is at higher level of threat than current
      ; receive obstacle info from other turtle
      set color ([color] of transmitting-turtle)
      set threat-x ([threat-x] of transmitting-turtle)
      set threat-y ([threat-y] of transmitting-turtle)
      ;set obj-threat ([obj-threat] of transmitting-turtle)
    ]

    ; turtle cannot share information until turtle-comms-speed ticks have passed
    set new-info? true
  ]
end 

; returns true if turtle is within range of a transmitting turtle

to-report near-turtle-with-info
  report (any? turtles in-radius turtle-comms-radius with [(color != green) and not (new-info?) ])
end 

; returns transmission from nearby turtle

to-report get-turtle-with-info
  report (one-of turtles in-radius turtle-comms-radius with [(color != green)])
end 

to-report near-obstacle
  report (any? patches in-radius turtle-detect-radius with [ pcolor != black ])
end 

; returns data from nearby object

to-report detect-obj
  report (one-of patches in-radius turtle-detect-radius with [(pcolor != black)])
end 

to check-neighbors
  ; every turtle checks turtle to left and right of them
  ; in reality this check is within some cone going out to left and right of them

  ; if neither left or right neighbor
    ; try to find neighbors
  ; else if left neighbor
    ; calculate left y difference
    ; test if too far ahead of left neighbor, too far behind left
  ; else if right neighbor
    ; calculate right y difference
    ; test if too far ahead of right neighbor, too far behind right

  ; rotate left and find neighbors in a cone with range 30 degrees, radius 25
  let left-neighbor min-one-of cone-search-neighbors 270 search-fov search-radius [ distance myself ]
  let right-neighbor min-one-of cone-search-neighbors 90 search-fov search-radius [distance myself ]

  (ifelse
    left-neighbor = nobody and right-neighbor = nobody ; nobody in sight
    [ find-neighbor ]
    left-neighbor != nobody ; found a left-side neighbor
    [
      let left-dist ycor - ( [ ycor ] of left-neighbor )
      (ifelse
        left-dist > vertical-dist-thresh [ slow-down ]     ; too far ahead
        left-dist * -1 > vertical-dist-thresh [ speed-up ] ; too far behind
        [ randomize-velocity ])                            ; proceed normally
    ]

    right-neighbor != nobody ; found a right-side neighbor
    [
      let right-dist ycor - ( [ ycor ] of right-neighbor )
      (ifelse
        right-dist > vertical-dist-thresh [ slow-down ]
        right-dist * -1 > vertical-dist-thresh [ speed-up ]
        [ randomize-velocity ])
    ]

    )
end 

to find-neighbor
  ; what to do if vision of neighbor is interrupted?
  ; go random direction? idk
  set vx random 15
  set vy random 15

    ; alt. idea: coin-flip to either speed up or slow down. code:
;  let coinflip? (random 1 = 0)
;  ifelse coinflip?
;    [ speed-up ]
;    [ slow-down ]
end 

to slow-down
  set vy vy - velocity-adjust
  set vx 0
end 

to speed-up
  set vy vy + velocity-adjust
  set vx 0
end 

to towards-threat [ x y ]
  ; current idea: have x and y coords of threat / turtle who saw threat
  ; can easily adapt to take in ID of turtle and just get coordinates via:
  ; [ xcor ] of turtle-id, [ ycor ] of turtle-id
  ; make unit vector from x and y coords, scale by different "threat-speeds" to get vx, vy
  let magnitude sqrt((x - xcor) ^ 2 + (y - ycor) ^ 2)
  let unit-x (x - xcor) / magnitude
  let unit-y (y - ycor) / magnitude
  ifelse threat-high?
    [ set vx threat-speed-high * unit-x
      set vy threat-speed-high * unit-y ]
    [ set vx threat-speed-low * unit-x
      set vy threat-speed-low * unit-y ]
end 

to randomize-velocity
  set vy velocity-base + (velocity-variance * random-float 1)
  set vx 0
  ; range = [velocity-base, base + variance]
end 

to-report cone-search-neighbors [rotation fov-angle dist]
  set heading rotation ; rotate to given angle: north = 0, 90 = east, west = 270
  if visualize? [
    let patchesToColor patches in-cone dist fov-angle
    ask patchesToColor [
      set pcolor red ]
    ask patches in-cone dist fov-angle [ set pcolor black ]
    if obstacles? [ spawn-threats ]
  ]
  let result other turtles in-cone dist fov-angle
  set heading 0 ; reset back to original heading (assumed north, 0)

  report result  ; think of "report" as a return statement in regular code
end 

to move
  avoid-near-obstacles
  setxy (xcor + vx) (ycor + vy)
end 

to avoid-near-obstacles
  if (any? patches in-radius avoidance-radius with [ pcolor != black ]) [
    let nearest-obstacle min-one-of (patches in-radius avoidance-radius with [ pcolor != black ]) [ distance myself ]
    ifelse [ pcolor ] of nearest-obstacle = green
    [ set vx 5 ]
    [ towards-threat (- 1 * [ pxcor ] of nearest-obstacle) (-1 * [ pycor ] of nearest-obstacle) ]
  ]
end 

There is only one version of this model, created over 4 years ago by Tanner Poling.

Attached files

No files

This model does not have any ancestors.

This model does not have any descendants.