是否有与Netlogo等价的python解压缩或bash的间接参数扩展?

时间:2018-08-29 22:37:10

标签: netlogo

背景

我用this answer根据由乌龟的流行程度或适合度决定的概率分布选择了一只乌龟。

问题

我正在尝试传递一个参数,该参数确定概率分布所依据的是乌龟的哪个属性。

问题

如何在netlogo中执行等效于参数的“ python解包”?

示例代码

turtles-own
[
  fitness
  strength
  degree     ;;Node's Connectness
  popularity
  wealth
]

to-report pick-turtle-biased-by-property [turtle-list property-to-unpack]
  let prob-list []
  let turtle-list []

  ask turtles [ 
       set prob-list lput [[property-to-unpack] of self ] prob-list
       set turtle-list lput self turtle-list
  ]

  report first rnd:weighted-one-of-list (map list turtle_list prob-list) last
end

1 个答案:

答案 0 :(得分:3)

您要执行的操作的关键是使用“匿名报告者”传递“解包属性”。请参阅编程指南的Anonymous procedures部分。

这是一个完整的例子:

extensions [ rnd ]

turtles-own [
  strength
  wealth
]

to setup
  clear-all
  create-turtles 10 [
    set strength random 100
    set wealth random 100
  ]
end

to go
  print pick-turtle-biased-by-property [ -> strength ]
  print pick-turtle-biased-by-property [ -> wealth ]
end

to-report pick-turtle-biased-by-property [ property-to-unpack ]
  let pairs [ (list self runresult property-to-unpack) ] of turtles  
  report first rnd:weighted-one-of-list pairs last
end