在交界处分配概率

时间:2019-02-27 13:32:48

标签: netlogo probability traffic

您好,我是netlogo的新手,感谢您的协助。我在十字路口/交叉路口为汽车建模,该模型中的汽车使用道路的左侧移动。 我有2个问题。

  1. 为来自不同交叉路口的汽车分配概率,使其左转或右转或不转弯。

    • 例如,当Car X方向90到达斑块pxcor = 137和pycor = 155时,有0.3的可能性左转或0.7 连续进行的概率。一旦Car X达到pxcor = 138和pycor = 155就有0.4的概率 向右转弯和继续前进的概率为0.6。

    • 相同的逻辑将适用于Car Y航向270-这辆车有0.6的可能性向左转第一弯或0.4的可能性使车辆转弯 一旦到达补丁pxcor = 138和pycor = 154。一旦汽车Y达到pxcor = 137和Pycor = 155,它就有0.2的右转概率 或不转弯(直行)的概率为0.8

  2. 一旦汽车离开路口,我该如何使它们在世界范围内不被重新包装。我的模型中发生的事情是,一旦汽车x左转 是否以XY公路pxcor = 137和(pycor> 21且pycor <154)的形式出现在汽车XY上,并且意味着!=“” crossroad“

您可以参考我的代码和所附图片 谢谢。

breed[cars car]
breed[lightsR lightR]
breed[lightsL lightL]
breed[lightsU lightU]
breed[lightsD lightD]
breed[crossings crossing]
cars-own[
  speed          
  maxSpeed     
]
globals[
  redV
  greenV
  redH
  greenH
  speedLimit
]

patches-own[
  meaning        
  will-cross?
]
to setup
  ca
  set speedLimit speed-limit
  draw-roads
  place-cars
  place-lights
  draw-crossings
  reset-ticks
  tick
end
to go
  move-cars
  control-traffic-lights
  tick
end
to control-traffic-lights
  if ticks mod (50 * lights-interval * greenH + 65 * lights-interval * redH ) = 0 [change-color lightsR "H" change-color lightsL "H"]
  if ticks mod (50 * lights-interval * greenV + 65 * lights-interval * redV ) = 0 [change-color lightsU "V" change-color lightsD "V"]
end
to change-color [lights D]
  ask lights [
    ifelse color = red [
      ifelse D = "H" [
        set greenH greenH + 1
        ][
        set greenV greenV + 1]
        ]
    [
      ifelse D = "H" [
        set redH redH + 1][
        set redV redV + 1]
        ]
  ]
  ask lights [
    ifelse color = red [set color green] [set color red]
  ]

end
to draw-roads

  ;create crossroads
  ask patches with [(pxcor = 137 and pycor = 154 ) or (pxcor = 137 and pycor = 155 ) or ( pxcor = 138 and pycor = 155 )
                    or ( pxcor = 138 and pycor = 154 ) ] [
  set pcolor white
  set meaning "crossroad"
    ]
    ;roads-up
  ask patches with [pxcor = 137 and (pycor > 155 and pycor < 320) and meaning != "crossroad"] [
    set pcolor grey
    sprout 1 [
     set shape "road2"
     set color grey
     die
    ]
   set meaning "A-47-2-In"]

   ask patches with [pxcor = 137 and (pycor > 21 and pycor < 154)  and meaning != "crossroad"] [
    set pcolor grey
    sprout 1 [
     set shape "road2"
     set color grey
     set heading 270
     die
    ]
    set meaning "A-15-Exit"]

  ask patches with [pxcor = 138 and (pycor > 155 and pycor < 320) and meaning != "crossroad" ] [
    set pcolor grey
    sprout 1 [
      set shape "road2"
      set color grey
      set heading 90
      stamp die
    ]
    set meaning "A-47-2-Exit"]

   ask patches with [ pxcor = 138 and (pycor > 21 and pycor < 154) and meaning != "crossroad"] [
    set pcolor grey
    sprout 1 [
     set shape "road2"
     set color grey
     set heading 90
     stamp die
    ]
    set meaning "A-15-In"]

  ask patches with [ ( pycor =  155 and ( pycor > min-pycor and pycor < 157) ) and meaning != "crossroad" ] [
    set pcolor grey
    sprout 1[
    set shape "road2"
    set heading 360
    set color grey
    stamp die
  ]
   set meaning "M1-Left"
  ]
   ;roads-right
  ask patches with [ (pycor = 154) and ( pycor > min-pycor and pycor < 157 ) and meaning != "crossroad" ] [
    set pcolor grey
    sprout 1[
    set shape "road2"
    set color grey
    die
  ]
    set meaning "M1-Right"]

end

to draw-crossings

  ask patches with [(meaning = "A-47-2-Exit" or meaning = "A-47-2-In" or meaning =  "A-15-In" or meaning =  "A-15-Exit"  )
    and (pxcor = 137 or pxcor = 138 ) and ( pycor = 162 or pycor = 163 or pycor = 146 or pycor = 147) ][
    sprout-crossings 1 [
      set shape "crossing"
      set color white
      set heading 0
      set size 1
    ]
  ]

  ask patches with [(meaning = "M1-Left" or meaning = "M1-Right") and (pxcor = 121 or pxcor = 122 or pxcor = 214 or pxcor = 215) and
    (pycor = 154 or pycor = 155  )][
    sprout-crossings 1 [
      set shape "crossing"
      set heading 90
      set color white
      set size 1
    ]
  ]
ask crossings [
    set will-cross? false
    set meaning "crossing"
    stamp
    die
  ]

  ask patches with [meaning = "crossing"] [
    ask neighbors4 [
        ]
      ]
end

to place-cars

  ask n-of (num-of-cars / 2) patches with [pxcor = 138 and (pycor > 155 and pycor < 320) and count turtles-on neighbors = 0]
  [
      sprout-cars 1 [
        set size 2
        set shape "car top"
        set heading 180
        let s random 10
        if s < 7 [set maxSpeed speed-limit - 15 + random 16]
        if s = 7 [set maxSpeed speed-limit - 20 + random 6]
        if s > 7 [set maxSpeed speed-limit + random 16]
        set speed maxSpeed - random 20
      ]
    ]
 ask n-of (num-of-cars / 3 ) patches with [ (pycor =  155) and pxcor < 156  ] [
        sprout-cars 1 [
          set size 2
          set shape "car top"
          set heading 90
          let s random 10
          if s < 7 [set maxSpeed speed-limit - 15 + random 16]
          if s = 7 [set maxSpeed speed-limit - 20 + random 6]
          if s > 7 [set maxSpeed speed-limit + random 16]
          set speed maxSpeed - random 20
        ]
      ]


ask n-of (num-of-cars / 5) patches with [pycor = 154 and (pxcor > 243 )] [
      sprout-cars 1 [
        set size 2
        set shape "car top"
        set heading 270
        let s random 10
        if s < 7 [set maxSpeed speed-limit - 15 + random 16]
        if s = 7 [set maxSpeed speed-limit - 20 + random 6]
        if s > 7 [set maxSpeed speed-limit + random 16]
        set speed maxSpeed - random 20
      ]
    ]
end

to place-lights

  ask patches with [ pxcor = 138 and pycor = 156 ] [
    sprout-lightsD 1 [
      set color green
      set shape "lights"
    ]
  ]

  ask patches with [pxcor = 136 and pycor = 155 ] [
    sprout-lightsD 1 [
      set color red
      set shape "lights"
    ]
  ]

   ask patches with [pxcor = 139 and pycor = 154] [
    sprout-lightsD 1 [
      set color red
      set shape "lights"
    ]
  ]

  ask patches with [pxcor = 137 and pycor = 153] [
    sprout-lightsD 1 [
      set color green
      set shape "lights"
    ]
  ]

  set greenH 0
  set redH 1
  set redV 0
  set greenV 1
end

; Car procedures

to control-speed
  let car-ahead one-of cars-on patch-ahead 1.5
  ifelse car-ahead = nobody  [
    ifelse speed < maxSpeed [set speed speed + acceleration] [set speed speed - deceleration]
  ]
  [
    ifelse [speed] of car-ahead = 0 [set speed 0] [
      ifelse [speed] of car-ahead >= maxSpeed [
        set speed maxSpeed
        set speed speed - deceleration
      ] [

          set speed [speed] of car-ahead
          set speed speed - deceleration]
      ]
      ]
end

to move-cars
  ask cars [
    control-speed
    check-crossing
    ;whether traffic lights show red or green
    ifelse not any? (lightsR-on patch-ahead 1.5) with [color = red] and not any? (lightsL-on patch-ahead 1.5) with [color = red]
    and not any? (lightsD-on patch-ahead 1.5) with [color = red] and not any? (lightsU-on patch-ahead 1.5) with [color = red] [fd speed / 200 ] [set speed 0]

  ]
end

;Probabilitities of cars making a left or right turn or going straight
to check-crossing

  ask cars with [(pxcor = 138 and pycor = 154) and meaning = "crossroad" and heading = 180 ] [
    if random 100 < 50  [ set heading 270  move-to patch-ahead 0 ]

    ]

   ask cars with [(pxcor = 137 and pycor = 155) and meaning = "crossroad" and heading = 90 ] [
    if random 100 < 60  [ set heading 0 move-to patch-ahead 0 ]
    ]
end

Junction Picture

0 个答案:

没有答案