Esempio n. 1
2
  /**
   * Evaluates a solution
   *
   * @param solution The solution to evaluate
   */
  public void evaluate(Solution solution) {
    double fitness1, fitness2;
    int x, y;
    fitness1 = 0.0;
    fitness2 = 0.0;

    for (int i = 0; i < (numberOfCities_); i++) {
      for (int j = 0; j < (numberOfCities_); j++) {
        x = ((Permutation) solution.getDecisionVariables().variables_[0]).vector_[i];
        y = ((Permutation) solution.getDecisionVariables().variables_[0]).vector_[j];

        fitness1 += distanceMatrix_[i][j] * flujo1[x][y];
      }
    }

    for (int i = 0; i < (numberOfCities_); i++) {
      for (int j = 0; j < (numberOfCities_); j++) {
        x = ((Permutation) solution.getDecisionVariables().variables_[0]).vector_[i];
        y = ((Permutation) solution.getDecisionVariables().variables_[0]).vector_[j];

        fitness2 += distanceMatrix_[i][j] * flujo2[x][y];
      }
    }

    solution.setObjective(0, fitness1);
    solution.setObjective(1, fitness2);
  } // evaluate
Esempio n. 2
0
  /**
   * Evaluates a solution
   *
   * @param solution The solution to evaluate
   * @throws JMException
   */
  public void evaluate(Solution solution) throws JMException {
    DecisionVariables gen = solution.getDecisionVariables();

    double[] x = new double[numberOfVariables_];
    double[] f = new double[numberOfObjectives_];
    double[] theta = new double[numberOfObjectives_ - 1];
    int k = numberOfVariables_ - numberOfObjectives_ + 1;

    for (int i = 0; i < numberOfVariables_; i++) x[i] = gen.variables_[i].getValue();

    double g = 0.0;
    for (int i = numberOfVariables_ - k; i < numberOfVariables_; i++)
      g += java.lang.Math.pow(x[i], 0.1);

    double t = java.lang.Math.PI / (4.0 * (1.0 + g));
    theta[0] = x[0] * java.lang.Math.PI / 2;
    for (int i = 1; i < (numberOfObjectives_ - 1); i++) theta[i] = t * (1.0 + 2.0 * g * x[i]);

    for (int i = 0; i < numberOfObjectives_; i++) f[i] = 1.0 + g;

    for (int i = 0; i < numberOfObjectives_; i++) {
      for (int j = 0; j < numberOfObjectives_ - (i + 1); j++) f[i] *= java.lang.Math.cos(theta[j]);
      if (i != 0) {
        int aux = numberOfObjectives_ - (i + 1);
        f[i] *= java.lang.Math.sin(theta[aux]);
      } // if
    } // for

    for (int i = 0; i < numberOfObjectives_; i++) solution.setObjective(i, f[i]);
  } // evaluate
Esempio n. 3
0
  /** @param individual */
  void updateReference(Solution individual) {
    for (int n = 0; n < problem_.getNumberOfObjectives(); n++) {
      if (individual.getObjective(n) < z_[n]) {
        z_[n] = individual.getObjective(n);

        indArray_[n] = individual;
      }
    }
  } // updateReference
Esempio n. 4
0
  double fitnessFunction(Solution individual, double[] lambda) {
    double fitness;
    fitness = 0.0;

    if (functionType_.equals("_TCHE1")) {
      double maxFun = -1.0e+30;

      for (int n = 0; n < problem_.getNumberOfObjectives(); n++) {
        double diff = Math.abs(individual.getObjective(n) - z_[n]);

        double feval;
        if (lambda[n] == 0) {
          feval = 0.0001 * diff;
        } else {
          feval = diff * lambda[n];
        }
        if (feval > maxFun) {
          maxFun = feval;
        }
      } // for

      fitness = maxFun;
    } // if
    else if (functionType_.equals("_AGG")) {
      double sum = 0.0;
      for (int n = 0; n < problem_.getNumberOfObjectives(); n++) {
        sum += (lambda[n]) * individual.getObjective(n);
      }

      fitness = sum;

    } else if (functionType_.equals("_PBI")) {
      double d1, d2, nl;
      double theta = 5.0;

      d1 = d2 = nl = 0.0;

      for (int i = 0; i < problem_.getNumberOfObjectives(); i++) {
        d1 += (individual.getObjective(i) - z_[i]) * lambda[i];
        nl += Math.pow(lambda[i], 2.0);
      }
      nl = Math.sqrt(nl);
      d1 = Math.abs(d1) / nl;

      for (int i = 0; i < problem_.getNumberOfObjectives(); i++) {
        d2 += Math.pow((individual.getObjective(i) - z_[i]) - d1 * (lambda[i] / nl), 2.0);
      }
      d2 = Math.sqrt(d2);

      fitness = (d1 + theta * d2);
    } else {
      System.out.println("MOEAD.fitnessFunction: unknown type " + functionType_);
      System.exit(-1);
    }
    return fitness;
  } // fitnessEvaluation
Esempio n. 5
0
  /**
   * Evaluates a solution
   *
   * @param solution The solution to evaluate
   * @throws Exception
   */
  public void evaluate(Solution solution) throws Exception {
    ProblemVariables decisionVariables = solution.getProblemVariables();

    double[] f = new double[numberOfObjectives_];
    f[0] = decisionVariables.variables[0].getValue();
    double g = this.evalG(decisionVariables);
    double h = this.evalH(f[0], g);
    f[1] = h * g;

    solution.setObjective(0, f[0]);
    solution.setObjective(1, f[1]);
  } // evaluate
Esempio n. 6
0
  /**
   * Evaluates a solution
   *
   * @param solution The solution to evaluate
   * @throws JMException
   */
  public void evaluate(Solution solution) throws JMException {
    Variable[] variable = solution.getDecisionVariables();

    double[] f = new double[numberOfObjectives_];

    double x1 = variable[0].getValue();
    double x2 = variable[1].getValue();
    f[0] = 2.0 + (x1 - 2.0) * (x1 - 2.0) + (x2 - 1.0) * (x2 - 1.0);
    f[1] = 9.0 * x1 - (x2 - 1.0) * (x2 - 1.0);

    solution.setObjective(0, f[0]);
    solution.setObjective(1, f[1]);
  } // evaluate
Esempio n. 7
0
  /**
   * Evaluates a solution
   *
   * @param solution The solution to evaluate
   * @throws JMException
   */
  public void evaluate(Solution solution) throws JMException {
    Variable[] gen = solution.getDecisionVariables();

    Vector<Double> x = new Vector<Double>(numberOfVariables_);
    Vector<Double> y = new Vector<Double>(numberOfObjectives_);

    for (int i = 0; i < numberOfVariables_; i++) {
      x.addElement(gen[i].getValue());
      y.addElement(0.0);
    } // for

    LZ09_.objective(x, y);

    for (int i = 0; i < numberOfObjectives_; i++) solution.setObjective(i, y.get(i));
  } // evaluate
  //  --  --  --  --  --  --  --  --  --  --  --  --  --  --  --  --  --  --  --  --  --  --  --  --
  @Override
  public void evaluate(Solution solution) {
    double fitness0 = 0.0;
    double fitness1 = 0.0;
    double fitness2 = 0.0;
    double fitness3 = 0.0;
    boolean verificador;
    int x;
    int y;

    // percorre o vetor de solucoes
    for (int i = 0; i < numberOfElements_; i++) {

      // pega o id da classe
      x = ((Permutation) solution.getDecisionVariables()[0]).vector_[i];

      // percorre as colunas da matrix de dependencia
      for (int k = 0; k < numberOfElements_; k++) {

        // verifica se existe dependencia de x para k
        if (dependency_matrix_[x][k] == 1) {
          verificador = false;

          // verifica se a classe já exite
          for (int j = 0; j <= i; j++) {
            y = ((Permutation) solution.getDecisionVariables()[0]).vector_[j];
            if (y == k) {
              verificador = true;
            }
          }

          // adiciona os valores ao fitnesse se a classe não tiver sido testada ainda
          if (verificador == false) {
            fitness0 += attribute_coupling_matrix_[x][k];
            fitness1 += method_coupling_matrix_[x][k];
            fitness2 += method_return_type_matrix_[x][k];
            fitness3 += method_param_type_matrix_[x][k];
          }
        }
      }
    }

    // System.out.println(fitness0+" "+fitness1+" "+fitness2+" "+fitness3);
    solution.setObjective(0, fitness0);
    solution.setObjective(1, fitness1);
    solution.setObjective(2, fitness2);
    solution.setObjective(3, fitness3);
  }
Esempio n. 9
0
  /**
   * Evaluates a solution
   *
   * @param solution The solution to evaluate
   * @throws JMException
   */
  public void evaluate(Solution solution) throws JMException {
    XReal x = new XReal(solution);

    double[] f = new double[numberOfObjectives_];
    double sum1 = 0.0;
    for (int var = 0; var < numberOfVariables_; var++) {
      sum1 +=
          StrictMath.pow(
              x.getValue(var) - (1.0 / StrictMath.sqrt((double) numberOfVariables_)), 2.0);
    }
    double exp1 = StrictMath.exp((-1.0) * sum1);
    f[0] = 1 - exp1;

    double sum2 = 0.0;
    for (int var = 0; var < numberOfVariables_; var++) {
      sum2 +=
          StrictMath.pow(
              x.getValue(var) + (1.0 / StrictMath.sqrt((double) numberOfVariables_)), 2.0);
    }
    double exp2 = StrictMath.exp((-1.0) * sum2);
    f[1] = 1 - exp2;

    solution.setObjective(0, f[0]);
    solution.setObjective(1, f[1]);
  } // evaluate
Esempio n. 10
0
  /**
   * Evaluates the constraint overhead of a solution
   *
   * @param solution The solution
   * @throws JMException
   */
  public void evaluateConstraints(Solution solution) throws JMException {
    double[] constraint = new double[this.getNumberOfConstraints()];

    double x1 = solution.getDecisionVariables()[0].getValue();
    double x2 = solution.getDecisionVariables()[1].getValue();

    constraint[0] = 1.0 - (x1 * x1 + x2 * x2) / 225.0;
    constraint[1] = (3.0 * x2 - x1) / 10.0 - 1.0;

    double total = 0.0;
    int number = 0;
    for (int i = 0; i < this.getNumberOfConstraints(); i++)
      if (constraint[i] < 0.0) {
        number++;
        total += constraint[i];
      }

    solution.setOverallConstraintViolation(total);
    solution.setNumberOfViolatedConstraint(number);
  } // evaluateConstraints
Esempio n. 11
0
  /**
   * Evaluates a solution
   *
   * @param solution The solution to evaluate
   * @throws JMException
   */
  public void evaluate(Solution solution) throws JMException {
    DecisionVariables decisionVariables = solution.getDecisionVariables();

    double[] x = new double[numberOfVariables_];
    double[] fx = new double[numberOfVariables_];
    double g;
    double h;
    double sum;
    for (int i = 0; i < numberOfVariables_; i++) x[i] = decisionVariables.variables_[i].getValue();

    fx[0] = x[0];

    sum = 0.0;
    for (int i = 1; i < numberOfVariables_; i++) sum += (x[i] * x[i] - x[0]) * (x[i] * x[i] - x[0]);

    g = 1.0 + 9.0 * sum / (numberOfVariables_ - 1.0);
    h = 1.0 - Math.sqrt(x[0] / g);

    fx[1] = g * h;

    solution.setObjective(0, fx[0]);
    solution.setObjective(1, fx[1]);
  } // evaluate
Esempio n. 12
0
  /**
   * Evaluates a solution.
   *
   * @param solution The solution to evaluate.
   * @throws JMException
   */
  public void evaluate(Solution solution) throws JMException {
    Variable[] decisionVariables = solution.getDecisionVariables();

    double[] x = new double[numberOfVariables_];
    for (int i = 0; i < numberOfVariables_; i++) x[i] = decisionVariables[i].getValue();

    int count1, count2, count3;
    double sum1, sum2, sum3, yj, hj;
    sum1 = sum2 = sum3 = 0.0;
    count1 = count2 = count3 = 0;

    for (int j = 3; j <= numberOfVariables_; j++) {
      yj =
          x[j - 1] - 2.0 * x[1] * Math.sin(2.0 * Math.PI * x[0] + j * Math.PI / numberOfVariables_);
      hj = 4.0 * yj * yj - Math.cos(8.0 * Math.PI * yj) + 1.0;
      if (j % 3 == 1) {
        sum1 += hj;
        count1++;
      } else if (j % 3 == 2) {
        sum2 += hj;
        count2++;
      } else {
        sum3 += hj;
        count3++;
      }
    }

    solution.setObjective(
        0,
        Math.cos(0.5 * Math.PI * x[0]) * Math.cos(0.5 * Math.PI * x[1])
            + 2.0 * sum1 / (double) count1);
    solution.setObjective(
        1,
        Math.cos(0.5 * Math.PI * x[0]) * Math.sin(0.5 * Math.PI * x[1])
            + 2.0 * sum2 / (double) count2);
    solution.setObjective(2, Math.sin(0.5 * Math.PI * x[0]) + 2.0 * sum3 / (double) count3);
  } // evaluate
Esempio n. 13
0
  /**
   * Perform the mutation operation
   *
   * @param probability Mutation probability
   * @param solution The solution to mutate
   * @throws JMException
   */
  public void doMutation(double probability, Solution solution) throws JMException {
    if (PseudoRandom.randDouble() < probability) {
      List<Partition> partitions =
          ((MusicVariableAtonal) solution.getDecisionVariables()[0]).getPartitions();
      int s = partitions.size() - 1;
      int melodyIndex = PseudoRandom.randInt(0, s);
      Partition partition = partitions.get(melodyIndex);
      List<NotePos> notePositions = partition.getNotes();
      int startPartition = partition.getPosition();
      int endPartition = startPartition + partition.getLength();
      if (!notePositions.isEmpty() && notePositions.size() > 1) {
        int position = PseudoRandom.randInt(0, notePositions.size() - 1);
        NotePos note = notePositions.get(position);
        int index = notePositions.indexOf(note);
        if (random.nextBoolean()) {
          if (index >= 1) {
            NotePos prevNote = notePositions.get(index - 1);
            if (prevNote.getVoice() != note.getVoice()) {
              //								int prevPosition = prevNote.getPosition();
              int notePosition = note.getPosition();
              if (!containsNoteAtPosition(notePositions, notePosition - MIN_LENGTH)
                  && startPartition <= notePosition - MIN_LENGTH) {
                note.setPosition(notePosition - MIN_LENGTH);
                System.out.println("Shifted");
              }
            }
          }

        } else {
          if (index < notePositions.size() - 1) {
            NotePos nextNote = notePositions.get(index + 1);
            if (nextNote.getVoice() != note.getVoice()) {
              //								int nextPosition = nextNote.getPosition();
              int notePosition = note.getPosition();
              if (!containsNoteAtPosition(notePositions, notePosition + MIN_LENGTH)
                  && notePosition + MIN_LENGTH <= endPartition) {
                note.setPosition(notePosition + MIN_LENGTH);
                System.out.println("Shifted");
              }
            }
          }
        }
      }
    }
  }
Esempio n. 14
0
  /**
   * Perform the crossover operation
   *
   * @param probability Crossover probability
   * @param parent1 The first parent
   * @param parent2 The second parent
   * @return Two offspring solutions
   * @throws JMException
   */
  public Solution[] doCrossover(double probability, Solution parent1, Solution parent2)
      throws JMException {

    Solution[] offspring = new Solution[2];

    offspring[0] = new Solution(parent1);
    offspring[1] = new Solution(parent2);

    if (PseudoRandom.randDouble() < probability) {
      int crosspoint1;
      int crosspoint2;
      int chromosomeLength;
      DecisionVariables parent1Vars;
      DecisionVariables parent2Vars;
      DecisionVariables offspring1Vars;
      DecisionVariables offspring2Vars;

      // permutationLength = ((Permutation)parent1.getDecisionVariables().variables_[0]).getLength()
      // ;
      chromosomeLength = parent1.getDecisionVariables().size();
      parent1Vars = parent1.getDecisionVariables();
      parent2Vars = parent2.getDecisionVariables();
      offspring1Vars = offspring[0].getDecisionVariables();
      offspring2Vars = offspring[1].getDecisionVariables();

      // STEP 1: Get two cutting points
      crosspoint1 = PseudoRandom.randInt(0, chromosomeLength - 1);
      crosspoint2 = PseudoRandom.randInt(0, chromosomeLength - 1);

      while (crosspoint2 == crosspoint1)
        crosspoint2 = PseudoRandom.randInt(0, chromosomeLength - 1);

      if (crosspoint1 > crosspoint2) {
        int swap;
        swap = crosspoint1;
        crosspoint1 = crosspoint2;
        crosspoint2 = swap;
      } // if

      //        // STEP 2: Obtain the two children
      //
      //        for(int k = 0; k < crosspoint1; k++) {
      //        	offspring1Vars.variables_[k] = parent1Vars.variables_[k];
      //        	offspring2Vars.variables_[k] = parent2Vars.variables_[k];
      //        }
      //
      //       for(int k = crosspoint1; k < crosspoint2; k++) {
      //    	   offspring2Vars.variables_[k] = parent1Vars.variables_[k];
      //       	   offspring1Vars.variables_[k] = parent2Vars.variables_[k];
      //        } // for
      //
      //       for(int k = crosspoint2; k < chromosomeLength; k++) {
      //       	offspring1Vars.variables_[k] = parent1Vars.variables_[k];
      //       	offspring2Vars.variables_[k] = parent2Vars.variables_[k];
      //       }

      // STEP 2: Obtain the two children

      for (int k = 0; k < crosspoint1; k++) {
        offspring1Vars.variables_[k] = parent1Vars.variables_[k].deepCopy();
        offspring2Vars.variables_[k] = parent2Vars.variables_[k].deepCopy();
      }

      for (int k = crosspoint1; k < crosspoint2; k++) {
        offspring2Vars.variables_[k] = parent1Vars.variables_[k].deepCopy();
        offspring1Vars.variables_[k] = parent2Vars.variables_[k].deepCopy();
      } // for

      for (int k = crosspoint2; k < chromosomeLength; k++) {
        offspring1Vars.variables_[k] = parent1Vars.variables_[k].deepCopy();
        offspring2Vars.variables_[k] = parent2Vars.variables_[k].deepCopy();
      }

      Comparator dominance = new DominanceComparator();

      if (((dominance.compare(parent1, parent2) != -1)
              && (crosspoint2 - crosspoint1 >= chromosomeLength / 2))
          || ((dominance.compare(parent1, parent2) == -1)
              && (crosspoint2 - crosspoint1 < chromosomeLength / 2))) {
        offspring[0].setDecisionVariables(offspring1Vars);
        offspring[1].setDecisionVariables(offspring2Vars);
      } else if (((dominance.compare(parent2, parent1) != -1)
              && (crosspoint2 - crosspoint1 >= chromosomeLength / 2))
          || ((dominance.compare(parent2, parent1) == -1)
              && (crosspoint2 - crosspoint1 < chromosomeLength / 2))) {
        offspring[1].setDecisionVariables(offspring1Vars);
        offspring[0].setDecisionVariables(offspring2Vars);
      }

      //    else
      //    {
      //      Configuration.logger_.severe("TwoPointsCrossover.doCrossover: invalid " +
      //          "type" +
      //          parent1.getDecisionVariables().variables_[0].getVariableType());
      //      Class cls = java.lang.String.class;
      //      String name = cls.getName();
      //      throw new JMException("Exception in " + name + ".doCrossover()") ;
      //    } // else

    }

    return offspring;
  } // makeCrossover