Search in sources :

Example 1 with LinePlot

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;
}
Also used : LineChartAxis(plot.LineChartAxis) LinePlot(plot.LinePlot) JEasyFrame(utilities.JEasyFrame) LineChart(plot.LineChart)

Example 2 with LinePlot

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");
}
Also used : StatSummary(utilities.StatSummary) LineChartAxis(plot.LineChartAxis) LinePlot(plot.LinePlot) JEasyFrame(utilities.JEasyFrame) ArrayList(java.util.ArrayList) LineGroup(plot.LineGroup) LineChart(plot.LineChart)

Example 3 with LinePlot

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;
}
Also used : StateObservation(core.game.StateObservation) LinePlot(plot.LinePlot) SpaceBattleLinkState(gvglink.SpaceBattleLinkState)

Example 4 with LinePlot

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);
}
Also used : LinePlot(plot.LinePlot) FileWriter(java.io.FileWriter) ArrayList(java.util.ArrayList) BufferedWriter(java.io.BufferedWriter) PrintWriter(java.io.PrintWriter)

Example 5 with LinePlot

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);
}
Also used : LinePlot(plot.LinePlot) ArrayList(java.util.ArrayList) EvalMaxM(evodef.EvalMaxM) NoisySolutionEvaluator(evodef.NoisySolutionEvaluator) LineChart(plot.LineChart)

Aggregations

LinePlot (plot.LinePlot)8 LineChart (plot.LineChart)5 ArrayList (java.util.ArrayList)4 LineChartAxis (plot.LineChartAxis)3 JEasyFrame (utilities.JEasyFrame)3 StateObservation (core.game.StateObservation)1 StateObservationMulti (core.game.StateObservationMulti)1 EvalMaxM (evodef.EvalMaxM)1 NoisySolutionEvaluator (evodef.NoisySolutionEvaluator)1 SpaceBattleLinkState (gvglink.SpaceBattleLinkState)1 SpaceBattleLinkStateTwoPlayer (gvglink.SpaceBattleLinkStateTwoPlayer)1 BufferedWriter (java.io.BufferedWriter)1 File (java.io.File)1 FileWriter (java.io.FileWriter)1 PrintWriter (java.io.PrintWriter)1 LineGroup (plot.LineGroup)1 StatSummary (utilities.StatSummary)1