Search in sources :

Example 1 with SimpleGA

use of ga.SimpleGA in project SimpleAsteroids by ljialin.

the class TestEAGraphRunTrials method main.

public static void main(String[] args) throws Exception {
    // create and run a test
    // showing flexibility to create multiple graphs
    int nDims = 100, mValues = 2;
    double noise = 1.0;
    int nEvals = 1000;
    int nTrials = 10;
    NoisySolutionEvaluator solutionEvaluator = new EvalNoisyWinRate(nDims, mValues, noise);
    solutionEvaluator = new EvalMaxM(nDims, mValues, noise);
    // solutionEvaluator = new Eval2DNonLinear(8, noise);
    TestEAGraph tester = new TestEAGraph(solutionEvaluator, nEvals).setColor(Color.red);
    // Set up all the algorithms to test
    SimpleRMHC rmhc1 = new SimpleRMHC(1);
    SimpleRMHC rmhc5 = new SimpleRMHC(5);
    SimpleGA sga = new SimpleGA().setPopulationSize(20).setCrossoverRate(0.5);
    int windowLength = 40;
    CompactSlidingGA slidingGA = new CompactSlidingGA().setHistoryLength(windowLength);
    slidingGA.useBayesUpdates = false;
    // nDims * windowLength / 2;
    slidingGA.K = 1000;
    int nParents = 2;
    CompactBinaryGA cga = new CompactBinaryGA().setParents(nParents);
    // cga.K = nDims / 2; //  * nParents;  // setting from Jialin
    // nDims * nParents;
    cga.K = 1000;
    // cga.nToFlip = 2;
    // add them to the test list
    ArrayList<EvoAlg> evos = new ArrayList<>();
    // evos.add(new SlidingMeanEDA().setHistoryLength(30));
    evos.add(sga);
    evos.add(rmhc1);
    evos.add(rmhc5);
    evos.add(slidingGA);
    evos.add(cga);
    nParents = 20;
    CompactBinaryGA cga2 = new CompactBinaryGA().setParents(nParents);
    cga2.K = nDims * nParents * 2;
    // evos.add(cga2);
    // evos.add(rmhc1);
    // evos.add(rmhc5);
    // evos.add(new SlidingMeanEDA().setHistoryLength(30));
    Color[] colors = { Color.red, Color.green, Color.blue, Color.yellow, Color.cyan, Color.pink, Color.magenta };
    LineChart lineChart = new LineChart().setTitle(String.format("Noisy OneMax, %d dimensions", nDims));
    lineChart.setXLabel("Fitness Evaluations").setYLabel("Noise-Free Fitness");
    lineChart.xAxis = new LineChartAxis(new double[] { 0, 200, 400, 600, 800, 1000 });
    lineChart.yAxis = new LineChartAxis(new double[] { 40, 50, 60, 70, 80, 90, 100 });
    for (int i = 0; i < evos.size(); i++) {
        ElapsedTimer elapsedTimer = new ElapsedTimer();
        tester.setColor(colors[i]);
        TestEvoResults results = tester.runTrials(evos.get(i), nTrials);
        System.out.println("Tested: " + evos.get(i).getClass().getName());
        System.out.println(results.trueOpt);
        System.out.println(elapsedTimer);
        System.out.println();
        lineChart.addLines(results.linePlots);
        lineChart.addLineGroup(results.getLineGroup().setColor(colors[i]).setName(results.name));
    }
    LineGroup pVec = new LineGroup().setName("P-Vec").setColor(Color.white);
    for (ArrayList<Double> extra : extras) {
        // lineChart.addLine(new LinePlot().setColor(Color.white).setData(extra));
        pVec.add(extra);
    }
    // lineChart.addLineGroup(pVec);
    new JEasyFrame(lineChart, "Fitness Evolution");
    String dir = "results/javares/sweda/";
    // String filename = "resultsOneMax.png";
    String filename = "resultsOneMaxPVec.png";
    lineChart.saveImage(dir, filename);
}
Also used : SimpleGA(ga.SimpleGA) LineChartAxis(plot.LineChartAxis) ArrayList(java.util.ArrayList) CompactSlidingGA(ntuple.CompactSlidingGA) SimpleRMHC(ga.SimpleRMHC) CompactBinaryGA(ntuple.CompactBinaryGA) LineGroup(plot.LineGroup) LineChart(plot.LineChart)

Example 2 with SimpleGA

use of ga.SimpleGA in project SimpleAsteroids by ljialin.

the class GameRunnerTest method main.

public static void main(String[] args) {
    GameState.includeBuffersInScore = false;
    GameRunner gameRunner = new GameRunner().setLength(200);
    SimplePlayerInterface p1, p2;
    p1 = new RandomAgent();
    // p2 = new DoNothingAgent();
    EvoAlg evoAlg1 = new SimpleRMHC();
    // evoAlg1.mu
    int nEvals1 = 200;
    int seqLength1 = 10;
    int nEvals2 = 400;
    int seqLength2 = 5;
    SlidingMeanEDA evoAlg2 = new SlidingMeanEDA().setHistoryLength(40);
    SimpleGA simpleGA = new SimpleGA().setPopulationSize(20);
    EvoAgent evoAgent1 = new EvoAgent().setEvoAlg(evoAlg1, nEvals1).setSequenceLength(seqLength1);
    evoAgent1.setUseShiftBuffer(true);
    // evoAgent1.mu
    // evoAgent1.u
    // evoAgent1.setOpponent(new RandomAgent()).setUseShiftBuffer(false);
    // evoAgent1.setOpponent(new RandomAgent());
    p1 = evoAgent1;
    SimplePlayerInterface opponentModel;
    opponentModel = new DoNothingAgent();
    opponentModel = new RandomAgent();
    // p2 = new EvoAgent().setEvoAlg(simpleGA, nEvals).setSequenceLength(seqLength).setOpponent(opponentModel);
    // p2 = new EvoAgent().setEvoAlg(evoAlg1, nEvals/2).setSequenceLength(seqLength*2).setOpponent(opponentModel);
    EvoAgent evoAgent2 = new EvoAgent().setEvoAlg(evoAlg1, nEvals2).setSequenceLength(seqLength2).setOpponent(opponentModel);
    evoAgent2.setUseShiftBuffer(true);
    p2 = evoAgent2;
    p2 = getMCTSAgent(new GameState().defaultState(), 1);
    // p2 = new RandomAgent();
    gameRunner.setPlayers(p1, p2);
    // now play a number of games and observe the outcomes
    // verbose is set to true by default so after the games have been played
    // it will report the outcomes
    int nGames = 20;
    gameRunner.playGames(nGames);
    // now access the game logs to plot the scores
    gameRunner.plotGameScores();
// System.out.println(evoAlg2.pVec);
}
Also used : SimpleGA(ga.SimpleGA) SimpleRMHC(ga.SimpleRMHC) SlidingMeanEDA(ntuple.SlidingMeanEDA) EvoAlg(evodef.EvoAlg)

Aggregations

SimpleGA (ga.SimpleGA)2 SimpleRMHC (ga.SimpleRMHC)2 EvoAlg (evodef.EvoAlg)1 ArrayList (java.util.ArrayList)1 CompactBinaryGA (ntuple.CompactBinaryGA)1 CompactSlidingGA (ntuple.CompactSlidingGA)1 SlidingMeanEDA (ntuple.SlidingMeanEDA)1 LineChart (plot.LineChart)1 LineChartAxis (plot.LineChartAxis)1 LineGroup (plot.LineGroup)1