首页 > 解决方案 > 定义变量的区别

问题描述

我想定义一个新对象,但我不知道什么是最好的方法。我知道它let用于局部变量,这应该是主要区别。我正在 NetLogo 中创建一个列表,用于存储具有某些属性/属性的元素。因为稍后我需要从创建的列表中选择一项,如果我需要将对象创建为local变量或global( turtle’s own) 变量来执行此操作,我需要了解什么。两种可能的情况应该是:

1)创建对象作为全局变量:

globals [object
         attribute] 
turtles-own[
    my-list]

to setup 
create-turtles 5
ask turtles [
set my-list []
]
end

to-go

ask one-of turtles[
set attribute 5
set my-list lput object my-list]
end

2)创建对象作为局部变量:

turtles-own[
my-list]

to setup 
create-turtles 5
ask turtles [
set my-list []
]
end

to-go

ask one-of turtles[
let object “New object”
let attribute 5 ; this should be assigned to object
set my-list lput object my-list]
end`

应该还有另一种情况,即在 turtles-own 中定义的对象和属性(这是我更喜欢的情况):

turtles-own[
object 
attribute 
my-list]

to setup 
create-turtles 5
ask turtles [
set my-list []
]
end

to-go

ask one-of turtles[
set object “New object”
set attribute 5 ; this should be assigned to object
set my-list lput object my-list]
end

我需要实现的是以下内容:

  1. 将对象存储在列表中的海龟
  2. 具有属性的对象(或者您也可以将其解释为创建具有属性的对象的海龟)
  3. 海龟可以根据属性选择从该列表中选择一个对象。(这是最重要的一点,因为我稍后需要处理对象及其属性)。

你能帮我理解我需要如何实现它吗?

我想避免孵化或为对象创建品种,因为我不确定是否能够在整个代码中管理它。但是,如果这是唯一正确的方法,我会这样做。

感谢您的时间。

标签: netlogo

解决方案


Val,我知道你最初问过“我想问你使用 let 和 set 的区别”。但后来你解释了你需要实现什么,这就是我的回答。所以,这个问题的标题不是很准确。

我不确定这是继续进行的唯一方法,但据我所知,我尝试使用两个品种(人和对象)来解决您的问题。我希望你能从下面的代码中看到如何做到这一点——除非你想同时影响人和物体,否则不要在任何地方使用“ask-turtles”。

当然,如果您遇到问题,请在此处发布问题。

工作代码如下。

我解释你问题的方式,

  • 对象是持久的和全局的,
  • 任何人在任何时候对对象属性的更改都会更新每个代理的 my-list 中该对象的该属性的值
  • 每个人都有自己的我的对象列表,可能是空的。
  • 不同agent的my-lists会有所不同
  • 每个人都可以根据属性从他们的列表中搜索一个对象,然后我将结果对象(如果有的话)存储在他们当前的选择中。
  • 每个人都应该能够选择任何对象并将该对象添加到他们的我的列表中

实施设计选择:

  • 使用两个品种,“人”和“对象”,它们拥有不同的变量。
  • 每个对象都有一个属性和一个对象类型(例如“apple”或“orange”)
  • 每个人都拥有一个我的列表和当前选择。my-list 是一个列表, current-choice 是一个代理(可能 =没人

下面代码的特点

  • 该代码创建了 5 个人和 6 个对象用于测试目的。所有这些都只是留在显示器上的原点。

  • 在“go”步骤的每次迭代中,随机人选择两个随机对象并将它们添加到他们的 my-list 中,然后根据属性值定位其中一个。

  • 该代码列出了 my-list 中具有所需属性的所有对象,其中可能包含零个、一个或多个对象,并根据列表的长度执行一些合理的操作。

  • 从未从我的列表中删除任何项目,或者检查它们是否已经存在。

  • 这就是全部。运行 setup,运行 go(一次),然后检查结果。

我希望这是有帮助的。如果我没有回答你的问题,请在这里告诉我。

韦德

;; this is the second version of the code. It has some comments removed or
;; corrected, and initializes current-choice to nobody instead of []

globals [found?]      ;; keep track of whether we found an object  

breed [objects object]
breed [people person]


objects-own [
  object-type      ;  say "apple" or "orange" or "widget"
  attribute        ;  user defined, apparently an integer
]

people-own[
current-choice    ; an agent  
  my-list]        ; a list of objects 


to setup 
  clear-all
  make-people
  make-objects
  reset-ticks
end

to go

  ;; for debugging we'll push two objects onto the my-list for a random agent
  ;; then find one of them with the right attributes and pull it into current-choice

ask one-of people[
    ;; select a random apple, set its attribute to 111, 
    ;; and push it onto this turtle's my-list  

    set current-choice one-of objects with [object-type = "apple"]   
    ask current-choice [set attribute 111 ]              
    set my-list ( lput  current-choice my-list )    

    ;; select a random orange, set its attribute to 222, 
    ;; and push it onto this turtle's mylist  

    set current-choice one-of objects with [object-type = "orange"]    
    ask current-choice [set attribute 222 ]               
    set my-list ( lput  current-choice my-list )     

    ;; ok, let's see if we can find an object in my-list with attribute 111

    let want-attribute 111    ;; for debugging purposes


    ;; make a list of ALL objects on the list with the desired attribute
    ;; as far as we know,there may be many, one, or zero objects with this attribute

    let found-list filter   [ i -> want-attribute =[attribute] of i ]   my-list


    if-else length found-list > 0     
    [ ;; we found at least one match
      set found? true

      ;;  pick a random object from the list of objects which have the desired attribute

      set current-choice one-of found-list
    ]
    [ ;; we didn't find any matches
       set current-choice nobody
       set found? false
      ;; do whatever you do when there is no matching object
    ]

     ;; if desired, check out how things ended up by uncommenting the next
     ;; two lines:
     ;;  inspect current-choice  
     ;;  inspect self

   ]
tick  ;; update the tick counter
end

to make-people
  create-people 5    
   [
    set current-choice nobody
    set my-list []
] 
end

to make-objects
create-objects 3    ; create 3 apples
  [ set object-type "apple"
    set attribute 333 ]

 create-objects 3    ; create 3 oranges
  [ set object-type "orange"
    set attribute 555 ]
end

;; Above code was created by Wade Schuette,  3-November-2019
;; modified sightly 4-November-2019
;; No copyright is claimed.  Enjoy!

推荐阅读