use of utilities.StatSummary in project SimpleAsteroids by ljialin.
the class NTuple method add.
public void add(int[] x, double v) {
// for each address that occurs, we're going to store something
StatSummary ss = getStatsForceCreate(x);
ss.add(v);
nSamples++;
}
use of utilities.StatSummary in project SimpleAsteroids by ljialin.
the class NTuple method getStatsForceCreate.
/**
* Get stats but force creation if it does not already exists
* @param x
* @return
*/
public StatSummary getStatsForceCreate(int[] x) {
double address = address(x);
if (ntArray != null) {
StatSummary ss = ntArray[(int) address];
if (ss == null) {
ss = new StatSummary();
nEntries++;
ntArray[(int) address] = ss;
}
return ss;
} else {
StatSummary ss = ntMap.get(address);
if (ss == null) {
ss = new StatSummary();
nEntries++;
ntMap.put(address, ss);
}
return ss;
}
}
use of utilities.StatSummary in project SimpleAsteroids by ljialin.
the class NTupleBanditEA method fitness.
StatSummary fitness(SolutionEvaluator evaluator, int[] sol) {
StatSummary ss = new StatSummary();
for (int i = 0; i < nSamples; i++) {
double fitness = evaluator.evaluate(sol);
ss.add(fitness);
}
return ss;
}
use of utilities.StatSummary in project SimpleAsteroids by ljialin.
the class NTupleSystemTest method main.
// create a way to make N-Tuple systems ...
public static void main(String[] args) {
// NTupleSystem nts = new NTupleSystem(new TenSpace(nDims, 2));
int m = 2;
FitnessSpace evalTrue = new EvalNoisyWinRate(nDims, m);
FitnessSpace evalNoisy = new EvalNoisyWinRate(nDims, m, 1.0);
NTupleSystem nts = new NTupleSystem();
nts.setSearchSpace(evalNoisy);
// nts.addTuples();
// nts.printSummaryReport();
// nts = new NTupleSystem(BattleGameSearchSpace.getSearchSpace());
// nts.addTuples();
// now add some random data
ElapsedTimer t = new ElapsedTimer();
double[] hist = new double[50];
int nReps = 1;
int nPoints = 1000;
nPoints = (int) SearchSpaceUtil.size(nts.searchSpace);
nPoints *= 100;
for (int i = 0; i < nPoints; i++) {
int[] p = SearchSpaceUtil.randomPoint(nts.searchSpace);
for (int j = 0; j < nReps; j++) {
double value = evalNoisy.evaluate(p);
// index of histogram calculation assumes that
// value is in range 0 .. 1
int ix = (int) (value * (hist.length - 1));
// System.out.println(ix) + ;
// hist[ix]++;
nts.addPoint(p, value);
}
}
// BarChart.display(hist, "Distribution");
System.out.format("Added %d points\n", nPoints);
nts.printSummaryReport();
System.out.println(t);
System.out.println("Now testing ...");
System.out.println(t);
// nPoints = 1000;
StatSummary ss = new StatSummary();
Ranker<Integer> trueRank = new Ranker<>();
Ranker<Integer> estRank = new Ranker<>();
// ensure we sample all the points in the search space when testing
nPoints = (int) SearchSpaceUtil.size(nts.searchSpace);
// the rank correlation was originally computed in-ine in the code,
// but this method has now been superceded by a separate utility class
// whose use is also demonstrated
RankCorrelation rc = new RankCorrelation();
for (int i = 0; i < nPoints; i++) {
int[] p = SearchSpaceUtil.randomPoint(nts.searchSpace);
p = SearchSpaceUtil.nthPoint(nts.searchSpace, i);
// make the value depend on a few of the indices rather than
// the actual values, just for a simple test
// System.out.println("Probing: " + Arrays.toString(p));
// Double value = nts.get(p);
Double value = nts.getMeanEstimate(p);
if (value != null) {
double trueVal = evalTrue.evaluate(p);
double diff = Math.abs(trueVal - value);
rc.add(i, value, trueVal);
ss.add(diff);
// System.out.format("%d\t %.34f\t %.4f\n", i, value, trueVal);
trueRank.add(trueVal, i);
estRank.add(value, i);
// System.out.println();
}
// System.out.println(ss);
// nts.addPoint(p, value);
// System.out.println("Returned value = " + value);
// System.out.println();
}
System.out.println();
System.out.println(ss);
System.out.println(t);
// now show the ranks
double sumSquaredDiff = 0;
for (int i = 0; i < nPoints; i++) {
// System.out.println(i + "\t " + trueRank.getRank(i) + "\t " + estRank.getRank(i));
int[] x = SearchSpaceUtil.nthPoint(nts.searchSpace, i);
// System.out.println(Arrays.toString(nts.getExplorationVector(x)) + " : " + nts.getExplorationEstimate(x));
// System.out.println();
sumSquaredDiff += sqr(trueRank.getRank(i) - estRank.getRank(i));
}
// System.out.println("diffSum = " + sumSquaredDiff);
double spearmanCoefficient = 1 - sumSquaredDiff * 6 / (nPoints * nPoints * nPoints - nPoints);
System.out.format("Spearman correlation = %.4f\n", spearmanCoefficient);
System.out.println("And the RankCorrelation utility class test: ");
double rankCorrelation = rc.rankCorrelation();
System.out.println("RC = " + rankCorrelation);
// nts.printDetailedReport();
}
use of utilities.StatSummary in project SimpleAsteroids by ljialin.
the class PatternDistribution method add.
public PatternDistribution add(Pattern p, double w) {
StatSummary ss = statMap.get(p);
if (ss == null) {
ss = new StatSummary();
statMap.put(p, ss);
}
ss.add(w);
tot += w;
return this;
}
Aggregations