如何在netlogo中设置2路4车道

时间:2018-08-26 07:48:08

标签: netlogo

所以我只是在研究netlogo库中的2通道流量模型。我对代码进行了一些更改,以使汽车可以双向行驶。但是我不知道如何在正确的位置产卵它们。谁能帮我?我认为这与create-mice和create-frogs中的pycor有关。谢谢。

breed [mice mouse]
breed [frogs frog]
globals [
  selected-car   ; the currently selected car
  lanes          ; a list of the y coordinates of different lanes
]

turtles-own [
  speed         ; the current speed of the car
  top-speed     ; the maximum speed of the car (different for all cars)
  target-lane-up  ; the desired lane of the car
  target-lane-down
  patience      ; the driver's current level of patience

]

to setup
  clear-all
  draw-road
  create-or-remove-cars
  set selected-car one-of turtles
  ask selected-car [ set color red ]
  reset-ticks
end

to create-or-remove-cars

  let road-patches patches with [ member? pycor lanes ]
  if number-of-cars > count road-patches [
    set number-of-cars count road-patches
  ]

  create-mice (number-of-cars / 2 - count mice) [
    set color car-color
    set shape "car"
    move-to one-of free road-patches
    set target-lane-up pycor
    set heading 90
    set top-speed 0.5 + random-float 0.5
    set speed 0.5
    set patience random max-patience
  ]

  create-frogs (number-of-cars / 2 - count frogs) [
    set color car-color
    set shape "car flipped"
    move-to one-of free road-patches
    set target-lane-down pycor
    set heading -90
    set top-speed 0.5 + random-float 0.5
    set speed 0.5
    set patience random max-patience
  ]

  if count turtles > number-of-cars [
    let n count turtles - number-of-cars
    ask n-of n [ other turtles ] of selected-car [ die ]
  ]

end

to-report free [ road-patches ] ; turtle procedure
  let this-car self
  report road-patches with [
    not any? turtles-here with [ self != this-car ]
  ]
end

to draw-road
  ask patches [
    set pcolor green - random-float 0.5
  ]
  set lanes n-values number-of-lanes [ n -> number-of-lanes - (n * 2) - 1 ]
  ask patches with [ abs pycor <= number-of-lanes ] [
    set pcolor grey - 2.5 + random-float 0.25
  ]
  draw-road-lines
  draw-line-middle
end

to draw-road-lines
  let y (last lanes) - 1 ; start below the "lowest" lane
  while [ y <= first lanes + 1 ] [
       if not member? y lanes [
      ifelse abs y = number-of-lanes
        [ draw-line y yellow 0 ]  ; yellow for the sides of the road
        [ draw-line y white 0.5 ] ; dashed white between lanes
    ]
    set y y + 1 ; move up one patch
  ]
end

to draw-line [ y line-color gap ]
  create-turtles 1 [
    setxy (min-pxcor - 0.5) y
    hide-turtle
    set color line-color
    set heading 90
    repeat world-width [
      pen-up
      forward gap
      pen-down
      forward (1 - gap)
    ]
    die
  ]
end

to draw-line-middle
  create-turtles 1
  [
    set heading 90
    set color yellow
    pen-down
    fd world-width
    die
  ]
end

to go
  create-or-remove-cars
  ask mice [ move-forward ]
  ask frogs [move-backward]
  ask mice with [ patience <= 0 ] [ choose-new-lane ]
  ask frogs with [ patience <= 0 ] [ choose-new-lane ]
  ask mice with [ ycor != target-lane-up ] [ move-to-target-lane ]
  ask frogs with [ ycor != target-lane-down ] [ move-to-target-lane ]
  tick
end

to move-forward 
  set heading 90
  speed-up-car ; we tentatively speed up, but might have to slow down
  let blocking-cars other turtles in-cone (1 + speed) 180 with [ y-distance <= 1 ]
  let blocking-car min-one-of blocking-cars [ distance myself ]
  if blocking-car != nobody [
    set speed [ speed ] of blocking-car
    slow-down-car
  ]
  forward speed
end

to move-backward 
  set heading -90
  speed-up-car
  let blocking-cars other turtles in-cone (1 + speed) 180 with [ y-distance <= 1 ]
      let blocking-car min-one-of blocking-cars [ distance myself ]
      if blocking-car != nobody [
        set speed [ speed ] of blocking-car
        slow-down-car
      ]
      forward speed
    end

to slow-down-car 
  set speed (speed - deceleration)
  if speed < 0 [ set speed deceleration ]
  set patience patience - 1
end

to speed-up-car 
  set speed (speed + acceleration)
  if speed > top-speed [ set speed top-speed ]
end

to choose-new-lane ; turtle procedure
  let other-lanes remove ycor lanes
  if not empty? other-lanes [
    let min-dist min map [ y -> abs (y - ycor) ] other-lanes
    let closest-lanes filter [ y -> abs (y - ycor) = min-dist ] other-lanes
    set target-lane-up one-of closest-lanes
    set target-lane-down one-of closest-lanes
    set patience max-patience
  ]
end

to move-to-target-lane 
  set heading ifelse-value (target-lane-up < ycor) [ 180 ] [ 0 ]
  let blocking-mice other mice in-cone (1 + abs (ycor - target-lane-up)) 180 with [ x-distance <= 1 ]
  let blocking-mouse min-one-of blocking-mice [ distance myself ]
  ifelse blocking-mouse = nobody [
    forward 0.2
    set ycor precision ycor 1 ; to avoid floating point errors
  ] [
    ifelse towards blocking-mouse <= 180 [ slow-down-car ] [ speed-up-car ]
  ]
  set heading ifelse-value (target-lane-down < ycor) [ 180 ] [ 0 ]
  let blocking-frogs other frogs in-cone (1 + abs (ycor - target-lane-down)) 180 with [ x-distance <= 1 ]
  let blocking-frog min-one-of blocking-frogs [ distance myself ]
  ifelse blocking-frog = nobody [
    forward 0.2
    set ycor precision ycor 1 ; to avoid floating point errors
  ] [
    ifelse towards blocking-frog <= 180 [ slow-down-car ] [ speed-up-car ]
  ]
end

to-report x-distance
  report distancexy [ xcor ] of myself ycor
end

to-report y-distance
  report distancexy xcor [ ycor ] of myself
end

to select-car
  if mouse-down? [
    let mx mouse-xcor
    let my mouse-ycor
    if any? turtles-on patch mx my [
      ask selected-car [ set color car-color ]
      set selected-car one-of turtles-on patch mx my
      ask selected-car [ set color red ]
      display
    ]
  ]
end

to-report car-color
  report one-of [ blue sky cyan yellow green] + 1.5 + random-float 1.0
end

to-report number-of-lanes
  report 4
end


; Copyright 1998 Uri Wilensky.
; See Info tab for full copyright and license.

0 个答案:

没有答案