在 AnyLogic 中创建一个从 1 到 n 计数的变量

问题描述

我希望在 AnyLogic 中添加一个每小时从 1 到 217 计数的变量,以便用作设置参数行引用的选择条件。

我假设我需要使用事件或状态图,但我真的很纠结,无法在网上找到任何东西。

如果您有任何提示,请告诉我,任何帮助将不胜感激

谢谢, 塔什

解决方法

在这种情况下不需要状态机,因为这可以使用计算或定时事件来实现。 AnyLogic 具有 time() 函数,该函数以模型时间测量单位的 double 形式返回自模型启动以来的时间。

例如:如果模型时间单位是 seconds 并且它已经运行了 2hr 2min 10sec 那么 time(SECOND) 将返回 7330.0(它总是一个 double 值)。一个小时的 1/217 对应于大约 3600/217 = 16.58 秒。此外,java 有一个方便的函数 Math.floor() 可以将 double 值向下舍入,因此 Math.floor(8.37) = 8.0

将它们组装在一起:

   // how many full hours have elapsed from the start of the model
   double fullHrsFromStart = Math.floor(time(HOUR));
   // how many seconds have elapsed in the current model hour
   double secondsInCurrentHour = time(SECOND) - fullHrsFromStart * 3600.0;
   // how many full 16.58 (1/217th of an hour) intervals have elapsed
   int fullIntervals = (int)(secondsInCurrentHour / 16.58);

这可以打包成一个函数并随时调用,而且速度非常快。

或者:可以创建一个 Event,它每 16.58 秒将一些 count 增加 1,当计数达到 217 时十次将其重置为 0。