2010-03-31 43 views
2

我想創建一個高速公路模擬和NetLogo中驅動程序的行爲。NetLogo 4.1 - 高速公路的實施(造成汽車碰撞的問題)

我有一些問題,我正在努力解決。

這裏是我的代碼:

globals 
[ 
    selected-car ;; the currently selected car 
    average-speed ;; average speed of all the cars 
    look-ahead 
] 

turtles-own 
[ 
    speed   ;; the current speed of the car 
    speed-limit ;; the maximum speed of the car (different for all cars) 
    lane   ;; the current lane of the car 
    target-lane ;; the desired lane of the car 
    change?  ;; true if the car wants to change lanes 
    patience  ;; the driver's current patience 
    max-patience ;; the driver's maximum patience 
] 

to setup 
    ca 
    import-drawing "my_road3.png" 
    set-default-shape turtles "car" 
    crt number_of_cars 
     [ setup-cars ] 
end 

to setup-cars 
    set color blue 
    set size .9 
    set lane (random 3) 
    set target-lane (lane + 1) 
    setxy round random-xcor (lane + 1) 
    set heading 90 
    set speed 0.1 + random 9.9 
    set speed-limit (((random 11)/10) + 1) 
    set change? false 
    set max-patience ((random 50) + 10) 
    set patience (max-patience - (random 10)) 
    ;; make sure no two cars are on the same patch 
    loop 
    [ 
    ifelse any? other turtles-here 
    [ fd 1 ] 
    [ stop ] 
    ;if count turtles-here > 1 
    ; fd 0.1 
    ;if 
    ; 
    ;ifelse (any? turtles-on neighbors) or (count turtles-here > 1) 
    ;[ 
    ; ifelse (count turtles-here = 1) 
    ; [ if any? turtles-on neighbors 
    ;  [ 
    ;  if distance min-one-of turtles-on neighbors [distance myself] > 0.9 
    ;  [stop] 
    ;  ] 
    ; ] 
    ; [ fd 0.1 ] 
    ;] 
    ;[ stop ] 
    ] 
end 

to go 
    drive 
end 

to drive 
    ;; first determine average speed of the cars 
    set average-speed ((sum [speed] of turtles)/number_of_cars) 
    ;set-current-plot "Car Speeds" 
    ;set-current-plot-pen "average" 
    ;plot average-speed 
    ;set-current-plot-pen "max" 
    ;plot (max [speed] of turtles) 
    ;set-current-plot-pen "min" 
    ;plot (abs (min [speed] of turtles)) 
    ;set-current-plot-pen "selected-car" 
    ;plot ([speed] of selected-car) 

    ask turtles 
    [ 
    ifelse (any? turtles-at 1 0) 
    [ 
     set speed ([speed] of (one-of (turtles-at 1 0))) 
     decelerate 
    ] 
    [ 
     ifelse (look-ahead = 2) 
     [ 
     ifelse (any? turtles-at 2 0) 
     [ 
      set speed ([speed] of (one-of turtles-at 2 0)) 
      decelerate 
     ] 
     [ 
      accelerate 
      if count turtles-at 0 1 = 0 and ycor < 2.5 
      [lt 90 
      fd 1 
      rt 90] 
     ] 
     ] 
     [accelerate 
     if count turtles-at 0 1 = 0 and ycor < 2.5 
      [lt 90 
      fd 1 
      rt 90] 
      ] 
    ] 
    if (speed < 0.01) 
    [ set speed 0.01 ] 
    if (speed > speed-limit) 
    [ set speed speed-limit ] 
    ifelse (change? = false) 
    [ signal ] 
    [ change-lanes ] 
    ;; Control for making sure no one crashes. 
    ifelse (any? turtles-at 1 0) and (xcor != min-pxcor - .5) 
    [ set speed [speed] of (one-of turtles-at 1 0) ] 
    [ 
     ifelse ((any? turtles-at 2 0) and (speed > 1.0)) 
     [ 
     set speed ([speed] of (one-of turtles-at 2 0)) 
     fd 1 
     ] 
     [jump speed] 
    ] 
    ] 
    tick 
end 

;; increase speed of cars 
to accelerate ;; turtle procedure 
    set speed (speed + (speed-up/1000)) 
end 

;; reduce speed of cars 
to decelerate ;; turtle procedure 
    set speed (speed - (slow-down/1000)) 
end 

to signal 
    ifelse (any? turtles-at 1 0) 
    [ 
    if ([speed] of (one-of (turtles-at 1 0))) < (speed) 
    [ set change? true ] 
    ] 
    [ set change? false ] 
end 

;; undergoes search algorithms 
to change-lanes ;; turtle procedure 
    show ycor 
    ifelse (patience <= 0) 
    [ 
    ifelse (max-patience <= 1) 
    [ set max-patience (random 10) + 1 ] 
    [ set max-patience (max-patience - (random 5)) ] 
    set patience max-patience 
    ifelse (target-lane = 0) 
    [ 
     set target-lane 1 
     set lane 0 
    ] 
    [ 
     set target-lane 0 
     set lane 1 
    ] 
    ] 
    [ set patience (patience - 1) ] 

    ifelse (target-lane = lane) 
    [ 
    ifelse (target-lane = 0) 
    [ 
     set target-lane 1 
     set change? false 
    ] 
    [ 
     set target-lane 0 
     set change? false 
    ] 
    ] 
    [ 
    ifelse (target-lane = 1) 
    [ 
     ifelse (pycor = 2) 
     [ 
     set lane 1 
     set change? false 
     ] 
     [ 
     ifelse (not any? turtles-at 0 1) 
     [ set ycor (ycor + 1) ] 
     [ 
      ifelse (not any? turtles-at 1 0) 
      [ set xcor (xcor + 1) ] 
      [ 
      decelerate 
      if (speed <= 0) 
      [ set speed 0.1 ] 
      ] 
     ] 
     ] 
    ] 
    [ 
     ifelse (pycor = -2) 
     [ 
     set lane 0 
     set change? false 
     ] 
     [ 
     ifelse (not any? turtles-at 0 -1) 
     [ set ycor (ycor - 1) ] 
     [ 
      ifelse (not any? turtles-at 1 0) 
      [ set xcor (xcor + 1) ] 
      [ 
      decelerate 
      if (speed <= 0) 
      [ set speed 0.1 ] 
      ] 
     ] 
     ] 
    ] 
    ] 
end 

我知道它有點亂,因爲我使用從庫中的其他型號代碼。

我想知道如何製造汽車碰撞。我想不出任何想法。 正如你注意到我的經紀人有幾乎相同的大小作爲補丁(我把它設置爲0.9,這樣你可以區分兩輛車之間的空間,當他們相鄰設置,我圍繞座標,使他們被設置爲補丁的中心)。

在我的加速程序中,我將座席設置爲左轉,移動1,在循環中右轉。我想知道是否有一個命令可以讓代理從一個通道跳到另一個通道(到它左邊的補丁旁邊),而不會使其轉向並移動。

最後,如果您注意到我創建的汽車代碼,將檢查其左側車道上的貼片以及貼在它前面和後面的貼片。所以如果左邊的3個補丁是空的,那麼它可以改變車道。模糊的部分是,當我運行安裝程序時,有時(並非總是)按下Go鍵,汽車從3條基本車道中熄滅。

爲了理解這一點,我有7條車道。我不使用的是中間一條,這是第0條。然後,在第0條和第3條以下有3條車道。所以我使用的代碼是指我設置汽車的上方3條車道,但由於某些原因,某些汽車改變車道,然後轉到-3車道然後-2車道,等等。

如果有人能給我一個提示,我會非常感激。

預先感謝您。

提示:如果您想在netlogo中試用此代碼,請記住,在界面選項卡上,我有2個按鈕,一個設置,一個以及3個滑塊,名稱分別爲number_of_cars,speed-up,slow-down。

回答

3

要檢查碰撞,只需要讓龜找到最接近的其他龜。如果他們的距離小於1,那麼他們就會崩潰。因此:

let closest min-one-of other turtles [distance myself] 
if (distance closest < 1) [ 
    ;;we crashed 
] 
+0

我有類似的想法,但它似乎並沒有工作。檢查它: 如果距離min-one-of-turtles-neighbours [距離自己]> 0.9 [停止] 但由於某種原因,一個以上的烏龜(汽車)被設置爲相同的補丁和他們當我檢查大於1!這怎麼可能呢? – solidsn2004 2010-04-01 08:53:31

+1

由於幾何。一個補丁是1x1,如果一個龜在補丁的中心,另一個在另一個補丁的邊緣,或者在補丁的中心,則它們相距不止1。 – 2010-04-08 10:56:41

2

重,「出於某種原因不止一個海龜(汽車)都設置到相同的補丁,當我檢查大於1時它們之間的距離這怎麼可能!?」考慮同一個補丁對角的兩隻烏龜。它們之間的距離是1.414 ...(對角線的長度),儘管它們在同一個補丁上。