Netlogo: How to stop a turtle for a certain ticks with a specific patch in the middle of world?

笑着哭i 提交于 2019-12-10 11:57:39

问题


I am a beginner. I already checked the programming guidance dictionary. I am considering a two-lane road (eg, road 1, road 2) model. And also I am considering a model in which the turtle specified by the specified patch((10 0) and (20 2)) stops for 10 ticks. However, I do not know how to write and specify the specific parameter for xcor and ycor for each road (Eg xcor and ycor on road 1, xcor and ycor on road 2). And also I do not know how to write and controol the parameter "speed" within the set-speed syntax. The following is the sample small model. To avoid complication, this sample model has only one road. This sample model is failing and the turtle does not stop at patch(10 0). Probably I need your advice. Thank you.

globals [ count-tick ]
turtles-own [ speed flag-A ]

to setup
  clear-all
  resize-world 0 50 min-pycor max-pycor
  ask patches [ setup-road ]
  reset-ticks
end

to setup-road
  if ( pycor < 1 ) and ( pycor > -1 ) [ set pcolor white ]
end

to create-car
  crt 1 [
    set color blue
    setxy min-pxcor 0
    set heading 90
    set speed 1
  ]
end

This is the main body of the model.

to go

  if (count turtles-on patch 0 0 = 0) [
    create-car
    ask (turtles-on patch 0 0) [
      set flag-A FALSE
    ]
  ]

  ask (turtles-on patch 10 0) [
    set flag-A TRUE
    set count-tick 10
  ]

  if count-tick > 0 [
    set count-tick count-tick - 1
    ask (turtles-on patch 10 0) with [flag-A = TRUE]
    [
      set color red
      set speed 0
    ]
  ]

  if count-tick = 0 [
    ask (turtles-on patch 10 0) with [flag-A = TRUE]
      [
        set speed 1
        set flag-A FALSE
    ]
  ]

  if (count turtles-on patch max-pxcor 0 > 0) [
    ask min-one-of turtles [who][
      die
    ]
  ]

  set-speed
  tick
end

This is the parallel update to control the speed.

to set-speed
  ask turtles with [ xcor < 10 ] [
    let turtle-ahead one-of turtles-on patch-ahead 1
    ifelse turtle-ahead = nobody
      [ set speed 1
        fd speed
    ]
    [ set speed 0
    ]
  ]
    ask turtles with [ 10 < max-pxcor ] [
    let turtle-ahead one-of turtles-on patch-ahead 1
    ifelse turtle-ahead = nobody
      [ set speed 1
        fd speed
    ]
    [ set speed 0
    ]
  ]
end

回答1:


Okay, as a general rule, add ONE element at a time to your model. Test that element and then only add the next element once everything works. In your case you are trying to do several things without any of them working - moving cars, pausing them for 10 ticks, making one of them die at the end of the road, doing something unspecified with their speed, and probably other things I didn't immediately notice.

You also have several conceptual problems here - the biggest is that count-tick is a turtle variable, but you are treating it as a global variable because if count-tick... should be inside an ask turtles block. Think about it this way, if you have 10 cars created, there are 10 copies of the variable count-tick so which one are you checking with the if statement.

You also haven't told your turtles to move, but that may be in the code you haven't shown. Keeping as much of your code as I can, this is what I think you are trying to do. This will create a car at the left, have it move to the right, pause at the correct place for 10 ticks and turn red, then move again, killing it when it gets to the end.

globals [ count-tick ]
turtles-own [ speed flag-A ]

to setup
  clear-all
  resize-world 0 50 min-pycor max-pycor
  ask patches [ setup-road ]
  reset-ticks
end

to setup-road
  if ( pycor < 1 ) and ( pycor > -1 ) [ set pcolor white ]
end

to create-car
  crt 1 [
    set color blue
    setxy min-pxcor 0
    set heading 90
    set speed 1
    set flag-A FALSE
  ]
end

to go

  if (count turtles-on patch 0 0 = 0) [
    create-car
  ]

  ask (turtles-on patch 10 0) [
    set flag-A TRUE
    set count-tick 10
  ]

  ask (turtles-on patch 10 0) with [flag-A = TRUE] [
    set color red
    set speed 0
    set count-tick count-tick - 1

    if count-tick = 0 [
      set speed 1
      set flag-A FALSE
    ]
  ]

  if (count turtles-on patch max-pxcor 0 > 0) [
    ask min-one-of turtles-on patch max-pxcor 0 [who][
      die
    ]
  ]

  ask turtles [ forward speed ]

  tick
end


来源:https://stackoverflow.com/questions/47365862/netlogo-how-to-stop-a-turtle-for-a-certain-ticks-with-a-specific-patch-in-the-m

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!