示例#1
0
 public static Pelilauta bayesSimulate(Pelilauta lauta, int[] amafTaulu) {
   double probability;
   double maxProbability;
   int maxSimple;
   while (lauta.getMoveNumber() < 700) {
     maxSimple = -1;
     maxProbability = -1;
     for (int i = 0; i < Pelilauta.getKoko() * Pelilauta.getKoko(); i++) {
       if (!PlacementHandler.onkoLaillinenSiirto(lauta, Pelilauta.toX(i), Pelilauta.toY(i))
           || PlacementHandler.tuhoaakoSiirtoOmanSilman(
               lauta, Pelilauta.toX(i), Pelilauta.toY(i))) {
         continue;
       }
       probability = 0;
       for (int k = -1; k < 2; k++) {
         for (int l = -1; l < 2; l++) {
           if (Pelilauta.onLaudalla(Pelilauta.toX(i) + k, Pelilauta.toY(i) + l)) {
             if (lauta.getTurn() == Pelilauta.MUSTA) {
               probability +=
                   Pattern.patternMovePredictions(
                       Pattern.match(lauta, Pelilauta.toX(i) + k, Pelilauta.toY(i) + l), -k, -l);
             } else {
               probability +=
                   Pattern.patternMovePredictions(
                       Pattern.swapColors(
                           Pattern.match(lauta, Pelilauta.toX(i) + k, Pelilauta.toY(i) + l)),
                       -k,
                       -l);
             }
           }
         }
       }
       if (probability > maxProbability) {
         maxSimple = i;
       }
     }
     if (maxSimple == -1) {
       if (lauta.isPassedOnLastMove()) {
         break;
       }
       PlacementHandler.pass(lauta);
       continue;
     }
     PlacementHandler.pelaaSiirto(lauta, Pelilauta.toX(maxSimple), Pelilauta.toY(maxSimple));
     if (amafTaulu[maxSimple] == 0) {
       amafTaulu[maxSimple] = lauta.getTurn();
     }
   }
   // System.err.println("Siirtoja: " + lauta.getMoveNumber());
   return lauta;
 }
示例#2
0
  public double[] bayesExpand2(Pelilauta lauta, int max) {
    this.children = new NodenLapset();
    double[] values = new double[Pelilauta.getKoko() * Pelilauta.getKoko()];
    double[] negatives = new double[values.length];
    double sum;
    Node node;
    children.setMaxKoko(max);
    int pattern;
    int location;
    for (int i = 0; i < values.length; i++) {
      values[i] = 0;
    }
    for (int i = 0; i < Pelilauta.getKoko() * Pelilauta.getKoko(); i++) {
      if (!PlacementHandler.onkoLaillinenSiirto(lauta, Pelilauta.toX(i), Pelilauta.toY(i))) {
        continue;
      }
      pattern = Pattern.match(lauta, Pelilauta.toX(i), Pelilauta.toY(i));
      if (lauta.getTurn() == Pelilauta.VALKEA) {
        pattern = Pattern.swapColors(pattern);
      }

      for (int k = -1; k < 2; k++) {
        for (int l = -1; l < 2; l++) {
          if (Pelilauta.onLaudalla(Pelilauta.toX(i) + k, Pelilauta.toY(i) + l)) {
            location = Pelilauta.toSimple(Pelilauta.toX(i) + k, Pelilauta.toY(i) + l);
            // values[location] += Math.log(Pattern.patternMovePredictions(pattern, k, l)) -
            // Math.log(Pattern.patternMovePredictions(pattern, k, l));
            values[location] += Pattern.patternMovePredictions(pattern, k, l);
          }
        }
      }
    }
    sum = 0;
    for (int i = 0; i < Pelilauta.getKoko() * Pelilauta.getKoko(); i++) {
      if (!PlacementHandler.onkoLaillinenSiirto(lauta, Pelilauta.toX(i), Pelilauta.toY(i))) {
        continue;
      }
      sum += values[i];
    }

    for (int i = 0; i < values.length; i++) {
      if (!PlacementHandler.onkoLaillinenSiirto(lauta, Pelilauta.toX(i), Pelilauta.toY(i))) {
        continue;
      }
      values[i] = values[i] / (sum);
      node = new Node(lauta, Pelilauta.toX(i), Pelilauta.toY(i));
      node.raveVierailut += 5000 * values[i];
      node.raveVoitot += (node.raveVierailut * 56) / 100;
      this.children.addNode(node);
    }
    children.sort();
    return values;
  }
  @Override
  protected MatchResult match(final List<LeafPattern> left, List<LeafPattern> collected) {
    if (collected == null) {
      collected = list();
    }

    List<LeafPattern> l = left;
    List<LeafPattern> c = collected;

    for (final Pattern pattern : getChildren()) {
      final MatchResult m = pattern.match(l, c);

      l = m.getLeft();
      c = m.getCollected();

      if (!m.matched()) {
        return new MatchResult(false, left, collected);
      }
    }

    return new MatchResult(true, l, c);
  }
示例#4
0
  private static Pattern getPattern(
      Map<String, NamedCondition> transitionConditions, PatternGroup patternGroup) {
    Pattern pattern = new Pattern();
    for (Step step : patternGroup.steps) {
      Pattern next;
      if (step.isPattern()) {
        next = getPattern(transitionConditions, step.patternGroup);

      } else {
        NamedCondition condition = transitionConditions.get(step.ref);
        if (condition == null)
          throw new IllegalArgumentException("No such condition [" + step.ref + "]");
        next = Pattern.match(condition);
      }
      if (step.repeat) next = next.repeat();
      if (step.optional) next = next.optional();
      next = Pattern.capture(next);
      pattern =
          patternGroup.type == GroupType.OR
              ? Pattern.branch(pattern, next)
              : Pattern.chain(pattern, next);
    }
    return pattern.minimize();
  }
示例#5
0
 @Override
 public <T extends TreeCursor<Value, Type>> boolean match(T tree, Environment<T> env) {
   return pat.match(tree, env);
 }
示例#6
0
  public void bayesExpand(Pelilauta lauta) {
    double[] values = new double[Pelilauta.getKoko() * Pelilauta.getKoko()];
    int[] strengths = new int[values.length];
    children = new NodenLapset();
    double boardSum = 0.0;
    int strengthOfPrediction = 0;
    Node newNode;

    for (int i = 0; i < values.length; i++) {
      values[i] = Pattern.valueOf(Pattern.match(lauta, Pelilauta.toX(i), Pelilauta.toY(i)));
      strengths[i] = Pattern.getSeenTotal(Pattern.match(lauta, Pelilauta.toX(i), Pelilauta.toY(i)));
      boardSum += values[i];
      strengthOfPrediction +=
          Pattern.getSeenTotal(Pattern.match(lauta, Pelilauta.toX(i), Pelilauta.toY(i)));
    }
    Pelilauta testiLauta;
    double difference;
    int differenceInStrength;
    for (int i = 0; i < values.length; i++) {
      if (!PlacementHandler.onkoLaillinenSiirto(lauta, Pelilauta.toX(i), Pelilauta.toY(i))) {
        continue;
      }
      difference = 0.0;
      differenceInStrength = 0;
      testiLauta = lauta.kopioi();
      PlacementHandler.pelaaSiirto(testiLauta, Pelilauta.toX(i), Pelilauta.toY(i));
      for (int x = -1; x < 2; x++) {
        for (int y = -1; y < 2; y++) {
          if (Pelilauta.onLaudalla(Pelilauta.toX(i) + x, Pelilauta.toY(i) + y)) {
            difference +=
                values[i]
                    - Pattern.valueOf(
                        Pattern.match(testiLauta, Pelilauta.toX(i) + x, Pelilauta.toY(i) + y));
            differenceInStrength +=
                strengths[i]
                    - Pattern.getSeenTotal(
                        Pattern.match(testiLauta, Pelilauta.toX(i), Pelilauta.toY(i)));
          }
        }
      }
      newNode = new Node(lauta, Pelilauta.toX(i), Pelilauta.toY(i));
      newNode.raveVierailut =
          (int) Math.round(Math.log(strengthOfPrediction - differenceInStrength) * 1000);
      if (lauta.getTurn() == Pelilauta.MUSTA) {
        newNode.raveVoitot =
            (int)
                Math.round(
                    newNode.raveVierailut * (1 / (1 + Math.pow(Math.E, boardSum - difference))));
      } else {
        newNode.raveVoitot =
            (int)
                Math.round(
                    newNode.raveVierailut
                        * (1 - (1 / (1 + Math.pow(Math.E, boardSum - difference)))));
      }
      if (1.0 * newNode.raveVoitot / newNode.raveVierailut > 0.44) {
        children.addNode(newNode);
      }
    }
    children.setMaxKoko(branchingFactor);
    Node passaus = new Node(lauta, -1, -1);
    children.addNode(passaus);
    children.sort();
  }