;*************************** OBSERVER PROCEDURES ****************************** breeds [living working visiting1 visiting2] patches-own [vacant?] to setup ct crt number-of-cars ask-turtles [setCounter 0 setshape cross setc red setspeed 1 setSpeedLimit 1 if (who <= (number-of-cars / 5)) [setbreed living] if (who > (number-of-cars / 5) and who <= (number-of-cars / 2)) [setbreed working] if (who > (number-of-cars / 2) and who <= (number-of-cars / 1.25)) [setbreed visiting1] if (who > (number-of-cars / 1.25)) [setbreed visiting2] if breed = living [setParkTime 48] if breed = working [setParkTime 32] if breed = visiting1 [setParkTime 2] if breed = visiting2 [setParkTime 8] loop [ifelse ((pc-at 0 0) = black and count-turtles-here = 1 ) [stop] [ ifelse (random 2) = 0 [seth 0] [ifelse (random 2) = 0 [seth 90][ifelse (random 2) = 0 [seth 180] [seth 270]]] fd random 25 ifelse (random 2) = 0 [seth 0] [ifelse (random 2) = 0 [seth 90][ifelse (random 2) = 0 [seth 180] [seth 270]]] fd random 25 ]]] startcountaverage end to clear-cars ct end to empty-spots output (6 - (count-turtles-with [pc = 9])) ask-patches [ifelse (pc = white and (count-turtles-at 0 0) = 0) [set vacant? true] [set vacant? false] ] end to count-steps-to-find-parking output (sum-of-turtles [steps-to-find-parking] / (count-turtles-with [color = yellow])) end to count-average ; this is for statistical analysis: paste these number into excel and calculate the mean, median and standard deviation show (round average-of-turtles-with [color = yellow] [steps-to-find-parking]) wait 1 end ;*************************** TURTLE PROCEDURES ****************************** turtles-own [speed SpeedLimit ParkTime Parked? steps-to-find-parking Counter] to check-patches-after-park if (pc-ahead = 7) or (pc-ahead = 9) [ seth (heading + 90) check-patches-after-park ] end to park ; has to be done so that agent will look for parking until found if (Parked? = false)[ if pc not= white [setc yellow] if (pc-ahead = 2 and (pc-at 0 0) = 2) [ifelse (random 2) = 0 [fd 3 set steps-to-find-parking (steps-to-find-parking + 3)] [ifelse (random 2) = 0 [rt 90 fd 2 set steps-to-find-parking (steps-to-find-parking + 2)][fd 1 lt 90 fd 2 set steps-to-find-parking (steps-to-find-parking + 3)]]] check-patches-after-park ifelse (count-turtles-towards heading 1) > 0 ;if there is a turtle 1 space ahead, decelerate [setspeed speed-of one-of-turtles-towards heading 1 decelerate] [ifelse lookahead = 2 ;if lookahead=2, check 2 spaces ahead also [ifelse (count-turtles-at heading 1) > 0 [setspeed speed-of one-of-turtles-towards heading 2 decelerate] [accelerate]] ;else accelerate [accelerate]] if speed < 0.01 [setspeed 0.01] ;also adjust speed based on SpeedLimit and radar if speed > SpeedLimit [setspeed SpeedLimit] ; here is how the actual parking move happens ; first, parking at your own side of the road and then parking at the opposite side of the road, parking on horisontal streets is also allowed. ; augment the counter, which counts the program iterations during which a car parks (instead of real time) ifelse ((pc-at 0 0) = 0 and (pc-at 1 0) = 9 and (count-turtles-at 1 0) = 0) or ((pc-at 0 0) = 0 and (pc-at (-1) 0) = 9 and (count-turtles-at (-1) 0) = 0) or ((pc-at 0 0) = 0 and (pc-at -2 0) = 9 and (count-turtles-at -2 0) = 0) or ((pc-at 0 0) = 0 and (pc-at 2 0) = 9 and (count-turtles-at 2 0) = 0) or ((pc-at 0 0) = 0 and (pc-at 0 1) = 9 and (count-turtles-at 0 1) = 0) or ((pc-at 0 0) = 0 and (pc-at 0 (-1)) = 9 and (count-turtles-at 0 (-1)) = 0) or ((pc-at 0 0) = 0 and (pc-at 0 -2) = 9 and (count-turtles-at 0 -2) = 0) or ((pc-at 0 0) = 0 and (pc-at 0 2) = 9 and (count-turtles-at 0 2) = 0) or ((pc-at 0 0) = 9) [ ; here we augment the parking counter and check if the counter is full, in which case a car leaves if ((pc-at 0 0) = 9 and (pc-at (-1) 0) = 0) [ setCounter Counter + 1 if Counter > ParkTime [setCounter 0 seth 270 fd 1 seth 0 setParked? true stop]] if ((pc-at 0 0) = 9 and (pc-at 1 0) = 0) [ setCounter Counter + 1 if Counter > ParkTime [setCounter 0 seth 90 fd 1 seth 180 setParked? true stop]] if ((pc-at 0 0) = 9 and (pc-at 0 (-1)) = 0) [setCounter Counter + 1 if Counter > ParkTime [setCounter 0 seth 180 fd 1 seth 270 setParked? true stop]] if ((pc-at 0 0) = 9 and (pc-at 0 1) = 0) [setCounter Counter + 1 if Counter > ParkTime [setCounter 0 seth 0 fd 1 seth 90 setParked? true stop]] if ((pc-at 0 0) = 0 and (pc-at 1 0) = 9 and (count-turtles-at 1 0) = 0) [setc red set steps-to-find-parking 0 seth 90 fd 1 ] if ((pc-at 0 0) = 0 and (pc-at (-1) 0) = 9 and (count-turtles-at (-1) 0) = 0) [setc red set steps-to-find-parking 0 seth 270 fd 1 ] if ((pc-at 0 0) = 0 and (pc-at -2 0) = 9 and (count-turtles-at -2 0) = 0)[setc red set steps-to-find-parking 0 seth 270 fd 2 ] if ((pc-at 0 0) = 0 and (pc-at 2 0) = 9 and (count-turtles-at 2 0) = 0) [setc red set steps-to-find-parking 0 seth 90 fd 2 ] if ((pc-at 0 0) = 0 and (pc-at 0 1) = 9 and (count-turtles-at 0 1) = 0) [setc red set steps-to-find-parking 0 seth 0 fd 1 ] if ((pc-at 0 0) = 0 and (pc-at 0 (-1)) = 9 and (count-turtles-at 0 (-1)) = 0) [setc red set steps-to-find-parking 0 seth 180 fd 1 ] if ((pc-at 0 0) = 0 and (pc-at 0 -2) = 9 and (count-turtles-at 0 -2) = 0)[setc red set steps-to-find-parking 0 seth 180 fd 2 ] if ((pc-at 0 0) = 0 and (pc-at 0 2) = 9 and (count-turtles-at 0 2) = 0) [setc red set steps-to-find-parking 0 seth 0 fd 2 ]] [if (pc-at 0 0) not= white [ ifelse (pc-ahead = 2 and (pc-at 0 0) = 2) [ifelse (random 2) = 0 [leap 3 set steps-to-find-parking (steps-to-find-parking + 3)] [ifelse (random 2) = 0 [rt 90 fd 2 set steps-to-find-parking (steps-to-find-parking + 2)][fd 1 lt 90 fd 2 set steps-to-find-parking (steps-to-find-parking + 3)]]] [check-patches-after-park fd speed set steps-to-find-parking (steps-to-find-parking + speed)]]] park ] end to drive ; each egnt will park and then drive for a certain time if (pc-at 1 0) = 7 or (pc-at 1 0) = 9 [seth 0 setParked? false park repeat (parking-interval * 10) [ ; these are turtles driving up if (pc-ahead = 2 and (pc-at 0 0) = 2) [ifelse (random 2) = 0 [leap 3] [ifelse (random 2) = 0 [rt 90 fd 2][fd 1 lt 90 fd 2]]] check-patches-after-park ifelse (count-turtles-at 0 1) > 0 ;if there is a turtle 1 space ahead, decelerate [setspeed speed-of one-of-turtles-at 0 1 decelerate] [ifelse lookahead = 2 ;if lookahead=2, check 2 spaces ahead also [ifelse (count-turtles-at 0 1) > 0 [setspeed speed-of one-of-turtles-at 0 2 decelerate] [accelerate]] ;else accelerate [accelerate]] if speed < 0.01 [setspeed 0.01] ;also adjust speed based on SpeedLimit and radar if speed > SpeedLimit [setspeed SpeedLimit] fd speed ] ] if (pc-at (-1) 0) = 7 or (pc-at (-1) 0) = 9 [seth 180 setParked? false park repeat (parking-interval * 10) [ ; these are turtles driving up if (pc-ahead = 2 and (pc-at 0 0) = 2) [ifelse (random 2) = 0 [leap 3] [ifelse (random 2) = 0 [rt 90 fd 2][fd 1 lt 90 fd 2]]] check-patches-after-park ifelse (count-turtles-at 0 (-1)) > 0 ;if there is a turtle 1 space ahead, decelerate [setspeed speed-of one-of-turtles-at 0 (-1) decelerate] [ifelse lookahead = 2 ;if lookahead=2, check 2 spaces ahead also [ifelse (count-turtles-at 0 (-1)) > 0 [setspeed speed-of one-of-turtles-at 0 (-2) decelerate] [accelerate]] ;else accelerate [accelerate]] if speed < 0.01 [setspeed 0.01] ;also adjust speed based on SpeedLimit and radar if speed > SpeedLimit [setspeed SpeedLimit] fd speed ] ] if (pc-at 0 1) = 7 or (pc-at 0 1) = 9 [seth 270 setParked? false park repeat (parking-interval * 10) [ ; these are turtles driving up if (pc-ahead = 2 and (pc-at 0 0) = 2) [ifelse (random 2) = 0 [leap 3] [ifelse (random 2) = 0 [rt 90 fd 2][fd 1 lt 90 fd 2]]] check-patches-after-park ifelse (count-turtles-at (-1) 0) > 0 ;if there is a turtle 1 space ahead, decelerate [setspeed speed-of one-of-turtles-at (-1) 0 decelerate] [ifelse lookahead = 2 ;if lookahead=2, check 2 spaces ahead also [ifelse (count-turtles-at (-1) 0) > 0 [setspeed speed-of one-of-turtles-at (-2) 0 decelerate] [accelerate]] ;else accelerate [accelerate]] if speed < 0.01 [setspeed 0.01] ;also adjust speed based on SpeedLimit and radar if speed > SpeedLimit [setspeed SpeedLimit] fd speed ] ] if (pc-at 0 (-1)) = 7 or (pc-at 0 (-1)) = 9 [seth 90 setParked? false park repeat (parking-interval * 10) [ ; these are turtles driving up if (pc-ahead = 2 and (pc-at 0 0) = 2) [ifelse (random 2) = 0 [leap 3] [ifelse (random 2) = 0 [rt 90 fd 2][fd 1 lt 90 fd 2]]] check-patches-after-park ifelse (count-turtles-at 1 0) > 0 ;if there is a turtle 1 space ahead, decelerate [setspeed speed-of one-of-turtles-at 1 0 decelerate] [ifelse lookahead = 2 ;if lookahead=2, check 2 spaces ahead also [ifelse (count-turtles-at 2 0) > 0 [setspeed speed-of one-of-turtles-at 2 0 decelerate] [accelerate]] ;else accelerate [accelerate]] if speed < 0.01 [setspeed 0.01] ;also adjust speed based on SpeedLimit and radar if speed > SpeedLimit [setspeed SpeedLimit] fd speed ] ] if (pc-ahead = 2 and (pc-at 0 0) = 2) [ifelse (random 2) = 0 [leap 3] [rt 90 fd 2]] if (pc-at 0 0 )= 2 [fd 1] end end to accelerate setspeed (speed + (speedup / 10000)) end to decelerate setspeed speed - (slowdown / 1000) end