我想问一下如何将一只乌龟创建的元素添加到它自己的列表和邻居的列表中。
我创建了一个局部变量neighbours
,定义为turtle-set in-link-neighbors with [breed=breed1]
。然后,我要求邻居添加创建的元素(new_elem),然后遵循该论坛中用户提供的答案。
代码是:
to create_elements
create-elements 1[
set person one-of turtles with [breed = breed1 or breed = breed2]
ifelse [breed = breed1] of person
[
set attribute1 random-float 1
set attribute2 random-float 1
set function1 (1 + attribute1)
]
[
set attribute1 random-float 1
set attribute2 random-float 1
set function2 (1 - attribute1)
]
let neighbours (turtle-set self in-link-neighbors with [breed = breed1] of person)
ask neighbours
[set my-list fput new_elem my-list]
]
]
end
我知道let neighbours (turtle-set self in-link-neighbors with [breed = breed1] of person)...
中存在错误,但是也将元素放入列表中,因为我认为新元素是在品种elements
中定义的。实际上,错误消息显示
元素品种不拥有变量“我的列表”
是否可以指定turtle-set
的品种?我需要的只是考虑breed1
和breed2
,而不考虑elements
品种。
例如,由于我正在考虑将人作为元素的创建者,所以写
let neighbours (turtle-set person in-link-neighbors with [breed = breed1] of person)
ask neighbours
[set my-list fput new_elem my-list]
如果正确的话,我如何考虑到其属性进一步将其添加为列表中的“新元素”。
例如:set my-list fput (list attribute1 attribute2) of elements my-list
)向我返回诸如(0.134531 0.14141) (0.91844 0.42176)
之类的值的列表...有什么建议可以让您更高兴地看到但具有相同信息的建议?在接下来的步骤中,我将需要跟踪这些值。
希望您能对我做错的事情有所了解。
谢谢
答案 0 :(得分:1)
Val,这是一些工作代码,它们创建一个新元素并更新每个人的我的列表。
这可能并不是您想要的,但我希望它能提供一些帮助。 您没有提供所有有关品种的信息,而是谁拥有什么,所以我一探究竟。
我将更复杂的命令分解为多个较短的命令,以调试正在发生的事情以及崩溃的地方。我添加了一些打印语句。
如果您将其加载,运行设置程序并单击一次“开始”,则可以看到网络中的座席与他们旁边的“谁”号链接在一起,因此您可以轻松地检查自己的 想。 my-list的值也会在命令部分中打印出来。
“执行”步骤的另外两次迭代应验证它们是否在输出所需的内容。
我不知道您是否希望new_elem仅位于create_elements部分的本地,或者您是否希望它在全球范围内可用,所以我在两个地方都添加了注释。
我希望这会有所帮助!代码本身还有更多注释。请特别注意,您必须将我的列表初始化为空列表,否则将默认为数字零,并且您的fput语句将失败,并带有有关期望列表的注释并找到零。
globals [
;;new_elem ;; might be needed as a global ??
]
;; ///////////////////// note that there is no breed required called "people"
breed [ elements element]
breed [ breed1 lion ]
breed [ breed2 tiger ]
;; ///////////////////// i punted on which entity owned "function1 and function2
breed1-own [ attribute function1 function2 my-list]
breed2-own [ attribute function1 function2 my-list]
to setup
clear-all
;; make some turtles with breed = breed1 (say, red lions)
create-breed1 2 [ set size 2 set shape "circle" set color red
setxy random-xcor random-ycor
]
;; make some turtles with breed = breed2 (say, green tigers)
create-breed2 2 [ set size 2 set shape "circle" set color green
setxy random-xcor random-ycor
]
;; //////////////////////////////////////////////////////////////////////////
;; NOTE!!! If you don't initialize my-list to an empty LIST type,
;; fput will generate an error when you try to fput a new element onto it
;; this is ugly but fast:
ask breed1 [create-links-with other breed1
set my-list [] ]
ask breed1 [create-links-with breed2
set my-list [] ]
ask breed2 [create-links-with other breed2
set my-list [] ]
ask breed2 [create-links-with breed1
set my-list [] ]
ask turtles [set label who] ;; ///////////////////////// for debugging
;; set new_elem nobody ;; this could be a global so it can persist
;; outside create_element, in which case
;; initilize it here
reset-ticks
end
to go
create_elements
tick
end
to create_elements
;; declare the local variables, all agent-sets of some type
let new_elem nobody ;; declare this locally if we don't need to persist it
;; outside the create_elements context
;; and remove the declaration in [ globals ]
;; and initializtion in setup
let personx nobody
let xx-neighbors nobody
;; do just this creation here in order to see what's going on more easily
;; than combining multiple operations into a long complex command
create-elements 1 [ set new_elem self ]
type "new element is this agentset " show new_elem
set personx one-of turtles with [breed = breed1 or breed = breed2]
ifelse [breed = breed1] of personx
[ ;; if it is breed1
ask personx [
set attribute random-float 1
set function1 (1 + attribute)
]
]
[ ;; else, it must be breed2
ask personx [
set attribute random-float 1
set function2 (1 - attribute)
]
]
;;//////////////////////////////////////// for debugging
type "the personx we defined above is " print personx
ask personx [set xx-neighbors (turtle-set self in-link-neighbors )]
if-else xx-neighbors != nobody ;; If it has any members, which should
;; always be true since we added self to the set
[
ask xx-neighbors
[
set my-list fput new_elem my-list
;; ////////////////////////////////////for debugging
type "here's the resulting my-list: " show my-list
]
]
[ print "ERROR didn't find any neighbors or self" ]
end