首页 > 解决方案 > 你如何让海龟只跟随我从加载到 netlogo 的 shapefile 创建的绿色补丁?

问题描述

到目前为止,这是我的代码。我只希望海龟跟随绿线,但是当我设置模型时,它们只会永远沿着它们所面对的随机方向继续。

代码:

extensions [gis]
breed [observer]

turtles-own [ vision-distance vision-width steps green-steps gray-steps attr-prob]
patches-own [land nearest-patch]

to setup
  clear-all
  create-turtles 10
  set-default-shape turtles "butterfly"
  ask turtles [set size 25
  if pxcor = min-pxcor [die]]
  reset-ticks
  let view gis:load-dataset "City_Plan_Boundary.shp"
  gis:set-world-envelope gis:envelope-of view
  foreach gis:feature-list-of view
  [
    gis:set-drawing-color green
    gis:draw ? 1.0
  ]

end

to go
  ask turtles [
    count-steps

pen-down
    let green_target turtles
    let perceived_patches patches in-cone vision-distance vision-width
    let patch-under-me patch-here    set green_target perceived_patches with [ (pcolor = green and self != patch-under-me) or (pcolor = black and self != patch-under-me)]
    ifelse  count green_target != 0 [
          let target min-one-of green_target[ distance myself ]
          let target_heading towards target
          move-to patch-at-heading-and-distance target_heading 1 ]
          [ fd 1]

     ]


end

to count-steps
  set steps steps + 1
  ifelse land = green [set green-steps green-steps + 1][set gray-steps gray-steps + 1] ;;Does not currently account for CYAN Park squares
end

标签: gisnetlogo

解决方案


首先,foreach应用 GIS 层的循环只是在 NetLogo 的绘图层中创建线,而不是实际更改补丁本身。您必须将 shapefile 的某些组件分配给补丁本身,以便海龟能够评估它们 - 查看模型库中的“GIS 一般示例”模型。

其次,寻路可以通过多种方式完成,这实际上取决于您尝试建模的行为。对于一些示例,请查看模型库中的“前瞻”示例,或者查看下面的玩具模型以获得非常简单的方法:

to setup
  ca
  ask patch min-pxcor 0 [
    set pcolor green    
    sprout 1 [ 
      set color red 
      pd
    ]
    spread-right
  ] 
  reset-ticks
end

to spread-right 
  if pxcor < max-pxcor [
    ask one-of neighbors with [ pxcor = [pxcor] of myself + 1] [
      set pcolor green
      spread-right
    ]
  ]
end

to go 
  ask turtles [
    let target one-of neighbors in-cone 1.5 90 with [ pcolor = green ]
    ifelse target != nobody [
      face target
      move-to target
    ] [
      rt one-of [ 45 -45 ]
    ]
  ]
  tick
end

推荐阅读