ClassCastException: Cannot Cast Agent to Custom Agent Type in AnyLogic - Stack Overflow

Firstly, I'm new to AnyLogic and currently using it for a project report.I am working on an AnyLo

Firstly, I'm new to AnyLogic and currently using it for a project report.

I am working on an AnyLogic simulation where I generate wind turbine blades (BladeAgent) using a Source block in Main. Each blade should be assigned a random length (wingLength) from {70, 75, 80} upon creation. However, I keep encountering the following error during execution:

Exception during discrete event execution: class com.anylogic.engine.Agent cannot be cast to class mymodel.BladeAgent (com.anylogic.engine.Agent and mymodel.BladeAgent are in unnamed module of loader 'app')

My Setup:

Agent Type:

  • I created a custom agent BladeAgent with an integer variable wingLength (public).
  • In Main, I use a Source block to generate instances of BladeAgent.
  • The Agent type for the Source block is correctly set to BladeAgent. Code in On exit of Source block: ((BladeAgent) agent).wingLength = randomFrom(70, 75, 80);

I also tried:

BladeAgent blade = (BladeAgent) agent;
blade.wingLength = randomFrom(70, 75, 80);

Both result in the same ClassCastException error.

Agent Population: I have set BladeAgent to be a Population of agents in Main rather than a Single agent. Other Fixes Tried:

  • Restarted AnyLogic and cleaned the project.
  • Verified BladeAgent is public.
  • Created an explicit Agent Link in Main to reference BladeAgent.

Firstly, I'm new to AnyLogic and currently using it for a project report.

I am working on an AnyLogic simulation where I generate wind turbine blades (BladeAgent) using a Source block in Main. Each blade should be assigned a random length (wingLength) from {70, 75, 80} upon creation. However, I keep encountering the following error during execution:

Exception during discrete event execution: class com.anylogic.engine.Agent cannot be cast to class mymodel.BladeAgent (com.anylogic.engine.Agent and mymodel.BladeAgent are in unnamed module of loader 'app')

My Setup:

Agent Type:

  • I created a custom agent BladeAgent with an integer variable wingLength (public).
  • In Main, I use a Source block to generate instances of BladeAgent.
  • The Agent type for the Source block is correctly set to BladeAgent. Code in On exit of Source block: ((BladeAgent) agent).wingLength = randomFrom(70, 75, 80);

I also tried:

BladeAgent blade = (BladeAgent) agent;
blade.wingLength = randomFrom(70, 75, 80);

Both result in the same ClassCastException error.

Agent Population: I have set BladeAgent to be a Population of agents in Main rather than a Single agent. Other Fixes Tried:

  • Restarted AnyLogic and cleaned the project.
  • Verified BladeAgent is public.
  • Created an explicit Agent Link in Main to reference BladeAgent.
Share asked Mar 4 at 10:28 AndreasbgAndreasbg 92 bronze badges 4
  • Is the error triggered at the randomFrom code line? Else, where is it triggered? – Benjamin Commented Mar 4 at 11:09
  • Yes, the error is triggered at the randomFrom line. The full error message appears as soon as the simulation starts generating agents from the Source block. – Andreasbg Commented Mar 4 at 11:30
  • Either your agent type does not have the field or you setup the properties wrongly. Share some screenshots :). Pic is worth 1000 words – Benjamin Commented Mar 4 at 15:40
  • Agree with Ben. A picture of your canvas would be helpful. If you are using a source block, you can get this error when you create agents of type "Agent" instead of "BladeAgent". There are a few different places on the source block where you set agent type/custom population, so it can be easy to miss. If not create via source block, it could be several other issues that a screen shot would be helpful to debug. – Amy Brown Greer Commented Mar 4 at 15:51
Add a comment  | 

1 Answer 1

Reset to default 0

This looks like an issue with the Source block. On the Source block, you should make sure to set the new agent to be created to be of the correct type you want (Source Block Help

If the source block is configured correctly I'd expect you wouldn't have to type cast. E.g. you would not need ((BladeAgent) agent).wingLength = randomFrom(70, 75, 80); as the agent would already be a BladeAgent; if the Source block was configured correctly you'd need only agent.wingLength = randomFrom(70, 75, 80);

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745050598a4608362.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信