Search in sources :

Example 6 with LinePlot

use of plot.LinePlot 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 7 with LinePlot

use of plot.LinePlot in project SimpleAsteroids by ljialin.

the class TestEAGraph method runTrial.

public ArrayList<Double> runTrial(EvoAlg ea) {
    evaluator.reset();
    int[] solution = ea.runTrial(evaluator, nFitnessEvals);
    // System.out.println("Solution: " + Arrays.toString(solution) + " : " + solutionEvaluator.trueFitness(solution));
    trueFit.add(evaluator.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 : evaluator.logger().bestYetSolutions) {
        noiseFree.add(evaluator.trueFitness(p));
    }
    linePlots.add(new LinePlot().setData(noiseFree).setColor(color));
    if (evaluator.isOptimal(solution)) {
        nTrueOpt.add(1);
    }
    return noiseFree;
}
Also used : LinePlot(plot.LinePlot) ArrayList(java.util.ArrayList)

Example 8 with LinePlot

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

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