Netlogo:如何在世界中间使用特定补丁将海龟停下来停下来? [英] Netlogo: How to stop a turtle for a certain ticks with a specific patch in the middle of world?

查看:329
本文介绍了Netlogo:如何在世界中间使用特定补丁将海龟停下来停下来?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是初学者.我已经检查了编程指南词典. 我正在考虑两条车道的道路(例如1号道路,2号道路)模型. 而且我也在考虑一个模型,其中由指定补丁((10 0)和(20 2))指定的乌龟停下来10个滴答声. 但是,我不知道如何为每条道路编写和指定xcor和ycor的特定参数(例如,道路1的xcor和ycor,道路2的xcor和ycor). 而且我也不知道如何在set-speed语法中编写和控制参数"speed". 以下是样本小型模型.为了避免复杂化,此样本模型只有一条路.该示例模型失败,乌龟没有停止在patch(10 0)处. 可能我需要您的建议.谢谢.

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

这是模型的主体.

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

推荐答案

好的,通常,一次向模型添加一个元素.测试该元素,然后在一切正常后再添加下一个元素.在您的情况下,您正在尝试做几件事而使它们都不工作:移动汽车,将其暂停10个滴答声,使其中之一在路的尽头死掉,对速度进行未指定的操作以及可能是我做的其他事情不会立即注意到.

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.

您在这里还有几个概念上的问题-最大的问题是count-tick是一个乌龟变量,但是您将其视为全局变量,因为if count-tick...应该在ask turtles块内.这样考虑一下,如果您创建了10辆汽车,那么将有10个变量count-tick的副本,因此您要使用if语句检查哪一个.

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.

您也没有告诉乌龟移动,但这可能是在您未显示的代码中.我会尽量保留您的代码,这是我认为您正在尝试做的事情.这将在左侧创建一辆汽车,将其向右移动,在正确的位置停顿10个滴答声,然后变红,然后再次移动,直到到达终点时将其杀死.

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

这篇关于Netlogo:如何在世界中间使用特定补丁将海龟停下来停下来?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆