Ejemplo n.º 1
0
  // Derives QFunctions for the given value function and simulates the
  // greedy policy for the given number of trials and steps per trial.
  // Returns final value of every trial.
  public ArrayList simulate(int trials, int steps, long rand_seed) {
    ArrayList values = new ArrayList();
    _r = new Random(rand_seed);

    for (int trial = 1; trial <= trials; trial++) {

      System.out.println("\n -----------\n   Trial " + trial + "\n -----------");

      // Initialize state
      _state = new ArrayList();
      _nVars = _mdp._alVars.size();
      for (int c = 0; c < (_nVars << 1); c++) {
        _state.add("-");
      }
      Iterator i = _mdp._alVars.iterator();
      _vars = new TreeSet();
      while (i.hasNext()) {
        String s = (String) i.next();
        if (!s.endsWith("\'")) {
          Integer gid = (Integer) _mdp._tmVar2ID.get(s);
          _vars.add(gid);

          // Note: assign level (level is gid-1 b/c gids in order)
          _state.set(gid.intValue() - 1, _r.nextBoolean() ? TRUE : FALSE);
        }
      }
      // System.out.println(_mdp._context.printNode(_mdp._valueDD) + "\n" + _state);
      double reward = _mdp._context.evaluate(_mdp._rewardDD, _state);
      System.out.print(" " + PrintState(_state) + "  " + MDP._df.format(reward));

      // Run steps
      for (int step = 1; step <= steps; step++) {

        // Get action
        Action a;
        if (_bUseBasis) {
          a = getBasisAction();
        } else {
          a = getAction();
        }

        // Execute action
        executeAction(a);

        // Update reward
        reward =
            (_mdp._bdDiscount.doubleValue() * reward)
                + _mdp._context.evaluate(_mdp._rewardDD, _state);

        System.out.println(", a=" + a._sName);
        System.out.print(
            " " + PrintState(_state) + "  " + MDP._df.format(reward) + ": " + "Step " + step);
      }
      values.add(new Double(reward));
      System.out.println();
    }

    return values;
  }
Ejemplo n.º 2
0
 public void testNaNValues() throws ClassNotFoundException, IOException {
   testWriteAndRead(
       new ComplexClass(
           rand.nextInt(),
           rand.nextLong(),
           "hello",
           rand.nextBoolean(),
           Double.NaN,
           rand.nextFloat()));
 }
Ejemplo n.º 3
0
 public void testWritingRandomValue() throws ClassNotFoundException, IOException {
   testWriteAndRead(
       new ComplexClass(
           rand.nextInt(),
           rand.nextLong(),
           Integer.toString(rand.nextInt()),
           rand.nextBoolean(),
           rand.nextDouble(),
           rand.nextFloat()));
 }
Ejemplo n.º 4
0
  static void testCopyInputStreamToFile(int size) throws IOException {
    Path tmpdir = createTempDirectory("blah");
    Path source = tmpdir.resolve("source");
    Path target = tmpdir.resolve("target");
    try {
      boolean testReplaceExisting = rand.nextBoolean();

      // create source file
      byte[] b = new byte[size];
      rand.nextBytes(b);
      write(source, b);

      // target file might already exist
      if (testReplaceExisting && rand.nextBoolean()) {
        write(target, new byte[rand.nextInt(512)]);
      }

      // copy from stream to file
      InputStream in = new FileInputStream(source.toFile());
      try {
        long n;
        if (testReplaceExisting) {
          n = copy(in, target, StandardCopyOption.REPLACE_EXISTING);
        } else {
          n = copy(in, target);
        }
        assertTrue(in.read() == -1); // EOF
        assertTrue(n == size);
        assertTrue(size(target) == size);
      } finally {
        in.close();
      }

      // check file
      byte[] read = readAllBytes(target);
      assertTrue(Arrays.equals(read, b));

    } finally {
      deleteIfExists(source);
      deleteIfExists(target);
      delete(tmpdir);
    }
  }
Ejemplo n.º 5
0
  private InputStream generateData() {
    final String LINE_SEPARATOR = System.getProperty("line.separator");

    StringWriter writer = new StringWriter();

    int maxMale = numGuests / 2;
    int maxFemale = numGuests / 2;

    int maleCount = 0;
    int femaleCount = 0;

    // init hobbies
    List hobbyList = new ArrayList();
    for (int i = 1; i <= maxHobbies; i++) {
      hobbyList.add("h" + i);
    }

    Random rnd = new Random();
    for (int i = 1; i <= numGuests; i++) {
      char sex = rnd.nextBoolean() ? 'm' : 'f';
      if (sex == 'm' && maleCount == maxMale) {
        sex = 'f';
      }
      if (sex == 'f' && femaleCount == maxFemale) {
        sex = 'm';
      }
      if (sex == 'm') {
        maleCount++;
      }
      if (sex == 'f') {
        femaleCount++;
      }

      List guestHobbies = new ArrayList(hobbyList);

      int numHobbies = minHobbies + rnd.nextInt(maxHobbies - minHobbies + 1);
      for (int j = 0; j < numHobbies; j++) {
        int hobbyIndex = rnd.nextInt(guestHobbies.size());
        String hobby = (String) guestHobbies.get(hobbyIndex);
        writer.write(
            "(guest (name n" + i + ") (sex " + sex + ") (hobby " + hobby + "))" + LINE_SEPARATOR);
        guestHobbies.remove(hobbyIndex);
      }
    }
    writer.write("(last_seat (seat " + numSeats + "))" + LINE_SEPARATOR);

    writer.write(LINE_SEPARATOR);
    writer.write("(context (state start))" + LINE_SEPARATOR);

    return new ByteArrayInputStream(writer.getBuffer().toString().getBytes());
  }
Ejemplo n.º 6
0
  Item() {
    z = rand.nextBoolean();
    b = (byte) rand.nextInt();
    c = (char) rand.nextInt();
    s = (short) rand.nextInt();
    i = rand.nextInt();
    f = rand.nextFloat();
    j = rand.nextLong();
    d = rand.nextDouble();

    zary = new boolean[ARRAYLEN];
    bary = new byte[ARRAYLEN];
    cary = new char[ARRAYLEN];
    sary = new short[ARRAYLEN];
    iary = new int[ARRAYLEN];
    fary = new float[ARRAYLEN];
    jary = new long[ARRAYLEN];
    dary = new double[ARRAYLEN];
    oary = new Object[ARRAYLEN];

    for (int i = 0; i < ARRAYLEN; i++) {
      zary[i] = rand.nextBoolean();
      bary[i] = (byte) rand.nextInt();
      cary[i] = (char) rand.nextInt();
      sary[i] = (short) rand.nextInt();
      iary[i] = rand.nextInt();
      fary[i] = rand.nextFloat();
      jary[i] = rand.nextLong();
      dary[i] = rand.nextDouble();
      oary[i] = new Integer(rand.nextInt());
    }

    char[] strChars = new char[STRLEN];
    for (int i = 0; i < STRLEN; i++) {
      strChars[i] = (char) rand.nextInt();
    }
    str = new String(strChars);
  }
Ejemplo n.º 7
0
 public void testNullValues() throws ClassNotFoundException, IOException {
   testWriteAndRead(
       new ComplexClass(
           rand.nextInt(), rand.nextLong(), null, rand.nextBoolean(), null, rand.nextFloat()));
 }
Ejemplo n.º 8
0
  public void nextLevel() {
    boolean loaded = false;
    final GameBoard boardView = (GameBoard) this.findViewById(R.id.gameBoard);
    this.level++;

    AssetManager am = getResources().getAssets();

    try {
      List<String> allTutoLevels =
          new LinkedList<String>(Arrays.asList(am.list("levels/tutorial")));

      // if(addMsg){
      //    allTutoLevels.addAll(Arrays.asList(am.list("msg")));
      // }

      Log.d(TAG, allTutoLevels.toString());

      for (String name : allTutoLevels) {
        if (name.startsWith(this.level + ".")) {
          BufferedReader br =
              new BufferedReader(new InputStreamReader(am.open("levels/tutorial/" + name)));
          String line;
          String levelJSON = "";

          while ((line = br.readLine()) != null) {
            levelJSON += line + "\n";
          }

          br.close();

          game.initGame(
              levelJSON, boardView.getMeasuredWidth() / 60, boardView.getMeasuredHeight() / 60);
          loaded = true;
        }
      }
    } catch (IOException e) {
      e.printStackTrace();
    }

    if (!loaded) {
      Random r = new Random();

      List<String> allLevels = new ArrayList<>();

      try {
        allLevels = Arrays.asList(am.list("levels"));
      } catch (IOException e) {
      }

      if (r.nextBoolean() && !allLevels.isEmpty() && allLevels.size() != allDoneLevels.size()) {
        try {
          int nLevel;
          do {
            nLevel = r.nextInt(allLevels.size());
          } while (allDoneLevels.contains(allLevels.get(nLevel)));

          String name = allLevels.get(nLevel);
          BufferedReader br = new BufferedReader(new InputStreamReader(am.open("levels/" + name)));

          String line;
          String levelJSON = "";

          while ((line = br.readLine()) != null) {
            levelJSON += line + "\n";
          }

          br.close();

          allDoneLevels.add(name);

          game.initGame(
              levelJSON, boardView.getMeasuredWidth() / 60, boardView.getMeasuredHeight() / 60);
        } catch (IOException e) {
          this.game.initGame(
              level, boardView.getMeasuredWidth() / 60, boardView.getMeasuredHeight() / 60);
        }
      } else {
        this.game.initGame(
            level, boardView.getMeasuredWidth() / 60, boardView.getMeasuredHeight() / 60);
      }
    }

    // am.close();

    boardView.setGame(this.game);
    boardView.invalidate();
    boardView.getHowdyShadeView().invalidate();

    Toast.makeText(this, this.level + "", Toast.LENGTH_SHORT).show();

    Log.i(
        TAG,
        "Max tiles : "
            + (boardView.getMeasuredWidth() / 60) * (boardView.getMeasuredHeight() / 60));
  }
Ejemplo n.º 9
0
 static boolean heads() {
   return rand.nextBoolean();
 }
Ejemplo n.º 10
0
  public static Vector<Double> trainNode(
      String filename,
      int outputRepresentation,
      String inputRepresentation,
      int filter,
      double learningRate,
      int epochs) {
    Vector<Double> node = null; // initialize node to null
    double error = 1; // initialize error
    double output = 0; // initialize output
    Vector<Double> trainError = new Vector<Double>();
    double meansq = 0;
    String binTarget = "";
    String binaryRep = "";
    int target;
    openFile(filename); // open our file for training
    firstRead = true;
    int targetCounter = 0;
    while ((target = readFileIntoInputVector(inputRepresentation))
        != -2) { // while we have not reached out end of file error
      if (outputRepresentation == 4) {
        binTarget = Integer.toBinaryString(target); // convert target to binary
        int[] targetArray = new int[4]; // array to hold binary ints
        for (int h = 0; h < targetArray.length; h++) { // add binary elements to int array
          try {
            targetArray[targetArray.length - h - 1] =
                Character.digit(binTarget.charAt(binTarget.length() - h - 1), 10);
          } catch (IndexOutOfBoundsException e) { // if there is empty spaces, fill with zeros
            targetArray[targetArray.length - h - 1] = 0;
          }
        }
        target = targetArray[filter];
        binTarget = "";
        for (int i = 0; i < targetArray.length; i++) {
          binTarget = binTarget + targetArray[i];
        }
      }

      if ((filter == target && outputRepresentation == 10) || outputRepresentation != 10) {
        if (node == null) {
          node = new Vector<Double>();
          for (int i = 0; i < inputs.size(); i++) { // for each input
            double randWeight = 1; // initialize random weight
            while (randWeight > 0.15) // while out random isn't less than 0.15
            randWeight = random.nextDouble(); // get a new random double
            if (random.nextBoolean()) randWeight = randWeight * -1; // randomly set to negative
            node.addElement(new Double(randWeight)); // store random weight
          }
        }
        targetCounter++;
        // train for number of epochs
        for (int i = 0; i < epochs; i++) { // for each epoch
          double weightedSum = 0; // set/reset the weighted sum to 0
          for (int j = 0; j < inputs.size(); j++) {
            weightedSum += (inputs.elementAt(j) * node.elementAt(j)); // calculate weighted sum
          }
          output = activation(weightedSum); // retrieve output
          if (outputRepresentation == 1 || outputRepresentation == 10) output = output * 10;
          else if (outputRepresentation == 4) { // each node must have a whole number
            if (output > 0.5) output = 1;
            else output = 0;
          }

          error = target - output; // calculate error

          if (i == epochs - 1) {
            if (outputRepresentation == 4) {
              if (filter == 0) {
                allOutputs.ensureCapacity(targetCounter);
                allOutputs.add(Integer.toString((int) output));
              } else {
                allOutputs.set(
                    targetCounter - 1, allOutputs.get(targetCounter - 1) + ((int) output));
              }
            }
            trainError.add(error);
            if (Math.round(error) == 0 && outputRepresentation != 4) {
              trainCountCorrect++;
            } else if (outputRepresentation == 4 && filter == 3) {
              if (allOutputs.get(targetCounter - 1).equals(binTarget)) {
                trainCountCorrect++;
              }
            }
          }

          for (int k = 0; k < inputs.size(); k++) { // for each input
            double derivative =
                (1.64872 * Math.exp(weightedSum))
                    / (Math.pow(1.64872 + Math.exp(weightedSum), 2)); // calculate new weights
            double newWeight =
                node.elementAt(k)
                    + (learningRate * inputs.elementAt(k) * error * derivative); // cont.
            node.setElementAt(newWeight, k); // update weights
          }
        }
      }
      inputs.clear();
    }
    errorVect.addElement((Vector<Double>) trainError.clone());
    trainError.clear();
    if (outputRepresentation == 4 && filter == 3)
      System.out.println(
          "Training Percent: " + (100 * (double) trainCountCorrect / (double) targetCounter));
    if (outputRepresentation == 10 && filter == 9)
      System.out.println(
          "Training Percent: " + (10 * (double) trainCountCorrect / (double) targetCounter));
    if (outputRepresentation == 1)
      System.out.println(
          "Training Percent: " + (100 * (double) trainCountCorrect / (double) targetCounter));
    closeFile();
    return node; // node is now trained for an epoch
  }