【问题标题】:How to set value of an agent variable same as the value of another variable of same agent in Netlogo如何在 Netlogo 中将代理变量的值设置为与同一代理的另一个变量的值相同
【发布时间】:2021-02-03 09:33:14
【问题描述】:

我正在尝试根据 NetLogo 中同一代理的另一个变量的值来分配代理变量的值。 在我的模型中,代理有两个变量,一个是“my-batches”,另一个是“my-targets”。在每个时间步,代理都尝试根据一系列标准找到目标来放置它们的批次。在初始化时,'my-batches' 的值是随机分配的(范围在 0 -15 之间)。我应该如何编码以便代理找到与他们的“我的批次”相同数量的“我的目标”?我正在尝试类似的东西

turtles-own [my-batches my-targets distance-travelled ] 
patches-own [roads? target-cell? resource]

to setup   
ca
crt num-turtles

ask turtles [
    set my-batches random 15  
    setxy random-xcor random-ycor
    set distance-travelled 0     ; initial travel distance is zero   ]

ask patches [      
    set resource random 9    
    ifelse resource = 0    
       [ set target-cell? false]  [ set target-cell? true ]
reset-ticks 
end

to go  
if ticks > 365 [ stop ]   
find-my-targets  
calculate-distance    
tick  
end 

to find-my-targets

**if target-cell? false [ ask turtles [ set my-targets [ n-of [ my-batches ] of myself ] ]]**

end

有什么帮助吗?

另外,任何人都可以提供示例模型来计算代理行进的累积距离(从起点到终点)吗?

【问题讨论】:

    标签: netlogo


    【解决方案1】:

    find-my-targets 过程目前是由观察者执行的过程。所以你首先要使用ask turtles;或者,使该过程由海龟执行。使用第二种方式(更符合过程名称),会是:

    to go
      if ticks > 365 [ stop ]
      ask turtles [
        find-my-targets
        calculate-distance
      ]
      tick
    end
    
    to find-my-targets
      if (not target-cell?) [
        set my-targets n-of my-batches patches
      ]
    end
    

    这留下了几个疑问:

    • 您希望my-targets 成为任何补丁还是仅作为目标单元格的补丁?如果是后者,那么您应该使用set my-targets n-of (my-batches) (patches with [target-cell?])(括号仅用于可读性)。
    • 你想让my-targets在每次乌龟在一个不是目标单元的补丁上时被重置吗?因为这就是现在正在发生的事情,只是指出来......

    至于距离问题(这真的应该是一个单独的问题),只需这样使用distancexy原语(你没有说你的海龟是如何移动的,所以我会发明它):

    to move-and-calculate-distance
      let startingx xcor
      let startingy ycor
    
      move-to one-of my-targets
    
      let last-distance-travelled (distancexy startingx startingy)
      set distance-travelled (distance-travelled + last-distance-travelled)
    end
    

    PS:注意random 15 给出了一个 0-14 区间的整数。如果您希望 my-batches 为 0 到 15 之间的整数,请使用 random 16

    【讨论】:

      猜你喜欢
      • 2019-10-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-23
      • 1970-01-01
      相关资源
      最近更新 更多