Larimow
Larimow

Reputation: 145

Accessing a continuesly updated list forwards and backwards

I have a quite special problem and it is hard to break it down to a few lines of code and a few sentences but I will try to do so. I am currently working on a software for real time optimization for alarms in process plants. The software receives new alarms from a server every time new alarms appear. Several of these alarms have the same root cause and my software analyses these alarms and groups them if they are related to each other. My problem is comparing new alarms with old ones. I store the old alarms in a global list and append new alarms to this list when they appear. The new alarms have to be analyzed whether they can be grouped with themselves and with the old alarms. The old alarms don't need to be regrouped again. As an example I have three old (o) alarms and three new ones (n). The list will look like o1 o2 o3 n1 n2 n3. Now n1 has to be compared to o1, o2, o3, n2 and n3. n2 has to be compared to o1,o2,o3 and n3 and so on.

I used the following code to do this:

    List<ALARM_ITEM> puffer = new List<ALARM_ITEM>();
            puffer.AddRange(localOldAlarmList);
            puffer.AddRange(localNewAlarmList);
            localMergedList = puffer;



    int mainListCounter = localOldAlarmList.Count;

    for (; mainListCounter < localMergedList.Count; mainListCounter++)
                {
                    /*if there are new elements just these elemnts will be used as static items*/
                    ALARM_ITEM staticAlarmItem = localMergedList[mainListCounter];

        for (int j = -1; j <= 1; j += 2)
        {

                            if (j < 0)
                                counterRunner = localOldAlarmListLength - 1;
                            else
                                counterRunner = mainListCounter + 1;



                            //Check against any ALARM_ITEM in timeframe
                            bool inTimeframe = true;

                            while (inTimeframe)
                            {

                                if ((counterRunner >= localMergedList.Count) || (counterRunner) < 0)
                                        break;

                                 ALARM_ITEM groupCandidate = new ALARM_ITEM();
                                    groupCandidate = localMergedList[counterRunner];


                                 //... several if clauses

                                MergeTwoAlarmGroups(staticAlarmItem, groupCandidate);

                                 if (j < 0)
                                        counterRunner -= 1;
                                    else
                                        counterRunner += 1;

                }
            }
        } 

Now what shall I say... I modified this approach working about 36 working hours now but still the software does just compare new alarms between each other and does not link it to the old alarms. The grouping algorithm is several pages long and I tried to break it down as far as I can. If anyone can give me an advice what I'm doing wrong I would be really glad because I'm getting mad over this problem. It's the first time I'm really stuck in this project and I'm programming this software for more than three month now.

Kind regards Larimow

Upvotes: 1

Views: 156

Answers (2)

Nevyn
Nevyn

Reputation: 2683

Ok...lets see if I can take a try, most of this will be pseudocode so here goes:

List listOfAllAlarms //a specific subset contianing a representative instance of EACH old alarm GROUP!!, and the new alarms
List listOfNewAlarms //Only the new alarms

foreach (alarm a in listOfNewAlarms)
{
    foreach (alarm b in listOfAllAlarms)
    {
        if (a.Equals(b))
            continue;

        Group = //compare a and b

        if (Group != null) //new group
            //Assign a to group b
    }
}       

The important bit there is the two groups, and the way you loop through them. In the outer list, you only look at the newest alarms. In the inner list, you compare against the global set of alarms. I would actually prefer to keep the new alarms out of the grouping set, but I do understand that you could end up with multiple alarms in the list of new alarms that all belong in one group, but its not an already existing group. If you CAN keep them separate, I would do it more like this:

List listOfAlarmGroups //a specific subset contianing a representative instance of only the already existing old alarm groups
List listOfNewAlarms //Only the new alarms

foreach (alarm a in listOfNewAlarms)
{
    Group G = null;//set -> ReSet G
    foreach (alarm b in listOfAlarmGroups)
    {
        G = //compare a and b

        if (G != null) 
            break;//found the group
    }

    if (G != null)
        //assign a to group G
    else
    {
        //create new group for a
        //add new group to listOfAlarmGroups
    }
}   

The differences between the two are that the inner loop ONLY looks at groups, but never actively compares to the other new items, but if an Item is found that doesn't group to an existing group, it gets a new group, and the NEXT alarm will be comparing to that new group along with the old ones. Same effect, slightly different algorithm.

Upvotes: 1

Katriel
Katriel

Reputation: 123772

I don't fully understand your algorithm. I would use a

Dictionary<ALARM_ITEM, HashSet<ALARM_ITEM>>

as the data structure, where the values are groups of events and the keys are representatives of each group. When a new alarm a arrives, the algorithm would be

  1. for each key k, check whether a is grouped with k
    • if so, add it to the hashset stored at k and break
  2. otherwise, create a new hashset values containing just a, and add (a, values) to the dictionary

In other words, you store the groups of alarm in a dict whose keys are "representative" alarms for the groups. To add a new alarm, you only have to compare it against the representative alarms.


This assumes that if a groups with b and b groups with c then a groups with c. If that is not the case, there isn't necessarily a consistent way of grouping the alarms. And if it is the case then the grouping relation is an equivalence, so the algorithm will work.

Upvotes: 0

Related Questions