0

Is there a way to find the Mean of a named property in a LinkedHashMap belonging to each of a Collection of Agents, without iterating the collection?

In plain Java terms, that could be restated as "find the Mean of a property stored in a LinkedHashMap belonging to each item of a List". Indeed, it has to be done from a List, as the Agent collection has to be filtered first, based upon the value of a variable.

Duplicating the LinkedHashMap property in a standalone parameter is not an option, as there are actually 135 of them, which is one reason why they're in the LinkedHashMap, and it would therefore negate any performance benefit when compared to iterating the one for which the mean is required at any given point.

In response to Benjamin's question, here's my current code to do it manually, where sParam is the stub of the LinkedHashMap item name and iSegment is the unique:

// Iterate the Products:
for (Product oProduct : Products.findAll(
        p -> p.v_active == true
    )) {

    // Add the Segment value for this Param:
    iCountProducts++;
    dblTotal += oProduct.v_paramMap.getOrDefault(sParam +  Integer.toString(iSegment), 0.0);

}

// Calculate the mean:
dblReturn = dblTotal / iCountProducts;

I'm simply wondering if there's an averageWhere style that can be adapted to this situation. No biggie if not - the above runs 1000 times in 0.008 seconds.

Rich Harding
  • 645
  • 6
  • 14
  • Can you clarify your model structure? What agent population, where is the LHM, what is its structure, how do parameters come into play? Feel free to share a screen as well for clarity :) – Benjamin Apr 26 '20 at 17:54
  • See edit. Really not a major issue, as per the last line in the edit, just hadn't found the question posed anywhere (including searching Java solutions) and I prefer elegant to clumsy. – Rich Harding Apr 26 '20 at 19:05
  • 1
    Can't think of a better way (except using Java streams, etc. but that is not an AnyLogic issue and will likely not improve things much). There is no build-in way to do it another way, if that was the question :-) – Benjamin Apr 27 '20 at 06:48
  • I didn't think there was, just figured it was worth posing the question, as I had already refactored it to the above. – Rich Harding Apr 27 '20 at 09:25

0 Answers0