swarm-support
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Using Averager ( was Re: syntax etc.


From: pauljohn
Subject: Using Averager ( was Re: syntax etc.
Date: Fri, 12 Oct 2001 08:23:54 -0500

Thanks for asking! This reminded me how much I forget if I don't
write things down :)

Marcus made a working example of how to create an Averager
directly, it is in java here:
http://lark.cc.ukans.edu/~pauljohn/SwarmFaq/WorkingExampleCode/java/AveragerUsage.txt

Since you are writing in Obj-C, you have to look carefully, but
I think you can see the pattern.  I could probably translate
that into Obj-C if you can't figure how it works. 

I'm looking at EZGraph.m now to see how it creates Averagers,
and I think you need something like this:
 
// Need a selector for the method in your class that gives back
data.  
// Swarm has a macro to get Selectors, so suppose your class
// has "giveHappyNumber" as a method.  note HeatbugObserverSwarm
uses 
// the same M() to get a selector that gets values from agents
to
// put into the sequence.

   SEL  probeSel = M(giveHappyNumber);  

//create averager, set the width of the moving average. 
//There is a new feature for moving averages,
//and to turn that off set width = 0;
   int width=0; 

  anAverager = [Averager createBegin: getZone (self)];
  [anAverager setCollection: aCollection];
  [anAverager setWidth: width];
  [anAverager setProbedSelector: probeSel];
  anAverager = [anAverager createEnd];

Now, when you want your "anAverager" to calculate the average,
you do

   [anAverager update];

and it will do a "survey" of your collection, using the method
you told it to get a number from each element of the collection.
Use the getValue methods to get numbers out.

    double val1 = [anAverager getAverage];
    double val2 = [anAverager  getStdDev];

and so forth. You have to look inthe source code to see all the
possibilities. it is in src/analysis/Averager.h.


You have to look there because there are some changes in the
Averager class that don't show up in the old documentation. I
dont think they affect what you are doing.  Averager was always
there to help with creating graphs or summaries of collections.
Look at Heatbugs, for example, where it has the EZGraph create
an Averager. Later I created a separate class called
MovingAverage that I would use to keep just any old average, but
rather than summarizing a collection, I wanted individual agents
to use these to keep summaries of their various experiences. 
For example, a heatbug could keep an average of its heat
experiences. I could have agents create averagers, put values in
with "addValueToAverage", and get calculations out.

Then we integrated the functionality of the two classes, so now
you can create an Averager object either to summarize a
collection (as used in EZGraphs or your example) or to be used
by an agent (my new way). Furthermore, these can be moving
averagers (just remember last "width" observations) or regular
averages, for which the width is 0 (zero).
 
I did the first drafts at this, and Looking at Averager.m right
now, i see that Marcus has done a good bit of reorganization and
cleaning up.  (My code almost never has correct style and I'm
not usually smart enough to put Macros that clean up usage, so I
know it is his handiwork. There are also several related
optimizations to speed up updates that I'm sure he did). 

What separates the two uses of Averager is whether you set a
collection or not.  As in the example above, to summarize a
collection you need to set a collection and a selector to get
the data.  There is a shortcut to this from EZGraph, as in:

 [unhappyGraph createAverageSequence: "unhappiness"
                         withFeedFrom: [heatbugModelSwarm
getHeatbugList]
                         andSelector: M(getUnhappiness)];

When you create an Averager, if you don't set a collection, it
assumes you are keeping records for a single object.  Note the
key is that the Averager object is inherited from MessageProbe,
and so you tell it what method to use to get the information it
wants with the "setProbedSelector" method. Or, if you want, you
can ignore that part of the functionality and just manually
stick values into the averager whenever you want with
"addValueToAverage".  

I see now my time would have been better spent writing a couple
of working examples, and will try to remember that next time.


Holger Perlt wrote:
> 
> Hello,
> 
> I would like to ask again for the solution of my problem concerning
> the Averager. There are two problems:
> 
> (I) Using the Averager (getting the numbers  from a List):
> ***********************************************************
> 
> Inside AFonds.m (defining AFonds) I have the build routine:
> 
> - buildObjects
> {
> 
>   AvPreisList = [List create:  [self getZone]];
> 
>   AveragePreis = [Averager createBegin: [self getZone]];
>   [AveragePreis setCollection: AvPreisList];//here I would assigne the
> AvPreisList to the Averager
>   AveragePreis = [AveragePreis createEnd];
> 
>   return self;
> 
> }
> 
> The Averager is used inside getWinAvPreis:
> 
> - (double) getWinAvPreis
> {
> 
>   double ap;
>   Integer *hh;
>   ap = 0.0;
> 
>    if(Time < 10){
>     hh = [Integer create: [self getZone]];
>     [hh setNum: PreisPerAktie];
>     [AvPreisList addLast: hh];
>     ap = AvPreis;
>   }
>   else{
> 
>     [AvPreisList removeFirst];
>     hh = [Integer createBegin: [self getZone]];
>     [hh setNum: PreisPerAktie];
>     [AvPreisList addLast: hh];
> 
>     ap = [AveragePreis getAverage]; //this call is the problem:
> AvPreisList has a getNum - method
>                                         // to export its numbers; how can I 
> tell it this to AveragePreis???
>     }
> 
>         return ap;
> }
> 
> (II) Adding to the list:
> ************************
> 
> In the routine getWinAvPreis (above) I have recognized that probably no
> element
> is added with [AvPreisList addLast: hh]. Using
> 
> ap = (double)([AvPreisList getCount]);
> 
> and showing ap via EZGraph  in the ObserverSwarm  I see only zero. I.e.,
> no element has been added
> to the list?!
> 
> Best regards,
> 
> Holger
> 
> --
> PD Dr. habil. Holger Perlt


-- 
Paul E. Johnson                       email: address@hidden
Dept. of Political Science           
http://lark.cc.ukans.edu/~pauljohn
University of Kansas                  Office: (785) 864-9086
Lawrence, Kansas 66045                FAX: (785) 864-5700

                  ==================================
   Swarm-Support is for discussion of the technical details of the day
   to day usage of Swarm.  For list administration needs (esp.
   [un]subscribing), please send a message to <address@hidden>
   with "help" in the body of the message.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]