让Net龟在NetLogo中的2个元素之间完成

时间:2018-02-28 13:45:57

标签: netlogo

我是NetLogo的新手,我试图模拟一组海龟必须从一个随机空间进入目标点和墙壁之间,但到目前为止我还是我只是检索了一部分代码,使它们自己放置在该目标点附近的所有方向(海龟有能力相互说话)。

但是,我无法让他们完成任何一方...任何提示?

非常感谢

1 个答案:

答案 0 :(得分:3)

我不确定你是否还想让他们围绕目标点聚集,或者只是移动到目标点之上。这是获得你所追求的一种方式。它定义了一个目标点和一组安全补丁。目标点下面的海龟试图穿过目标点到达安全区域。评论中的更多细节/解释

设置:

globals [ goal-spot safe-patches ]

to setup
  ca
  ;   Set the wall
  ask patches with [ pycor = max-pycor ] [ set pcolor blue ]

  ;  Set the goal-patch
  set goal-spot patch 0 0
  ask goal-spot [ set pcolor green ]

  ;     Set up a triangular safe-patches zone
  let check ( [pycor] of goal-spot - [pxcor] of goal-spot ) - 1
  let gpx [pxcor] of goal-spot
  set safe-patches patches with [
    pycor > [pycor] of goal-spot and
    pycor < max-pycor and
    ( ( pxcor < gpx and pycor + pxcor > check ) or
      ( pxcor >= gpx and pycor - pxcor > check ) ) ]

  ;  Make some turtles
  let n-turtles 255
  if n-turtles > count safe-patches [
    set n-turtles count safe-patches 
  ]
  crt n-turtles [
    set shape "person"
    set color green
    move-to one-of patches with [ pycor < [pycor] of goal-spot and pycor < max-pycor]
    if pycor <= [pycor] of goal-spot [
      set color red
    ]
  ]
  ask turtles [pd]
  reset-ticks
end

运动:

to go
  ;   Any turtles that are not on the safe spot, or share a patch
  ask turtles with [not member? patch-here safe-patches or any? other turtles-here] [
    let target nobody

    ;     If below the goal spot, target it. If not, target a free safe patch
    ifelse pycor < [pycor] of goal-spot [
      set target goal-spot
    ] [
      set target min-one-of ( safe-patches with [ 
        not any? other turtles-here ] ) [distance myself
      ]
    ]

    ;     Face target, move to it.
    if target != nobody [
      face target
      move-to patch-ahead 1
    ]
  ]

  ;  Stop when all turtles are safe and on their own patch
  if not any? turtles with [ 
    not member? patch-here safe-patches or
    any? other turtles-here 
  ] [
    stop
  ]

  tick
end

希望能让你开始吧!

行为示例:

enter image description here

enter image description here

修改

根据你的评论 - 是的,你可以做那样的事情。当然有很多方法可以解决这个问题,但实际的实施方式会根据您的实际模型和意图等而改变。例如,这里有一个使用两个安全区域的修改:

globals [ goal-spots safe-patches ]

to setup
  ca

  ;   Set the walls
  ask ( patch-set 
    patches with [ pycor = max-pycor ] 
    patches with [ pxcor = max-pxcor ] 
    )
    [
    set pcolor blue
  ]

  let possible-safe patches with [ pcolor = black ]

  ;     Set up two triangular safe-patches
  crt 1 [
    set heading 0 
    fd 7 
    set goal-spots patch-here
    set safe-patches possible-safe in-cone 20 90
    set heading 90
    setxy 7 0
    set goal-spots ( patch-set goal-spots patch-here )
    set safe-patches ( 
      patch-set 
      safe-patches 
      possible-safe in-cone 20 90 ) 
    die
  ]

  ask safe-patches [
    set pcolor green - 4
  ]

  ;  Make some turtles
  let n-turtles 100
  if n-turtles > count safe-patches [
    set n-turtles count safe-patches
  ]
  crt n-turtles [
    set shape "person"
    set color green
    move-to one-of patches with [ pycor < 0 and pxcor < 0 ]
    set color red
  ]
  ask turtles [pd]

  reset-ticks
end


to go
  ;   Any turtles that are not on the safe spot, or share a patch
  ask turtles with [not member? patch-here safe-patches or any? other turtles-here] [
    let target nobody

    ;     If below the goal spot, target it. If not, target a free safe patch
    ifelse not member? patch-here safe-patches [
      set target min-one-of goal-spots [ distance myself ]
    ] [
      set target min-one-of ( safe-patches with [
        not any? other turtles-here ] ) [distance myself
      ]
    ]

    ;     Face target, move to it.
    if target != nobody [
      face target
      move-to patch-ahead 1
    ]
  ]

  ;  Stop when all turtles are safe and on their own patch
  if not any? turtles with [
    not member? patch-here safe-patches or
    any? other turtles-here
  ] [
    stop
  ]

  tick
end

请注意,这只是一个有明显问题的方法的快速示例(例如,如果你在一个补丁上有太多的乌龟,一只或两只海龟会因为试图获得&#34;最近的安全区,即使它已满。我想说如果你能以某种方式定义你的两个安全区域然后遇到问题,那么值得发布一个新问题来弄清楚如何解决你遇到的任何新问题。