GenericValueFunction.java
Home
/
bdi-jade-test /
src /
br /
ufrgs /
inf /
bdi4jade /
examples /
planselection /
GenericValueFunction.java
/*
* Created on 30 Jul 2011 20:14:49
*/
package br.ufrgs.inf.bdi4jade.examples.planselection;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
/**
* @author ingrid
*
*/
public class GenericValueFunction<T> {
private Double average;
private Pair<Double> minMax;
private Double standardDeviation;
private Double total;
private Map<T, Double> values;
public GenericValueFunction() {
this.values = new HashMap<T, Double>();
this.minMax = null;
this.total = null;
this.average = null;
this.standardDeviation = null;
}
public synchronized void addValue(T key, Double value) {
this.values.put(key, value);
if (minMax == null) {
this.minMax = new Pair<Double>(value, value);
} else {
if (minMax.getValue1() > value)
minMax.setValue1(value);
if (minMax.getValue2() < value)
minMax.setValue2(value);
}
total = null;
average = null;
standardDeviation = null;
}
private synchronized void calculateAverage() {
average = (values.size() == 0) ? null : getTotal() / values.size();
}
private synchronized void calculateStandardDeviation() {
Double variance = getVariance();
standardDeviation = (variance == null) ? null : Math.pow(variance, 0.5);
}
/**
* @return the average
*/
public synchronized Double getAverage() {
if (average == null)
calculateAverage();
return average;
}
public int getCount() {
return values.size();
}
public Double getMax() {
return minMax == null ? null : minMax.getValue2();
}
public Double getMin() {
return minMax == null ? null : minMax.getValue1();
}
/**
* @return the standardDeviation
*/
public synchronized Double getStandardDeviation() {
if (standardDeviation == null)
calculateStandardDeviation();
return standardDeviation;
}
/**
* @return the total
*/
public synchronized Double getTotal() {
if (total == null) {
this.total = 0.0;
for (Double value : values.values()) {
total += value;
}
}
return total;
}
public Double getValue(T key) {
return values.get(key);
}
public synchronized Double getVariance() {
long n = 0;
double mean = 0;
double s = 0.0;
for (Double x : values.values()) {
n++;
double delta = x - mean;
mean += delta / n;
s += delta * (x - mean);
}
// if you want to calculate std deviation
// of a sample change this to (s/(n-1))
return n == 0 ? null : (s / n);
}
public Set<T> keySet() {
return values.keySet();
}
public String stats() {
StringBuffer sb = new StringBuffer();
sb.append("Total = ").append(getTotal()).append("\n");
sb.append("Count = ").append(getCount()).append("\n");
sb.append("Min = ").append(getMin()).append("\n");
sb.append("Max = ").append(getMax()).append("\n");
sb.append("Average = ").append(getAverage()).append("\n");
sb.append("Standard Deviation = ").append(getStandardDeviation());
return sb.toString();
}
public String toString() {
StringBuffer sb = new StringBuffer();
for (T key : values.keySet()) {
sb.append("R(").append(key).append(") = ").append(values.get(key))
.append("\n");
}
sb.append(stats());
return sb.toString();
}
public String toStringTab() {
StringBuffer sb = new StringBuffer();
sb.append("Key\tValue\n");
for (T key : values.keySet()) {
sb.append(key).append("\t").append(values.get(key)).append("\n");
}
sb.append(stats());
return sb.toString();
}
}