AnyLogic创建一个火车中铁路车辆的确定性顺序。

huangapple go评论58阅读模式
英文:

AnyLogic creating a deterministic order of railcars in a train

问题

我正在使用Rail Pallette创建火车。我已经创建了一个火车代理和几种不同类型的铁路车辆代理。我已经查看了Hump Yard教程;其中详细介绍了如何创建一个包含多种铁路车辆类型的随机生成火车。

我需要按照确定性顺序创建一个包含多种不同铁路车辆的火车。我想象中有一个createUniform()函数,它接受类并按照给定的顺序创建车辆,直到达到火车长度。

我尝试了几种不同的方法。最接近的方法是创建自己的createUniform()函数,但我无法返回不同的车辆类型,因为它们的类不匹配。我尝试让它返回Agent,但仍然会出现错误:无法将OpenCar强制转换为Agent

是否有一种方法可以为我的所有车辆创建一个父类,以便它们可以从一个函数中返回?

英文:

I'm creating a train using the Rail Pallette. I've created a train agent and several different types of Rail Car agents. I've gone through the Hump Yard Tutorial; this details how to create a randomly generated train with several types of rail cars.

I need to create a train with several different rail cars in a deterministic order. I imagine a createUniform() function that takes the classes and creates the cars in the order given until the train length is reached.

I've tried several different avenues. The closest is creating my own createUniform() function, but I am not able to return the different car types as their classes don't match. I've attempted to have it return Agent but this still results in an error: cannot cast OpenCar to Agent.

Is there a way to create a parent class for all my cars so that they can be returned from one function?

答案1

得分: 1

如果您正在使用TrainSource块,在"新的铁路车辆"部分,您可以切换到代码而不是代理选择器。他们为您提供了一个carindex参数,您可以使用它来定义车辆。如果您不想编写特殊函数,可以使用三元运算符。

例如:

(carindex < 2)?
  new Locomotive() :
  (carindex < 10) :
    new HopperCar() :
    new ContainerCar()

在这种情况下,前两辆车(carindex 0和1)将是机车,然后carindex 2到9将是卸料车,10及以上将是集装箱车。

英文:

If you are using the TrainSource block, under the New rail car section you can switch to code instead of the agent picker. They provide you with a carindex parameters, which you can use to define the car. This can be used with the ternary operator if you don't want to write an special function.

For example:

(carindex &lt; 2)?
  new Locomotive() :
  (carindex &lt; 10) :
    new HopperCar() :
    new ContainerCar()

In this case, the first two cars (carindex 0 and 1) will be Locomotives, then carindex 2 to 9 will be HopperCars, and 10 onwards will be ContainerCars.

答案2

得分: 0

代替在函数中返回汽车类型,我创建了一个 Agent 对象,将其设置为我的汽车对象,然后返回该 Agent 对象。

Agent newCar = null;
newCar = new CustomerCarClass();
return newCar;

这使我能够将自定义函数设置为返回一个 Agent 对象。

英文:

Instead of returning a type of car in the function I created an Agent object, set that equal to my car object, then returned that Agent object.

Agent newCar = null;
newCar = new CustomerCarClass();
return newCar;

This allows me to set the custom function as returning an Agent object.

huangapple
  • 本文由 发表于 2023年7月20日 22:03:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/76730705.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定