首页 > 解决方案 > 我可以根据比较变量值的结果将乌龟指向特定的补丁吗?

问题描述

标签: netlogo

解决方案


patch-group-id is a variable owned by patches and group-id is owned by turtles. So you need to tell NetLogo which group-id the patches are trying to match to. Try this:

ask turtles
[ let target one-of patches with [patch-group-id = [group-id] of myself]
]

Another way to do this is to drop the idea of group-id and simply have each turtle remember its home patch. Conceptually, this implements the idea that a group is entirely defined by its home patch. So your setup would look something like:

turtles-own
[ my-home
]

to setup
  clear-all
  ask n-of n-groups patches
  [ sprout turtles 25
    [ set my-home patch-here
    ]
    set pcolor gray
  ]
end

Then you never need to construct the target, you simply get then to go to their variable my-home.

If you went down this path, you would also need to change the code that uses group-id. For example, you said that sometimes the turtles change their group-id, instead of set group-id [group-id] of ... you would have set my-home [my-home] of ...


推荐阅读