Beispiel #1
0
 private int getRandomGame(int num) {
   int ranGame;
   Random randomNum = new Random();
   ranGame = randomNum.nextInt(num) + 1;
   while (!gameRecord.containsKey(ranGame)) {
     ranGame = randomNum.nextInt(num) + 1;
   }
   return ranGame;
 }
Beispiel #2
0
  private JSONObject specialMove(int gameCount) {
    Random randomNum = new Random();
    GameInfo gameInfo;
    JSONObject jObj = new JSONObject();
    JSONObject action = new JSONObject();
    int game;
    int addPoints;
    int special;
    // Gets random number to denote which game to use action on
    game = getRandomGame(gameCount);
    gameInfo = gameRecord.get(game);
    // Checks if all specials are used
    if (gameInfo.checkSpecial()) {
      return moveUser(gameCount);
    }
    // Gets random number to denote which special move to use
    special = randomNum.nextInt(4);
    while (gameInfo.checkSpecialMove(special)) {
      special = randomNum.nextInt(4);
    }
    gameInfo.useSpecialMove(special);

    addPoints = randomNum.nextInt(41) - 20;
    gameInfo.addPoints(addPoints);
    gameInfo.incrementCount();

    try {
      jObj.put("game", game);

      action.put("actionType", "specialMove");
      action.put("actionNumber", gameInfo.getCount());
      action.put("pointsAdded", addPoints);
      if (special == SHUFFLE) {
        action.put("move", "Shuffle");
      } else if (special == CLEAR) {
        action.put("move", "Clear");
      } else if (special == INVERT) {
        action.put("move", "Invert");
      } else {
        action.put("move", "Rotate");
      }
      action.put("points", gameInfo.getPoints());

      jObj.put("action", action);
      jObj.put("user", gameInfo.getUser());
    } catch (JSONException e) {
      System.out.println("could not put");
    }
    return jObj;
  }
Beispiel #3
0
 private JSONObject createNewUser(int gameId) {
   int userId;
   JSONObject jObj = new JSONObject();
   JSONObject action = new JSONObject();
   Random randomNum = new Random();
   userId = randomNum.nextInt(max_users) + 1;
   while (userIds[userId] == true) {
     userId = randomNum.nextInt(max_users) + 1;
   }
   userIds[userId] = true;
   try {
     jObj.put("game", gameId);
     action.put("actionType", "gameStart");
     action.put("actionNumber", 1);
     jObj.put("action", action);
     jObj.put("user", "u" + userId);
   } catch (JSONException e) {
     System.out.println("could not put");
   }
   gameRecord.put(gameId, new GameInfo("u" + userId, userId));
   return jObj;
 }
Beispiel #4
0
  private JSONObject moveUser(int gameCount) {
    Random randomNum = new Random();
    GameInfo gameInfo;
    JSONObject jObj = new JSONObject();
    JSONObject action = new JSONObject();
    JSONObject coords = new JSONObject();
    int game;
    int xCoord;
    int yCoord;
    int addPoints;
    // Gets random number to denote which game to use action on
    game = getRandomGame(gameCount);

    xCoord = randomNum.nextInt(20) + 1;
    yCoord = randomNum.nextInt(20) + 1;
    addPoints = randomNum.nextInt(41) - 20;
    gameInfo = gameRecord.get(game);
    gameInfo.addPoints(addPoints);
    gameInfo.incrementCount();
    try {
      jObj.put("game", game);

      coords.put("x", xCoord);
      coords.put("y", yCoord);

      action.put("actionType", "Move");
      action.put("actionNumber", gameInfo.getCount());
      action.put("location", coords);
      action.put("pointsAdded", addPoints);
      action.put("points", gameInfo.getPoints());

      jObj.put("action", action);
      jObj.put("user", gameInfo.getUser());
    } catch (JSONException e) {
      System.out.println("could not put");
    }
    return jObj;
  }
Beispiel #5
0
  private void createData(int size, String outputFilename) {
    int command;
    int userId;
    int gameCount = 1;
    Random randomNum = new Random();
    File outputFile = new File(outputFilename);
    PrintWriter writer = null;
    JSONObject jObj = null;
    userIds = new boolean[max_users + 1];
    gameRecord = new Hashtable<Integer, GameInfo>();

    try {
      writer = new PrintWriter(new FileWriter(outputFile));
    } catch (IOException e) {
      System.out.println("Could not create file " + outputFilename);
    }

    writer.write("[\n");

    for (int i = 0; i < size; i++) {
      if (gameRecord.isEmpty()) {
        jObj = createNewUser(gameCount++);
      } else if (gameRecord.size() == max_users) {
        jObj = endGame(gameCount);
      } else {
        command = randomNum.nextInt(14);
        switch (command) {
          case 0:
            jObj = createNewUser(gameCount++);
            break;
          case 1:
          case 2:
          case 3:
          case 4:
          case 5:
          case 6:
          case 7:
          case 8:
            jObj = moveUser(gameCount);
            break;
          case 9:
          case 10:
          case 11:
          case 12:
            jObj = specialMove(gameCount);
            break;
          case 13:
            jObj = endGame(gameCount);
            break;
        }
      }
      printObj(jObj, writer);
      if (i != (size - 1)) {
        writer.write(",\n");
      } else {
        writer.write("\n");
      }
    }
    writer.write("]");
    writer.close();
  }
Beispiel #6
0
  public static void main(String[] args) throws Throwable {
    DATASET = System.getProperty("dataset", "movielens-pos.json");
    TRAINPERCENT = Double.parseDouble(System.getProperty("trainPercent", "0.8"));
    TESTPERCENT = Double.parseDouble(System.getProperty("testPercent", "0.5"));
    RUNS = Integer.parseInt(System.getProperty("runs", "1"));
    PREDICTIONS = Integer.parseInt(System.getProperty("predictions", "1"));
    // rand = new Random( Integer.parseInt( System.getProperty( "seed", "1" ) ) );
    rand = new Random();

    System.out.println("File: " + DATASET);
    System.out.println("Train Percent: " + TRAINPERCENT);
    System.out.println("Test Percent: " + TESTPERCENT);
    System.out.println("Runs: " + RUNS);
    System.out.println("Predictions: " + PREDICTIONS);
    docs = DatasetOld.loadDataset(DATASET);

    for (Algorithm alg : algs) {
      System.out.print(alg.name + "\t");
      double total = 0.0;

      long trainTime = 0;
      long predictTime = 0;

      for (int run = 0; run < RUNS; run++) {
        ArrayList<HashMap<Integer, Double>> traindocs = new ArrayList<HashMap<Integer, Double>>();
        ArrayList<HashMap<Integer, Double>> testdocs = new ArrayList<HashMap<Integer, Double>>();

        for (HashMap<Integer, Double> doc : docs) {
          if (rand.nextDouble() < TRAINPERCENT) traindocs.add(doc);
          else testdocs.add(doc);
        }

        long startTime = System.nanoTime();
        alg.train(traindocs);
        trainTime += System.nanoTime() - startTime;

        int successes = 0;

        for (HashMap<Integer, Double> testdoc : testdocs) {
          HashMap<Integer, Double> givenwords = new HashMap<Integer, Double>();
          HashSet<Integer> testwords = new HashSet<Integer>();

          for (int word : testdoc.keySet()) {
            if (rand.nextDouble() < TESTPERCENT) {
              testwords.add(word);
            } else {
              givenwords.put(word, testdoc.get(word));
            }
          }

          startTime = System.nanoTime();
          double[] scores = alg.predict(givenwords);
          predictTime += System.nanoTime() - startTime;
          // System.out.println(Arrays.toString( scores ));
          PriorityQueue<Pair> pq = new PriorityQueue<Pair>();

          for (int i = 0; i < scores.length; i++) {
            if (givenwords.containsKey(i)) {
              continue;
            }

            if (pq.size() < PREDICTIONS) {
              pq.add(new Pair(i, scores[i]));
            }
            if (scores[i] > pq.peek().score) {
              pq.poll();
              pq.add(new Pair(i, scores[i]));
            }
          }

          while (!pq.isEmpty()) {
            Pair pair = pq.poll();
            // System.out.println( WordIndex.get( pair.word ) + "\t" + pair.score + "\t" +
            // testwords.contains( pair.word ) );
            if (testwords.contains(pair.word)) {
              successes++;
            }
          }
        }

        total += (double) successes / PREDICTIONS / testdocs.size();
      }

      System.out.println(
          total / RUNS
              + "\t"
              + (trainTime / 1000000000.0 / RUNS)
              + "\t"
              + (predictTime / 1000000000.0 / RUNS));
    }
  }