如何将项目添加到孵化另一个代理集的邻居列表中?
How to add items to neighbours' lists of an agentset that hatches another one?
我目前有一个代理集 (breed1
) 孵化另一个代理集 (breed2
)。我想要的是编辑 breed2
(attribute1
和 attribute2
),然后将编辑的项目(在阴影中称为 item
)添加到 [= 的列表中11=]的邻居。
已编辑:
在 Wade Schuette 的回答后处理代码,我写了以下内容
breed [people person]
breed [items item_1]
people-own
[
my-list
attribute1
attribute2
]
items-own
[
selected
attribute1
attribute2
]
to setup
clear-all
;; make turtles to test
create-people 1 [ set my-list [1 2 3 ] ]
create-people 1 [ set my-list [ ] ]
create-people 1 [ set my-list [22 33 ] ]
create-people 1 [ set my-list ["a" "b" "c"] ]
create-people 1 [ set my-list [3 4 5 ] ]
;; make a network
ask people [ create-links-with other people ]
reset-ticks
end
to go
let picked nobody
let neighbours nobody
ask one-of people
[
set attribute1 random-float 1
set attribute2 random-float 1
hatch-items 1 [
set selected picked
let this-item self
ask myself[
print (word "turtle " self " has item " this-item " with attribute1 " attribute1 "and attribute2 " attribute2)
set my-list lput this-item my-list
show my-list
]
ask link-neighbors [ print (word "Person " who " has this my-list " my-list)
set attribute1 (attribute1 + random-float 1)
set attribute2 (attribute2 + 3)
set my-list lput this-item my-list
print (word "added item " this-item " with attribute1 " attribute1 " with attribute2 " attribute2)
show my-list
]
]
]
tick
end
如您所见,我的困难在于考虑乌龟的邻居以及更新我想添加到它们列表中的项目的信息(属性 1 和 2)。
轻微 错别字:您的一种方法使用 in-link-neighbors,另一种方法使用 link-neighbors。除非你想排除出站 links,我想你想要 link-neighbors.
基本上,您要问的一个 设计问题 是在孵化之前找到调用者的 link 邻居集并将其保存在属性中是否更好,或者在孵化时在本地找到它们并在孵化后忘记列表。我没有尝试过您的代码,但我认为这两种方法都可以。如果 links 的集合是静态的,你可以找到它一次,存储它,而不是每次孵化新代理时都费心去寻找它,并节省一些时间。如果 links 的集合是动态的,无论如何你每次都需要查找它,所以将它保存在属性中是没有意义的。
你问如何验证代码。我不知道你的任何一个建议是什么意思,所以我会回答一般情况。在此处发布之前,这些可能是您可以执行并且应该执行的步骤,以修复所有您可以自行修复的问题。
规则 #1:从简单开始,在增加复杂性之前进行大量工作。
您的代码已经违反规则 #1。当您只能使用一个进行测试时,您设置了两个属性。如果第二个属性代码导致错误,使您无法检查整体逻辑是否正常工作,该怎么办。就此而言,开始使用零属性进行测试。注释掉该代码并逐渐将其添加回来,并在每一步再次检查。
规则 #2:采取小步骤。
在添加更多代理之前,确认代码仅适用于一个代理。通过 "go" 步骤并停止测试。在继续第二步之前获得那么完美。等等
我们都可以梦想有一天 NetLogo 拥有一个真正的调试器,让您一次单步执行代码或设置断点。但事实并非如此。当 go 步骤结束并且 "stop" 生效时,您已经失去了上下文。局部变量已经蒸发,无法检查。 "stack" 嵌套调用丢失
我知道在上下文中查看动态细节的唯一方法是设置新的全局变量,这很混乱,或者大量使用嵌入式 "print" 语句。
你也可以插入一个错误语句导致运行次错误,检查调用堆栈,有机会喘口气看看那时的输出。
error " here is the calling stack, halting execution entirely here."
或者,您可以使用 用户消息语句 使代码暂停,让您知道某些事情已经发生变化,或者永远不会发生的情况 (! ) 现在已经发生了。这为您提供了 "halt" 或继续 运行 的不错选择,就好像什么都没发生一样。
if x > 5 [ user-message " Alert -- X is over 5!! " ]
其他评论者,请在这里插话并分享其他方法!
我发现的最有用的打印语句标记了几个感兴趣的变量并确定了它们的执行位置,以便您可以在输出中区分它们。查看 3 个变量 x、y 和 z 的示例:
print ( word "In step 3, x = " x " y = " y " z = " z )"
如果有一种方法可以在编辑器中切换这些语句的可见性,那就太好了,但是没有。您至少可以通过添加一个全局变量来切换语句 运行s 或是否被跳过,例如 "verbose?" 并更改打印语句以利用它。 (见下文)
然后您可以打开或关闭一次打印所有这些语句,而不是将它们注释掉,或者更糟,删除它们。一般来说,不要删除它们,因为有一天您需要修改并重新验证代码,您将需要再次返回这些代码以确认修改正在做您希望他们做的事情并且没有破坏某些东西新的。好的 "print" 声明物超所值,值得努力落实。
这是进行选择性打印的好方法。声明一个名为 "xprint" 的新命令,该命令仅在全局变量 "verbose?" 为真时打印。可以在setup中设置一次,或者在go mid-stream中修改,还是设置verbose?在您单步执行代码的命令中心中为 true 或 false。
然后您可以使用 "xprint" 而不是 "print" 来切换打印或不打印。
xprint ( word "In step 3, x = " x " y = " y " z = " z )"
这确实降低了代码中包含大量打印语句的成本,因此它确实简化了验证代码和稍后重新验证代码的过程。
;; This shows how to use a global variable to turn on or off print statements,
;; which you might want to use while developing and testing code
globals [
verbose? ;; true means print lots of things, false means don't print them
]
to setup
clear-all
set verbose? true ;; or false, whatever. You can also use the Command Center
;; to set this true or false in the middle of a run
reset-ticks
end
to go
let x random 10 ;; just for illustrating how this works
xprint (word "At tick " ticks " x = " x )
tick
end
to xprint [ stuff ]
if verbose? [ print stuff ]
end
您在第一个答案的评论中说:
The code works, but I have not been able to print all the neighbours
of the selected turtle and see if the item was added or not to their
lists. .... However, my difficulties are in
considering and defining the neighbours of the turtle (myself).
你能解释一下你遇到了什么困难吗?我们可以选择一个更简单的例子来处理吗?您遇到的问题是 "myself" 和 "self" 一词的含义取决于您嵌套在 ask-whoever calls 中的深度吗?
这里有一些更容易开始的代码。你能用这段代码创建问题吗?
顺便说一句,如果你是另一个ask循环中的ask循环,关键字"myself"将被定义,你可以使用
print (word "At point 3, self = " self ", and myself = " myself)
看看谁是谁。我不知道如何测试 "myself" 是否存在,因为如果不存在,则会引发错误 -- testing = nobody doesn't work and is-agent?如果我自己不存在,则不起作用。任何人?你怎么知道我是否存在?
反正没关系。 这里有一个解决方法。只要等到填充部分完成后,当你回到第一个代理的上下文时,你就可以使用
ask link-neighbors [ print (word "turtle " who " has this my-list " my-list) ]
它应该可以工作!
turtles-own
[
my-list
]
to setup
clear-all
;; make turtles to test
create-turtles 1 [ set my-list [1 2 3 ] ]
create-turtles 1 [ set my-list [ ] ]
create-turtles 1 [ set my-list [22 33 ] ]
create-turtles 1 [ set my-list ["a" "b" "c"] ]
create-turtles 1 [ set my-list [3 4 5 ] ]
;; make a network
ask turtles [ create-links-with other turtles ]
reset-ticks
end
to go
ask one-of turtles
[
ask link-neighbors [ print (word "turtle " who " has this my-list " my-list) ]
]
tick
end
您只需进行两处更改即可使您的代码正常工作。
在尝试询问 link-neighbors 之前,将阴影语句的右括号 ( ] ) 向上移动。现在,您正在为孵化代理请求 link-neighbors,而不是调用它的代理,因此它找到 none 并默默地失败。
如果你这样做,你会得到一个关于这个项目未定义的错误。那是因为你在舱口代码中声明了它,所以当舱口代码关闭时它就消失了。要解决此问题,请在调用填充代码之前声明此项(let this-item "")可以正常工作,但请务必使用 LET 而不是 SET。
然后以后更新linked neighbors 时,你只需要设置它,因为你已经声明了它。
我认为通过这些更改它会奏效。这就是我要做的工作。我把它放在许多调试 xprint 语句中并设置全局冗长?正如我之前提到的那样,设置为 true 以激活它们。执行所有这些打印语句揭示了 link-neighbors 突然消失的问题——这就是我找到错误的方式。
设置全局详细?在设置中设置为 false 以停用它们并获得干净的输出。
globals [
verbose? ;; to turn on debugging statements
]
breed [people person]
breed [items item_1]
people-own
[
my-list
attribute1
attribute2
]
items-own
[
selected
attribute1
attribute2
]
to setup
clear-all
set verbose? true ;; true means printa lot of stuff
;; make turtles to test
create-people 1 [ set my-list [1 2 3 ] ]
create-people 1 [ set my-list [ ] ]
create-people 1 [ set my-list [22 33 ] ]
create-people 1 [ set my-list ["a" "b" "c"] ]
create-people 1 [ set my-list [3 4 5 ] ]
;; make a network
ask people [ create-links-with other people ]
;; ADDED next 2 lines
xprint (word "Created this many links in setup: " count links)
ask person 0 [
let fastcount count link-neighbors
xprint ( word "In setup, person " who " has this many links " fastcount )
]
reset-ticks
end
to go
let picked nobody
let neighbours nobody
ask one-of people with [ who = 0 ]
[
let fastcount count link-neighbors
xprint ( word "Entering ask-person, person " who " has this many links " fastcount )
set attribute1 random-float 1
set attribute2 random-float 1
xprint ( word "Before hatch-items, we have this...")
xprint (word "turtle " self " has no item hatched, "
" with attribute1 " precision attribute1 2 " and attribute2 " precision attribute2 2)
let this-item 0 ;; this was down inside hatch-items, so it evaporated when
;; finishing hatch-items, causing a problem
;; DECLARE it up here, then SET it inside hatch-items
hatch-items 1 [
set selected picked
set this-item self ;; this was down here inside hatch-items, move it up 4 lines
xprint ( word "confirming this-item value is: " this-item )
ask myself[
xprint ( word "inside hatch-items , inside ask-myself, we have this...")
xprint (word "turtle " self " has item " this-item
" with attribute1 " precision attribute1 2 " and attribute2 " precision attribute2 2)
set my-list lput this-item my-list
xprint ( word "hatched item now has my-list = " my-list )
] ;; end of ask myself
];;; <====== you want to close context of hatch-items up here, not down below
;;; so that ask link-neighbors will work
;; ADDED THIS NEXT LINES FOR DEBUGGING
xprint "we are done with ask myself, but still inside hatch-items"
set fastcount count link-neighbors
xprint ( word "After ask myself, but still inside hatch-items, person " who " has this many links " fastcount )
xprint ( " the code posted in the revised question failed here")
xprint ( " PERSON should be person 0 , link count should be 4 ")
xprint ( " SO ASK LINK-NEIGHBORS WIll fail !!!")
xprint " asking link-neighbors to update their lists now"
ask link-neighbors [ print (word "Person " who " has this my-list " my-list)
set attribute1 (attribute1 + random-float 1)
set attribute2 (attribute2 + 3)
set my-list lput this-item my-list
xprint (word "added item " this-item " with attribute1 " attribute1 " with attribute2 " attribute2)
show my-list
]
;;] ;; closes context of hatch-items, try closing hatch-items before asking link-neighbors
]
tick
end
to xprint [ stuff ]
if verbose? [ print stuff ]
end
我目前有一个代理集 (breed1
) 孵化另一个代理集 (breed2
)。我想要的是编辑 breed2
(attribute1
和 attribute2
),然后将编辑的项目(在阴影中称为 item
)添加到 [= 的列表中11=]的邻居。
已编辑: 在 Wade Schuette 的回答后处理代码,我写了以下内容
breed [people person]
breed [items item_1]
people-own
[
my-list
attribute1
attribute2
]
items-own
[
selected
attribute1
attribute2
]
to setup
clear-all
;; make turtles to test
create-people 1 [ set my-list [1 2 3 ] ]
create-people 1 [ set my-list [ ] ]
create-people 1 [ set my-list [22 33 ] ]
create-people 1 [ set my-list ["a" "b" "c"] ]
create-people 1 [ set my-list [3 4 5 ] ]
;; make a network
ask people [ create-links-with other people ]
reset-ticks
end
to go
let picked nobody
let neighbours nobody
ask one-of people
[
set attribute1 random-float 1
set attribute2 random-float 1
hatch-items 1 [
set selected picked
let this-item self
ask myself[
print (word "turtle " self " has item " this-item " with attribute1 " attribute1 "and attribute2 " attribute2)
set my-list lput this-item my-list
show my-list
]
ask link-neighbors [ print (word "Person " who " has this my-list " my-list)
set attribute1 (attribute1 + random-float 1)
set attribute2 (attribute2 + 3)
set my-list lput this-item my-list
print (word "added item " this-item " with attribute1 " attribute1 " with attribute2 " attribute2)
show my-list
]
]
]
tick
end
如您所见,我的困难在于考虑乌龟的邻居以及更新我想添加到它们列表中的项目的信息(属性 1 和 2)。
轻微 错别字:您的一种方法使用 in-link-neighbors,另一种方法使用 link-neighbors。除非你想排除出站 links,我想你想要 link-neighbors.
基本上,您要问的一个 设计问题 是在孵化之前找到调用者的 link 邻居集并将其保存在属性中是否更好,或者在孵化时在本地找到它们并在孵化后忘记列表。我没有尝试过您的代码,但我认为这两种方法都可以。如果 links 的集合是静态的,你可以找到它一次,存储它,而不是每次孵化新代理时都费心去寻找它,并节省一些时间。如果 links 的集合是动态的,无论如何你每次都需要查找它,所以将它保存在属性中是没有意义的。
你问如何验证代码。我不知道你的任何一个建议是什么意思,所以我会回答一般情况。在此处发布之前,这些可能是您可以执行并且应该执行的步骤,以修复所有您可以自行修复的问题。
规则 #1:从简单开始,在增加复杂性之前进行大量工作。
您的代码已经违反规则 #1。当您只能使用一个进行测试时,您设置了两个属性。如果第二个属性代码导致错误,使您无法检查整体逻辑是否正常工作,该怎么办。就此而言,开始使用零属性进行测试。注释掉该代码并逐渐将其添加回来,并在每一步再次检查。
规则 #2:采取小步骤。
在添加更多代理之前,确认代码仅适用于一个代理。通过 "go" 步骤并停止测试。在继续第二步之前获得那么完美。等等
我们都可以梦想有一天 NetLogo 拥有一个真正的调试器,让您一次单步执行代码或设置断点。但事实并非如此。当 go 步骤结束并且 "stop" 生效时,您已经失去了上下文。局部变量已经蒸发,无法检查。 "stack" 嵌套调用丢失
我知道在上下文中查看动态细节的唯一方法是设置新的全局变量,这很混乱,或者大量使用嵌入式 "print" 语句。
你也可以插入一个错误语句导致运行次错误,检查调用堆栈,有机会喘口气看看那时的输出。
error " here is the calling stack, halting execution entirely here."
或者,您可以使用 用户消息语句 使代码暂停,让您知道某些事情已经发生变化,或者永远不会发生的情况 (! ) 现在已经发生了。这为您提供了 "halt" 或继续 运行 的不错选择,就好像什么都没发生一样。
if x > 5 [ user-message " Alert -- X is over 5!! " ]
其他评论者,请在这里插话并分享其他方法!
我发现的最有用的打印语句标记了几个感兴趣的变量并确定了它们的执行位置,以便您可以在输出中区分它们。查看 3 个变量 x、y 和 z 的示例:
print ( word "In step 3, x = " x " y = " y " z = " z )"
如果有一种方法可以在编辑器中切换这些语句的可见性,那就太好了,但是没有。您至少可以通过添加一个全局变量来切换语句 运行s 或是否被跳过,例如 "verbose?" 并更改打印语句以利用它。 (见下文)
然后您可以打开或关闭一次打印所有这些语句,而不是将它们注释掉,或者更糟,删除它们。一般来说,不要删除它们,因为有一天您需要修改并重新验证代码,您将需要再次返回这些代码以确认修改正在做您希望他们做的事情并且没有破坏某些东西新的。好的 "print" 声明物超所值,值得努力落实。
这是进行选择性打印的好方法。声明一个名为 "xprint" 的新命令,该命令仅在全局变量 "verbose?" 为真时打印。可以在setup中设置一次,或者在go mid-stream中修改,还是设置verbose?在您单步执行代码的命令中心中为 true 或 false。
然后您可以使用 "xprint" 而不是 "print" 来切换打印或不打印。
xprint ( word "In step 3, x = " x " y = " y " z = " z )"
这确实降低了代码中包含大量打印语句的成本,因此它确实简化了验证代码和稍后重新验证代码的过程。
;; This shows how to use a global variable to turn on or off print statements,
;; which you might want to use while developing and testing code
globals [
verbose? ;; true means print lots of things, false means don't print them
]
to setup
clear-all
set verbose? true ;; or false, whatever. You can also use the Command Center
;; to set this true or false in the middle of a run
reset-ticks
end
to go
let x random 10 ;; just for illustrating how this works
xprint (word "At tick " ticks " x = " x )
tick
end
to xprint [ stuff ]
if verbose? [ print stuff ]
end
您在第一个答案的评论中说:
The code works, but I have not been able to print all the neighbours of the selected turtle and see if the item was added or not to their lists. .... However, my difficulties are in considering and defining the neighbours of the turtle (myself).
你能解释一下你遇到了什么困难吗?我们可以选择一个更简单的例子来处理吗?您遇到的问题是 "myself" 和 "self" 一词的含义取决于您嵌套在 ask-whoever calls 中的深度吗?
这里有一些更容易开始的代码。你能用这段代码创建问题吗?
顺便说一句,如果你是另一个ask循环中的ask循环,关键字"myself"将被定义,你可以使用
print (word "At point 3, self = " self ", and myself = " myself)
看看谁是谁。我不知道如何测试 "myself" 是否存在,因为如果不存在,则会引发错误 -- testing = nobody doesn't work and is-agent?如果我自己不存在,则不起作用。任何人?你怎么知道我是否存在?
反正没关系。 这里有一个解决方法。只要等到填充部分完成后,当你回到第一个代理的上下文时,你就可以使用
ask link-neighbors [ print (word "turtle " who " has this my-list " my-list) ]
它应该可以工作!
turtles-own
[
my-list
]
to setup
clear-all
;; make turtles to test
create-turtles 1 [ set my-list [1 2 3 ] ]
create-turtles 1 [ set my-list [ ] ]
create-turtles 1 [ set my-list [22 33 ] ]
create-turtles 1 [ set my-list ["a" "b" "c"] ]
create-turtles 1 [ set my-list [3 4 5 ] ]
;; make a network
ask turtles [ create-links-with other turtles ]
reset-ticks
end
to go
ask one-of turtles
[
ask link-neighbors [ print (word "turtle " who " has this my-list " my-list) ]
]
tick
end
您只需进行两处更改即可使您的代码正常工作。 在尝试询问 link-neighbors 之前,将阴影语句的右括号 ( ] ) 向上移动。现在,您正在为孵化代理请求 link-neighbors,而不是调用它的代理,因此它找到 none 并默默地失败。
如果你这样做,你会得到一个关于这个项目未定义的错误。那是因为你在舱口代码中声明了它,所以当舱口代码关闭时它就消失了。要解决此问题,请在调用填充代码之前声明此项(let this-item "")可以正常工作,但请务必使用 LET 而不是 SET。
然后以后更新linked neighbors 时,你只需要设置它,因为你已经声明了它。
我认为通过这些更改它会奏效。这就是我要做的工作。我把它放在许多调试 xprint 语句中并设置全局冗长?正如我之前提到的那样,设置为 true 以激活它们。执行所有这些打印语句揭示了 link-neighbors 突然消失的问题——这就是我找到错误的方式。
设置全局详细?在设置中设置为 false 以停用它们并获得干净的输出。
globals [
verbose? ;; to turn on debugging statements
]
breed [people person]
breed [items item_1]
people-own
[
my-list
attribute1
attribute2
]
items-own
[
selected
attribute1
attribute2
]
to setup
clear-all
set verbose? true ;; true means printa lot of stuff
;; make turtles to test
create-people 1 [ set my-list [1 2 3 ] ]
create-people 1 [ set my-list [ ] ]
create-people 1 [ set my-list [22 33 ] ]
create-people 1 [ set my-list ["a" "b" "c"] ]
create-people 1 [ set my-list [3 4 5 ] ]
;; make a network
ask people [ create-links-with other people ]
;; ADDED next 2 lines
xprint (word "Created this many links in setup: " count links)
ask person 0 [
let fastcount count link-neighbors
xprint ( word "In setup, person " who " has this many links " fastcount )
]
reset-ticks
end
to go
let picked nobody
let neighbours nobody
ask one-of people with [ who = 0 ]
[
let fastcount count link-neighbors
xprint ( word "Entering ask-person, person " who " has this many links " fastcount )
set attribute1 random-float 1
set attribute2 random-float 1
xprint ( word "Before hatch-items, we have this...")
xprint (word "turtle " self " has no item hatched, "
" with attribute1 " precision attribute1 2 " and attribute2 " precision attribute2 2)
let this-item 0 ;; this was down inside hatch-items, so it evaporated when
;; finishing hatch-items, causing a problem
;; DECLARE it up here, then SET it inside hatch-items
hatch-items 1 [
set selected picked
set this-item self ;; this was down here inside hatch-items, move it up 4 lines
xprint ( word "confirming this-item value is: " this-item )
ask myself[
xprint ( word "inside hatch-items , inside ask-myself, we have this...")
xprint (word "turtle " self " has item " this-item
" with attribute1 " precision attribute1 2 " and attribute2 " precision attribute2 2)
set my-list lput this-item my-list
xprint ( word "hatched item now has my-list = " my-list )
] ;; end of ask myself
];;; <====== you want to close context of hatch-items up here, not down below
;;; so that ask link-neighbors will work
;; ADDED THIS NEXT LINES FOR DEBUGGING
xprint "we are done with ask myself, but still inside hatch-items"
set fastcount count link-neighbors
xprint ( word "After ask myself, but still inside hatch-items, person " who " has this many links " fastcount )
xprint ( " the code posted in the revised question failed here")
xprint ( " PERSON should be person 0 , link count should be 4 ")
xprint ( " SO ASK LINK-NEIGHBORS WIll fail !!!")
xprint " asking link-neighbors to update their lists now"
ask link-neighbors [ print (word "Person " who " has this my-list " my-list)
set attribute1 (attribute1 + random-float 1)
set attribute2 (attribute2 + 3)
set my-list lput this-item my-list
xprint (word "added item " this-item " with attribute1 " attribute1 " with attribute2 " attribute2)
show my-list
]
;;] ;; closes context of hatch-items, try closing hatch-items before asking link-neighbors
]
tick
end
to xprint [ stuff ]
if verbose? [ print stuff ]
end