Exemplo n.º 1
0
 public void onHitByBullet(HitByBulletEvent e) {
   if (fitness - getEnergy() > 15) {
     select++;
     if (select == 4) {
       elapsedTime[select - 1] = (int) e.getTime() - time;
       time = (int) e.getTime();
       sortIndividuals();
       for (int i = 1; i < 4; i++)
         if (Math.random() < 0.7) population[i] = crossover(population[0], population[i]);
       for (int i = 0; i < 4; i++)
         if (Math.random() < 0.1) population[i] = mutation(population[i]);
       select = 0;
     }
     fitness = (int) getEnergy();
   }
 }
Exemplo n.º 2
0
 public int mutation(int individual) {
   int ind2;
   do {
     ind2 = individual + (int) (-1 + 2 * Math.random());
   } while (ind2 < 1 || ind2 >= 6);
   return ind2;
 }
Exemplo n.º 3
0
  public void run() {
    population = new int[4];
    elapsedTime = new int[4];

    for (int i = 0; i < 4; i++) population[i] = (int) (Math.random() * 4 + 1);

    fitness = (int) getEnergy();

    while (true) {
      time = (int) getTime();
      switch (population[select]) {
        case 1:
          individual = behaviors.FORWARD_AND_REDO;
          break;
        case 2:
          individual = behaviors.CIRCULAR;
          break;
        case 3:
          individual = behaviors.DODGE;
          break;
        case 4:
          individual = behaviors.WALLS;
          break;
        case 5:
          individual = behaviors.CRAZY;
          break;
        default:
          System.out.println("Error!");
          break;
      }

      switch (individual) {
        case CRAZY:
          setTurnGunRight(99999);
          execute();
          crazyMoves();
          break;
        case FORWARD_AND_REDO:
          setTurnGunRight(99999);
          execute();
          forwardAndRedoMoves();
          break;
        case CIRCULAR:
          setTurnGunRight(99999);
          execute();
          circularMoves();
          break;
        case WALLS:
          setTurnGunRight(99999);
          execute();
          betaWallsMoves();
          break;
        case DODGE:
          dodgeMoves();
          break;
      }
    }
  }
Exemplo n.º 4
0
  public void onScannedRobot(ScannedRobotEvent e) {
    robotLocation = new Point2D.Double(getX(), getY());
    enemyAbsoluteBearing = getHeadingRadians() + e.getBearingRadians();
    enemyDistance = e.getDistance();
    enemyLocation = vectorToLocation(enemyAbsoluteBearing, enemyDistance, robotLocation);

    // Change direction at random
    if (Math.random() < 0.015) {
      movementLateralAngle *= -1;
    }
    move();
    execute();

    // radar
    setTurnRadarRightRadians(
        Utils.normalRelativeAngle(enemyAbsoluteBearing - getRadarHeadingRadians()) * 2);

    /*
     * Circular Gun from wiki
     */
    double absBearing = e.getBearingRadians() + getHeadingRadians();

    // Finding the heading and heading change.
    double enemyHeading = e.getHeadingRadians();
    double enemyHeadingChange = enemyHeading - oldEnemyHeading;
    oldEnemyHeading = enemyHeading;

    double deltaTime = 0;
    double predictedX = getX() + e.getDistance() * Math.sin(absBearing);
    double predictedY = getY() + e.getDistance() * Math.cos(absBearing);
    while ((++deltaTime) * BULLET_SPEED
        < Point2D.Double.distance(getX(), getY(), predictedX, predictedY)) {

      // Add the movement we think our enemy will make to our enemy's current X and Y
      predictedX += Math.sin(enemyHeading) * (e.getVelocity());
      predictedY += Math.cos(enemyHeading) * (e.getVelocity());

      // Find our enemy's heading changes.
      enemyHeading += enemyHeadingChange;

      // If our predicted coordinates are outside the walls, put them 18
      // distance units away from the walls as we know
      // that that is the closest they can get to the wall (Bots are
      // non-rotating 36*36 squares).
      predictedX = Math.max(Math.min(predictedX, getBattleFieldWidth() - 18), 18);
      predictedY = Math.max(Math.min(predictedY, getBattleFieldHeight() - 18), 18);
    }
    // Find the bearing of our predicted coordinates from us.
    double aim = Utils.normalAbsoluteAngle(Math.atan2(predictedX - getX(), predictedY - getY()));

    // Aim and fire.
    setTurnGunRightRadians(Utils.normalRelativeAngle(aim - getGunHeadingRadians()));
    setFire(BULLET_POWER);

    setTurnRadarRightRadians(Utils.normalRelativeAngle(absBearing - getRadarHeadingRadians()) * 2);
  }
Exemplo n.º 5
0
 public int crossover(int individual1, int individual2) {
   return individual1 + (individual2 - individual1) * (int) Math.random();
 }
Exemplo n.º 6
0
 public void crazyMoves() {
   setTurnLeft(Math.random() * 400 * (-1 + 2 * Math.random()));
   execute();
   ahead(Math.random() * 400 * (-1 + 2 * Math.random()));
 }