我正在尝试对Netlogo中的高速公路和驾驶员的行为进行模拟。

我有一些我努力解决的问题。

这是我的代码:

    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,以便您可以区分2辆汽车之间的空间,然后将它们彼此隔开,我绕着坐标,以便将它们设置为中心补丁)。

在加速过程中,我将代理设置为向左转,移动1,右转。我想知道是否有一个命令可以让我让代理从一个车道跳到另一个车道(左侧的旁边的补丁),而不会使其转弯并移动。

最后,如果您注意到代码,我创建的汽车会检查左侧的车道上的旁边的补丁,并在其前面的贴片和背面的补丁。因此,如果其左侧的3个补丁是空的,则可以更换车道。模糊的部分是,当我运行设置时,我有时会(并非总是)按下3个基本车道。

为了理解这一点,我有7条车道。我不使用的中间一个是车道0。然后,下面的车道0和3的顶部有3条车道。因此,我正在使用的代码是指我设置汽车的上部3车道,但是由于某种原因,有些汽车更换车道,然后转到-3 lane -3然后 - 2等。

如果有人可以给我一个小费,我将非常感谢。

先感谢您。

提示:如果您想在netlogo中尝试此代码,请记住,在接口选项卡上,我有2个按钮一个设置和一个,以及3个带有名称的滑块:number_of_of_cars,加速,减速。

有帮助吗?

解决方案

要检查撞车事故,只需要求乌龟找到最接近的乌龟即可。如果他们的距离小于1,那么他们崩溃了。像这样:

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

其他提示

重新“出于某种原因。它们之间的距离为1.414 ...(对角线的长度),即使它们在同一贴片上。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top