Search in sources :

Example 6 with LineChart

use of plot.LineChart in project SimpleAsteroids by ljialin.

the class HyperParamTuneRunner method plotConvergence.

private void plotConvergence(EvolutionLogger logger, int[] solution) {
    LineChart lineChart = new LineChart().setBG(Color.gray);
    lineChart.plotBG = Color.white;
    // now the plots we will add are as follows
    ArrayList<Double> bestMatches = new ArrayList<>();
    ArrayList<Double> bestCumulative = new ArrayList<>();
    ArrayList<Double> sampleMatches = new ArrayList<>();
    ArrayList<Double> sampleCumulative = new ArrayList<>();
    StatSummary cumul = new StatSummary();
    for (int[] best : logger.bestYetSolutions) {
        double x = match(best, solution);
        bestMatches.add(x);
        cumul.add(x);
        bestCumulative.add(cumul.mean());
    }
    StatSummary sample = new StatSummary();
    for (int[] sol : logger.solutions) {
        double x = match(sol, solution);
        sampleMatches.add(x);
        sample.add(x);
        sampleCumulative.add(sample.mean());
    }
    // LinePlot matchPlot = new LinePlot().
    LineGroup lgMatch = new LineGroup().setName("Best Match").setColor(Color.red).add(bestMatches);
    LineGroup lgCumul = new LineGroup().setName("Best Cumul").setColor(Color.black).add(bestCumulative);
    lineChart.addLineGroup(lgMatch);
    lineChart.addLineGroup(lgCumul);
    LineGroup lgSampleMatch = new LineGroup().setName("Sample Match").setColor(Color.green).add(sampleMatches);
    LineGroup lgSampleCumul = new LineGroup().setName("Sample Cumul").setColor(Color.magenta).add(sampleCumulative);
    lineChart.addLineGroup(lgSampleMatch);
    lineChart.addLineGroup(lgSampleCumul);
    lineChart.setXLabel("Iteration");
    lineChart.setYLabel("Candidate == solution");
    lineChart.yAxis = new LineChartAxis(new double[] { 0, 1 });
    lineChart.xAxis = new LineChartAxis(new double[] { 0, bestMatches.size() / 2, bestMatches.size() });
    new JEasyFrame(lineChart, "NTBEA Best Guess Convergence");
}
Also used : StatSummary(utilities.StatSummary) LineChartAxis(plot.LineChartAxis) JEasyFrame(utilities.JEasyFrame) ArrayList(java.util.ArrayList) LineGroup(plot.LineGroup) LineChart(plot.LineChart)

Example 7 with LineChart

use of plot.LineChart in project SimpleAsteroids by ljialin.

the class EvolveMarioLevelTest method plotData.

private void plotData(ArrayList<Double> data) {
    LineChart lineChart = LineChart.easyPlot(data);
    int mid = (data.size() - 1) / 2;
    int end = data.size() - 2;
    System.out.println("Endpoint: " + end);
    lineChart.xAxis = new LineChartAxis(new double[] { 0, mid, end });
    StatSummary ss = new StatSummary().add(data);
    lineChart.yAxis = new LineChartAxis(new double[] { ss.min(), ss.max() });
    lineChart.title = "Evolution of Fitness";
    lineChart.setXLabel("Iterations").setYLabel("Fitness");
    new JEasyFrame(lineChart, "KL-Based PCG");
}
Also used : LineChartAxis(plot.LineChartAxis) StatSummary(utilities.StatSummary) JEasyFrame(utilities.JEasyFrame) LineChart(plot.LineChart)

Example 8 with LineChart

use of plot.LineChart 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)

Example 9 with LineChart

use of plot.LineChart in project SimpleAsteroids by ljialin.

the class GameActionSpaceAdapterMulti method evaluate.

@Override
public double evaluate(int[] actions) {
    // take a copy of the current game state and accumulate the score as we go along
    // System.out.println("Checking action length: " + actions.length + " : " + sequenceLength);
    // System.out.println("PLayer id: " + playerID);
    StateObservationMulti 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(playerID);
    double discount = 1.0;
    double denom = 0;
    double discountedTot = 0;
    double total = 0;
    // need to do the visual stuff here ...
    LinePlot linePlot = null;
    if (visual) {
        if (lineChart == null) {
            lineChart = new LineChart().setBG(Color.gray);
            lineChart.xAxis = new LineChartAxis(new double[] { 0, sequenceLength / 2, sequenceLength });
            lineChart.yAxis = new LineChartAxis(new double[] { -50, -25, 0, 25, 50 });
            lineChart.plotBG = Color.white;
            lineChart.setYLabel("Score");
            lineChart.setXLabel("Rollout depth");
            frame = new JEasyFrame(lineChart, "Score versus depth");
        }
        float grey = (nEvals % 100) / 150.0f;
        // add in a zero for the first element of the plot, since there
        // will be zero difference before any action has been taken
        linePlot = new LinePlot().setColor(new Color(grey, grey, grey));
    // linePlot = new LinePlot().setColor(Color.red);
    }
    // deltas.add(0);
    for (int i = 0; i < actions.length; i++) {
        // Note here that we need to look at the advance method which takes multiple players
        // hence an array of actions
        // the idea is that we'll pad out the
        int myAction = actions[i];
        int opAction = random.nextInt(obs.getAvailableActions(opponentID).size());
        // opAction = AsteroidsGameState.doNothing;
        Types.ACTIONS[] acts = new Types.ACTIONS[2];
        acts[playerID] = gvgaiActions[myAction];
        acts[opponentID] = gvgaiActions[opAction];
        for (int k = 0; k < actionRepeat; k++) {
            obs.advance(acts);
        }
        discountedTot += discount * (obs.getGameScore(playerID) - initScore);
        if (useHeuristic && obs instanceof SpaceBattleLinkStateTwoPlayer) {
            SpaceBattleLinkStateTwoPlayer state = (SpaceBattleLinkStateTwoPlayer) obs;
            discountedTot += state.getHeuristicScore();
        }
        denom += discount;
        discount *= discountFactor;
        if (linePlot != null) {
            // linePlot.add(discountedTot);
            double delta = obs.getGameScore((playerID)) - initScore;
            linePlot.add(delta);
            deltas.add(delta);
        }
    }
    if (visual) {
        linePlots.add(linePlot);
    }
    nEvals++;
    double delta;
    if (useDiscountFactor) {
        delta = discountedTot / denom;
    } else {
        delta = obs.getGameScore(playerID) - initScore;
    }
    delta += noiseLevel * random.nextGaussian();
    logger.log(delta, actions, false);
    return delta;
}
Also used : LineChartAxis(plot.LineChartAxis) LinePlot(plot.LinePlot) JEasyFrame(utilities.JEasyFrame) StateObservationMulti(core.game.StateObservationMulti) LineChart(plot.LineChart) SpaceBattleLinkStateTwoPlayer(gvglink.SpaceBattleLinkStateTwoPlayer)

Example 10 with LineChart

use of plot.LineChart in project SimpleAsteroids by ljialin.

the class TestEASimple method main.

public static void main(String[] args) {
    for (int w = 30; w <= 30; w = w + 10) {
        outputName = "data/noisefree_w" + w + ".dat";
        File f = new File(outputName);
        if (f.exists() && !f.isDirectory()) {
            f.delete();
        }
        // run configuration for an experiment
        // todo need an easy and general way to log the best solution yet
        // just add a new log method to the logger
        useFirstHit = false;
        // DefaultMutator.flipAtLeastOneValueDefault = true;
        // DefaultMutator.defaultPointProb = 1.0;
        // select which one to use
        // solutionEvaluator = new EvalMaxM(nDims, mValues, noise);
        solutionEvaluator = new EvalNoisyWinRate(nDims, mValues, noise);
        // solutionEvaluator = new Eval2DNonLinear(8, noise);
        System.out.println("Running experiment with following settings:");
        System.out.println("Solution evaluator: " + solutionEvaluator.getClass());
        System.out.format("Use first hitting time    :\t %s\n", useFirstHit);
        System.out.format("RMHC: (flip at least one) :\t %s\n", DefaultMutator.flipAtLeastOneValueDefault);
        System.out.format("Point mutation probability:\t %.4f\n", DefaultMutator.defaultPointProb / nDims);
        ElapsedTimer t = new ElapsedTimer();
        // StatSummary nt = testNTupleBanditEA(nTrialsNTupleBanditEA);
        // System.out.println(t);
        // DefaultMutator.totalRandomChaosMutation = true;
        int defaultK = 2000;
        CompactBinaryGA cga = new CompactBinaryGA(defaultK);
        cga.nParents = 10;
        // each time we run a test, we want to get
        // the way the fitness evolves over time
        // hence we need to get a list of arrays for each experiment
        // have an alpha less than 1 to be able to spot overlapping lines more easily
        float alpha = 0.8f;
        // lineColor = new Color(1f, 1f, 0, alpha);
        // testEvoAlg(new SimpleRMHC());
        // lineColor = new Color(0f, 1f, 1, alpha);
        // testEvoAlg(cga);
        // 
        // lineColor = new Color(1f, 0f, 1, alpha);
        // testEvoAlg(new CompactSlidingGA(2000).setHistoryLength(w));
        // lineColor = Color.getHSBColor(0.7f, 1, 1);
        // lineColor = Color.red;
        // testEvoAlg(new CompactSlidingModelGA().setHistoryLength(w));
        // lineColor = Color.getHSBColor(0.5f, 1, 1);
        lineColor = Color.green;
        // testEvoAlg(new NTupleBanditEA());
        // lineColor = Color.getHSBColor(0.3f, 1, 1);
        lineColor = Color.blue;
        testEvoAlg(new SlidingMeanEDA().setHistoryLength(w));
        // testBanditEA();
        System.out.println(t);
        LineChart lineChart = new LineChart();
        for (LinePlot line : linePlots) {
            lineChart.addLine(line);
        }
        // add a linear plot
        // LinePlot linear = new LinePlot();
        // linear.setColor(Color.white);
        // for (int i = 0; i < nDims; i++)
        // linear.add(solutionEvaluator.optimalIfKnown()  * (0.5 + i / (2.0 * nDims)));
        // lineChart.addLine(linear);
        new JEasyFrame(lineChart, "Evolution Traces");
    }
}
Also used : LinePlot(plot.LinePlot) File(java.io.File) LineChart(plot.LineChart)

Aggregations

LineChart (plot.LineChart)10 LineChartAxis (plot.LineChartAxis)7 JEasyFrame (utilities.JEasyFrame)7 ArrayList (java.util.ArrayList)5 LinePlot (plot.LinePlot)5 LineGroup (plot.LineGroup)4 StatSummary (utilities.StatSummary)4 StateObservationMulti (core.game.StateObservationMulti)1 EvalMaxM (evodef.EvalMaxM)1 NoisySolutionEvaluator (evodef.NoisySolutionEvaluator)1 SimpleGA (ga.SimpleGA)1 SimpleRMHC (ga.SimpleRMHC)1 SpaceBattleLinkStateTwoPlayer (gvglink.SpaceBattleLinkStateTwoPlayer)1 File (java.io.File)1 TreeSet (java.util.TreeSet)1 IntArrayPattern (ntbea.IntArrayPattern)1 NTuple (ntbea.NTuple)1 CompactBinaryGA (ntuple.CompactBinaryGA)1 CompactSlidingGA (ntuple.CompactSlidingGA)1