Beispiel #1
0
  /**
   * use the index of each slot, together with constraints only one queen in each row from different
   * angle e.g for var at position [0][0] has index 0, bdd && (NOT0 v NOT1) && (NOT0 v NOT2) &&
   * (NOT0 v NOT3) && (NOT0 v NOT4) horizontal && (NOT0 v NOT5) && (NOT0 v NOT10) && (NOT0 v NOT15)
   * && (NOT0 v NOT20) vertical && (NOT0 v NOT6) && (NOT0 v NOT12) && (NOT0 v NOT18) && (NOT0 v
   * NOT24) diagonal && one queen per row rule oneQueen
   *
   * @return BDD
   */
  public BDD build() {
    BDD bdd = factory.one();

    for (int x = 0; x < size; x++) {
      BDD oneQueen = factory.zero();

      for (int y = 0; y < size; y++) {
        oneQueen.orWith(factory.ithVar(idx(x, y)));
        BDD var = factory.nithVar(idx(x, y)); // get negated index value

        // horizontal
        for (int i = (y + 1); i < size; i++) {
          bdd.andWith(var.or(factory.nithVar(idx(x, i))));
        }

        // vertical
        for (int i = (x + 1); i < size; i++) {
          bdd.andWith(var.or(factory.nithVar(idx(i, y))));
        }

        // diagonal
        for (int i = 1; (y + i) < size; i++) {
          if ((x - i) >= 0) {
            bdd.andWith(var.or(factory.nithVar(idx(x - i, y + i))));
          }
          if ((x + i) < size) {
            bdd.andWith(var.or(factory.nithVar(idx(x + i, y + i))));
          }
        }
      }
      bdd.andWith(oneQueen); // one row has to/only can have one queen
    }
    return bdd;
  }
  /* (non-Javadoc)
   * @see com.epochx.core.scorer.SemanticScorer#doScore(com.epochx.representation.CandidateProgram, com.epochx.representation.CandidateProgram)
   */
  public double doScore(GPCandidateProgram program1, GPCandidateProgram program2) {
    double score;

    // TODO figure out a better way of doing this
    if (super.getSemanticModule() instanceof BooleanSemanticModule) {
      ((BooleanSemanticModule) super.getSemanticModule()).start();
    }

    BooleanRepresentation program1Representation =
        (BooleanRepresentation) getSemanticModule().codeToBehaviour(program1);
    BooleanRepresentation program2Representation =
        (BooleanRepresentation) getSemanticModule().codeToBehaviour(program2);
    BDD rep1 = program1Representation.getBDD();
    BDD rep2 = program2Representation.getBDD();
    BDD diffRep1 = rep1.and(rep2.not());
    BDD diffRep2 = rep2.and(rep1.not());
    BDD finalDiff = diffRep1.or(diffRep2);
    score = finalDiff.satCount() * 100;

    // TODO figure out a better way of doing this
    if (super.getSemanticModule() instanceof BooleanSemanticModule) {
      ((BooleanSemanticModule) super.getSemanticModule()).stop();
    }

    return score;
  }