英文:
How save x y of patches with certain characteristics
问题
我想根据种植类型将坐标存储在一个空列表中。目前我有一个名为myland的空列表,代理将附近的东西存储在其中。
例如,我有番茄、大豆和牛,以及中间的一个乌龟。我有以下代码:
ask locals [set myland [plantation] of patches in-radius 5 with [plantation != "Cows"]]
这会为我带来农业用地的地块列表,但现在我想知道在该半径内这些地块的位置。
你会怎么做?
计划是以不同的颜色标记这些地块。
英文:
I want to store in an empty list the coordenates of a patch based on its type of cultivation. For now i have and empty list called myland in which the agents store whats near them.
For example, I have tomato soy and cows, and a turtle in the middle. I have this.
ask locals [set myland [plantation] of patches in-radius 5 with [plantation != "Cows"]]
This brings me the list of patches that are for agriculture, but now i want to know where in that radius are those patches.
How would you do it?
The plan is to color those patches differently
答案1
得分: 2
我建议你不要这样做。那条路通向痛苦和疯狂。
与其将种植字符串值存储在列表中,不如将这些补丁本身存储在一个代理集中。这将使其他一切变得更容易。
但首先,请记住NetLogo不是强类型的,大多数值都是不可变的。这意味着当你赋予一个新值时,之前的值会被替换。所以,如果你初始化一个变量为"空列表",然后使用"set"放置其他东西,那是没有关系的。
现在回到你的问题。
如果你的意图是根据补丁变量的值着色补丁,那么要求乌龟找到补丁,然后对它们着色似乎效率较低。
更简单的方法是只要求补丁根据种植值自己着色。
考虑以下:
let cow-color brown
let wheat-color yellow
let grass-color green
ask patches
[
ifelse
(Plantation = "cows")
[set pcolor cow-color]
(Plantation = "wheat")
[set pcolor wheat-color]
;; 否则
[set pcolor grass-color]
]
值得注意的是,你的示例代码没有返回一个补丁的列表。它返回了一个字符串列表,来自种植变量。
但你的想法是正确的。通常最好将补丁或乌龟本身存储在一个代理集中。这通常会使其他一切变得更容易。
你的原始示例可以是:
ask locals
[
set myland patches in-radius 5
;; 对myland进行各种操作
]
英文:
I would suggest that you do not do that. That that way lies pain and madness.
Rather than storing the plantation string value in a list, store the patches themselves in an agentset. This will make everything else easier.
But first, remember that NetLogo is not strongly typed, and most values are immutable. This means that is doesn't matter was was in a variable before, when you assign a new value, the old value is replaced. So, it doesn't matter if you initialize a variable to an "empty list" if you then use 'set' to put something else there.
Now to your question.
If your intent is to color patches based on the value of a patch variable, it seems less efficient to ask turtles to find patches, and then color them
It would be simpler to just ask the patches to color themselves based on the plantation value.
Consider:
Let cow-color brown
Let wheat-color yellow
Let grass-color green
ask patches
[
Ifelse
( Plantation = "cows" )
[ Set pcolor cow-color ]
( Plantation = "wheat" )
[ Set pcolor wheat-color ]
;; Otherwise
[ Set pcolor grass-color ]
]
It's also worth noting that your sample code did not return a list of patches. It returned a list of strings, from the plantation variable.
But you had the right idea. It's usually better to store the patches or turtles themselves in an agentset. It usually makes everything else easier.
Your original example could be:
Ask locals
[
Set myland patches in-radius 5
;; Do all kinds of things with myland
]
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论