Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2012/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Netlogo 如何将项目添加到邻居';孵化另一个代理集的代理集列表?_Netlogo - Fatal编程技术网

Netlogo 如何将项目添加到邻居';孵化另一个代理集的代理集列表?

Netlogo 如何将项目添加到邻居';孵化另一个代理集的代理集列表?,netlogo,Netlogo,我目前有一个代理集(breed1)正在孵化另一个代理集(breed2)。我想要的是编辑breed2(attribute1和attribute2),然后将编辑的项(在阴影中称为item)添加到breed1的邻居列表中 编辑: 在Wade Schuette的回答之后,我编写了以下代码 breed [people person] breed [items item_1] people-own [ my-list attribute1 attribute2 ] items-own [

我目前有一个代理集(
breed1
)正在孵化另一个代理集(
breed2
)。我想要的是编辑
breed2
attribute1
attribute2
),然后将编辑的项(在阴影中称为
item
)添加到
breed1
的邻居列表中

编辑: 在Wade Schuette的回答之后,我编写了以下代码

breed [people person]
breed [items item_1]


people-own
[
  my-list
  attribute1
  attribute2 
]

items-own
[
  selected
  attribute1
  attribute2
]

to setup
  clear-all

   ;; make turtles to test
   create-people 1 [ set my-list [1 2 3 ] ] 
   create-people 1 [ set my-list [ ] ] 
   create-people 1 [ set my-list [22 33 ] ] 
   create-people 1 [ set my-list ["a" "b" "c"] ] 
   create-people 1 [ set my-list [3  4 5 ] ] 

    ;; make a network
  ask people [ create-links-with other people ]


    reset-ticks
end

to go

  let picked nobody
  let neighbours nobody

ask one-of people 
  [
    set attribute1 random-float 1
    set attribute2 random-float 1

    hatch-items 1 [
      set selected picked
      let this-item self 
      ask myself[
        print (word "turtle "  self " has item " this-item " with attribute1 " attribute1 "and attribute2 " attribute2) 
      set my-list lput this-item my-list
        show my-list
      ]
    ask link-neighbors [ print (word "Person "  who " has this my-list " my-list) 
      set attribute1 (attribute1 + random-float 1)
      set attribute2 (attribute2 + 3)
      set my-list lput this-item my-list
      print (word "added item " this-item " with attribute1 " attribute1 " with attribute2 " attribute2)
      show my-list
      ]
  ]
  ]

  tick

end

如你所见,我的困难在于考虑海龟的邻居,以及更新我想添加到他们列表中的项目的信息(属性1和属性2)

Minor打字错误:您的一个方法用于链接邻居,另一个方法使用链接邻居。除非您想排除出站链接,否则我认为您需要链接邻居

基本上,您要问的一个设计问题是,在图案填充之前查找调用方的链接邻居集并将其保存在属性中,还是在图案填充时本地查找并在图案填充后忘记列表更好。我还没有试过你的代码,但我认为这两种方法都可以。如果链接集是静态的,您可以找到它一次,存储它,而不必在每次孵化新代理时再次查找它,这样可以节省一些时间。如果链接集是动态的,则无论如何每次都需要查找它,因此没有必要将其保存在属性中

您询问如何验证代码。我不知道你的建议是什么意思,所以我会回答一般情况。在这里发布之前,这些可能是您可以做的步骤,也应该做的步骤,以修复您自己可以修复的所有问题

规则#1:从简单开始,在增加复杂性之前先做大量工作。

;; This shows how to use a global variable to turn on or off print statements,
;; which you might want to use while developing and testing code

globals [
  verbose?   ;; true means print lots of things,  false means don't print them
]

to setup
  clear-all
  set verbose? true  ;; or false, whatever.  You can also use the Command Center
                     ;; to set this true or false in the middle of a run
  reset-ticks
end

to go
  let x random 10         ;; just for illustrating how this works
  xprint (word "At tick " ticks " x = " x )
  tick
end

to xprint [ stuff ]
  if  verbose? [ print stuff ]
end

您的代码已违反规则1。当只能使用一个属性进行测试时,可以设置两个属性。如果第二个属性代码导致错误,使您无法检查整个逻辑是否正常工作,该怎么办。因此,从零属性开始测试。注释掉该代码,然后逐渐添加回代码,在每个步骤中再次检查

规则2:采取小步骤。

;; This shows how to use a global variable to turn on or off print statements,
;; which you might want to use while developing and testing code

globals [
  verbose?   ;; true means print lots of things,  false means don't print them
]

to setup
  clear-all
  set verbose? true  ;; or false, whatever.  You can also use the Command Center
                     ;; to set this true or false in the middle of a run
  reset-ticks
end

to go
  let x random 10         ;; just for illustrating how this works
  xprint (word "At tick " ticks " x = " x )
  tick
end

to xprint [ stuff ]
  if  verbose? [ print stuff ]
end

在添加更多代理之前,请确认代码仅适用于一个代理。通过一次“开始”步骤并停止测试。在进入第二步之前,要做到完美。等等

我们都可以梦想有一天NetLogo有一个真正的调试器,让您一次一个语句地执行代码,或者设置断点。但事实并非如此。当“开始”步骤结束并且“停止”生效时,您已经失去了上下文。局部变量已蒸发,无法检查。嵌套调用的“堆栈”丢失

我所知道的在上下文中查看动态细节的唯一方法是设置新的全局变量,这很混乱,或者大量使用嵌入的“print”语句

您还可以插入一条错误语句以导致运行时错误,检查调用堆栈,并有机会在该点上呼吸一下并查看输出

error " here is the calling stack, halting execution entirely here."
或者,您可以使用用户消息语句使代码暂停,让您知道某些内容已更改,或者现在已发生了不应该发生的情况(!)。这为您提供了“暂停”或继续运行的良好选项,就好像什么都没发生一样

if x > 5 [ user-message " Alert -- X is over 5!! " ]
其他评论者,在这里加入并分享其他方法

我发现的最有用的print语句标记了几个感兴趣的变量,并标识了它们的执行位置,以便您可以在输出中区分它们。查看3个变量x、y和z的示例:

 print ( word "In step 3,  x = " x " y = " y " z = " z )"
如果有一种方法可以在编辑器中切换这些语句的可见性,那就太好了,但是没有。您至少可以通过添加全局变量(例如“verbose”)并更改print语句来利用它来切换语句是运行还是跳过。(见下文)

然后您可以打开或关闭一次打印所有这些语句,而不是将它们注释掉,或者更糟的是,删除它们。一般来说,不要删除它们,因为总有一天你会需要修改和重新验证代码,你会再次需要这些代码来确认这些修改正在做你想让他们做的事情,并且没有破坏新的东西。好的“印刷”声明是值得的,值得付出努力

这里有一个选择打印的好方法。声明一个名为“xprint”的新命令,该命令仅在全局变量“verbose?”为true时打印。您可以在设置中设置一次,或在go mid stream中修改,或设置详细?在命令中心,您可以单步执行代码

然后您可以使用“xprint”而不是“print”,在这里您可以切换打印或不打印

 xprint ( word "In step 3,  x = " x " y = " y " z = " z )"
这确实降低了在代码中包含大量打印语句的成本,因此它确实简化了验证代码和以后重新验证代码的过程。

;; This shows how to use a global variable to turn on or off print statements,
;; which you might want to use while developing and testing code

globals [
  verbose?   ;; true means print lots of things,  false means don't print them
]

to setup
  clear-all
  set verbose? true  ;; or false, whatever.  You can also use the Command Center
                     ;; to set this true or false in the middle of a run
  reset-ticks
end

to go
  let x random 10         ;; just for illustrating how this works
  xprint (word "At tick " ticks " x = " x )
  tick
end

to xprint [ stuff ]
  if  verbose? [ print stuff ]
end


次要打字错误:您的一个方法用于链接邻居,另一个方法使用链接邻居。除非您想排除出站链接,否则我认为您需要链接邻居

基本上,您要问的一个设计问题是,在图案填充之前查找调用方的链接邻居集并将其保存在属性中,还是在图案填充时本地查找并在图案填充后忘记列表更好。我还没有试过你的代码,但我认为这两种方法都可以。如果链接集是静态的,您可以找到它一次,存储它,而不必在每次孵化新代理时再次查找它,这样可以节省一些时间。如果链接集是动态的,则无论如何每次都需要查找它,因此没有必要将其保存在属性中

您询问如何验证代码。我不知道你们俩是怎么回事
turtles-own
[
  my-list 
]
to setup
  clear-all

   ;; make turtles to test
   create-turtles 1 [ set my-list [1 2 3 ] ] 
   create-turtles 1 [ set my-list [ ] ] 
   create-turtles 1 [ set my-list [22 33 ] ] 
   create-turtles 1 [ set my-list ["a" "b" "c"] ] 
   create-turtles 1 [ set my-list [3  4 5 ] ] 

    ;; make a network
  ask turtles [ create-links-with other turtles ]


    reset-ticks
end

to go
ask one-of turtles 
  [
    ask link-neighbors [ print (word "turtle "  who " has this my-list " my-list) ]
  ]

  tick

end

globals [ 
  verbose?   ;; to turn on debugging statements
]

breed [people person]
breed [items item_1]


people-own
[
  my-list
  attribute1
  attribute2 
]

items-own
[
  selected
  attribute1
  attribute2
]

to setup
  clear-all
  set verbose? true  ;; true means printa lot of stuff

   ;; make turtles to test
   create-people 1 [ set my-list [1 2 3 ] ] 
   create-people 1 [ set my-list [ ] ] 
   create-people 1 [ set my-list [22 33 ] ] 
   create-people 1 [ set my-list ["a" "b" "c"] ] 
   create-people 1 [ set my-list [3  4 5 ] ] 

    ;; make a network
  ask people [ create-links-with other people ]

  ;; ADDED next 2 lines
  xprint (word "Created this many links in setup: "  count links)  
  ask person 0 [

    let fastcount count link-neighbors
    xprint ( word "In setup,   person " who " has this many links " fastcount ) 
  ]


    reset-ticks
end

to go

  let picked nobody
  let neighbours nobody


  ask one-of people with [ who = 0 ]
  [ 
    let fastcount count link-neighbors
    xprint ( word "Entering ask-person,   person " who " has this many links " fastcount ) 

    set attribute1 random-float 1
    set attribute2 random-float 1

    xprint ( word "Before hatch-items, we have this...")
    xprint (word "turtle "  self " has no item hatched, " 
        " with attribute1 " precision attribute1 2 " and attribute2 " precision attribute2 2) 
    let this-item 0   ;; this was down inside hatch-items, so it evaporated when 
                            ;; finishing hatch-items, causing a problem
                            ;; DECLARE it up here, then SET it inside hatch-items
    hatch-items 1 [
      set selected picked
      set this-item self   ;; this was down here inside hatch-items, move it up 4 lines

     xprint ( word "confirming this-item value is: " this-item )

      ask myself[

      xprint ( word "inside hatch-items ,  inside ask-myself, we have this...")
      xprint (word "turtle "  self " has item " this-item 
       " with attribute1 " precision attribute1 2 " and attribute2 " precision attribute2 2) 

      set my-list lput this-item my-list
       xprint ( word  "hatched item now has my-list = "    my-list )

      ] ;; end of ask myself

    ];;; <====== you want to close context of hatch-items up here, not down below
     ;;; so that ask link-neighbors will work



       ;; ADDED THIS NEXT LINES FOR DEBUGGING  
       xprint "we are done with ask myself,  but still inside hatch-items"
       set fastcount count link-neighbors
       xprint ( word "After ask myself, but still inside hatch-items,   person " who " has this many links " fastcount )  
       xprint ( " the code posted in the revised question failed here")
       xprint ( " PERSON should be person 0  , link count should be 4 ")
       xprint ( " SO ASK  LINK-NEIGHBORS WIll fail !!!")


    xprint " asking link-neighbors to update their lists now"

    ask link-neighbors [ print (word "Person "  who " has this my-list " my-list) 
      set attribute1 (attribute1 + random-float 1)
      set attribute2 (attribute2 + 3)
      set my-list lput this-item my-list
      xprint (word "added item " this-item " with attribute1 " attribute1 " with attribute2 " attribute2)
      show my-list
      ]
;;] ;; closes context of hatch-items,  try closing hatch-items before asking link-neighbors

  ]

  tick

end

to xprint [ stuff ]
  if verbose? [ print stuff ]
end