Smart Cars

Smart Cars preview image

1 collaborator

Default-person Hossein Sabzian (Author)

Tags

artificial intelligence 

Tagged by Hossein Sabzian over 6 years ago

Visible to everyone | Changeable by everyone
Model was written in NetLogo 6.0.1 • Viewed 735 times • Downloaded 44 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.)


WHAT IS IT?

This model explains how moving agents (cars) handle following conditions: 1) When they have a lot of collisions (condensiy in the road) 2) when they have get trapped by external blocks

HOW IT WORKS

Agents move from left to right. When they face a bolck, they search ( up and down) to find an exit way. When agents can not find a way out, collision ( condesity) will gradually increase and when it becomes more than the the threshold-of-collision, some agents (trucks) will automatically crack some blocks and make some holes (exit ways). If agents become trapped ( blocked by some blocks from ahead, up and down so they can't search) if the number of trapped agents becomes more than trapping-threshold, the trapped agents do some specific behaviors to get out of the trap.

HOW TO USE IT

Buttons

"setup" for initialization "go" for iteration "go once" for iteration in one time "create-wall" for creating walls in the interface "remove-wall" for removing walls (an available blocks) in the interface "vibrate-wall" for vibrating the walls and blocks "activate-an-indicator" for randomly selecting a car to draw a line in the road "unactivate-an-indicator" for unactivating the indicator "clear-drawing" for clearing the drawings over the road "stop one of cars" for randomly stopping one of cars "unstop the stopped" for unstopping the stopped car(s)

Switch

"routes in wall?" ON for exsitence of routes in the wall and OFF ON for non-exsitence of routes in the wall

Sliders

"number-of-agent" for setting the number of agents "unmber-of-routes-in-wall" for determining the number of routes in the wall ( if "routes in wall?" switch is ON) "threshold-of-collision" shows critical point of collision ( condensity) level where some agents (truck) automatically crack some blocks and make some holes in the wall (way-out) "trapping-threshold" show the critical point of trapping where the trapped agents do some specific behaviors to get out of the trap.

THINGS TO NOTICE

when an agent is trapped, it can not move ahead, up and down So the a trapped agent is not completely enclosed by blocks.

THINGS TO TRY

left-click in the intrace and make a block

left-click on a block and remove it

vibrate the wall and see the behavior of agents

trap a number of agents by making some blocks in its way (ahead, up and down) and see how it can find a way out of it.

EXTENDING THE MODEL

extend the car to have a changeable speed. add some environmental factors such as rain,fog and ...to affect the performance of cars.

CREDITS AND REFERENCES

Author: Hossein Sabzian Developmen date: 2/20/2019 Email: Sabzeyan@yahoo.com

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 over 6 years ago by Hossein Sabzian.

Attached files

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

This model does not have any ancestors.

This model does not have any descendants.