/**
   * 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 {
    try {
      if ((solution.getType().getClass() == BinarySolutionType.class)
          || (solution.getType().getClass() == BinaryRealSolutionType.class)) {
        for (int i = 0; i < solution.getDecisionVariables().length; i++) {
          Binary variable = (Binary) solution.getDecisionVariables()[i];
          BitSet set = new BitSet(variable.getNumberOfBits());
          int count = variable.getNumberOfBits();
          double countD = count;
          /* number of bits flipped is B(n, p) distributed (n = number of bits, p = mutationProbability),
           * this can be approximated with N(np, sqrt(np * (1-p)))
           */
          int x =
              (int)
                  Math.round(
                      Math.sqrt(probability * (1 - probability) * countD)
                              * pseudoRandom.nextGaussian()
                          + countD * probability);

          // very low probability in some special cases that x is bigger then the bitcount
          x = Math.min(x, count);

          Set<Integer> indices = new HashSet<Integer>();
          while (indices.size() < x) {
            indices.add(pseudoRandom.randInt(0, count - 1));
          }
          for (Integer j : indices) set.set(j);
          variable.bits_.xor(set);
        }
      } // if
      else { // Integer representation
        for (int i = 0; i < solution.getDecisionVariables().length; i++)
          if (pseudoRandom.randDouble() < probability) {
            int value =
                (int)
                    (pseudoRandom.randInt(
                        (int) solution.getDecisionVariables()[i].getLowerBound(),
                        (int) solution.getDecisionVariables()[i].getUpperBound()));
            solution.getDecisionVariables()[i].setValue(value);
          } // if
      } // else
    } catch (ClassCastException e1) {
      Configuration.logger_.severe(
          "BitFlipMutation.doMutation: " + "ClassCastException error" + e1.getMessage());
      Class cls = java.lang.String.class;
      String name = cls.getName();
      throw new JMException("Exception in " + name + ".doMutation()");
    }
  } // doMutation
示例#2
0
文件: Main.java 项目: ai-se/SPL
  public static int numViolatedConstraints(Binary b) {

    // IVecInt v = bitSetToVecInt(b);
    int s = 0;
    for (List<Integer> constraint : ProductLineProblem.constraints) {
      boolean sat = false;

      for (Integer i : constraint) {
        int abs = (i < 0) ? -i : i;
        boolean sign = i > 0;
        if (b.getIth(abs - 1) == sign) {
          sat = true;
          break;
        }
      }
      if (!sat) {
        s++;
      }
    }

    return s;
  }