Smart Cars

Smart Cars preview image

1 collaborator

Default-person Hossein Sabzian (Author)

Tags

artificial intelligence 

Tagged by Hossein Sabzian almost 6 years ago

Visible to everyone | Changeable by everyone
Model was written in NetLogo 6.0.1 • Viewed 504 times • Downloaded 23 times • Run 0 times
Download the 'Smart Cars' 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

breed [cars car]
breed [ blocks block]

cars-own [

speed
last-heading

blocked?
free?
collided?
trapped?
previous-collided?
time-of-collision

first-xcor
first-ycor
current-xcor
current-ycor

memory
]

globals [
mouse-clicked?
blockage?
]

to setup
ca


set-default-shape blocks "circle"

set mouse-clicked? false
set blockage? false

ask patches [set pcolor green + 2]

create-cars number-of-agents [

set shape ifelse-value (random-float 1 > 0.9) ["car"] ["truck" ]
if shape = "car" [set size 1.5]
if shape = "truck" [ set size 1.5]
set color one-of [gray blue white]
setxy number random-ycor
set heading 90

set last-heading heading
set speed 1

set blocked? false
set free? true
set collided? false
set previous-collided? false
set trapped? false


set first-xcor 0
set first-ycor 0
set current-xcor 0
set current-ycor 0

set memory []
find-empty-place
]

wall-creation

 reset-ticks
end 

to go


make-block

ask cars [First-position first-check   get-out-of-congestion  second-position ]

ask cars [ analysis ]

ask cars with [collided?] [ free-the-collided  ]  ;; Collided Cars

ask cars with [trapped?] [ free-the-trapped ] ;;Trapped Cars


tick
end 

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; Helper Procedures

  ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;Go procedures

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;1

to  make-block
  ifelse mouse-down? [
    if not mouse-clicked? [
      set mouse-clicked? true
      ask patch mouse-xcor mouse-ycor [ toggle-blocks ]
    ]
  ] [
    set mouse-clicked? false
  ]
end 

to toggle-blocks

   let nearby-blocks blocks in-radius 0.5
  ifelse any? nearby-blocks [

    ask nearby-blocks [ die ]
  ] [

    sprout-blocks 1 [
      set color red
      set size 1
      ask patches in-radius 0.1 [ sprout-blocks 1 [
      set color brown
      set size 1]]
    ]
  ]
end 
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;1


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;2

to First-position
set first-xcor xcor
set first-ycor ycor
end 
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;2

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;3

to first-check
let is-road? patch-ahead speed != nobody
ifelse is-road? [is-blockage?] [set last-heading heading rt 180] ;; one-way block
end 

to is-blockage?
ifelse not any? blocks-on patch-ahead speed [plus-memory trim-memory  fd speed set heading 90] [ minus-memory trim-memory ifelse last-heading = 0 [set heading 180 ][blocked-way] ]
end 

to blocked-way
  ifelse last-heading = 180 [set heading 0] [ ifelse (color = gray or color = green) [set heading 0] [set heading 180] ]
end 

to trim-memory
 if length memory > 2 [ set memory remove-item 2 memory]
end 

to plus-memory
 set memory fput 1 memory
end 

to minus-memory
 set memory fput -1 memory
end 
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;3


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;4

to get-out-of-congestion
  let companions other cars-on patch-here
  ask companions [ set heading one-of [0 180] fd speed set heading 90]
end 
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;4


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;5

to second-position
 set current-xcor xcor
set current-ycor ycor
end 
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;5


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;6

to analysis

if sum  memory > 0 [ set free? true set blocked? false set collided? false set previous-collided? false  set trapped? false ]

if sum  memory = 0 [ set free? false set blocked? true set collided? false set previous-collided? false  set trapped? false ]

if sum  memory < 0 [set free? false set blocked? false set collided? true if not previous-collided? [set previous-collided? true set time-of-collision ticks] ]

if (ticks = (time-of-collision + 100) and previous-collided? ) [ if collided? [ set trapped? true  ] ]
end 
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;6

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;7

to free-the-collided
  if (percent-of-collided-cars) > threshold-of-collision [
    let cracker one-of cars with [collided? and shape = "truck"]
    if cracker != nobody [ask cracker [ask blocks in-radius 1.5 [die]]]]
end 
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;7

to free-the-trapped
  if (percent-of-trapped-cars) > trapping-threshold [

find-a-way-out
  ]
end 



;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;setup procedures

to find-a-way-out
  ifelse heading = 0 [

    lt 90 walk choose-patch

  ] [

    ifelse heading = 90 [

   lt 180 walk  choose-patch

    ]

    [ rt 90 walk choose-patch  ]
  ]
end 

to walk
  ifelse random-float 1 > 0.5 [ fd speed + 0.5 ] [ fd speed ]
end 

to choose-patch
ifelse random-float 1 > 0.5 [ lt 90 fd speed lt 90] [rt 90 fd speed rt 90]
end 

to find-empty-place
  if any? other cars-here [fd speed find-empty-place]
end 

to activate-an-indicator
ask one-of cars with [ pen-mode = "up"] [pd]
end 

to unactivate-an-indicator
ask one-of cars with [ pen-mode = "down"] [pu]
end 

to wall-creation

ask patches with [ pxcor = 0.8 * max-pxcor and pycor > 0] [ sprout-blocks 1 [set color black] ]
ask patches with [ pxcor = 0.8 * max-pxcor and pycor <= 0] [ sprout-blocks 1 [set color black]]

ask patches with [ pxcor = 0.7 * max-pxcor and pycor > 0] [ sprout-blocks 1 [set color green]]
ask patches with [ pxcor = 0.7 * max-pxcor and pycor <= 0] [ sprout-blocks 1[set color green]]

ask patches with [ pxcor = 0.6 * max-pxcor and pycor > 0] [ sprout-blocks 1 [set color blue]]
ask patches with [ pxcor = 0.6 * max-pxcor and pycor <= 0] [ sprout-blocks 1 [set color blue]]

ask patches with [ pxcor = 0.5 * max-pxcor and pycor > 0] [ sprout-blocks 1 [set color red]]
ask patches with [ pxcor = 0.5 * max-pxcor and pycor <= 0] [ sprout-blocks 1[set color red]]

ask patches with [ pxcor = 0.4 * max-pxcor and pycor > 0] [ sprout-blocks 1[set color yellow]]
ask patches with [ pxcor = 0.4 * max-pxcor and pycor <= 0] [ sprout-blocks 1[set color yellow]]

ask patches with [ pxcor = 0.3 * max-pxcor and pycor > 0] [ sprout-blocks 1[set color violet]]
ask patches with [ pxcor = 0.3 * max-pxcor and pycor <= 0] [ sprout-blocks 1[set color violet]]

ask patches with [ pxcor = 0.2 * max-pxcor and pycor > 0] [ sprout-blocks 1[set color gray]]
ask patches with [ pxcor = 0.2 * max-pxcor and pycor <= 0] [ sprout-blocks 1[set color gray]]

ask patches with [ pxcor = 0.1 * max-pxcor and pycor > 0] [ sprout-blocks 1[set color brown]]
ask patches with [ pxcor = 0.1 * max-pxcor and pycor <= 0] [ sprout-blocks 1[set color brown]]

 if routes-in-walls? [
ask n-of Number-of-routes-in-walls blocks with [ color = black] [ die]
ask n-of Number-of-routes-in-walls blocks with [ color = green] [ die]
ask n-of Number-of-routes-in-walls blocks with [ color = blue] [ die]
ask n-of Number-of-routes-in-walls blocks with [ color = red] [ die]
ask n-of Number-of-routes-in-walls blocks with [ color = yellow] [ die]
ask n-of Number-of-routes-in-walls blocks with [ color = violet] [ die]
ask n-of Number-of-routes-in-walls blocks with [ color = gray] [ die]
ask n-of Number-of-routes-in-walls blocks with [ color = brown] [ die] ]
end 

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;Reporters

to-report number
  let a (0.9 * min-pxcor)
  let b (0.8 * min-pxcor)
  let c (0.7 * min-pxcor)
  let d (0.6 * min-pxcor)
  ifelse random-float 1 > 0.5 [ report one-of (list a b) ]   [ report one-of (list c d) ]
end 

to-report percent-of-collided-cars
  report (count cars with [collided?]) / (count cars )
end 

to-report percent-of-trapped-cars
  report (count cars with [trapped?]) / (count cars )
end 

There is only one version of this model, created almost 6 years ago by Hossein Sabzian.

Attached files

File Type Description Last updated
Smart Cars.png preview Preview for 'Smart Cars' almost 6 years ago, by Hossein Sabzian Download

This model does not have any ancestors.

This model does not have any descendants.