Rolling Averages

From Robowiki
Revision as of 14:24, 16 February 2009 by CrazyBassoonist (talk | contribs) (Grammar correction)
Jump to navigation Jump to search

Rolling Averages, also known as Moving Averages, were brought to the robocode community by Paul Evans. This kind of average allows you to keep only recent data upon averaging. Here is the first code published by Paul and still used among top bots in the rumble.

public static double rollingAvg(double value, double newEntry, double n, double weighting ) {
    return (value * n + newEntry * weighting)/(n + weighting);
} 

Here is the same implementation as used by SpareParts. It is easier to understand but slower.

class Averager {
    private ArrayList<Double> data = new ArrayList();
    public void add(double v) {
        data.add(0, (Double)v);
    }
    public void get(int dept) {
        int i = 0;
        double d;
        for (Double v : data) {
            i++;
            if (i > depth) break;
            d += (double)v;
        }
        return d/i;
    }
}

Note that code above is untested.