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
  ]
在哪里

我的疑问是:

  • 如何将此对象及其属性保留在“内存”/“列表”中? 我需要在内存中保存,因为海龟(breed1)可以通过时间交换/传播对象/病毒,但这个对象可能会更改其属性的值

  • 将对象和/或其属性添加到列表中是否更好

  • 如果我想在选择后更改其值,我该怎么做

    谢谢你抽出时间

  • 好吧,你问了

    (1) 如何将此对象及其属性保留在“内存”/“列表”中? 我需要像海龟(breed1)一样保持记忆 通过时间交换/传播对象/病毒,但可能 对象可以更改其属性的值

    一旦你创建了它们,这些对象将一直存在,直到你杀死它们。这意味着每个对象所拥有的属性也将保持不变,而无需您的任何努力

    查看代码,您可以通过将全局变量“new_object”设置为所选对象的编号来跟踪选择的对象

    具有给定who编号的对象可以由任何人随时更改其属性,并且所有其他代理都可以看到该新值

    你还问:

    (2) 将对象和/或其属性添加到列表中是否更好 属性

    我会回答,不要把对象或对象的属性放在我的列表上将who编号放在我的列表中。从who编号可以找到带有该who编号的对象,从该对象可以找到属性

    好的,如果你这样做了,你能按属性对列表排序吗?是的,你可以用一些聪明的代码来做到这一点。关键行是根据对象属性对列表进行排序的行,这些对象在列表my list中有其who编号

     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