0

I have an agent-based simulation where I have a collection called collection_dailyInfection which has the numbers of new infections that arise on a daily basis. The collection looks a bit like this:

  • Day 1: 0
  • Day 2: 3
  • Day 4: 3
  • Day 5: 6
  • Day 6: 1
  • . . .

I'm now trying to write a function that adds up the total number of infections on any particular day. For example: on Day 6- total infections = 0+3+3+6+1 = 13.

The syntax for calculating the sum is: double sum( collection, value ) - Returns the sum of values in the given collection.

For my particular example, this would be double sum( collection_dailyInfection, *value*), but I'm not sure what I should put in the 'value' argument. Could some help me out please?

user3qpu
  • 67
  • 1
  • 10
  • Which other calls are ok? Maybe `double sum( collection, key )` (meaning the keys should be summarized)? If so, it makes sense. – Stefan Nov 13 '17 at 13:00
  • Could you show a (part) working program, it might help identify a proper solution. – achAmháin Nov 13 '17 at 13:05

2 Answers2

2

These functions (methods) provided by AnyLogic for calculating statistics on collections use a fairly advanced feature of Java: the functional programming stuff added in Java 8. So the required syntax is not at all obvious. The main help page (AnyLogic Help --> Parameters, Variables, Collections --> Collections --> Functions to collect statistics on a collection) has a link to the UtilitiesCollection class where these methods are defined.

You have a collection collection_dailyInfection of daily infection counts; let's assume you specified this in AnyLogic as being of collection class ArrayList with elements class as int, and you used a cyclic event to add the count each simulated day.

Your sum expression should therefore be

sum( collection_dailyInfection, c -> c.doubleValue())

The c is just an arbitrary identifier for the current element that the sum is on (effectively this sum method is looping through your collection) and the -> is a special Java 8 functional programming operator. When you specify type int in AnyLogic for your collection contents, they are actually stored as Integer objects which are object versions of the int primitives. (See any Java textbook to understand this.)

Thus, each entry (Integer object) has a method doubleValue which returns the value of the integer as a double. (AnyLogic's sum function needs the 'value' bit to be a double; i.e., a real (floating point) number.)

(anupam691997's answer is a 'pure Java' solution ignoring the AnyLogic context.)

Stuart Rossiter
  • 2,432
  • 1
  • 17
  • 20
0

Value should be an integer, It will denote that day till which you want the total number of infections.

For example in this case the value of value should be 6. Since you want to the total number of infections till 6th day.

Here is a working demo of what you wanted

import java.util.*;
import java.lang.*;
import java.io.*;

class Ideone
{

    public static void main (String[] args) throws java.lang.Exception
    {
        ArrayList<Integer> list;
        Scanner sc=new Scanner(System.in);
        list=new ArrayList<Integer>();  /* this list will hold all the values*/
        int total=sc.nextInt(); /* total number of values in the list */
        int value=sc.nextInt(); /* the day till which you want the total infection */

        for(int i=0;i<total;i++)  /*taking input in the list */
            list.add(sc.nextInt());

        double sum=calculateSum(list,value); /* function to compute sum*/

        System.out.println(sum);
    }

    static double calculateSum(ArrayList<Integer> list,int value)
    {
        double sum=0.0;
        for(int j=0;j<value;j++) /* calculates sum from day 1 till value*/
            sum=sum+list.get(j);
        return sum; 
    }


}
anupam691997
  • 310
  • 2
  • 8