我目前有一个代理集(breed1
)孵化了另一个代理集(breed2
)。我想要的是编辑breed2
(attribute1
和attribute2
),然后将已编辑的项目(在阴影线中称为item
)添加到{{ 1}}的邻居。
编辑: 在Wade Schuette回答后处理代码,我写了以下内容
breed1
如您所见,我的困难在于考虑乌龟的邻居并更新我要添加到其列表中的物品的信息(属性1和2)。
答案 0 :(得分:1)
次要错字:您的一种方法使用链接内邻居,另一种方法使用链接邻居。除非您想排除出站链接,否则我认为您需要链接邻居。
基本上,您要问的一个设计问题是在孵化之前找到调用者的链接邻居集合并将其保存在属性中还是在创建时在本地查找更好?孵化并在孵化后忘记列表。我没有尝试过您的代码,但是我认为这两种方法都可以。如果链接集是静态的,则可以一次找到它,将其存储起来,而不必每次孵化一个新代理时都再次查找它,从而节省了一些时间。如果链接集是动态的,则无论如何都需要每次都对其进行查找,因此没有必要将其保存在属性中。
您询问如何验证。我不知道您的建议是什么意思,所以我将回答一般情况。这些步骤可能是您可以执行的步骤,应该在将其发布到此处之前进行操作,以修复所有可以自己修复的问题。
规则1:从简单开始,并在增加复杂性之前做很多工作。
您的代码已违反规则1。当您只能使用一个属性进行测试时,可以设置两个属性。如果第二个属性代码导致错误,使您无法检查总体逻辑是否正常,该怎么办。为此,请从零属性开始测试。注释掉该代码,然后逐步添加回去,并在每个步骤中再次进行检查。
规则2:采取一些小步骤。
在添加更多代理之前,请确保该代码仅适用于一个代理。通过“执行”步骤并停止测试。在继续下一步之前,要获得如此完美的表现。等等
我们都可以梦想有一天NetLogo拥有一个真正的调试器,该调试器使您一次可以单步执行一个语句或设置断点。但事实并非如此。等到go步骤结束并且“停止”生效时,您已经失去了上下文。局部变量已消失,无法检查。嵌套调用的“堆栈”丢失。
我知道在上下文中查看动态详细信息的唯一方法是设置新的全局变量(这很麻烦),或者大量使用嵌入式的“ print”语句。
您还可以插入 error语句以引起运行时错误,检查调用堆栈,并有机会屏息并查看此时的输出。
error " here is the calling stack, halting execution entirely here."
或者,您可以使用 user-message语句使代码暂停,以告知您某些事情已更改,或者已经发生了永远不应该发生的条件(!)。这为您提供了“暂停”或继续运行的好选择,就像什么也没发生一样。
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 )"
如果可以在编辑器中切换这些语句的可见性,那就很好,但是没有。您至少可以通过添加全局变量(例如“ verbose?”)来切换该语句是运行还是被跳过。并更改打印语句以利用它。 (请参见下文)
然后,您可以一次打开或关闭所有这些语句的打印,而不是将它们注释掉,或者更糟的是删除它们。通常,不要删除它们,因为有一天您将需要修改并重新验证代码,并且您将需要再次返回这些代码以确认所做的修改是否符合您希望它们做的事情并且没有破坏某些内容。新。好的“印刷”声明值得其投入,也值得努力实现。
这是进行选择性打印的好方法。声明一个新命令“ xprint”,该命令仅在全局变量“ verbose?”时打印是真的。您可以在设置过程中设置一次,也可以在流媒体中进行修改,或者设置详细?在“命令中心”中单步执行代码的正确与否。
然后,您可以在希望切换打印或不打印的地方使用“ 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
答案 1 :(得分:1)
您只需要进行两项更改即可使代码正常工作。 在尝试请求链接邻居之前,将舱口语句的右括号(]上移。现在,您要为已孵化的代理请求链接邻居,而不是调用它的代理,因此它找不到任何代理并且会自动失败。
如果执行此操作,则会收到有关此项目未定义的错误。那是因为您在填充代码中声明了它,所以当填充代码关闭时它就消失了。要解决此问题,请在调用填充代码之前声明此项目(让该项目为“”)可以正常工作,但请确保使用LET not SET。
然后,在更新链接的邻居时,只需设置SET,因为您已经声明了它。
我认为通过这些更改,它会起作用。这就是我要工作的。我把它调试了很多xprint语句并设置了全局详细信息?如我前面提到的,将其激活。完成所有这些打印语句后,就会发现链接邻居突然消失的问题-这就是我定位错误的方式。
设置全局详细信息?设置为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
答案 2 :(得分:0)
您在对第一个答案的评论中说:
该代码有效,但是我无法打印所有邻居 所选乌龟的外观,看看是否已将其添加到它们的 列表。 ....但是,我的困难在于 考虑并定义乌龟的邻居(我自己)。
您能解释一下您遇到的困难吗?我们可以选择一个更简单的示例吗?您对“代理人”这个词的理解是“我自己”和“自我”是什么意思,这取决于您对询问者的嵌套程度如何?
下面是一些更简单的代码。您可以使用此代码创建问题吗?
顺便说一句,如果您是另一个Ask循环中的一个Ask循环,则将定义关键字“ myself”,您可以使用
print (word "At point 3, self = " self ", and myself = " myself)
看看谁是谁。我不知道如何测试“我自己”是否存在,因为如果不存在,这会引发错误-测试=没有人无法工作并且是代理人?如果我自己不存在,将无法正常工作。任何人?你怎么知道自己是否存在?
无论如何都没关系。 这是一种解决方法。只要等到第一个特工的上下文中的填充部分完成后,就可以使用
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