Beispiel #1
0
  @Override
  public Individual[] cross(Individual[] entities) {
    int size = entities[0].getLupusArray().length;
    List<double[][]> dad = entities[0].getLupusMatrix();
    List<double[][]> mom = entities[1].getLupusMatrix();

    double[] son1 = new double[size];
    double[] son2 = new double[size];
    int cutPoint = RandomGenerator.getInt(1, dad.size());

    int index = 0;
    for (int i = 0; i < cutPoint; i++) {
      double[][] dadMatrix = dad.get(i);
      double[][] momMatrix = mom.get(i);

      for (int j = 0; j < dadMatrix.length; j++) {
        for (int k = 0; k < dadMatrix[j].length; k++) {
          son1[index] = dadMatrix[j][k];
          son2[index++] = momMatrix[j][k];
        }
      }
    }

    for (int i = cutPoint; i < dad.size(); i++) {
      double[][] dadMatrix = dad.get(i);
      double[][] momMatrix = mom.get(i);

      for (int j = 0; j < dadMatrix.length; j++) {
        for (int k = 0; k < dadMatrix[j].length; k++) {
          son1[index] = dadMatrix[j][k];
          son2[index++] = momMatrix[j][k];
        }
      }
    }

    Individual indSon1 = Individual.creator(entities[0].getData(), son1);
    Individual indSon2 = Individual.creator(entities[0].getData(), son2);

    return new Individual[] {indSon1, indSon2};
  }
  @Override
  public List<Individual> select(List<Individual> population, int generation, int ggToSelect) {
    List<Individual> newGeneration = new ArrayList<Individual>();

    double distance = 1.0d / ggToSelect;
    double r = RandomGenerator.getDouble() / ggToSelect;

    for (int i = 0; i < ggToSelect; i++) {
      double f = 0;
      int j = 0;
      Individual individual = null;

      do {
        individual = population.get(j++);
        f += individual.getApptitude();
      } while (f < r);
      newGeneration.add(individual);
      r += distance;
    }

    return newGeneration;
  }