Exemple #1
0
  @Override
  public void onCompleted(Chromosome chromosome) {

    List<Gene> genes = chromosome.getGenes();

    // For debugging result
    calculateFitness(chromosome);

    for (Gene gene : genes) {
      System.out.println(NODES.get(gene.getValue()).getName());
    }
  }
Exemple #2
0
  @Override
  public double calculateFitness(Chromosome chromosome) {

    List<Gene> genes = chromosome.getGenes();
    double[] totalDistance = new double[1];
    int l = genes.size();
    int i = 0;
    Set<Integer> uniqueIds = new HashSet<Integer>();
    Set<Integer> uniqueRealNodeIds = new HashSet<Integer>();
    Node nodeA = null;
    Node nodeB = null;

    while (i < l) {

      if (nodeA == null) {
        nodeA = calculateAndGetNode(genes.get(i++), uniqueIds, uniqueRealNodeIds, totalDistance);
      }

      if (nodeB == null && i < l) {
        nodeB = calculateAndGetNode(genes.get(i++), uniqueIds, uniqueRealNodeIds, totalDistance);
      }

      if (nodeA != null && nodeB != null) {

        if (nodeA.getId() == nodeB.getId()) {
          totalDistance[0] += FEE_SAME_NEIGHBOURS; // Eğer iki nokta birbirinin aynı ise
        } else {
          totalDistance[0] += calculateDistance(nodeA, nodeB);
        }

        nodeA = nodeB;
        nodeB = null;
      }
    }

    totalDistance[0] += (6 - uniqueRealNodeIds.size()) * FEE_REAL_NODE_PASS;

    return TOTAL_FITNESS - totalDistance[0]; // ten million
  }