在环形交叉路口的Netlogo,分配和行车道

时间:2015-11-04 00:49:37

标签: netlogo

[

globals [ gl_my-car gl_cars-count gl_accelerator gl_decelerator gl_speed-max gl_speed-min gl_speed-maxcircle gl_speed-maxright gl_distance-straight 
gl_distance-left gl_distance-right gl_degree-per-foot gl_side gl_slowdown gl_straightdistance 
gl_distanceto00 gl_carminseparation gl_reactiontime
  gl_motospeed-max gl_my-moto gl_moto-count
  gl_truckspeed-max gl_my-truck gl_truck-count
  gl_busspeed-max gl_my-bus gl_bus-count
  gl_motominseparation  gl_truckminseparation  gl_busminseparation 
  ]

breed [cartop car]
breed [mototopview moto] 
breed [trucktopview truck]
breed [bustopview bus]
turtles-own [ 
  speed droundabout headingdirection 
  lane
  ]

to setup
clear-all

set gl_cars-count 0
; 100 ticks per second
set gl_moto-count 0
set gl_truck-count 0
set gl_bus-count 0
set gl_accelerator 0.001 ; 10 ft per second per second
set gl_decelerator 0.001 ; 10 ft per second per second
set gl_speed-max 0.5133333333333 ; 35mph, 51.3333 ft per second
set gl_speed-min 1.e-10
set gl_speed-maxcircle gl_speed-max * 0.5 ; half normal speed
set gl_speed-maxright gl_speed-max * 0.62 ; about 60% normal speed
set gl_distance-straight 34.77
set gl_distance-left 58.34
set gl_distance-right 35.2
set gl_degree-per-foot 3.81972
set gl_side sqrt (15 ^ 2 - 6 ^ 2)
set gl_slowdown (gl_speed-max ^ 2 - gl_speed-maxcircle ^ 2) / (2 * (gl_decelerator))
set gl_straightdistance gl_slowdown + gl_side
set gl_distanceto00 sqrt (gl_straightdistance ^ 2 + 6 ^ 2)
set gl_carminseparation 15; assume car lenght 10' & distance between cars 5'
set gl_motominseparation 10
set gl_truckminseparation 20
set gl_busminseparation 25
set gl_reactiontime 100; 100 ticks or 1 second reaction time

;;;tao xe oto
create-cartop NumberCartop
[
  set color (random 3 * 40 + 15) ; 15=red=go right, 55=green=go straight, 95=sky=go left
  set shape "car top"
  set size 5
  set speed gl_speed-max - random-float .1 ;;thhiet lap hinh dang xe và bao cao vi tri
  set droundabout 0
  set headingdirection 0
  distribute-cars
]

;;;;tao xe may
create-mototopview Numbermotos
   [
     set color (random 3 * 40 + 15 )
     set shape "motortopview"
     set size 3
     set speed gl_motospeed-max - random-float .1 ;;thhiet lap hinh dang xe và bao cao vi tri
     set droundabout 0
     set headingdirection 0
     distribute-motos
   ]

;;; tao xe tai
create-trucktopview Numbertruck
   [
     set color (random 3 * 40 + 15 )
     set shape "trucktopview"
     set size 7
     set speed gl_truckspeed-max - random-float .1 ;;thhiet lap hinh dang xe và bao cao vi tri
     set droundabout 0
     set headingdirection 0
     distribute-truck
   ]
   
  ;; tao xe bus
  create-bustopview Numberbus
   [
     set color (random 3 * 40 + 15 )
     set shape "bustopview"
     set size 7
     set speed gl_busspeed-max - random-float .1 ;;thhiet lap hinh dang xe và bao cao vi tri
     set droundabout 0
     set headingdirection 0
     distribute-bus
   ]
set gl_my-car one-of cartop
watch gl_my-car
set gl_my-moto one-of mototopview
watch gl_my-moto 
set gl_my-truck one-of trucktopview
watch gl_my-truck 
set gl_my-bus one-of bustopview
watch gl_my-bus 
ask patches 
[
  ;;;tao vong xuyen
     if (pxcor ^ 2 + pycor ^ 2 < 35 ^ 2) and (pxcor ^ 2 + pycor ^ 2 > 10 ^ 2)
    [
     set pcolor grey
    ]
    ;;;;tao cay xanh trong vong xuyen
    if (pxcor ^ 2 + pycor ^ 2 < 7 ^ 2) and (pxcor ^ 2 + pycor ^ 2 > 0 ^ 2)
    [
     set pcolor blue
    ]
    ;;;; tao duong phan van tri
    
  
    if  abs (pxcor) > 0 and abs (pxcor) < 7 and (pxcor ^ 2 + pycor ^ 2 > 10 ^ 2)
    [
     set pcolor grey
    ]
   
        ;;;tao giai phan cach giua duong pham van dong
    if abs (pycor) >= 0 and abs (pycor) <= 1.5 and (abs (pxcor) > 35)
    [
     set pcolor yellow
     ]
    ;;;tao lan oto duong pham van dong
    if abs (pycor) > 1.5 and abs (pycor) < 17 and (pxcor ^ 2 + pycor ^ 2 > 10 ^ 2)
    [
     set pcolor grey
     ]
    ;;tao dai phan cach lan xe may duong pham van dong
     if abs (pycor) >= 17 and abs (pycor) <= 19 and (abs (pxcor) > 35)
    [
     set pcolor yellow
    ]
    
    ;;;tao lan xe may duong pham van dong
     if abs (pycor) > 19 and abs (pycor) < 26 and (pxcor ^ 2 + pycor ^ 2 > 10 ^ 2)
    [
     set pcolor grey
    ]
    ;;;tao lan duong re phai pham van dong - phan van tri và phan van tri-pham van dong phia ben duoi
    if ((abs (pxcor) - 55 < pycor) and (abs (pxcor) - 35 > pycor) and  abs (pxcor) > -10 and pycor < -10)
    [
     set pcolor grey
    ]
     ;;;tao lan duong re phai pham van dong - phan van tri phia ben tren
    if (( (- pxcor) + 55 > pycor) and ( (- pxcor) + 35 < pycor) and (pxcor) > 0 and pycor > 10)
    [
     set pcolor grey
    ]
    ;;;tao lan duong re phai phan van tri - pham van dong phia ben tren
    if (( ( pxcor) + 55 > pycor) and ( ( pxcor) + 35 < pycor) and (pxcor) < 0 and pycor > 10)
    [
     set pcolor grey
     ]
    
    
]  
reset-ticks
end


;;;phan phoi xe oto
to distribute-cars 
  set heading random 4 * 90
  ;;lan phan van tri ben phai
  if (heading = 0)
  [setxy (1 + random(pxcor + 6)) (40 + random (max-pycor - 30)) * (2 * random 2 - 1)]
   
   
  ;;lan phan van tri ben trai 
  if (heading = 180)
  [setxy (-1 + random(pxcor - 6)) ((40 + random (max-pycor - 30)) * (2 * random 2 - 1))]
    ;;lan pham van dong ben duoi
   
    if (heading = 90)
    [setxy ((40 + random (max-pxcor - 38)) * (2 * random 2 - 1)) (-3 + random(pycor - 13 ))]
    
  ;;;lan pham van dong ben tren
     
    if (heading = 270)
      [setxy ((40 + random (max-pxcor - 38)) * (2 * random 2 - 1)) (3 + random(pycor + 13 ))]
   
  if any? other turtles-here
  [distribute-cars ]
end

;;;phan phoi xe may
to distribute-motos
  set heading random 4 * 90
  ;;;lan phan van tri ben phai
  if (heading = 0)
 [setxy (1 + random(pycor + 6)) (40 + random (max-pycor - 30)) * (2 * random 2 - 1)]
  
 
   ;;lan pham van dong ben duoi
    if (heading = 90)
    [setxy ((40 + random (max-pxcor - 38)) * (2 * random 2 - 1)) (-20 + random(pycor - 6 ))]
  
  
  ;;lan phan van tri ben trai 
  if (heading = 180)
  [setxy (-1 + random(pycor - 6)) ((40 + random (max-pycor - 30)) * (2 * random 2 - 1))]
  
  ;;;lan pham van dong ben tren
  if (heading = 270)
      [setxy ((40 + random (max-pxcor - 38)) * (2 * random 2 - 1)) (20 + random(pycor + 6 ))]
 
    
  if any? other turtles-here
  [distribute-motos ]
end
;;;phan phoi xe tai
to distribute-truck 
  set heading random 4 * 90
  ;;lan phan van tri ben phai
  if (heading = 0)
  [setxy (1 + random(pxcor + 6)) (60 + random (max-pycor - 30)) * (2 * random 2 - 1)]
   
   
  ;;lan phan van tri ben trai 
  if (heading = 180)
  [setxy (-1 + random(pxcor - 6)) ((60 + random (max-pycor - 30)) * (2 * random 2 - 1))]
    ;;lan pham van dong ben duoi
   
    if (heading = 90)
    [setxy ((60 + random (max-pxcor - 38)) * (2 * random 2 - 1)) (-3 + random(pycor - 13 ))]
    
  ;;;lan pham van dong ben tren
     
    if (heading = 270)
      [setxy ((60 + random (max-pxcor - 38)) * (2 * random 2 - 1)) (3 + random(pycor + 13 ))]
  if any? other turtles-here
  [distribute-truck ]
end
;;;phan phoi xe buyt
to distribute-bus 
set heading random 4 * 90
  ;;lan phan van tri ben phai
  if (heading = 0)
  [setxy (1 + random(pxcor + 6)) (60 + random (max-pycor - 30)) * (2 * random 2 - 1)]
   
   
  ;;lan phan van tri ben trai 
  if (heading = 180)
  [setxy (-1 + random(pxcor - 6)) ((60 + random (max-pycor - 30)) * (2 * random 2 - 1))]
    ;;lan pham van dong ben duoi
   
    if (heading = 90)
    [setxy ((60 + random (max-pxcor - 38)) * (2 * random 2 - 1)) (-3 + random(pycor - 13 ))]
    
  ;;;lan pham van dong ben tren
     
    if (heading = 270)
      [setxy ((60 + random (max-pxcor - 38)) * (2 * random 2 - 1)) (3 + random(pycor + 13 ))]
  if any? other turtles-here
  [distribute-bus ]
end

to go
ask cartop [goallcar]
ask mototopview [goallmoto]
ask trucktopview [goalltruck]
ask bustopview [goallbus]
  tick
  if ticks > 30000 [stop]
end 


to goallcar
  let myhd heading
 set headingdirection (subtract-headings myhd (towardsxy 0 0))
ifelse color = red and distancexy 0 0 < 31.7
[
goright
]
[
ifelse distancexy 0 0 <= 15
[
gostraightleft
]
[
gomainroad
]
]
plot [speed] of gl_my-car * 360000 / 5280 ; convert from feet per tick to mph
end
to goright
; if [ pcolor ] of patch-ahead 23 != grey

 
if headingdirection > 0 and headingdirection < 40 ; begin right turn
[
set gl_cars-count gl_cars-count + 1
set droundabout 0
;;;rt 45


if heading = 90 
[ 
  ;; ask cars
  if distancexy 0 0 = -55 and distancexy 0 0 = 55
  [
   
    rt 50
    if pycor = -20
    [ 
      rt 90
    ]
  ]   
    
]
rt 45    
]  



ifelse droundabout < gl_distance-right 
[
let myhd heading
let d gl_distance-right - droundabout
ifelse d > 12
[
let t d / speed 
let cars-45degrees cartop with [(subtract-headings heading myhd = 45) and (distance myself < 52.35)]
let cars-ininterval cars-45degrees with [(distancexy 0 0) + speed * t > 15 and (distancexy 0 0) + 
speed * t < 46.39]
ifelse any? cars-ininterval
[
let deceleration-to-stop (speed ^ 2 / (2 * (d - 12)))
set speed speed - deceleration-to-stop
]
[
set speed speed + gl_accelerator
]
] 
[
set speed speed + gl_accelerator
]
if (speed > gl_speed-maxright) [set speed gl_speed-maxright]
if (speed < gl_speed-min) [set speed gl_speed-min]
; plot [speed] of gl_my-car * 360000 / 5280 ; convert from feet per tick to mph
fd speed
set droundabout droundabout + speed
]
[ 
; plot [speed] of gl_my-car * 360000 / 5280; convert from feet per tick to mph
rt 45
fd speed
set droundabout 0
readjustcordinate
]
    
end
to gostraightleft
; if [ pcolor ] of patch-ahead 5 != grey
if droundabout = 0 ; begin roundabout left turn or go straight
[
set gl_cars-count gl_cars-count + 1
set speed gl_speed-maxcircle
set droundabout 0.00001 ; start roundabout
rt 66.422
]
ifelse ((color = sky and droundabout < gl_distance-left ) or (color = green and droundabout < 
gl_distance-straight ))
[
let carsinroundabout other cartop with [distancexy 0 0 < 15]
fd speed
lt speed * gl_degree-per-foot
set droundabout droundabout + speed
let d distancexy 0 0 / 14.9999
setxy (xcor / d) (ycor / d) 
]
[ 
; plot [speed] of gl_my-car * 360000 / 5280; convert from feet per tick to mph
rt 66.422
fd speed
set droundabout 0
readjustcordinate
]
end
to gomainroad
let myhd heading
let dfromcenter (distancexy 0 0)
let speed1 speed; save the initial speed for later use
if ( dfromcenter > 15)
[ 
let dseparation gl_carminseparation + speed * gl_reactiontime 
let cars-same-direction other cartop with [heading = myhd]
ifelse any? cars-same-direction
[
let cars-ahead other cars-same-direction with [(distance myself) != 0 and (towards myself) != myhd]
ifelse any? cars-ahead
[
let car-nearest (min-one-of cars-ahead [distance myself])
let dtocar-nearest distance car-nearest
let vofcar-nearest [speed] of car-nearest
ifelse dtocar-nearest < dseparation
[
if speed >= vofcar-nearest 
[
set speed speed - gl_decelerator 
]
if dtocar-nearest < gl_carminseparation
[ 
set speed speed - gl_decelerator
if speed >= vofcar-nearest 
[ set speed vofcar-nearest - gl_decelerator ]
]
]
[ set speed speed + gl_accelerator ] ; end car-nearest
]
[ set speed speed + gl_accelerator ] ; end cars-ahead
]
[ set speed speed + gl_accelerator ] ; end cars-same-direction
] ; end distancexy 0 0
; if ( (distancexy 0 0) > 15 and (distancexy 0 0) < 16.7)
; [ 
; let cars-45degree cars with [(subtract-headings heading myhd = -45) ]
; ]
if ( dfromcenter > 25 and dfromcenter < 50 )
[ 
let d dfromcenter - 25 
let t d / speed
let cars-inroundabout cartop with [(distancexy 0 0) <= 15]
let cars-ininterval cars-inroundabout with [(subtract-headings heading myhd > 0 )]
if any? cars-ininterval
[ 
let deceleration-to-stop (speed ^ 2 / (2 * d))
set speed speed - deceleration-to-stop
]
]
let vmax gl_speed-max
if (dfromcenter < gl_distanceto00)
[
let speed2 speed; save the just calculated speed
set speed speed1; restore the initial speed
ifelse color = red
[
let d sqrt ( dfromcenter ^ 2 - 36) - 31 + gl_distance-right
let t d / speed
let cars-perpendicular cartop with [(subtract-headings heading myhd = 90) and (subtract-headings 
(towards myself) myhd < 0)]
let cars-ininterval cars-perpendicular with [(distancexy 0 0) + speed * t > 15 and (distancexy 0 0) + 
speed * t < 46.39]
ifelse any? cars-ininterval
[
let deceleration-to-stop (speed ^ 2 / (2 * (d - 12)))
set speed speed - deceleration-to-stop
]
[
set speed speed + gl_accelerator
]
if (speed > speed2) [set speed speed2]
]
[
set speed speed2
]
let dfromcircle sqrt(dfromcenter ^ 2 - 36) - gl_side ; gl_side = sqrt(15^2+6^2)
set vmax sqrt(gl_speed-max ^ 2 - 2 * gl_decelerator * (gl_slowdown - dfromcircle)) ; gl_slowdown = (v^2 - v0^2)/(2a)
]
if vmax > gl_speed-max 
[ 
set vmax gl_speed-max 
]
if speed > vmax 
[
set speed vmax
]
if speed < gl_speed-min
[
set speed gl_speed-min 
] 
forward speed
end



;;;;;chay xe moto
to goallmoto
let myhd heading
set headingdirection (subtract-headings myhd (towardsxy 0 0))
ifelse color = red and distancexy 0 0 < 31.7
[
goright
]
[
ifelse distancexy 0 0 <= 15
[
gostraightleft
]
[
gomainroad
]
]
plot [speed] of gl_my-car * 360000 / 5280 ; convert from feet per tick to mph
end


;;;chay xe truck
to goalltruck
let myhd heading
set headingdirection (subtract-headings myhd (towardsxy 0 0))
ifelse color = red and distancexy 0 0 < 31.7
[
goright
]
[
ifelse distancexy 0 0 <= 15
[
gostraightleft
]
[
gomainroad
]
]
plot [speed] of gl_my-car * 360000 / 5280 ; convert from feet per tick to mph
end


;;;chay xe bus
to goallbus
let myhd heading
set headingdirection (subtract-headings myhd (towardsxy 0 0))
ifelse color = red and distancexy 0 0 < 31.7
[
goright
]
[
ifelse distancexy 0 0 <= 15
[
gostraightleft
]
[
gomainroad
]
]
plot [speed] of gl_my-car * 360000 / 5280 ; convert from feet per tick to mph
end

to chuyenlan
  distribute-cars 
 goallcar
   ;;;if heading = 0 and color = red
  ;;;[
   ;; if 
   ;;rt 45
  ;; if (xcor = -10)
  ;;  ]
    ;;if any? other turtles-here
 ;; [chuyenlan ]
 ;; ]
end

to readjustcordinate
if (heading > 355 or heading < 5)
[
set heading 0
set xcor 6
]
if (heading > 85 and heading < 95)
[
set heading 90
set ycor -6
]
if (heading > 175 and heading < 185)
[
set heading 180
set xcor -6
]
if (heading > 265 and heading < 275)
[
set heading 270
set ycor 6
]
end 

[![1]

如何编写运行汽车的算法。三车道。便宜的车左车道1和车道2车直行,车道3便宜车。随机初始车。 (右边的低红色汽车,直行绿色,天空向左转弯)。如果汽车红色làcó全部移动到右车道的便宜的汽车到1位置。绿色的车辆直奔直道,天空车向左侧车道便宜。

修改部分驱动算法。因为它运行错误

[1]]

如何编写运行汽车的算法。三车道。便宜的车左车道1和车道2车直行,车道3便宜车。随机初始车。 (右边的低红色汽车,直行绿色,天空向左转弯)。如果汽车红色làcó全部移动到右车道的便宜的汽车到1位置。绿色的车辆直奔直道,天空车向左侧车道便宜。

修改部分驱动算法。因为它运行错误

[1]

http://i.stack.imgur.com/59IAs.jpg

0 个答案:

没有答案