ArrayList<ArrayList> results = new ArrayList<ArrayList>();
HashSet<String> uniqueConfigs = new HashSet<String>();
// map from algo to the current best scores and the corresponding config
HashMap<String,Hashtable> algo2BestConfig = new HashMap<String,Hashtable>();
TObjectDoubleHashMap algo2BestScore = new TObjectDoubleHashMap();
// store console
PrintStream consoleOut = System.out;
PrintStream consoleErr = System.err;
for (int ci = 0; ci < configs.size(); ++ci) {
Hashtable c = configs.get(ci);
// if this a post-tune config, then generate seed and test files
if (Defaults.GetValueOrDefault((String) c.get("is_final_run"), false)) {
String splitId = Defaults.GetValueOrDie(c, "split_id");
c.put("seed_file", c.remove("seed_base") + "." + splitId + ".train");
c.put("test_file", c.remove("test_base") + "." + splitId + ".test");
}
// output file name is considered a unique identifier of a configuration
String outputFile = GetOutputFileName(c, opDir, idenStr);
if (uniqueConfigs.contains(outputFile)) {
continue;
}
uniqueConfigs.add(outputFile);
if (opDir != null) {
c.put("output_file", outputFile);
}
System.out.println("Working with config: " + c.toString());
try {
// reset System.out so that the log printed using System.out.println
// is directed to the right log file
String logFile = GetLogFileName(c, logDir, idenStr);
// if the log file exists, then don't repeat
File lf = new File(logFile);
if (skipExistingConfigs && lf.exists()) {
continue;
}
FileOutputStream fos = new FileOutputStream(new File(logFile));
PrintStream ps = new PrintStream(fos);
System.setOut(ps);
System.setErr(ps);
results.add(new ArrayList());
JuntoConfigRunner.apply(c, results.get(results.size() - 1));
UpdateBestConfig((String) c.get("algo"), algo2BestScore,
algo2BestConfig, c, results.get(results.size() - 1));
// reset System.out back to the original console value
System.setOut(consoleOut);
System.setErr(consoleErr);
// close log file
fos.close();
} catch (FileNotFoundException fnfe) {
fnfe.printStackTrace();
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
// print out the best parameters for each algorithm
Iterator algoIter = algo2BestConfig.keySet().iterator();
while (algoIter.hasNext()) {
String algo = (String) algoIter.next();
System.out.println("\n#################\n" +
"BEST_CONFIG_FOR " + algo + " " +
algo2BestScore.get(algo) + "\n" +
CollectionUtil.Map2StringPrettyPrint(algo2BestConfig.get(algo)));
// run test with tuned parameters, if requested
if (finalTestConfigFile != null) {
Hashtable finalTestConfig = (Hashtable) algo2BestConfig.get(algo).clone();