如何为不同的海龟存储不同的列表值

时间:2019-04-07 12:38:51

标签: netlogo

编辑: 每只乌龟都有不同的值,具体取决于它的连接方式,我有几个不同用途的列表。这些列表之一似乎为所有海龟都存储了一次。我的问题是,如何在列表中存储仅与每只乌龟相关的值,而不将所有值混合在一个列表中。 结果是我得到了这样的东西:

(turtle 0): [3.1198376765467213 2.296024229601798 3.42548843517858 -1.259846009171373 -0.7503525744180024 0.8329075988682271 2.5179361772122446 2.499561039717374] (turtle 1): [3.1198376765467213 2.296024229601798 3.42548843517858 -1.259846009171373 -0.7503525744180024 0.8329075988682271 2.5179361772122446 2.499561039717374] (turtle 2): [3.1198376765467213 2.296024229601798 3.42548843517858 -1.259846009171373 -0.7503525744180024 0.8329075988682271 2.5179361772122446 2.499561039717374] (turtle 3): [3.1198376765467213 2.296024229601798 3.42548843517858 -1.259846009171373 -0.7503525744180024 0.8329075988682271 2.5179361772122446 2.499561039717374]

实际上结果应该是这样

(turtle 0): [3.1198376765467213 2.296024229601798] (turtle 1): [3.42548843517858 -1.259846009171373] (turtle 2): [-0.7503525744180024 0.8329075988682271] (turtle 3): [2.5179361772122446 2.499561039717374]

这是我尝试过的代码:(请注意,calc-payoff是一个计算每只海龟的收益值的函数,它工作得很好)

ask turtles [ calc-payoff set p_list lput ([payoff] of self) p_list reset]

AND

添加的部分: 问题是,由于不满足if条件,因此某些乌龟到年底都具有turtle_list2 = 0。但是,我希望这一回合继续进行,直到所有海龟都找到稳定的价值为止。 (这部分在刻度线之后)

ask turtles [ calc-payoff set p_list lput payoff p_list reset]

ask turtles [create-links-to other turtles 
calc-payoff2
set turtle_list lput payoff turtle_list 
reset]

ask turtles [
ask one-of links [die]
calc-payoff2
set turtle_list lput payoff turtle_list
reset]

tick
set tickcount tickcount + 1
if tickcount >= 3[  
ask turtles   
[
let p1 item (length turtle_list - 3) turtle_list
let p2 item (length turtle_list - 2) turtle_list
let p3 item (length turtle_list - 1 ) turtle_list
if p1 < p2 [if p2 > p3 [ set turtle_list2 (list p2) ]]
set payoff_list lput (sum turtle_list2 ) payoff_list]
user-message (word "stability is reached at payoff = "  sum payoff_list)]
end



1 个答案:

答案 0 :(得分:1)

在没有看到您的代码的情况下很难确定,但是我的猜测是p_listglobals变量而不是turtles-own变量。有关更多详细信息,请参阅NetLogo用户手册的variables section

例如,看看以下玩具设置:

globals [ global-list ]

turtles-own [ turtle-list ]

to setup
  ca
  set global-list []
  crt 10 [
    move-to one-of patches
    set global-list lput xcor global-list
    set turtle-list ( list xcor )
  ]  
  reset-ticks
end

运行该设置后,如果执行print global-list,则应该看到类似以下的输出:

[-14 4 -3 11 -16 -2 4 8 -1 -9]

如果我们要求乌龟显示自己的清单,

ask turtles [ show turtle-list ]

您应该看到类似以下内容的

observer> ask turtles [ show turtle-list ]
(turtle 3): [-14]
(turtle 5): [8]
(turtle 9): [4]
(turtle 7): [-16]
(turtle 2): [4]
(turtle 0): [11]
(turtle 6): [-3]
(turtle 8): [-9]
(turtle 1): [-1]
(turtle 4): [-2]

每只乌龟都有该列表的自己版本。

编辑:

要让乌龟更新其列表,可以使用与问题中使用的语法完全相同的lput-例如,以下循环直到随机绘制的浮点值小于0.25,然后打印乌龟淘汰他们的turtle-list。对于循环的每次迭代,海龟都会移动,然后添加到它们的turtle-list中:

to go 
  loop [
    if random-float 1 < 0.25 [
      ask turtles [ show turtle-list ]
      stop
    ]
    ask turtles [
      fd 1 
      set turtle-list lput xcor turtle-list
    ] 
  ]
end

您得到的输出如下:

(turtle 2): [13 13 13 13]
(turtle 5): [-4 -4.3583679495453005 -4.716735899090601 -5.0751038486359015]
(turtle 9): [14 13.015192246987791 12.030384493975582 11.045576740963373]
(turtle 7): [-3 -2.5 -2 -1.5]
(turtle 3): [-2 -2.3420201433256693 -2.6840402866513386 -3.026060429977008]
(turtle 0): [7 7.951056516295154 8.902113032590307 9.853169548885461]
(turtle 4): [-14 -13.947664043757056 -13.895328087514113 -13.842992131271169]
(turtle 8): [12 11.35721239031346 10.71442478062692 10.071637170940381]
(turtle 1): [16 15.674431845542843 15.348863691085686 15.02329553662853]
(turtle 6): [-16 16.426423563648953 15.852847127297908 15.279270690946863]