예제 #1
0
  public BattleRunner() {
    // Enable log messages from Robocode
    RobocodeEngine.setLogMessagesEnabled(true);

    // Create the RobocodeEngine
    engine = new RobocodeEngine(new java.io.File("C:/robocode")); // Run from C:/robocode

    // Add our own battle listener to the RobocodeEngine
    engine.addBattleListener(battleObserver);

    // Don't show the Robocode battle view
    engine.setVisible(false);

    battlefield = new BattlefieldSpecification(800, 600); // 800x600
  }
예제 #2
0
  public void runGA(int populationSize, int generationSize, int genomeSize) {
    int generation = 1;
    int parentsSelected = 5;
    BattleResults[] results;

    ArrayList<String> genomePaths = GAHelper.generatePopulation(populationSize, genomeSize);

    for (String genomePath : genomePaths) {
      Genome selected = GenomeIO.load(genomePath);
      RobotGenerator.compile(selected);

      String robotName = RobotGenerator.getClassPath(selected);
      RobotSpecification[] selectedRobots =
          engine.getLocalRepository(robotName + ",sample.RamFire");
      BattleSpecification battleSpec = new BattleSpecification(1, battlefield, selectedRobots);
      engine.runBattle(battleSpec, true);

      results = battleObserver.getResults();
      BattleResults result = results[0]; // Index 0 will always be the index of our generated Robot

      // Evaluate fitness - Keeping it simple for now (fitness = score)
      selected.setFitness(new Double(result.getScore()));
      GenomeIO.save(selected);

      System.out.println(
          String.format(
              "Genome result: generation:%d id:%d - FITNESS:%f",
              selected.getGeneration(), selected.getId(), selected.getFitness()));
    }

    while (generation != generationSize) {
      generation++;

      genomePaths =
          GAHelper.produceNextGeneration(generation, parentsSelected, populationSize, genomeSize);

      for (String genomePath : genomePaths) {
        Genome selected = GenomeIO.load(genomePath);
        RobotGenerator.compile(selected);

        String robotName = RobotGenerator.getClassPath(selected);
        RobotSpecification[] selectedRobots =
            engine.getLocalRepository(robotName + ",sample.RamFire");
        BattleSpecification battleSpec = new BattleSpecification(1, battlefield, selectedRobots);
        engine.runBattle(battleSpec, true);

        results = battleObserver.getResults();
        BattleResults result =
            results[0]; // Index 0 will always be the index of our generated Robot

        // Evaluate fitness - Keeping it simple for now (fitness = score)
        selected.setFitness(new Double(result.getScore()));
        GenomeIO.save(selected);

        System.out.println(
            String.format(
                "Genome result: generation:%d id:%d - FITNESS:%f",
                selected.getGeneration(), selected.getId(), selected.getFitness()));
      }
    }

    engine.close();
    System.exit(0);
  }