Netlogo 如何选择与邻居共享的对象
我正在研究一个模型,海龟可以交换物体,比如病毒、疾病或只是一个球。 随机选择一只海龟后,这只海龟创建了一个对象,如下所示Netlogo 如何选择与邻居共享的对象,netlogo,Netlogo,我正在研究一个模型,海龟可以交换物体,比如病毒、疾病或只是一个球。 随机选择一只海龟后,这只海龟创建了一个对象,如下所示 create-object 1[ hide-turtle set new_object self set chosen? false] set selected one-of breed1 ask selected [ set attribute random-float 1 set my-list fput attribute m
create-object 1[ hide-turtle set new_object self set chosen? false]
set selected one-of breed1
ask selected [
set attribute random-float 1
set my-list fput attribute my-list
]
把它放进海龟和它的邻居共享的名单里。
现在,我要考虑的是另一种选择,同样的海龟,即选择一个(旧)项目收集在其列表的基础上的项目的属性:越高的价值,更可能是选择。
ask one-of breed1
let selected-objects objects with [not chosen?]
let choice rnd:weighted-one-of selected-objects [attribute]
ask choice [set chosen? true]
set my-list fput choice my-list
]
在哪里
我的疑问是:
let temp2-list sort-on [ attribute ] objects with [ member? who [my-list] of myself ]
我想你第三个问题的答案现在应该很清楚了
如果我想在选择后更改其值,我该怎么做
下面的工作代码应该有助于解释这个答案。安装程序生成一个
代理和6个对象组成一个随机列表和一个相应的
属性。Go生成对象和属性的排序列表。对象
在具有最高属性的列表中标识
请注意,对象拥有[attribute]。你没提那件事
示例代码是大量在线记录的
globals [
chosen_object_who
acting_breed1_who
]
breed[breed1 breed_1]
breed[objects object]
objects-own
[ attribute ]
breed1-own [
my-list ;; list of who numbers of objects, initialize to [] or it may fail
attr-list ;; list of attributes of the my-list objects, in the same order
attribute-of-chosen-object
who-of-chosen-object
my-list? ;; unknown
]
to setup
clear-all
print "creating 5 objects"
create-objects 5 [ set attribute random 100
print (word "created object " who " with attribute " attribute )
]
create-breed1 1 [
set my-list []
set attr-list []
set my-list? false
set attribute-of-chosen-object -1
set who-of-chosen-object -1
]
ask one-of breed1 [
set acting_breed1_who who
;; add six random object to my-list then remove duplicates
repeat 6 [
set my-list fput [who] of one-of objects my-list
]
set my-list remove-duplicates my-list
;; create the list of corresponding attributes
set attr-list map [i -> [attribute] of object i ] my-list
print (word "at end of setup, here's the my-list: " my-list )
print (word "here's the corresponding attr-list : " attr-list )
print ""
]
reset-ticks
end
to go
ask one-of breed1 [ ;; there is only one for this example
;; here's the magic command line
let temp2-list sort-on [ attribute ] objects with [ member? who [my-list] of myself ]
;; print (word "temp2-list: " temp2-list)
;; yields a list like [(object 3) (object 1) (object 4) (object 2) (object 0)]
;; it looks like an agent set but it's actually a list of objects in my-list,
;; but sorted into order by ascending attributes.
;; generate the new my-list, in the new order
set my-list map [i -> [who] of i ] temp2-list
set attr-list map [i -> [attribute] of i ] temp2-list
print "=========== after sort ====================="
print (word "sorted my-list now is " my-list)
print (word "matching attr-list is " attr-list )
print ""
;; You can select the best choice as the last item in the list, giving you the
;; who number of the object which has the highest attribute value
let best-choice 999
if-else length my-list > 0
[ set best-choice last my-list ]
[ error " my-list is empty! "]
;; saved as a global
set chosen_object_who best-choice
;; also save variables in the breed1 we are looking at now
set attribute-of-chosen-object last attr-list
set who-of-chosen-object last my-list
print (word "Assuming we want the object with the highest attribute")
print ""
print (word " The best choice object-who is " best-choice )
print (word " the attribute of that choice is " last attr-list )
print " "
print " all set to do something with the chosen object "
print " the choice is known both locally and globally "
inspect self
]
tick
end
谢谢韦德的回答和解释。所以你建议使用对象的索引。有趣!你能解释一下这部分代码的作用吗<代码>;;您可以选择最佳选项作为列表中的最后一项,为您提供;;属性值最高的对象的编号让best choice 999。它是否应该检查每个项目,直到找到属性值最高的项目?
globals [
chosen_object_who
acting_breed1_who
]
breed[breed1 breed_1]
breed[objects object]
objects-own
[ attribute ]
breed1-own [
my-list ;; list of who numbers of objects, initialize to [] or it may fail
attr-list ;; list of attributes of the my-list objects, in the same order
attribute-of-chosen-object
who-of-chosen-object
my-list? ;; unknown
]
to setup
clear-all
print "creating 5 objects"
create-objects 5 [ set attribute random 100
print (word "created object " who " with attribute " attribute )
]
create-breed1 1 [
set my-list []
set attr-list []
set my-list? false
set attribute-of-chosen-object -1
set who-of-chosen-object -1
]
ask one-of breed1 [
set acting_breed1_who who
;; add six random object to my-list then remove duplicates
repeat 6 [
set my-list fput [who] of one-of objects my-list
]
set my-list remove-duplicates my-list
;; create the list of corresponding attributes
set attr-list map [i -> [attribute] of object i ] my-list
print (word "at end of setup, here's the my-list: " my-list )
print (word "here's the corresponding attr-list : " attr-list )
print ""
]
reset-ticks
end
to go
ask one-of breed1 [ ;; there is only one for this example
;; here's the magic command line
let temp2-list sort-on [ attribute ] objects with [ member? who [my-list] of myself ]
;; print (word "temp2-list: " temp2-list)
;; yields a list like [(object 3) (object 1) (object 4) (object 2) (object 0)]
;; it looks like an agent set but it's actually a list of objects in my-list,
;; but sorted into order by ascending attributes.
;; generate the new my-list, in the new order
set my-list map [i -> [who] of i ] temp2-list
set attr-list map [i -> [attribute] of i ] temp2-list
print "=========== after sort ====================="
print (word "sorted my-list now is " my-list)
print (word "matching attr-list is " attr-list )
print ""
;; You can select the best choice as the last item in the list, giving you the
;; who number of the object which has the highest attribute value
let best-choice 999
if-else length my-list > 0
[ set best-choice last my-list ]
[ error " my-list is empty! "]
;; saved as a global
set chosen_object_who best-choice
;; also save variables in the breed1 we are looking at now
set attribute-of-chosen-object last attr-list
set who-of-chosen-object last my-list
print (word "Assuming we want the object with the highest attribute")
print ""
print (word " The best choice object-who is " best-choice )
print (word " the attribute of that choice is " last attr-list )
print " "
print " all set to do something with the chosen object "
print " the choice is known both locally and globally "
inspect self
]
tick
end