use of plot.LinePlot in project SimpleAsteroids by ljialin.
the class GameRunner method plotGameScores.
public LineChart plotGameScores() {
LineChart lineChart = new LineChart();
for (GameLog gameLog : gameLogs) {
LinePlot linePlot = new LinePlot().setData(gameLog.scores).setRandomColor();
lineChart.addLine(linePlot);
}
lineChart.xAxis = new LineChartAxis(new double[] { 0, nSteps / 2, nSteps });
lineChart.setXLabel("Game Tick");
lineChart.setYLabel("Score");
String title = String.format("%s (%d) v. %s (%d)", p1.toString(), p1Wins, p2.toString(), p2Wins);
lineChart.setTitle(title);
lineChart.bg = Color.gray;
lineChart.plotBG = Color.white;
double[] scoreTics = new double[] { scores.min(), 0, scores.max() };
// double[] scoreTics = new double[]{-100, 0, 1000, 2000 }; // scores.max()};
lineChart.yAxis = new LineChartAxis(scoreTics);
new JEasyFrame(lineChart, "Game Scores");
return lineChart;
}
use of plot.LinePlot in project SimpleAsteroids by ljialin.
the class SortedBernoulliTest method main.
public static void main(String[] args) {
LineChart lineChart = new LineChart();
LinePlot linePlot = new LinePlot();
LineGroup lineGroup = new LineGroup();
int nTrials = 100;
int nGroups = 288;
double pWin = 0.5;
ArrayList<StatSummary> stats = new ArrayList<>();
for (int i = 0; i < nGroups; i++) {
StatSummary ss = new StatSummary();
stats.add(ss);
for (int j = 0; j < nTrials; j++) {
int result = Math.random() < pWin ? 1 : -1;
ss.add(result);
}
}
Collections.sort(stats);
lineGroup.stats = stats;
lineChart.addLineGroup(lineGroup);
lineChart.bg = Color.gray;
lineChart.plotBG = Color.white;
lineChart.setTitle("Sorted Coin Toss Trials, n=100, p=0.5, error bars 1 std-err");
lineChart.setYLabel("Fitness");
lineChart.setXLabel("Trial");
lineChart.stdErrs = 1.0;
lineChart.xAxis = new LineChartAxis(new double[] { 0, 144, 288 });
lineChart.yAxis = new LineChartAxis(new double[] { -1, 0, 0.8 });
new JEasyFrame(lineChart, "Coin toss");
}
use of plot.LinePlot in project SimpleAsteroids by ljialin.
the class GameActionSpaceAdapter method evaluate.
@Override
public double evaluate(int[] actions) {
// take a copy of the current game state and accumulate the score as we go along
StateObservation obs = stateObservation.copy();
// note the score now - for normalisation reasons
// we wish to track the change in score, not the absolute score
double initScore = obs.getGameScore();
double discount = 1.0;
double denom = 0;
double discountedTot = 0;
// need to do the visual stuff here ...
LinePlot linePlot = null;
if (visual) {
float grey = (nEvals % 100) / 100;
linePlot = new LinePlot().setColor(new Color(grey, grey, grey));
}
for (int i = 0; i < sequenceLength; i++) {
obs.advance(gvgaiActions[actions[i]]);
discountedTot += discount * (obs.getGameScore() - initScore);
if (useHeuristic && obs instanceof SpaceBattleLinkState) {
SpaceBattleLinkState state = (SpaceBattleLinkState) obs;
discountedTot += state.getHeuristicScore();
}
denom += discount;
discount *= discountFactor;
if (linePlot != null) {
linePlot.add(discountedTot + Math.random() * 0);
}
}
if (visual) {
linePlots.add(linePlot);
}
nEvals++;
double delta;
if (useDiscountFactor) {
delta = discountedTot / denom;
} else {
delta = obs.getGameScore() - initScore;
}
delta += noiseLevel * random.nextGaussian();
logger.log(delta, actions, false);
return delta;
}
use of plot.LinePlot in project SimpleAsteroids by ljialin.
the class TestEASimple method runTrial.
static double runTrial(EvoAlg ea, StatSummary nTrueOpt, StatSummary trueFit) {
// grab from static var for now
NoisySolutionEvaluator evaluator = solutionEvaluator;
evaluator.reset();
int[] solution = ea.runTrial(evaluator, nFitnessEvals);
// System.out.println("Solution: " + Arrays.toString(solution) + " : " + solutionEvaluator.trueFitness(solution));
trueFit.add(solutionEvaluator.trueFitness(solution));
// linePlots.add(new LinePlot().setData(solutionEvaluator.logger().fa).setColor(lineColor));
// ok, instead look at the true fitnesses of the evaluated solutions
ArrayList<Double> noiseFree = new ArrayList<>();
// System.out.println("Best yet solutions length: " + solutionEvaluator.logger().bestYetSolutions.size());
for (int[] p : solutionEvaluator.logger().bestYetSolutions) {
noiseFree.add(solutionEvaluator.trueFitness(p));
}
// TODO: 23/06/2017 save data
try {
FileWriter fw = new FileWriter(outputName, true);
BufferedWriter bw = new BufferedWriter(fw);
PrintWriter out = new PrintWriter(bw);
for (int i = 0; i < noiseFree.size(); i++) {
out.print(noiseFree.get(i) + " ");
}
out.print("\n");
out.close();
} catch (Exception e) {
e.printStackTrace();
System.out.println("No such file exists.");
}
linePlots.add(new LinePlot().setData(noiseFree).setColor(lineColor));
if (useFirstHit && evaluator.logger().firstHit != null) {
// System.out.println("Optimal first hit?: " + evaluator.logger().firstHit);
nTrueOpt.add(evaluator.logger().firstHit);
} else if (evaluator.isOptimal(solution)) {
nTrueOpt.add(1);
}
return solutionEvaluator.trueFitness(solution);
}
use of plot.LinePlot in project SimpleAsteroids by ljialin.
the class PowerOfDifferencePairsTest method main.
// okay this is interesting: the paired idea does not work well when the
// vectors are far apart
// this might have been expected from the way that having the
// sliding history window too big causes deterioration in performance
public static void main(String[] args) {
// create the random vectors, score them
// and put them in a list
// now run an experiment each way to determine the arg max
// and then evaluate the quality of that
ScoredVectorLearner meanLearner = new MeanLearner();
ScoredVectorLearner diffLearner = new PairedDifferenceLearner();
ScoredVectorLearner[] learners = new ScoredVectorLearner[] { meanLearner, diffLearner };
int nTrials = 30;
int n = 100, m = 2;
double noise = 1.0;
NoisySolutionEvaluator evaluator = new EvalMaxM(n, m, noise);
int k = 500;
List<StatSummary> stats = new ArrayList<>();
for (ScoredVectorLearner learner : learners) {
stats.add(new StatSummary(learner.getClass().getSimpleName()));
}
LineChart lineChart = new LineChart();
for (int i = 0; i < nTrials; i++) {
// ProblemInstance problem = new ProblemInstance(n, m, k, evaluator).useRandomVecs();
ProblemInstance problem = new ProblemInstance(n, m, k, evaluator).useVecsAroundRandomPoint();
int ix = 0;
for (ScoredVectorLearner learner : learners) {
System.out.println("Testing: " + learner.getClass().getSimpleName());
int[] p = learner.learn(problem.scoredVecs, problem.evaluator);
// System.out.println(Arrays.toString(p));
System.out.println("True fitness is: " + evaluator.trueFitness(p));
stats.get(ix).add(evaluator.trueFitness(p));
System.out.println();
// now show evolution of fitness
// for (double x : learner.getFitness()) {
// System.out.println(x);
// }
Color color = ix++ % 2 == 0 ? Color.red : Color.blue;
LinePlot linePlot = new LinePlot().setData(learner.getFitness()).setColor(color);
lineChart.addLine(linePlot);
System.out.println(learner.getFitness().length);
}
}
new JEasyFrame(lineChart, "Fitness v. vectors processes");
for (StatSummary ss : stats) System.out.println(ss);
}
Aggregations