如何通过将变量名称和值传递给函数来更改座席变量? [英] How to change agent's variable by passing variable name and value to a function?

查看:83
本文介绍了如何通过将变量名称和值传递给函数来更改座席变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何通过将变量名称传递给函数来更改代理的特定变量?

How is it possible to change specific variable of an agent by passing variable name to a function?

例如,我的乌龟具有变量MONEY和以下功能:

for example I have turtles with variable MONEY and the following function:

to setVariable [varname varvalue]
[
   ask one-of turtles [ set varname varvalue ]
]
end

现在我要运行:

观察者>询问一只乌龟[setVariable MONEY 100] ;;我需要通过另一只乌龟问一下,因为我不能在观察者环境中直接使用金钱

observer> ask one-of turtles [setVariable MONEY 100] ;; I need to ask via another turtle since I cannot use MONEY directly in observer context

在没有给出任何错误的情况下,它不会设置我的变量.

And it does not set my variable without giving any errors.

有趣的是,您可以通过类似的方式读取变量:

Interestingly, you can read a variable in similar manner:

to showVariable [varname ]
    [
       ask one-of turtles [ show varname ]
    ]
end

因此,这里的问题是如何将我的函数输入转换"为乌龟的变量名,以实现SET的目的.

So the question here is how to "convert" my function input into turtle's variable name it would recognise well for SET purposes.

PS:我不想使用运行功能,因为它会使模型变慢. 感谢您的任何建议

PS: I don't want to use run function as it would slow down the model. Thanks for any suggestions

推荐答案

在类似的情况下,有很多可能的选项,我为每个选项创建一个任务,并将它们放在一个查找表中(使用表扩展名),每个字符串的键.然后,我可以为任何键查找适当的任务.它节省了嵌套的if/else结构,但是我还没有研究表查找的效率.

In a similar situation where there are a number of possible options, I create a task for each and put them in a lookup table (using the table extension) with a string key for each. Then I can lookup the appropriate task for any key. It saves having the nested if/else structures, but I haven't investigated the efficiency of the table lookup.

HTH, 查尔斯

这篇关于如何通过将变量名称和值传递给函数来更改座席变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆