如何在NetLogo中一次一个地将一些变量设置为海龟

时间:2015-01-08 02:01:59

标签: variables netlogo

在NetLogo中,我想一次一个地迭代每只乌龟,有两个品种;大人物和小人物。在查看一只乌龟时,我想分配其邻居概率,然后将这些概率放入一个列表中,将该列表相乘,然后使用该值来决定是否应该移动乌龟。然后,一旦循环完成了一只乌龟,这些值就会丢失,因此它们不会对下一个中央乌龟附近产生影响。我一直在使用这段代码,但我现在意识到它似乎是在重写这些价值,因为他们会问这些代码'在概率_产品中排在最后,但我不确定如何解决它。这里的大多数未定义变量都在GUI的滑块上。 谢谢!

   breed [ bigs big ]
breed  [ smalls small ] 
bigs-own [ probability]
  smalls-own [ probability]


to setup
  clear-all 
  set-default-shape bigs "square"
  set-default-shape smalls "square"

   ask n-of bigs-number patches with [ abs (min-pxcor - pxcor) > 2]
    [sprout-bigs 1 [ set color red ]] 

    ask n-of smalls-number patches with [not any? bigs-here] 
     [sprout-smalls 1 [ set color blue ]]

    reset-ticks
end


to go  
ask turtles with [count turtles-on neighbors4 < 4 ][  
  ;; so only turtles will a space in the neighbours 4 can move
       let vacant-patches neighbors4 with [not any? turtles-here ]
  ;print Probability_Product 

if count turtles-on neighbors4 < 4 [
             if random 1001 <= Probability_Product * 1000    ;;if random number in the probability range, the following happens 
                     [ move-to one-of vacant-patches ]]
     ]
  tick
end

to-report Probability_Product  
  if ( count turtles-on neighbors4 < 4 ) or ( count turtles-on neighbors4 = 0 ) [

ifelse breed = bigs 

[  ask bigs-on neighbors4 [set probability Prob_big_big_breaking]  ask smalls-on neighbors4 [set probability Prob_small_big_breaking]
   let prob-list ( sentence ([probability] of turtles-on neighbors4))
  print prob-list
ifelse prob-list != [] 
 [ report reduce  * prob-list ] ;; multiplies all the probabilities together
       [report 1 ]]

[  ask smalls-on neighbors4 [set probability Prob_small_small_breaking]  ask bigs-on neighbors4 [set probability Prob_small_big_breaking]
  let prob-list ( sentence ([probability] of turtles-on neighbors4))
  print prob-list
ifelse prob-list != [] 
 [ report reduce  * prob-list ] ;; multiplies all the probabilities together
       [report 1 ]]]

 end

1 个答案:

答案 0 :(得分:2)

这不是一个真正的答案(编辑:也许它现在是?),但这是一个必要的回应,它不适合评论。我在评论中还有其他问题。

我仍然不确定我是否在评论中回答了您的问题。我认为首先需要做一些事情来澄清你的代码应该做什么。首先,我已经编辑了你的问题中的代码,以便对其进行格式化,以使其更清晰(尽可能地保留你的风格)。其次,我在ask bigs之后和ask smalls之后的行上添加了右括号。最重要的是,我认为如果你能提供一个Minimal Working Example(MWE) - 你的程序中最小,最简单的版本仍然包含(a)运行的代码,并且(b)说明你的问题会有所帮助'试图解决。构建MWE需要一些工作,因为你必须弄清楚你可以拿出什么(因为留下它会让读者感到困惑),以及你必须留下什么,因为它对于创建问题至关重要。 (但是,当您尝试创建MWE时,有时您会自己找出问题的答案。)

例如,从运行的角度来看,这是一个MWE,但您必须更改它以说明您的问题。

breed [bigs big]
breed [smalls small]
bigs-own [probability]
smalls-own [probability]
globals [Prob_bigs_bigs_breaking Prob_smalls_smalls_breaking]

to setup
  reset-ticks
  ask n-of 20 patches [sprout-bigs 1 []]
  ask n-of 20 patches [sprout-smalls 1 []]
end

to go  
  ask turtles with [count turtles-on neighbors4 < 4 ][  
     ;; so only turtles will a space in the neighbours 4 can move
     move-turtle
  ]
  tick
end

to-report Probability_Product

  ask bigs with [count turtles-on neighbors4 < 4 ] [ 
    ;; self is each big in turn
    ask bigs-on neighbors4 [set probability Prob_bigs_bigs_breaking] 
  ]

  ask smalls with [count turtles-on neighbors4 < 4 ][
    ;; self is each small, in turn  
    ask smalls-on neighbors4 [set probability Prob_smalls_smalls_breaking]
  ]

  ;; Here self is the turtle that called move-turtle, which called Probability_Product.
  if any? turtles with [count turtles-on neighbors4 < 4 ][
    let prob-list ( sentence ([probability] of turtles-on neighbors4))
    if prob-list != [] 
      [ report reduce  * prob-list ] ;; multiplies all the probabilities together
  ]

  report 0
end

to move-turtle
  if random 1001 <= Probability_Product * 1000 [
    ;.....
  ]
end

接下来,我建议ask bigsask smalls行应在与包含if any? ...的函数分开的过程中完成。将这一切都集中在一个程序中会让人感到困惑,因为在ask中的Probability_Product块中,我们引用了由ask定义的海龟,但在if any?中,我们引用了由ask两个程序“上面”定义的乌龟,即由ask中的go定义,然后调用move-turtle,然后调用Probability_Product 。当我们到达if any?时,很难弄清楚neighbors4是什么相对的,因为它定义了上面的两个过程,因为Probability_Product也引用了所有bigs以及所有smalls

除了令人困惑之外,我不确定Probability_Product正在做你想做的事情。对于每只乌龟,此过程将要求所有bigs和所有smalls执行某些操作。所以,如果大小只是大海龟,Probability_Product要求所有海龟做某事,然后为下一只海龟再次要求所有海龟做同样的事情,依此类推。

编辑:

在我看来,MWE中应该做的Probability_Product可以通过以下函数更简单地完成。也许我误解了你的意图,可能在整个程序中还有更多需要完成的事情,因为下面的函数根本没有设置或使用probability变量。即使这不是你想要的,也许这个例子可以帮助你思考你需要做什么。

to-report new-Probability_Product
  let neighbor-count count turtles-on neighbors4
  let big-neighbor-count count bigs-on neighbors4
  let small-neighbor-count count smalls-on neighbors4

  if-else neighbor-count < 4 and neighbor-count > 0
    [ if-else is-big? self  ; self is turtle from ask in go procedure
        [ report (Prob_big_big_breaking ^ big-neighbor-count) *    ; if self is a big
                 (Prob_small_big_breaking ^ small-neighbor-count) ]
        [ report (Prob_small_big_breaking ^ big-neighbor-count) *  ; if self is a small
                 (Prob_small_small_breaking ^ small-neighbor-count) ] ]
    [ report 1]
end

这个函数背后的想法是probability变量在Probability_Product中唯一做的就是保持滑块中设置的值。然后这些值成倍增加。但我们可以直接乘以这些值。

(我的其他一些评论似乎仍然适用。)