street robbery simulation

street robbery simulation preview image

1 collaborator

Default-person Amrutha S (Author)

Tags

(This model has yet to be categorized with any tags)
Visible to everyone | Changeable by everyone
Model was written in NetLogo 5.0.3 • Viewed 840 times • Downloaded 67 times • Run 0 times
Download the 'street robbery simulation' 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

globals[tmp num-of-rob num-of-gang-rob total-victims current-criminal 
  current-health nearby-person nearby-women nearest-criminal other-criminal 
  gang-crime-factor max-gang-wealth max-health max-wealth avg-reward avg-reward-crime 
  episode fail win helped]

breed[people person]
breed[criminals criminal]
breed[police officer]
breed[women woman]
breed[ATM Atms]

police-own[current-help current-lady current-police]
turtles-own[init-xcor init-ycor ]
people-own[avg-wealth-people age wealth to-walk? to-be-rob? reward qtable Q Qnew fight crime-table robbed]
women-own[avg-wealth-women wealth to-walk? to-be-rob? reward qtable Q Qnew crime-table work robbed]
criminals-own[avg-wealth-criminals crime-factor gang-mates nearby-criminal 
  nearest-neighbor wealth health reward qtable Q Qnew to-walk? to-be-rob?]

to setup
  clear-all
  show date-and-time
  set avg-reward 0
  set avg-reward-crime 0
  set episode 0
  ifelse choose-road = "Road 1"
  [set-road]
  [set-path]  
  
  create-ATM 1
  [
    set shape "building store"
    set size 5
     move-to one-of patches with [not any? turtles-on self ]
    setxy 4 8
  ]
  create-people num-of-person
  [
    set Qnew 0
    set Q 0
    set shape "person business"
    set size 2
    set wealth random 5000
    set age random 50
    set to-walk? false
    set to-be-rob? false
    set reward 0
    set qtable [0]
    set crime-table []
    set robbed 0
    set avg-wealth-people mean [wealth] of people
    set init-xcor random-xcor
    set init-ycor random-ycor
    if age > 14
    [
      set fight random 2
      show word "fight " fight
    ]
    put-on-empty-road 
  ]
  
  create-women num-of-women
  [
    set Qnew 0
    set Q 0
    set shape "women"
    set size 2
    set wealth random 5000
    set to-walk? false
    set to-be-rob? false
    set reward 0
    set qtable [0]
    set crime-table []
    set robbed 0
    set avg-wealth-women mean [wealth] of women
    set init-xcor random-xcor
    set init-ycor random-ycor
    put-on-empty-road 
  ]
  
  create-criminals num-of-criminals
  [
    set shape "criminal"
    set size 2
    set wealth 0
    set crime-factor random 6
    show word "crime factor " crime-factor
    set init-xcor random-xcor
    set init-ycor random-ycor
    set health random 6
    show word "Health " health
    set Qnew 0
    set Q 0
    set to-walk? false
    set to-be-rob? false
    set reward 0
    set qtable [0]
    set fail 0
    set win 0
    put-on-empty-road 
  ]
  create-police num-of-police
  [
    set shape "person police"
    set size 2
    set helped 0
    set current-help nobody
    set init-xcor random-xcor
    set init-ycor random-ycor
    put-on-empty-road 
  ]
  
  set num-of-rob 0
  set num-of-gang-rob 0
  set total-victims num-of-women + num-of-person
  reset-ticks
end 

to set-road
  ask patches
    [ 
      set pcolor green
      if abs pxcor <= 1 or abs pycor <= 1
        [ set pcolor 9 ]
      
      if (abs pxcor <= 10 and abs pxcor > 7) or (abs pycor <= 10 and abs pycor > 7)
        [set pcolor 9 ]     
    ]
end 

to set-path
  clear-all
  ask patches
  [
    set pcolor green
    if (pxcor >= -9 and pxcor <= -8) and (pycor < 17 and pycor > -17)
    [set pcolor 9]
    if (pxcor >= -9 and pxcor <= 17) and (pycor < 11 and pycor > 8)
    [set pcolor 9]
    if (pycor >= -1 and pycor < 1) and (pxcor > -17 and pxcor < 17)
    [set pcolor 9]
    if (pycor >= -9 and pycor < -7) and (pxcor > -17 and pxcor < 5)
    [set pcolor 9]
    if (pycor >= -16 and pycor < -9) and (pxcor > 2 and pxcor < 5)
    [set pcolor 9]
    if (pycor >= -9 and pycor < -7) and (pxcor > -17 and pxcor < 5)
    [set pcolor 9]
    if (pycor >= -7 and pycor < -1) and (pxcor > 0 and pxcor < 3)
    [set pcolor 9]
    if (pycor >= -12 and pycor < -10) and (pxcor >= 5 and pxcor <= 16)
    [set pcolor 9]
    if (pycor >= -16 and pycor < -1) and (pxcor > 9 and pxcor < 12)
    [set pcolor 9]
    if (pycor >= 1 and pycor <= 8) and (pxcor > -3 and pxcor < 0)
    [set pcolor 9]
    if (pycor >= 1 and pycor < 4) and (pxcor > 5 and pxcor < 8)
    [set pcolor 9]
    if (pycor >= 11 and pycor <= 16) and (pxcor > 1 and pxcor < 4)
    [set pcolor 9]
    if (pycor >= 11 and pycor <= 16) and (pxcor > 14 and pxcor < 17)
    [set pcolor 9]
    if (pycor > 3 and pycor < 6) and (pxcor > 5 and pxcor < 8)
    [set pcolor 9]
    if (pycor > 3 and pycor < 9) and (pxcor > 7 and pxcor < 10)
    [set pcolor 9]
    if (pycor > 6 and pycor < 9) and (pxcor > -17 and pxcor < -9)
    [set pcolor 9]
    if (pycor > 14 and pycor < 17) and (pxcor > -17 and pxcor < -9)
    [set pcolor 9]
    if (pycor > -17 and pycor < -8) and (pxcor > -4 and pxcor < -1)
    [set pcolor 9]
    if (pycor > -17 and pycor < -14) and (pxcor > -17 and pxcor < 3)
    [set pcolor 9]
    if (pycor > 3 and pycor < 6) and (pxcor > -8 and pxcor < 6)
    [set pcolor 9]
    if (pycor > -7.5 and pycor < -5) and (pxcor > 11 and pxcor < 17)
    [set pcolor 9]
    if (pycor > 3 and pycor < 6) and (pxcor > 9 and pxcor < 15)
    [set pcolor 9]
    if (pycor > 0 and pycor < 4) and (pxcor > 12 and pxcor < 15)
    [set pcolor 9]
    if (pycor > 10 and pycor < 17) and (pxcor > 8 and pxcor < 11)
    [set pcolor 9]
    if (pycor > -9 and pycor < -6) and (pxcor > 4 and pxcor < 7)
    [set pcolor 9]
    if (pycor > -7 and pycor < -4) and (pxcor > 4 and pxcor < 10)
    [set pcolor 9]
    if (pycor > 13 and pycor < 15) and (pxcor > -8 and pxcor < -3)
    [set pcolor 9]
    if (pycor > 10 and pycor < 15) and (pxcor > -4 and pxcor < -1)
    [set pcolor 9]
  ]
end 

to put-on-empty-road  ;; turtle procedure
  move-to one-of patches with [not any? turtles-on self and pcolor = 9]
end 

to go
  
  ask people 
  [
    move 
    set to-walk? true 
    set to-be-rob? false
    learn 
    ;show Q
    ; consume
    if any? ATM-on neighbors[go-to-work]
  ]
  ask women 
  [
    move 
    set to-walk? true 
    set to-be-rob? false
    learn 
    ;show Q
    ;consume
    if any? ATM-on neighbors[go-to-work]
  ]
  ask criminals with [crime-factor < 6]
  [
    move
    set to-walk? true 
    set to-be-rob? false
    make-gang
    consume
    learn-crime
    
  ]
  ask criminals with [crime-factor >= 6]
  [
    move
    set to-walk? true 
    set to-be-rob? false
    check-rob
    consume
    learn-crime
    
  ]
  ask police[move]
  if police-help?
  [police-help]
  tick
end 

to move
  if can-move? 1[
    ifelse  [pcolor] of patch-ahead 1 = green 
    [ lt random-float 360 ]   
    [ fd speed]
  ]
end 

to go-to-work 
  ;let count-down 201
  if wealth < 1000
  [
    show "going to work"
    move-to one-of ATM
    show ticks
    set wealth wealth + 500
    show word " wealth after work " wealth
    
  ]
end 

to consume
  if wealth >= 0
  [set wealth wealth / 2 ]
end 

to make-gang
  find-gangmates
  if count gang-mates > 1
    [
      find-nearest-neighbor
      ifelse [crime-factor] of nearest-neighbor > 8
        [
          separate
          move
        ]
        [ align
          cohere 
          check-rob-gang
        ]
    ]
end 

to find-gangmates
  set gang-mates other criminals in-radius vision with[crime-factor <= 5]
end 

to find-nearest-neighbor ;; turtle procedure
  set nearest-neighbor min-one-of gang-mates [distance myself]
end 

to separate  ;; turtle procedure
  turn-away ([heading] of nearest-neighbor) random-float 360
  print word nearest-neighbor "Got seperated from gang"
  print word "Because crime factor is "  [crime-factor]of nearest-neighbor
end 

to align  ;; turtle procedure
  turn-towards average-gang-mate-heading 3
end 

to cohere  ;; turtle procedure
  turn-towards average-heading-towards-gang-mates 3
end 

to turn-away [new-heading max-turn]  ;; turtle procedure
  turn-at-most (subtract-headings heading new-heading) max-turn
end 

to turn-towards [new-heading max-turn]  ;; turtle procedure
  turn-at-most (subtract-headings new-heading heading) max-turn
end 

to-report average-gang-mate-heading  ;; turtle procedure
                                     ;; We can't just average the heading variables here.
                                     ;; For example, the average of 1 and 359 should be 0,
                                     ;; not 180.  So we have to use trigonometry.
  let x-component sum [dx] of gang-mates
  let y-component sum [dy] of gang-mates
  ifelse x-component = 0 and y-component = 0
    [ report heading ]
    [ report atan x-component y-component ]
end 

to-report average-heading-towards-gang-mates  ;; turtle procedure
                                              ;; "towards myself" gives us the heading from the other turtle
                                              ;; to me, but we want the heading from me to the other turtle,
                                              ;; so we add 180
  let x-component mean [sin (towards myself + 180)] of gang-mates
  let y-component mean [cos (towards myself + 180)] of gang-mates
  ifelse x-component = 0 and y-component = 0
    [ report heading ]
    [ report atan x-component y-component ]
end 

to turn-at-most [turn max-turn]  ;; turtle procedure
  ifelse abs turn > max-turn
    [ ifelse turn > 0
      [ rt max-turn ]
      [ lt max-turn ] ]
    [ rt turn ]
end 

to check-rob-gang
  set nearest-criminal one-of gang-mates with-max[crime-factor]
  set gang-crime-factor [crime-factor] of nearest-criminal
  set max-gang-wealth [wealth]of nearest-criminal
  set max-health [health]of nearest-criminal
  set max-wealth mean[wealth]of gang-mates
  ;show word "max wealth " max-wealth
  ;set other-criminal[0]
  set other-criminal gang-mates 
  ;show word "gang criminals are" other-criminal
  
  ask nearest-criminal
  [
    
    if (nearest-criminal != nobody)
    [
      set nearby-person people in-radius 1 ;of nearest-neighbor
      set nearby-women one-of women in-radius 1 ;of nearest-neighbor 
      let nearby-police one-of police-on neighbors; of nearest-neighbor
      
      ifelse( nearby-police = nobody )
        [
          if(  nearby-women != nobody )
          [
            ifelse (gang-crime-factor > 3 and max-wealth < 1000)
            [
              do-gang-low-rob-women
              set to-be-rob? true
              set to-walk? false   
              learn-crime 
              set win win + 1
            ][set fail fail + 1]
          ] 
        ] 
        [set helped helped + 1]  
      
      ifelse( nearby-police = nobody  )
        [
          if (nearby-person != nobody )
          [
            ifelse (gang-crime-factor > 1 and max-wealth < 1000)
            [          
              do-gang-low-rob-kid
              set to-be-rob? true
              set to-walk? false   
              learn-crime 
              set win win + 1
            ][ set fail fail + 1 ]  
          ]  
        ]   [set helped helped + 1]  
      
    ]
  ]
end 

to check-rob
  ask criminals
  [
    set nearby-criminal criminals in-radius 1
    set current-criminal one-of nearby-criminal
    set current-health [health]of current-criminal
    ;set current-wealth [wealth]of current-criminal
    if (current-criminal != nobody )
    [
      let nearby-police [police-on neighbors] of current-criminal
      set nearby-person [people in-radius 1 ] of current-criminal
      set nearby-women [women in-radius 1] of current-criminal
      ifelse( count nearby-police = 0  )
      [
        ifelse (count nearby-person = 1 )[
          ask current-criminal[
            if (crime-factor > 8 and wealth < 500)
            [
              do-high-rob
              set to-be-rob? true
              set to-walk? false
              learn-crime
              set win win + 1 
            ]
            set crime-factor crime-factor
            
          ]
        ][set fail fail + 1 ]
      ] 
      [ set helped helped + 1]
      ifelse( count nearby-police = 0 )
      [
        ifelse(count nearby-women >= 1)[
          ask current-criminal[
            if (crime-factor >= 6 and wealth < 500)
            [
              do-low-rob-women
              set to-be-rob? true
              set to-walk? false
              learn-crime
              set win win + 1
            ]
            set crime-factor crime-factor
          ]
        ][set fail fail + 1]
      ]   
      [set helped helped + 1]
      ifelse( count nearby-police = 0  )
      [
        ifelse(count nearby-person >= 1 )[
          ask current-criminal[
            if (crime-factor > 8 and wealth < 1000)
            [
              do-low-rob-kid
              set to-be-rob? true
              set to-walk? false 
              learn-crime 
            ]
            set crime-factor crime-factor
            
          ]
        ][set win win + 1]
      ] 
      [set helped helped + 1]
    ]
    set avg-wealth-criminals mean [wealth] of criminals
    
  ]
end 

to do-high-rob
  
  ask nearby-person
    [
      if (wealth > 0 and age > 14 and fight = 1)
        [ 
          if current-health >= 8
          [
            set tmp 0
            output-print "Found a person to rob"    
            output-print  nearby-person    
            set tmp wealth / 2
            set wealth wealth / 2
            output-print  word "High robbery on tick" ticks 
            output-print word " By " current-criminal
            set num-of-rob num-of-rob + 1
            set pcolor red 
            set to-be-rob? true
            set to-walk? false
            set robbed robbed + 1
            output-show word "no: of repeated robbery " robbed
            set crime-table lput current-criminal crime-table
            ;show word "crime-table " crime-table
            learn
            add-wealth-to-criminal
            
          ]
          
          set avg-wealth-people mean [wealth] of people
        ] 
    ]  
end 

to do-low-rob-women
  ask nearby-women
  [
    if (wealth > 0)
      [ 
        set tmp 0
        output-print "Found a woman to rob"  
        output-show  nearby-women        
        set tmp wealth; / 2
        set wealth 0; wealth / 2
        output-print  word "Low robbery on tick" ticks 
        output-print word "By " current-criminal
        set num-of-rob num-of-rob + 1
        set pcolor red 
        set to-be-rob? true
        set to-walk? false
        set robbed robbed + 1
        output-show word "no: of repeated robbery " robbed
        set crime-table lput current-criminal crime-table
        ;show word "crime-table " crime-table
        learn
        add-wealth-to-criminal
        
      ]
    
    set avg-wealth-women mean [wealth] of women
    
  ]
end 

to do-low-rob-kid
  ask nearby-person
    [
      ifelse (wealth > 0 and age <= 14)
        [ 
          set tmp 0
          output-print "Found a kid to rob"  
          output-show  nearby-person         
          set tmp wealth ;/ 2  
          set wealth 0; wealth / 2
          output-print  word "Low robbery on tick" ticks 
          output-print word "By " current-criminal
          set num-of-rob num-of-rob + 1
          set pcolor red 
          set to-be-rob? true
          set to-walk? false
          set robbed robbed + 1
          output-show word "no: of repeated robbery " robbed
          set crime-table lput current-criminal crime-table
          ;show word "crime-table " crime-table
          learn
          add-wealth-to-criminal
          
        ]
        [
          if ( fight = 0 and age > 14 and wealth  > 0)
          [
            if (current-health <  4)
            [
              set tmp 0
              output-print "Found a scared person to rob" 
              output-show  nearby-person          
              set tmp wealth; / 2  
              set wealth 0; wealth / 2
              output-print  word "Low robbery on tick" ticks 
              output-print word "By " current-criminal
              set num-of-rob num-of-rob + 1
              set pcolor red 
              set to-be-rob? true
              set to-walk? false
              set robbed robbed + 1
              output-show word "no: of repeated robbery " robbed
              set crime-table lput current-criminal crime-table
              learn
              add-wealth-to-criminal
              
            ]
          ]
          
        ]
      
      set avg-wealth-people mean [wealth] of people
    ]
end 

to add-wealth-to-criminal
  ask current-criminal
    [
      output-print word " Crime factor of " current-criminal
      output-print word " is " crime-factor
      set crime-factor crime-factor + 1 
      output-print word "Crime factor updated to " crime-factor
      set wealth wealth + tmp   
      output-print word "Wealth updated to " wealth
      
      wait pause
    ]
end 

to add-wealth-to-gangmates
  ask other-criminal
    [
      set wealth wealth + tmp
      set crime-factor crime-factor + 1
      output-show word "WEALTH OF GANG MATES IS " wealth
      output-show word "CRIME FACTOR OF GANG MATES IS " crime-factor
      
      wait pause
    ]
end 

to do-gang-low-rob-women
  ask nearby-women
  [
    if (wealth > 0)
      [ 
        set tmp 0
        output-print "Found a woman to rob by gang"   
        output-show   nearby-women     
        set tmp wealth / 2
        set wealth wealth / 2
        output-print  word "Gang low robbery on tick" ticks 
        set num-of-gang-rob num-of-gang-rob + 1
        set pcolor red 
        set to-be-rob? true
        set to-walk? false
        set robbed robbed + 1
        output-show word "no: of repeated robbery " robbed
        set crime-table lput nearest-criminal crime-table
        ;show word "crime-table " crime-table
        learn
        add-wealth-to-gangmates
        
      ]
    set avg-wealth-women mean [wealth] of women
  ]
end 

to do-gang-low-rob-kid
  ask nearby-person
    [
      ifelse (wealth > 0 and age <= 14)
        [ 
          
          output-print nearest-criminal
          set tmp 0
          output-print "Found a kid to rob by gang" 
          output-show nearby-person     
          set tmp wealth / 2
          set wealth wealth / 2
          output-print  word "Low robbery on tick" ticks 
          set num-of-gang-rob num-of-gang-rob + 1
          set pcolor red 
          set to-be-rob? true
          set to-walk? false
          set robbed robbed + 1
          output-show word "no: of repeated robbery " robbed
          set crime-table lput nearest-criminal crime-table
          ;show word "crime-table " crime-table
          learn
          add-wealth-to-gangmates
          
        ]
        [
          if (wealth > 0 and age > 14 and  fight = 0)
            [
              if max-health < 3
              [ 
                output-print nearest-criminal
                set tmp 0
                output-print "Found a scared person to rob by gang"     
                output-show nearby-person       
                set tmp wealth / 2
                set wealth wealth / 2
                output-print  word "Low robbery on tick" ticks 
                set num-of-gang-rob num-of-gang-rob + 1
                set pcolor red 
                set to-be-rob? true
                set to-walk? false
                set robbed robbed + 1
                output-show word "no: of repeated robbery " robbed
                set crime-table lput nearest-criminal crime-table
                ;show word "crime-table " crime-table
                learn
                add-wealth-to-gangmates
                
              ]
            ]
        ]
      
      set avg-wealth-people mean [wealth] of people
    ]  
end 

to police-help
  ask police
  [
    set current-help one-of people-on neighbors ;with [wealth > 0 or robbed > 0]
    set current-lady one-of women-on neighbors ;with [wealth > 0 or robbed > 0]
    ;set current-police one-of police-on neighbors
    ifelse (current-help != nobody); and current-police = nobody)
    [ 
     ; fd speed + 0.05
      move-to current-help
    ]
    [
    if (current-lady != nobody); and current-police = nobody)
    [
   ;  fd speed + 0.05
      move-to current-lady
    ]
    ]
  ] 
end 

to learn  
  if  to-walk? = true
  [
    set reward reward + 1
    set episode episode + 1
  ]
  ; show word "reward is " reward
  if to-be-rob? = true
  [
    set reward reward - 100
    set episode episode + 1
  ]
  let dir  min qtable
  let dirp position  dir qtable
  set Qnew Qnew + learning-rate * (reward + Q - Qnew) ;--perform Q-Learning 
  set qtable lput Qnew qtable
  set Q max qtable
  ;show word "Q is "Q
  ;show qtable
  let lng length qtable
  set avg-reward (avg-reward + reward) / lng
  ;show avg-reward
  if Q > 10000
  [avoid]
end 

to learn-crime  
  if  to-walk? = true
  [
    set reward reward - 0.01
    set episode episode + 1 
  ]
  ; show word "reward is " reward
  if to-be-rob? = true
  [
    set reward reward + 1
    set episode episode + 1
  ]
  let dir  min qtable
  let dirp position  dir qtable
  set Qnew Qnew + learning-rate * (reward + Q - Qnew) ;--perform Q-Learning
                                                      ;set qtable replace-item dirp  qtable Qnew
  
                                                      ;if Qnew > Q
                                                      ;[ ]
  set qtable lput Qnew qtable
  set Q max qtable
  let lng length qtable
  set avg-reward-crime (avg-reward-crime + reward) / lng
end 

to rest-of-people
  let rest people with [wealth > 0]
  ask  rest[
    output-show word "rest of people are " count rest
    output-show word "wealth " wealth
    output-show word "age " age
    output-show word "fight " fight
  ]
end 

to avoid
  let nearone one-of criminals-on neighbors
  let nearhelp one-of people-on neighbors
  let ok? false
  if member? nearone crime-table
    [
      if nearhelp != nobody
      [ask nearhelp [if fight = 1[ set ok? true]]]
      ifelse ok? = true
        [
          move-to nearhelp
          show word "moving to " nearhelp
        ]
        [
          ifelse [pcolor] of patch-ahead 1 = green
          [ lt random-float 360 ]   
          [back speed]
        ]
      ;show crime-table
      ;print Q
      show word "avoiding" nearone
    ]
end 

to make-movie
  ;; prompt user for movie location
  user-message "First, save your new movie file (choose a name ending with .mov)"
  let path user-new-file
  if not is-string? path [ stop ]  ;; stop if user canceled
                                   ;; run the model
  
  movie-start path
  movie-grab-view
  repeat 1000
    [ 
      go
      movie-grab-view 
    ]
  ;; export the movie
  movie-close
  user-message (word "Exported movie to " path)
end 

to watch-women
  let lady one-of women
  follow lady
  ask lady
  [
    set label who 
    set label-color black 
    show lady
    show word "wealth "wealth
  ]
end  

to watch-kids
  let kid one-of people with [age <= 14]
  follow kid
  ask kid
  [
    set label who
    set label-color black 
    show kid
    show word "wealth "wealth
    show word "age "age
  ]
end  

to watch-men
  let man one-of people with [age > 14]
  follow man
  ask man
  [
    set label who
    set label-color black 
    show man
    show word "wealth "wealth
    show word "age "age
    show word "fight "fight
  ]
end  

to watch-robbers
  let robber one-of criminals
  follow robber
  ask robber
  [
    set label who
    set label-color black 
    show robber
    show word "wealth "wealth
  ]
end  

There is only one version of this model, created over 11 years ago by Amrutha S.

Attached files

File Type Description Last updated
street robbery simulation.png preview Preview for 'street robbery simulation' about 11 years ago, by Amrutha S Download

This model does not have any ancestors.

This model does not have any descendants.