Example #1
0
  public void analyze() throws Exception {
    HashMap<String, ArrayList<String>> outputDataPaths = new HashMap<String, ArrayList<String>>();

    File outDir = new File(_outputPath);

    File reportDir = new File(_reportPath);
    if (!reportDir.exists()) reportDir.mkdirs();

    FilenameFilter filter =
        new FilenameFilter() {

          public boolean accept(File arg0, String arg1) {
            if (arg1.equals(".svn")) return false;
            else return true;
          }
        };

    // delete all in Statistics directory
    for (String s : reportDir.list(filter)) new File(_reportPath + "/" + s).delete();

    DataNode report = new DataNode("report");
    // list all Output|s directories
    for (String s : outDir.list(filter)) {
      String config = _outputPath + "/" + s + "/config.xml";
      if (new File(config).exists()) {
        List<DataNode> runs =
            processDirectorySet(
                new File(_outputPath + "/" + s).listFiles(), _outputPath, _reportPath, s);

        DataNode batch = new DataNode("batch");
        for (DataNode dn : runs) batch.putDataNode(dn);

        batch.putValue("id", s);

        String[] date = s.split("-");
        batch.putValue("date", date[0] + "-" + date[1]);
        batch.putDataNode(XmlHelper.readXml(new File(config)));

        report.putDataNode(batch);
      }
    }

    XmlHelper.writeXml(report, _reportPath + "/report.xml");

    TransformerFactory tFactory = TransformerFactory.newInstance();
    Transformer transformer =
        tFactory.newTransformer(
            new javax.xml.transform.stream.StreamSource(
                getClass().getResourceAsStream("report.xsl")));

    transformer.transform(
        new javax.xml.transform.stream.StreamSource(_reportPath + "/report.xml"),
        new javax.xml.transform.stream.StreamResult(
            new FileOutputStream(_reportPath + "/report.html")));
  }
Example #2
0
  public void runFromConfigFile(String configPath) throws Exception {
    ProblemConfig config = new ProblemConfig(XmlHelper.readXml(new File(configPath)));
    String problemID = config.getDataNode("Problem").getValueStr("id");
    String algorithmID = config.getDataNode("Algorithm").getValueStr("id");

    // provider and factory
    IProblemProvider provider = ProblemProvider.getProblemProviders().get(problemID);
    IAlgorithmFactory factory = provider.getAlgorithmFactory(algorithmID);

    // problem instance
    ProblemInstanceInfo instance = provider.initProblemInstance(config);

    // algorithm
    IAlgorithmAdapter algorithm = factory.createAlgorithm(instance, config);

    AlgorithmParams algNode = config.getAlgorithmParams();
    Object[][] solutions =
        provider.generateInitialSolutions(
            algNode.getDataNode("Parameters").getValueInt("numSolutions"), instance);

    System.out.printf("%s: %4s %s\n", "Problem", "", problemID);
    System.out.printf("%s: %2s %s\n", "Algorithm", "", algorithmID);
    System.out.printf("%s: %3s %s\n", "Instance", "", instance);
    System.out.println("Running ...");
    algorithm.solutionsFromPhenotype(solutions);
    algorithm.setParameters(algNode);
    algorithm.startSearching();
    solutions = algorithm.solutionsToPhenotype();

    // phenotype evaluator
    IPhenotypeEvaluator evaluator = provider.initPhenotypeEvaluator();
    double[] result = evaluator.evaluate(solutions[0], instance);

    System.out.printf("%s: %5s %s\n", "Result", "", result[0]);
    // System.out.println(": " + result[0]);

    System.out.printf("%s: %3s ", "Solution", "");
    for (int i = 0; i < solutions[0].length; i++) System.out.print(solutions[0][i] + " ");
    System.out.println();

    AlgorithmReport report = algorithm.getReport();
    report.save("output/" + System.currentTimeMillis() + ".xml");
  }
Example #3
0
  private List<DataNode> processDirectorySet(
      File[] files, String destDirPath, String reportFileName, String hash) throws Exception {
    HashMap<String, List<Double>> _fitnesses = new HashMap<String, List<Double>>();

    for (File d : files) {
      if (!d.isDirectory()) continue;

      File problemXml = new File(d.getPath() + "/Problem.xml");

      if (problemXml.exists()) {
        DataNode data = XmlHelper.readXml(problemXml);
        List<DataNode> solList = data.getDataNodes("Solution");
        if (solList.size() > 0) {
          double fitness = solList.get(solList.size() - 1).getValueDouble("fitness");
          String instance = data.getValueStr("instance");
          if (!_fitnesses.containsKey(instance)) _fitnesses.put(instance, new ArrayList<Double>());
          _fitnesses.get(instance).add(fitness);
        }
      }
    }

    ArrayList<DataNode> runSet = new ArrayList<DataNode>();

    for (String in : _fitnesses.keySet()) {
      DataNode run = new DataNode("run");
      run.putValue("instance", in);

      Pattern p = Pattern.compile("^[a-zA-Z]+([0-9]+).*");
      Matcher m = p.matcher(in);
      if (m.find()) {
        run.putValue("size", m.group(1));
      }

      run.putDataNode(new DataNode("stats"));

      DataNode stats = run.getDataNode("stats");

      double fitness = 0;
      double fitness2 = 0;
      double min = Integer.MAX_VALUE;
      double max = Integer.MIN_VALUE;
      int numSolutions = 0;

      for (Double f : _fitnesses.get(in)) {
        fitness += f;
        fitness2 += f * f;
        if (f < min) min = f;
        if (f > max) max = f;

        numSolutions++;
      }

      stats.putValue("minFitness", min);
      stats.putValue("avgFitness", fitness / numSolutions);
      stats.putValue(
          "devFitness",
          Math.sqrt(fitness2 / numSolutions - Math.pow(stats.getValueDouble("avgFitness"), 2)));
      stats.putValue(
          "cfvFitness", stats.getValueDouble("devFitness") / stats.getValueDouble("avgFitness"));
      stats.putValue("maxFitness", max);
      stats.putValue("numOfRuns", numSolutions);

      runSet.add(run);
      // XmlHelper.writeXml(run, destDirPath+"/"+reportFileName+".xml");
    }
    return runSet;
  }