CoordinateAscentLearner extends Learner { @Override public RetrievalModelInstance learn() throws Exception { final List<RetrievalModelInstance> learntParams = Collections.synchronizedList(new ArrayList<RetrievalModelInstance>()); if (threading) { ExecutorService threadPool = Executors.newFixedThreadPool(threadCount); final List<Exception> exceptions = Collections.synchronizedList(new ArrayList<Exception>()); final CountDownLatch latch = new CountDownLatch(restarts); for (int i = 0; i < restarts; i++) { final RetrievalModelInstance settingsInstance; if (initialSettings.size() > i) { settingsInstance = initialSettings.get(i).clone(); } else { settingsInstance = generateRandomInitalValues(); } settingsInstance.setAnnotation("name", name + "-randomStart-" + i); Thread t = new Thread() { @Override public void run() { try { RetrievalModelInstance s = runCoordAscent(settingsInstance); s.setAnnotation("score", Double.toString(evaluate(s))); learntParams.add(s); synchronized (outputPrintStream) { outputPrintStream.println(s.toString()); outputPrintStream.flush(); } } catch (Exception e) { exceptions.add(e); synchronized (outputTraceStream) { outputTraceStream.println(e.toString()); outputTraceStream.flush(); } } finally { latch.countDown(); } } }; threadPool.execute(t); } while (latch.getCount() > 0) { logger.info(String.format("Waiting for %d threads.", latch.getCount())); try { latch.await(10, TimeUnit.SECONDS); } catch (InterruptedException e) { } } threadPool.shutdown(); if (!exceptions.isEmpty()) { for (Exception e : exceptions) { System.err.println("Caught exception: \n" + e.toString()); e.printStackTrace(); } } } else { for (int i = 0; i < restarts; i++) { final RetrievalModelInstance settingsInstance; if (initialSettings.size() > i) { settingsInstance = initialSettings.get(i).clone(); } else { settingsInstance = generateRandomInitalValues(); } settingsInstance.setAnnotation("name", name + "-randomStart-" + i); try { RetrievalModelInstance s = runCoordAscent(settingsInstance); s.setAnnotation("score", Double.toString(evaluate(s))); learntParams.add(s); synchronized (outputPrintStream) { outputPrintStream.println(s.toString()); outputPrintStream.flush(); } } catch (Exception e) { System.err.println("Caught exception: \n" + e.toString()); e.printStackTrace(); synchronized (outputTraceStream) { outputTraceStream.println(e.toString()); outputTraceStream.flush(); } } } } if (learntParams.isEmpty()) { return generateRandomInitalValues(); } else { RetrievalModelInstance best = learntParams.get(0); double bestScore = Double.parseDouble(best.getAnnotation("score")); for (RetrievalModelInstance inst : learntParams) { double score = Double.parseDouble(inst.getAnnotation("score")); if (bestScore < score) { best = inst; bestScore = score; } } best.setAnnotation("name", name + "-best"); outputPrintStream.println(best.toString()); outputPrintStream.flush(); return best; } } CoordinateAscentLearner(Parameters p, Retrieval r); @Override RetrievalModelInstance learn(); RetrievalModelInstance runCoordAscent(RetrievalModelInstance parameterSettings);  }
@Test public void testCoordSearch() 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()); 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", "coord"); learnParams.set("qrels", qrels.getAbsolutePath()); List<Parameters> learnableParams = new ArrayList<>(); learnableParams.add(Parameters.parseString("{\"name\":\"0\",\"min\":0.0,\"max\":1.0}")); learnableParams.add(Parameters.parseString("{\"name\":\"1\",\"min\":0.0,\"max\":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", 1.0); learnParams.set("normalization", new ArrayList()); learnParams.getList("normalization", Parameters.class).add(normalRule); learnParams.set("restarts", 1); learnParams.set("initialParameters", new ArrayList()); learnParams.set("limitRange", true); learnParams.getList("initialParameters", Parameters.class).add(Parameters.parseString("{\"0\":0.9,\"1\":0.2}")); Learner learner = LearnerFactory.instance(learnParams, ret); assertNotNull(learner); RetrievalModelInstance res = learner.learn(); assertNotNull(res); } finally { if (index != null) { FSUtil.deleteDirectory(index); } if (qrels != null) { qrels.delete(); } } }