GridSearchLearner extends Learner { @Override public RetrievalModelInstance learn() throws Exception { List<String> params = new ArrayList<String>(this.learnableParameters.getParams()); RetrievalModelInstance settings; if (this.initialSettings.isEmpty()) { settings = super.generateRandomInitalValues(); } else { settings = this.initialSettings.get(0); } settings.setAnnotation("name", name); if (params.size() > 0) { RetrievalModelInstance best = gridLearn(0, params, settings, 0.0); best.normalize(); outputPrintStream.println(best.toString()); outputTraceStream.println("Final settings: " + best.toString()); return best; } outputTraceStream.println("No learnable parameters found. Quitting."); return settings; } GridSearchLearner(Parameters p, Retrieval r); @Override RetrievalModelInstance learn();  }
@Test public void testGridSearch() throws Exception { File index = null; File qrels = null; try { File[] files = TestingUtils.make10DocIndex(); files[0].delete(); FSUtil.deleteDirectory(files[1]); index = files[2]; qrels = FileUtility.createTemporary(); Retrieval ret = RetrievalFactory.instance(index.getAbsolutePath(), Parameters.create()); String qrelData = "q1 x 2 1\n" + "q1 x 5 1\n" + "q1 x 8 1\n" + "q2 x 3 1\n" + "q2 x 7 1\n"; StreamUtil.copyStringToFile(qrelData, qrels); Parameters learnParams = Parameters.parseString("{\"queries\": [{\"number\":\"q1\",\"text\":\"#combine:0=0.5:1=0.5( jump moon )\"}, {\"number\":\"q2\",\"text\":\"#combine:0=0.5:1=0.5( everything shoe )\"}]}"); learnParams.set("learner", "grid"); learnParams.set("qrels", qrels.getAbsolutePath()); List<Parameters> learnableParams = new ArrayList<>(); learnableParams.add(Parameters.parseString("{\"name\":\"0\", \"max\":1.0, \"min\":-1.0}")); learnableParams.add(Parameters.parseString("{\"name\":\"1\", \"max\":1.0, \"min\":-1.0}")); learnParams.set("learnableParameters", learnableParams); Parameters normalRule = Parameters.create(); normalRule.set("mode", "sum"); normalRule.set("params", Arrays.asList(new String[]{"0", "1"})); normalRule.set("value", 1D); learnParams.set("normalization", new ArrayList()); learnParams.getList("normalization", Parameters.class).add(normalRule); learnParams.set("gridSize", 3); learnParams.set("restarts", 1); Learner learner = LearnerFactory.instance(learnParams, ret); RetrievalModelInstance res = learner.learn(); Assert.assertNotNull(res); } finally { if (index != null) { FSUtil.deleteDirectory(index); } if (qrels != null) { Assert.assertTrue(qrels.delete()); } } }