2017-11-18 159 views
0

我是初学者。我已经检查过编程指导字典。 我正在考虑双车道道路(例如道路1,道路2)模型。 而且我正在考虑一个模型,其中由指定的补丁((10 0)和(20 2))指定的龟停止10次滴答。 但是,我不知道如何为每条道路编写和指定xcor和ycor的具体参数(例如道路1上的xcor和ycor,道路2上的xcor和ycor)。 而且我也不知道如何在设置速度语法内编写和控制参数“速度”。 以下是示例小型模型。为了避免并发症,这个样本模型只有一条道路。此示例模型失败,龟不会停在修补程序(10 0)处。 也许我需要你的建议。谢谢。Netlogo:如何阻止一只特定的蜱龟在世界中间的特定补丁?

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 

这是控制速度的并行更新。

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 

回答

3

好的,作为一般规则,一次添加一个元素到你的模型。测试该元素,然后只有在一切正常时才添加下一个元素。在你的情况下,你正在尝试做几件事情,而没有任何工作 - 移动汽车,把它们停留10次,让它们中的一个死在路的尽头,用他们的速度做未指定的事情,可能还有其他事情我没做过马上注意。

这里还有几个概念问题 - 最大的是count-tick是一个龟变量,但是您将它视为全局变量,因为if count-tick...应位于ask turtles块内。这样想一下,如果您创建了10辆汽车,则有10个变量计数复制副本,因此您使用if声明查看哪一个。

你也没有告诉你的海龟移动,但可能是你没有显示的代码。保持尽可能多的代码,这是我认为你正在尝试做的事情。这将在左侧创建一辆汽车,向右移动,在正确位置暂停10次刻度,然后转为红色,然后再次移动,当它结束时将其杀死。

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 
+0

非常感谢您对我的问题的建议和示例语法。这是一个很大的帮助。再一次非常感谢你。 – goodgest

+1

绝对同意仁的建议,一次只编码的东西。从一个非常小的工作程序开始,并且一次只添加一件事,然后再继续工作。 –

相关问题