Ejemplo n.º 1
0
  @Override
  public InputData readInput(Scanner scanner, int testCase) {

    InputData in = new InputData(testCase);

    in.N = scanner.nextInt();

    return in;
  }
Ejemplo n.º 2
0
  @Override
  public InputData readInput(Scanner scanner, int testCase) {

    InputData in = new InputData(testCase);

    in.N = scanner.nextInt();
    in.highest = new int[in.N - 1];

    for (int n = 0; n < in.N - 1; ++n) {
      in.highest[n] = scanner.nextInt();
    }

    return in;
  }
Ejemplo n.º 3
0
  @Override
  public void execute() throws IOException, RecommenderBuildException {
    LenskitConfiguration dataConfig = input.getConfiguration();
    LenskitRecommenderEngineBuilder builder = LenskitRecommenderEngine.newBuilder();
    for (LenskitConfiguration config : environment.loadConfigurations(getConfigFiles())) {
      builder.addConfiguration(config);
    }
    builder.addConfiguration(dataConfig, ModelDisposition.EXCLUDED);

    Stopwatch timer = Stopwatch.createStarted();
    LenskitRecommenderEngine engine = builder.build();
    timer.stop();
    logger.info("built model in {}", timer);
    File output = getOutputFile();
    CompressionMode comp = CompressionMode.autodetect(output);
    logger.info("writing model to {}", output);
    Closer closer = Closer.create();
    try {
      OutputStream stream = closer.register(new FileOutputStream(output));
      stream = closer.register(comp.wrapOutput(stream));
      engine.write(stream);
    } catch (Throwable th) { // NOSONAR using a closer
      throw closer.rethrow(th);
    } finally {
      closer.close();
    }
  }
Ejemplo n.º 4
0
  @Override
  public InputData readInput(Scanner scanner, int testCase) {

    InputData in = new InputData(testCase);
    in.N = scanner.nextInt();

    in.levels = Lists.newArrayList();

    /** Number of points(stars) prereq to play level 1 ; Number to play level 2 */
    for (int i = 0; i < in.N; ++i) {
      in.levels.add(new Level(scanner.nextInt(), scanner.nextInt()));
    }

    // log.info("TestCase {} Grid {}", testCase, in.grid);
    return in;
  }
Ejemplo n.º 5
0
 private LenskitRecommenderEngine loadEngine() throws RecommenderBuildException, IOException {
   File modelFile = options.get("model_file");
   if (modelFile == null) {
     logger.info("creating fresh recommender");
     LenskitRecommenderEngineBuilder builder = LenskitRecommenderEngine.newBuilder();
     for (LenskitConfiguration config : environment.loadConfigurations(getConfigFiles())) {
       builder.addConfiguration(config);
     }
     builder.addConfiguration(input.getConfiguration());
     Stopwatch timer = Stopwatch.createStarted();
     LenskitRecommenderEngine engine = builder.build();
     timer.stop();
     logger.info("built recommender in {}", timer);
     return engine;
   } else {
     logger.info("loading recommender from {}", modelFile);
     LenskitRecommenderEngineLoader loader = LenskitRecommenderEngine.newLoader();
     for (LenskitConfiguration config : environment.loadConfigurations(getConfigFiles())) {
       loader.addConfiguration(config);
     }
     loader.addConfiguration(input.getConfiguration());
     Stopwatch timer = Stopwatch.createStarted();
     LenskitRecommenderEngine engine;
     CompressionMode comp = CompressionMode.autodetect(modelFile);
     InputStream input = new FileInputStream(modelFile);
     try {
       input = comp.wrapInput(input);
       engine = loader.load(input);
     } finally {
       input.close();
     }
     timer.stop();
     logger.info("loaded recommender in {}", timer);
     return engine;
   }
 }
Ejemplo n.º 6
0
  @Test
  public void setCalculateSunrisesSunsets() {

    String[] coordinates = {InputData.LATITUDE_NAME, InputData.LONGITUDE_NAME};
    for (String memberName : coordinates) {
      assertTrue(
          memberName + " should NOT cause error, when sunrise/sunset calculation isn't needed.",
          !isError(memberName));
    }

    inputData.setCalculateSunrisesSunsets(InputData.DEFAULT_TRUE);

    for (String memberName : coordinates) {
      assertTrue("Null " + memberName + " should cause error.", isError(memberName));
    }
  }
Ejemplo n.º 7
0
 public static void configureArguments(ArgumentParser parser) {
   parser.description("Trains a recommendation model and write it to disk.");
   ScriptEnvironment.configureArguments(parser);
   InputData.configureArguments(parser);
   parser
       .addArgument("-o", "--output-file")
       .type(File.class)
       .metavar("FILE")
       .setDefault("model.bin")
       .help("write trained model to FILE");
   parser
       .addArgument("config")
       .type(File.class)
       .nargs("+")
       .metavar("CONFIG")
       .help("load algorithm configuration from CONFIG");
 }
Ejemplo n.º 8
0
 public static void configureArguments(ArgumentParser parser) {
   InputData.configureArguments(parser);
   ScriptEnvironment.configureArguments(parser);
   parser
       .addArgument("-c", "--config-file")
       .type(File.class)
       .action(Arguments.append())
       .metavar("FILE")
       .help("use configuration from FILE");
   parser
       .addArgument("-m", "--model-file")
       .type(File.class)
       .metavar("FILE")
       .help("load model from FILE");
   parser.addArgument("--print-channel").metavar("CHAN").help("also print value from CHAN");
   parser.addArgument("user").type(Long.class).metavar("USER").help("predict for USER");
   parser
       .addArgument("items")
       .type(Long.class)
       .metavar("ITEM")
       .nargs("+")
       .help("predict for ITEMs");
 }
Ejemplo n.º 9
0
  @Test
  public void setLatitudeLongitude() {

    java.lang.reflect.Method method;
    String[] coordinates = {InputData.LATITUDE_NAME, InputData.LONGITUDE_NAME};

    try {

      for (String memberName : coordinates) {

        String methodName = "set" + memberName;
        method = inputData.getClass().getMethod(methodName, String.class);
        inputData.initialize();
        inputData.setCalculateSunrisesSunsets(null);

        assertTrue(
            methodName
                + ": At the beginning, there should be no errors:\n\n"
                + inputData.toString(),
            !isError(memberName));

        method.invoke(inputData, "-961");
        assertTrue(
            methodName
                + ": Dont test range when sunrise/sunset calculation isn't needed:\n\n"
                + inputData.toString(),
            !isError(memberName));

        inputData.setCalculateSunrisesSunsets(InputData.DEFAULT_TRUE);
        double extreme = (memberName.equals(InputData.LATITUDE_NAME)) ? 90 : 180;

        double value = -(extreme + 0.1);
        method.invoke(inputData, "" + value);
        assertTrue(
            methodName + " @ " + value + ": should fail:\n\n" + inputData.toString(),
            isError(memberName));

        value = -extreme;
        method.invoke(inputData, "" + value);
        assertTrue(
            methodName + " @ " + value + ": should NOT fail:\n\n" + inputData.toString(),
            !isError(memberName));

        value = 0;
        method.invoke(inputData, "" + value);
        assertTrue(
            methodName + " @ " + value + ": should NOT fail:\n\n" + inputData.toString(),
            !isError(memberName));

        value = extreme;
        method.invoke(inputData, "" + value);
        assertTrue(
            methodName + " @ " + value + ": should NOT fail:\n\n" + inputData.toString(),
            !isError(memberName));

        value = extreme + 1.2;
        method.invoke(inputData, "" + value);
        assertTrue(
            methodName + " @ " + value + ": should fail:\n\n" + inputData.toString(),
            isError(memberName));
      }

    } catch (Exception e) {
      fail("Exception : " + e.getMessage());
    }
  }
Ejemplo n.º 10
0
  public OutputData learn(InputData id, Target tg, int k, String task) {
    this.k = k;
    OutputData ret;

    this.col = id.getCol();
    this.row = id.getRow();
    this.groupNum = id.getGroup();
    this.task = task;
    this.groupRangeUpperLimit = id.getGroupRangeUpperLimit();

    init();

    int t = 0;
    for (int iter = 0; iter < 100; iter++) { // tmp
      System.out.println(iter); // ***********************
      double diff = 0;
      for (int p = 0; p < id.getRow(); p++, t++) {
        // double tmpEta = eater / (t0 + t*0.1);
        // double tmpEta = eater / Math.pow((t + 1), power_t);
        double eta = 0.005;

        Map<Integer, Double> record = id.getOneRecord(p); // pick up one record
        double y = tg.getOneTarget(p); // pickup the target for the chosen record

        w0 = w0 - eta * (calcGrad(0, record, y, "w0") + 2 * lambda0 * w0);
        // System.out.println("w0:" + w0);//**************

        for (int i : record.keySet()) {
          double gradWi = calcGrad(i, record, y, "w"); // dloss(predict("w", i), y);
          int pi = pi(i);
          double nextWi = w[i] - eta * (gradWi + 2 * lambdaW.get(pi) * w[i]);
          w[i] = nextWi;
          for (int f = 0; f < k; f++) {
            double gradVij =
                dloss(
                    predict(y, record),
                    y,
                    i,
                    f,
                    "v",
                    record); // calcGradV(i, f, pi, y, "v"); // dloss(predict("v", i, f), y)
            // System.out.println("key:" + key +  ", f:" + f + ", groupOfKey:" + groupOfKey);
            // //******************
            V[i][f] -= eta * (gradVij + 2 * lambdaV[f][pi] * V[i][f]);
            // System.out.println("V[key][f]:" + V[key][f]); //***************
          }
        }
        if (task.equals("regression")) {
          double pred = predict(y, record);
          double d = y - pred;
          diff += d * d;
        } else if (task.equals("classification")) {
          if (y != predict(y, record)) {
            // System.out.println("tg:" + this.tg + ", predict:" + predict());	//****
            diff++;
          }
        }
      }
      // effect evaluation

      results.add(diff);
    }

    System.out.println(results); // ****

    ret = new OutputData(w0, w, V);
    return ret;
  }