手动投射代理并在 AnyLogic 中访问其 SelectOutputOut 类型的参数?

Manual casting of agents and accessing its parameter of type SelectOutputOut in AnyLogic?

我知道我可以通过将整个过程中的代理类型更改为通用 Agent 来在同一块中插入不同类型的代理(感谢 Amy,效果很好)。 snapshot

但是,我一直在研究如何使用手动铸造过程 selectOutputIn1 将它们取出来。每个代理类型都有一个名为 p_new_location 且类型为 SelectOutputOut 的参数。

我需要什么帮助

传递等同于 agent.p_new_location 的内容(如果通过 selectOutputIn1 正确转换)。

我试过的

使用手动转换创建函数:

if( agent instanceof Wife){
    return ((Wife)agent).p_new_location ;
} else if(agent instanceof Child_M){
    return ((Child_M)agent).p_new_location ;
} else if(agent instanceof Child_F){
    return ((Child_F)agent).p_new_location ;}

how it looks with the error

不幸的是,如您所见,有一个错误指出该方法必须 return 类型为 SelectOutputOut 的结果,尽管它已经被定义。

每个 class 中的参数类型看起来与 this. And from inside the simulation, it looks like this before passing the value and like this after passing its value. Furthermore, I noticed that the value of an uncommon parameter type like SelectOutputOut, is not showing during the simulation as shown here 完全一样。如您所见,参数 (agecountDbtaken) 都有它们的值,但 p_new_location.

没有

解决方案

再次感谢艾米:)

The compiler is seeing if / else if / else if. What if none of those are true? If your last else if is the only option, just change that to an else or put in appropriate code for what you want to do.

这就是代码现在的样子

if( agent instanceof Wife){
    return ((Wife)agent).p_new_location ;
    } else if(agent instanceof Child_M){
        return ((Child_M)agent).p_new_location ;
    } else{
        return ((Child_F)agent).p_new_location ;}

提前致谢;

是的,AnyLogic 可以轻松地在单个进程块中处理多种代理类型。需要牢记的几点:

  • 确保进程块设置为处理通用类型 "Agent" 或 parent class of mother, father, child.
  • 由于您有多种代理类型流经相同的块,您应该准备好进行一些转换以获取任何 class 特定信息。
  • AnyLogic 代理一次不能在多个流程图块中,即使它们可以在多个集合中。它们也可以不在流程图块中。在将代理发送到输入块之前,您必须首先将其从它所在的任何其他块中删除(如果它在一个块中)。例如,如果你们所有的妻子都在一个队列中,您需要在调用 enter.take( wife ) 代码行之前从队列中删除妻子代理。