定义变量的区别

Difference in defining a variable

我想定义一个新对象,但我不知道什么是最好的方法。 我知道 let 用于局部变量,这应该是主要区别。 我正在 NetLogo 中创建一个列表,用于存储一些 attributes/properties 的元素。因为稍后我需要从创建的列表中选择一项,如果我需要将对象创建为 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. 可以根据属性选择从该列表中拾取对象的海龟。 (这是最重要的一点,因为稍后我需要使用该对象及其属性)。

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

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

感谢您的宝贵时间。

Val,我知道你最初问 "I would like to ask you the difference in using let and set." 但后来你解释了你需要实现什么,这就是我的回答。所以,这个问题的标题不是很准确。

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

当然post这里有问题如果你运行遇到问题。

工作代码如下。

我解释你的问题的方式,

  • objects 是持久的和全局的,
  • 任何人在任何时候对 object 的属性所做的更改都会更新 每个代理的 my-list
  • 中那个 object 的属性值
  • 每个人都有自己的 my-list 个 object,可能是空的。
  • 不同代理的my-list会不一样
  • 每个人都可以根据属性从他们的列表中搜索 object 之一,我将结果 object(如果有的话)存储在他们的 current-choice 中。
  • 每个人都应该能够 select 任何 object 并添加 object 他们的 my-list

实施设计选择:

  • 使用两个品种,"people" 和 "objects",它们拥有不同的变量。
  • 每个 object 拥有一个属性和一个 object-type(例如 "apple" 或 "orange")
  • 每人拥有一个my-list和current-choice。 my-list 是一个列表,并且 current-choice 是代理人(可能 = nobody

以下代码的特点

  • 代码创建 5 人和 6 objects 用于测试目的。它们全部都停留在显示屏的原点。

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

  • 代码在 my-list 中列出了 所有 object 具有所需属性的列表, 其中可能包含零个、一个或多个 object,并根据 列表的长度。

  • 从未从 my-list 中删除任何项目,或检查它们是否已经存在。

  • 仅此而已。 运行 设置,运行 执行(一次),并检查结果。

希望对您有所帮助。如果我没有回答您的问题,请在此处告诉我。

韦德

;; 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!