Search in sources :

Example 1 with ConvNTuple

use of ntuple.ConvNTuple in project SimpleAsteroids by ljialin.

the class EvolveMarioLevelTest method runTrial.

// this should really take any type of EA, but at the moment it
// is restricted to the SimpleRMHC to allow a bespoke mutation operator
// to be plugged in
public double runTrial(SimpleRMHC ea, int nEvals, int[][] sample) {
    int nDims = imageWidth * imageHeight;
    int mValues = distinctValues(sample);
    System.out.println("Distinct values = " + mValues);
    ConvNTuple convNTuple = getTrainedConvNTuple(sample, mValues);
    System.out.println("nSamples: " + convNTuple.nSamples());
    // set the "clever" mutation operator
    ea.setMutator(new ConvMutator().setConvNTuple(convNTuple).setForceBorder(true));
    if (useInitialSeed) {
        ea.setInitialSeed(generateSeed(sample));
    }
    SolutionEvaluator evaluator = new EvalConvNTuple(nDims, mValues).setConvNTuple(convNTuple);
    SolutionEvaluator trainingEvaluator = new EvalConvNTuple(nDims, mValues).setConvNTuple(convNTuple);
    double fitnessFull = trainingEvaluator.evaluate(flatten(sample));
    String labelFull = String.format("Full Width Training Sample: %.6f", fitnessFull);
    LevelView.showMaze(flatten(sample), sample.length, sample[0].length, labelFull, tileColors);
    showSamples(sample, trainingEvaluator);
    evaluator.logger().setListener(this);
    int[] solution = ea.runTrial(evaluator, nEvals);
    // can set entire solution to the most likely individual
    // solution = setAll(solution, 2);
    double fitness = evaluator.evaluate(solution);
    String label = String.format("Fitness: %.6f", fitness);
    // solution = flatten(toRect())
    plotData(evaluator.logger().fa);
    LevelView.showMaze(solution, imageWidth, imageHeight, label, tileColors);
    return fitness;
}
Also used : SolutionEvaluator(evodef.SolutionEvaluator) ConvMutator(ntuple.operator.ConvMutator) ConvNTuple(ntuple.ConvNTuple)

Example 2 with ConvNTuple

use of ntuple.ConvNTuple in project SimpleAsteroids by ljialin.

the class EvolveMarioLevelTest method getTrainedConvNTuple.

public static ConvNTuple getTrainedConvNTuple(int[][] sample, int mValues) {
    int w = sample.length;
    int h = sample[0].length;
    System.out.println(w + " : " + h);
    ConvNTuple convNTuple = new ConvNTuple().setImageDimensions(w, h);
    convNTuple.setFilterDimensions(filterWidth, filterHeight);
    convNTuple.setMValues(mValues).setStride(stride);
    if (wrapAround)
        convNTuple.makeWrapAroundIndices();
    else
        convNTuple.makeIndices();
    convNTuple.reset();
    System.out.println("Address space size: " + convNTuple.addressSpaceSize());
    // System.out.println("Mean of empty summary: " + new StatSummary().mean());
    // now put some random data in to it
    ElapsedTimer timer = new ElapsedTimer();
    // 'x' is the Red Maze example explained here:
    // https://adamsmith.as/papers/wfc_is_constraint_solving_in_the_wild.pdf
    int[] x = flatten(sample);
    convNTuple.addPoint(x, 1);
    // now iterate over all the values in there
    System.out.println("Training finished: ");
    System.out.println(timer);
    // now check the klDiv
    System.out.println("Divergence = " + convNTuple.getKLDivergence(x, 1e-20));
    // // now add some random noise
    // 
    // for (int i=0; i<x.length; i++) {
    // if (random.nextDouble() < 0.01) {
    // x[i] = 0;
    // }
    // }
    // 
    // System.out.println("Divergence = " + convNTuple.getKLDivergence(x, 1e-20));
    // 
    // convNTuple.report();
    // System.out.println(timer);
    // now reset the indices to the true image size, but do not use wrap around
    convNTuple.setImageDimensions(imageWidth, imageHeight);
    if (wrapAround)
        convNTuple.makeWrapAroundIndices();
    else
        convNTuple.makeIndices();
    return convNTuple;
}
Also used : ElapsedTimer(utilities.ElapsedTimer) ConvNTuple(ntuple.ConvNTuple)

Example 3 with ConvNTuple

use of ntuple.ConvNTuple in project SimpleAsteroids by ljialin.

the class EvolvePatternTest method runTrial.

public double runTrial(SimpleRMHC ea, int nEvals) {
    ConvNTuple convNTuple = getTrainedConvNTuple();
    ea.setMutator(new ConvMutator().setConvNTuple(convNTuple));
    int nDims = imageWidth * imageHeight;
    int mValues = 3;
    SolutionEvaluator evaluator = new EvalConvNTuple(nDims, mValues).setConvNTuple(convNTuple);
    int[] solution = ea.runTrial(evaluator, nEvals);
    double fitness = evaluator.evaluate(solution);
    String label = String.format("Fitness: %.6f", fitness);
    System.out.println(label);
    System.out.println(Arrays.toString(solution));
    LevelView.showMaze(solution, imageWidth, imageHeight, label);
    new JEasyFrame(LineChart.easyPlot(evaluator.logger().fa), "Evolution of Fitness");
    return fitness;
}
Also used : SolutionEvaluator(evodef.SolutionEvaluator) ConvMutator(ntuple.operator.ConvMutator) JEasyFrame(utilities.JEasyFrame) ConvNTuple(ntuple.ConvNTuple)

Example 4 with ConvNTuple

use of ntuple.ConvNTuple in project SimpleAsteroids by ljialin.

the class PatternGenTest method main.

public static void main(String[] args) {
    int w = 4, h = 4;
    int filterWidth = 2, filterHeight = 2;
    ConvNTuple convNTuple = new ConvNTuple().setImageDimensions(w, h);
    convNTuple.setFilterDimensions(filterWidth, filterHeight);
    convNTuple.setMValues(3).setStride(1);
    convNTuple.reset();
    convNTuple.makeWrapAroundIndices();
    System.out.println("Address space size: " + convNTuple.addressSpaceSize());
    // System.out.println("Mean of empty summary: " + new StatSummary().mean());
    // now put some random data in to it
    ElapsedTimer timer = new ElapsedTimer();
    // 'x' is the Red Maze example explained here:
    // https://adamsmith.as/papers/wfc_is_constraint_solving_in_the_wild.pdf
    int[] x = { 0, 0, 0, 0, 0, 1, 1, 1, 0, 1, 2, 1, 0, 1, 1, 1 };
    // we'll use 'y' to probe the n-tuple for mean and novelty estimates
    int[] y = { 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 2, 1, 0, 1, 1, 1 };
    // give this a value of 1
    int[][] tests = { x, y };
    convNTuple.addPoint(x, 1);
    // now iterate over all the values in there
    System.out.println("Training finished: ");
    System.out.println(timer);
    convNTuple.report();
    System.out.println(timer);
    double epsilon = 1e-20;
    for (int[] test : tests) {
        System.out.println("Probe:  \t " + Arrays.toString(test));
        // System.out.println("Mean:   \t " + convNTuple.getMeanEstimate(test));
        // System.out.println("Explore:   \t " + convNTuple.getExplorationEstimate(test));
        // System.out.println("Stats:\t " + convNTuple.getNoveltyStats(test));
        System.out.println("pFit:\t " + convNTuple.getKLDivergence(test, epsilon));
        System.out.println();
    }
}
Also used : ElapsedTimer(utilities.ElapsedTimer) ConvNTuple(ntuple.ConvNTuple)

Example 5 with ConvNTuple

use of ntuple.ConvNTuple in project SimpleAsteroids by ljialin.

the class EvolvePatternTest method getTrainedConvNTuple.

public static ConvNTuple getTrainedConvNTuple() {
    int w = 4, h = 4;
    int filterWidth = 2, filterHeight = 2;
    ConvNTuple convNTuple = new ConvNTuple().setImageDimensions(w, h);
    convNTuple.setFilterDimensions(filterWidth, filterHeight);
    convNTuple.setMValues(3).setStride(1);
    convNTuple.makeWrapAroundIndices();
    convNTuple.reset();
    System.out.println("Address space size: " + convNTuple.addressSpaceSize());
    // System.out.println("Mean of empty summary: " + new StatSummary().mean());
    // now put some random data in to it
    ElapsedTimer timer = new ElapsedTimer();
    // 'x' is the Red Maze example explained here:
    // https://adamsmith.as/papers/wfc_is_constraint_solving_in_the_wild.pdf
    int[] x = { 0, 0, 0, 0, 0, 1, 1, 1, 0, 1, 2, 1, 0, 1, 1, 1 };
    convNTuple.addPoint(x, 1);
    // now iterate over all the values in there
    System.out.println("Training finished: ");
    System.out.println(timer);
    // convNTuple.report();
    // System.out.println(timer);
    // now reset the indices to the true image size, but do not use wrap around
    convNTuple.setImageDimensions(imageWidth, imageHeight);
    convNTuple.makeWrapAroundIndices();
    return convNTuple;
}
Also used : ElapsedTimer(utilities.ElapsedTimer) ConvNTuple(ntuple.ConvNTuple)

Aggregations

ConvNTuple (ntuple.ConvNTuple)5 ElapsedTimer (utilities.ElapsedTimer)3 SolutionEvaluator (evodef.SolutionEvaluator)2 ConvMutator (ntuple.operator.ConvMutator)2 JEasyFrame (utilities.JEasyFrame)1