示例#1
0
  /** test modifying a word by deleting from it */
  public void testModifyByDeleting() {
    Config config = new Config();
    config.population_max_generations = 1000;
    config.population_max_size = 10;

    Data data = new Data();
    data.createRandom(5, 5);

    Program program = new Program(config, data);

    program.words = "ADD:VAR_4;DIVIDE:VAR_4;DIVIDE:VAR_2;SUBTRACT:VAR_4;";
    String[] wordList =
        program.createWordListFromWords("ADD:VAR_4;DIVIDE:VAR_4;DIVIDE:VAR_2;SUBTRACT:VAR_4;");
    program.modifyByDeleting(0, wordList);
    assertEquals(program.words, "DIVIDE:VAR_4;DIVIDE:VAR_2;SUBTRACT:VAR_4;");

    program.words = "ADD:VAR_4;DIVIDE:VAR_4;DIVIDE:VAR_2;SUBTRACT:VAR_4;";
    wordList =
        program.createWordListFromWords("ADD:VAR_4;DIVIDE:VAR_4;DIVIDE:VAR_2;SUBTRACT:VAR_4;");
    program.modifyByDeleting(2, wordList);
    assertEquals(program.words, "ADD:VAR_4;DIVIDE:VAR_4;SUBTRACT:VAR_4;");

    program.words = "ADD:VAR_4;DIVIDE:VAR_4;DIVIDE:VAR_2;SUBTRACT:VAR_4;";
    wordList =
        program.createWordListFromWords("ADD:VAR_4;DIVIDE:VAR_4;DIVIDE:VAR_2;SUBTRACT:VAR_4;");
    program.modifyByDeleting(3, wordList);
    assertEquals(program.words, "ADD:VAR_4;DIVIDE:VAR_4;DIVIDE:VAR_2;");

    program.words = "ADD:VAR_4;DIVIDE:VAR_4;DIVIDE:VAR_2;SUBTRACT:VAR_4;";
    wordList =
        program.createWordListFromWords("ADD:VAR_4;DIVIDE:VAR_4;DIVIDE:VAR_2;SUBTRACT:VAR_4;");
    program.modifyByDeleting(4, wordList);
    assertEquals(program.words, "ADD:VAR_4;DIVIDE:VAR_4;DIVIDE:VAR_2;SUBTRACT:VAR_4;");
  }
示例#2
0
  /** test modifying the config */
  public void testModifyConfig() {
    Config config = new Config();
    config.population_max_generations = 1000;
    config.population_max_size = 10;

    Data data = new Data();
    data.createRandom(5, 5);

    Population population = new Population(config, data);
    population.programs[0].score = 10000;
    population.topScore = 0;
    population.modifyConfig(50000);
    assertTrue(true);
  }
示例#3
0
  /** setup a population */
  public void testSomeMethod() {
    Config config = new Config();
    config.population_max_generations = 1000;
    config.population_max_size = 10;

    Data data = new Data();
    data.createRandom(5, 5);

    Population population = new Population(config, data);

    config.program_success_score = config.program_initial_score;
    population = new Population(config, data);
    assertTrue(true);
  }
示例#4
0
  /** test the output to JavaScript */
  public void testRenderAsJS() {
    Config config = new Config();
    config.population_max_generations = 1000;
    config.population_max_size = 10;

    Data data = new Data();
    data.createRandom(5, 5);

    Population population = new Population(config, data);
    population.modifyConfig(50000);
    population.topScore = 0;
    population.programs[0].words =
        "ADD_CONSTANT:1;SUBTRACT_CONSTANT:1;MULTIPLY_CONSTANT:1;DIVIDE_CONSTANT:1;ADD:VAR_1;SUBTRACT:VAR_1;MULTIPLY:VAR_1;DIVIDE:VAR_1;";
    population.renderAsJS();
    assertTrue(true);
  }
示例#5
0
  /** test the copy of one population to another */
  public void testShallowCopy() {
    System.out.println("shallowCopy");

    Config config = new Config();
    config.population_max_generations = 1000;
    config.population_max_size = 10;

    Data data = new Data();
    data.createRandom(5, 5);

    Program programFrom = new Program(config, data);

    Program programTo = Program.shallowCopy(programFrom, config, data);

    assertEquals(programFrom.words, programTo.words);
    assertEquals(programFrom.score, programTo.score);
  }
示例#6
0
  /** test a program by modifying a word */
  public void testModifyByChanging() {
    System.out.println("modifyByChanging");

    Config config = new Config();
    config.population_max_generations = 1000;
    config.population_max_size = 10;

    Data data = new Data();
    data.createRandom(5, 5);

    Program program = new Program(config, data);

    program.words = "ADD:VAR_4;DIVIDE:VAR_4;DIVIDE:VAR_2;SUBTRACT:VAR_4;";
    String[] wordList =
        program.createWordListFromWords("ADD:VAR_4;DIVIDE:VAR_4;DIVIDE:VAR_2;SUBTRACT:VAR_4;");
    program.modifyByChanging(0, wordList, "CHANGE_TO");
    assertEquals(program.words, "CHANGE_TO;DIVIDE:VAR_4;DIVIDE:VAR_2;SUBTRACT:VAR_4;");

    program.words = "ADD:VAR_4;DIVIDE:VAR_4;DIVIDE:VAR_2;SUBTRACT:VAR_4;";
    wordList =
        program.createWordListFromWords("ADD:VAR_4;DIVIDE:VAR_4;DIVIDE:VAR_2;SUBTRACT:VAR_4;");
    program.modifyByChanging(1, wordList, "CHANGE_TO");
    assertEquals(program.words, "ADD:VAR_4;CHANGE_TO;DIVIDE:VAR_2;SUBTRACT:VAR_4;");

    program.words = "ADD:VAR_4;DIVIDE:VAR_4;DIVIDE:VAR_2;SUBTRACT:VAR_4;";
    wordList =
        program.createWordListFromWords("ADD:VAR_4;DIVIDE:VAR_4;DIVIDE:VAR_2;SUBTRACT:VAR_4;");
    program.modifyByChanging(3, wordList, "CHANGE_TO");
    assertEquals(program.words, "ADD:VAR_4;DIVIDE:VAR_4;DIVIDE:VAR_2;CHANGE_TO;");

    program.words = "ADD:VAR_4;DIVIDE:VAR_4;DIVIDE:VAR_2;SUBTRACT:VAR_4;";
    wordList =
        program.createWordListFromWords("ADD:VAR_4;DIVIDE:VAR_4;DIVIDE:VAR_2;SUBTRACT:VAR_4;");
    program.modifyByChanging(4, wordList, "CHANGE_TO");
    assertEquals(program.words, "ADD:VAR_4;DIVIDE:VAR_4;DIVIDE:VAR_2;SUBTRACT:VAR_4;");

    program.words = "ADD:VAR_4;DIVIDE:VAR_4;DIVIDE:VAR_2;SUBTRACT:VAR_4;";
    wordList =
        program.createWordListFromWords("ADD:VAR_4;DIVIDE:VAR_4;DIVIDE:VAR_2;SUBTRACT:VAR_4;");
    program.modifyByChanging(-1, wordList, "CHANGE_TO");
    assertEquals(program.words, "ADD:VAR_4;DIVIDE:VAR_4;DIVIDE:VAR_2;SUBTRACT:VAR_4;");
  }
示例#7
0
  /** test the evaluation function */
  public void testEval() {
    System.out.println("eval");

    Data data = new Data();
    data.createSequential(2, 5);

    Config config = new Config();
    config.population_max_generations = 1;
    config.population_max_size = 1;

    Program program = new Program(config, data);

    program.words = "ADD:VAR_1;";
    program.eval();
    assertEquals(12.0F, program.score);

    program.words = "ADD:VAR_1;ADD:VAR_3;";
    program.eval();
    assertEquals(8.0F, program.score);
  }
示例#8
0
  /** test all combinations of words */
  public void testScoreWord() {
    System.out.println("scoreWord");

    Data data = new Data();
    data.createSequential(1, 5);

    Config config = new Config();
    config.population_max_generations = 1;
    config.population_max_size = 1;

    Program program = new Program(config, data);

    assertEquals(
        4.0F,
        program.scoreWord(Program.createCleanWordsFromWords("ADD:VAR_4;"), data.inputData[0]));
    assertEquals(
        4.0F,
        program.scoreWord(
            Program.createCleanWordsFromWords("ADD:VAR_4;ADD:VAR_0;"), data.inputData[0]));
    assertEquals(
        11.0F,
        program.scoreWord(
            Program.createCleanWordsFromWords("ADD:VAR_4;ADD:VAR_3;ADD:VAR_0;ADD:VAR_4;"),
            data.inputData[0]));

    assertEquals(
        -4.0F,
        program.scoreWord(Program.createCleanWordsFromWords("SUBTRACT:VAR_4;"), data.inputData[0]));
    assertEquals(
        -4.0F,
        program.scoreWord(
            Program.createCleanWordsFromWords("SUBTRACT:VAR_4;SUBTRACT:VAR_0;"),
            data.inputData[0]));
    assertEquals(
        -11.0F,
        program.scoreWord(
            Program.createCleanWordsFromWords(
                "SUBTRACT:VAR_4;SUBTRACT:VAR_3;SUBTRACT:VAR_0;SUBTRACT:VAR_4;"),
            data.inputData[0]));

    assertEquals(
        0.0F,
        program.scoreWord(Program.createCleanWordsFromWords("MULTIPLY:VAR_4;"), data.inputData[0]));
    assertEquals(
        0.0F,
        program.scoreWord(
            Program.createCleanWordsFromWords("MULTIPLY:VAR_4;MULTIPLY:VAR_0;"),
            data.inputData[0]));
    assertEquals(
        0.0F,
        program.scoreWord(
            Program.createCleanWordsFromWords(
                "MULTIPLY:VAR_4;MULTIPLY:VAR_3;MULTIPLY:VAR_0;MULTIPLY:VAR_4;"),
            data.inputData[0]));
    assertEquals(
        16.0F,
        program.scoreWord(
            Program.createCleanWordsFromWords("ADD:VAR_4;MULTIPLY:VAR_4;"), data.inputData[0]));
    assertEquals(
        0.0F,
        program.scoreWord(
            Program.createCleanWordsFromWords("ADD:VAR_4;MULTIPLY:VAR_4;MULTIPLY:VAR_0;"),
            data.inputData[0]));
    assertEquals(
        28.0F,
        program.scoreWord(
            Program.createCleanWordsFromWords(
                "ADD:VAR_4;MULTIPLY:VAR_4;MULTIPLY:VAR_2;SUBTRACT:VAR_4;"),
            data.inputData[0]));

    assertEquals(
        0.0F,
        program.scoreWord(Program.createCleanWordsFromWords("DIVIDE:VAR_4;"), data.inputData[0]));
    assertEquals(
        0.0F,
        program.scoreWord(
            Program.createCleanWordsFromWords("DIVIDE:VAR_4;DIVIDE:VAR_0;"), data.inputData[0]));
    assertEquals(
        0.0F,
        program.scoreWord(
            Program.createCleanWordsFromWords(
                "DIVIDE:VAR_4;DIVIDE:VAR_3;DIVIDE:VAR_0;DIVIDE:VAR_4;"),
            data.inputData[0]));
    assertEquals(
        2.0F,
        program.scoreWord(
            Program.createCleanWordsFromWords("ADD:VAR_4;DIVIDE:VAR_2;"), data.inputData[0]));
    assertEquals(
        0.0F,
        program.scoreWord(
            Program.createCleanWordsFromWords("ADD:VAR_4;DIVIDE:VAR_4;DIVIDE:VAR_0;"),
            data.inputData[0]));
    assertEquals(
        -3.5F,
        program.scoreWord(
            Program.createCleanWordsFromWords(
                "ADD:VAR_4;DIVIDE:VAR_4;DIVIDE:VAR_2;SUBTRACT:VAR_4;"),
            data.inputData[0]));
  }