如何在任何logic中使用linkedhashmaps

发布于 2025-02-01 07:57:04 字数 412 浏览 3 评论 0 原文

我想有一个累积的母猪进入水槽(Deadsowsculledsows和 在过去的52周中。我为每周母猪创建了变量 使用循环事件在这些下沉位置的死亡。我希望为模拟的每个星期计算此累积数字。例如,在第10周 - 我想累积 第1至10周的死亡人数;在第52周 - 我想有一个第1周的累积号码 到52,对于第53周 - 我想在第2周到53周之间有一个累积数量, 等等。

有人建议我使用linkedhashmap,我同意,但是我不知道从哪里开始设置?我想将一周用作价值和每周死亡作为关键。我在哪里插入代码以将值放入LinkedHashMap中?

为了实现这一目标,我觉得我缺少组件。

https://i.sstatic.net/eense.png” alt =“ linkedhashmap”的死亡下沉和收集

I want to have a cumulative number of sows that enter the Sink (deadSowsCulledSows and
sowDeaths) over the last 52 weeks. I have created variables for weekly sow
deaths at these sink locations using cyclic events. I want this cumulative number to be calculated for every week of the simulation. For example, at week 10 – I want to have a cumulative
number of deaths for weeks 1 to 10; at week 52 – I want to have a cumulative number for week 1
to 52, and for week 53 – I want to have a cumulative number between weeks 2 and 53,
and so on.

It has been suggested that I use a LinkedHashMap, and I agree, but I don't know where to begin with setting this up? I want to use week as the value and weekly deaths as the key. Where do I insert the code to have the values put into the LinkedHashMap?

I feel like I am missing components in order to achieve this.

Death sinks and Collection for LinkedHashMap

Full model view

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

七秒鱼° 2025-02-08 07:57:05

听起来您想要过去52周的移动窗口。

这里有一些选择,但是我建议您最简单的选择是使用任何logic数据集对象,因为它已经具有您需要的“保持最大数量的样本”功能。

  1. 设置一个数据集,其中您将最大数量的示例数设置为52,请勿自动更新,也不要将时间作为X轴作为X轴,因为我们将自己设置此设置。

  1. 在水槽块中,您会增加每周变量。 (我认为您已经在做)

”在此处输入图像描述

  1. 创建一个hashmap。我建议您将本周用作关键和死亡人数作为价值(您的问题中的相反方式)

“在此处输入映像说明”

  1. 然后您有一些事件可以节省每周的值总和到数据集中,将变量重置为0。

然后获取数据集的总和并将其保存到LinkedHasmap。

您的每周活动的代码可以如下所示,

weekCounter ++;
deaths.add(weekCounter, weeklyDeaths);
weeklyDeaths = 0;
int tempSum = 0;
for (int i = 0; i < deaths.size(); i ++) {
    tempSum += deaths.getY(i);
}
mapOfDeathsPerWeek.put(weekCounter, tempSum);

在下面,我每周可以增加我创建的另一个变量,以跟踪几周。

It sounds like you want a moving window of the last 52 weeks.

There are a few options here but the easiest one I would suggest for you is to make use of the AnyLogic DataSet object as it already has this "keep up to a maximum number of samples" functionality which is what you need.

  1. Setup a data set where you set the maximum number of samples to keep to 52, do not update automatically and do not use time as x-axis as we will set this up ourselves.

enter image description here

  1. In the sink block you increase the weekly variable. (Which I assume you are already doing)

enter image description here

  1. Create a HashMap. I would suggest you use the week as the key and the number of deaths as the value (You had it the other way around in your question)

enter image description here

  1. Then you have some event that saves the weekly sum of values to the dataset, reset the variable to 0.

Then gets the sum of the data set and save it to the LinkedHasMap.

The code for your weekly event can be as below

weekCounter ++;
deaths.add(weekCounter, weeklyDeaths);
weeklyDeaths = 0;
int tempSum = 0;
for (int i = 0; i < deaths.size(); i ++) {
    tempSum += deaths.getY(i);
}
mapOfDeathsPerWeek.put(weekCounter, tempSum);

Where weekCounter is just another variable that I created that I can increase every week to keep track of the weeks.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文