Ejemplo n.º 1
0
  public void testPatterns(
      Pattern pCorreto, State state, List<RnaResult> results, List<Pattern> patterns, int hidden) {
    MultilayerPerceptron perceptron = new EQMFunction(Boolean.FALSE, 1).buildPerceptron(state);

    int number = eval(pCorreto, perceptron);
    log.fine(number + " de " + pCorreto.getX().length + " sem erro");
    results.add(
        new RnaResult(-2.0, 2.0, hidden, 0, number, pCorreto.getX().length, 0, state.getValue()));

    for (Pattern pattern : patterns) {
      number = eval(pattern, perceptron);
      log.fine(number + " de " + pCorreto.getX().length + " sem erro");
      results.add(
          new RnaResult(
              -2.0,
              2.0,
              hidden,
              0,
              number,
              pCorreto.getX().length,
              pattern.erro,
              state.getValue()));
    }
  }
Ejemplo n.º 2
0
  public void run() {
    try {

      List<Pattern> patterns = new ArrayList<Pattern>(4);

      Pattern pCorreto =
          loadFile120_8(
              Experimento_3.class.getResourceAsStream(
                  "/br/compnatural/atividade_3/char8_12x10.txt"),
              Experimento_3.class.getResourceAsStream(
                  "/br/compnatural/atividade_3/char8_8x8_1.txt"));

      Pattern p5Porcento =
          loadFile120_8(
              Experimento_3.class.getResourceAsStream(
                  "/br/compnatural/atividade_3/char8_12x10_5.txt"),
              Experimento_3.class.getResourceAsStream(
                  "/br/compnatural/atividade_3/char8_8x8_1.txt"));
      patterns.add(p5Porcento);
      p5Porcento.erro = 5;

      Pattern p10Porcento =
          loadFile120_8(
              Experimento_3.class.getResourceAsStream(
                  "/br/compnatural/atividade_3/char8_12x10_10.txt"),
              Experimento_3.class.getResourceAsStream(
                  "/br/compnatural/atividade_3/char8_8x8_1.txt"));
      patterns.add(p10Porcento);
      p10Porcento.erro = 10;

      Pattern p20Porcento =
          loadFile120_8(
              Experimento_3.class.getResourceAsStream(
                  "/br/compnatural/atividade_3/char8_12x10_20.txt"),
              Experimento_3.class.getResourceAsStream(
                  "/br/compnatural/atividade_3/char8_8x8_1.txt"));
      patterns.add(p20Porcento);
      p20Porcento.erro = 20;

      State.x = pCorreto;

      double[] weights = {2, -2};

      int[] hiddens = {5, 10, 25};

      List<RnaResult> results = new ArrayList<RnaResult>(20);

      ReportUnit reportUnit = null;
      State bestState = null;

      for (int hidden : hiddens) {
        for (int i = 0; i < weights.length; i += 2) {
          List<ReportGraphInfo> graphInfo = new ArrayList<ReportGraphInfo>(GENERATIONS + 1);
          List<ReportUnit> ds = new ArrayList<ReportUnit>(MAX_IT);

          for (int k = 0; k < GENERATIONS; k++) {
            graphInfo.add(new ReportGraphInfo(0d, 0d, 0));
          }

          RealSpecification specification = new RealSpecification();
          EQMFunction function = new EQMFunction(Boolean.TRUE, hidden);
          MultilayerPerceptron perceptron =
              MultilayerPerceptron.getMultilayerPerceptronTangenteOneHidden(
                  hidden,
                  pCorreto.getD().length,
                  pCorreto.getX()[0].length,
                  weights[i + 1],
                  weights[i],
                  true);
          State state = EQMFunction.buildState(perceptron);

          for (int j = 0; j < state.getCoordinate().size(); j++) {
            specification.addCoordinate("x", weights[i + 1], weights[i]);
          }

          log.fine("Inicio [" + weights[i + 1] + "," + weights[i] + "] - hidden(" + hidden + ")");

          for (int m = 0; i < MAX_IT; i++) {
            log.info("Nova iteracao " + i);
            reportUnit = new ReportUnit();
            GeneticAlgorithm lGenetic = new GeneticAlgorithm(50, 0.75f, 0.1f, Boolean.TRUE);

            reportUnit.setAlgorithm(new Experiment.AlgorithmWrapper(lGenetic, null));
            reportUnit.setFunction(function);

            long ini = System.nanoTime();

            specification.pm = new Float(lGenetic.pm);

            state = lGenetic.optimize(GENERATIONS, function, specification, reportUnit);

            if (bestState != null) {
              if (bestState.getValue() < state.getValue()) {
                bestState = state;
                replace(graphInfo, reportUnit.getReportGraphInfos());
              }
            } else {
              bestState = state;
              replace(graphInfo, reportUnit.getReportGraphInfos());
            }

            reportUnit.setTime(System.nanoTime() - ini);

            reportUnit.setTotalIteraction((double) hidden);

            ds.add(reportUnit);

            testPatterns(pCorreto, state, results, patterns, hidden);
          }
          Map parameters = new HashMap();
          parameters.put("nome", "FInal");
          parameters.put("ds", ds);

          avg(graphInfo, MAX_IT);
          reportUnit.setReportGraphInfo(new JRBeanCollectionDataSource(graphInfo));
          ReportManager.saveReport(
              "/otimizacao_grafico.jrxml", parameters, "experimento_2GA_" + hidden + ".pdf");
          log.fine("Fim");
        }
      }

      Collections.sort(
          results,
          new Comparator<RnaResult>() {

            @Override
            public int compare(RnaResult o1, RnaResult o2) {

              return o2.getMatchs() - o1.getMatchs();
            }
          });
      String result = "";
      for (RnaResult rnaResult : results) {
        result += rnaResult.toString();
      }

      log.info(result);

    } catch (FileNotFoundException e) {
      log.log(Level.SEVERE, "Arquivo nao encontrado", e);
    } catch (IOException e) {
      log.log(Level.SEVERE, "Erro ao ler o arquivo", e);
    }
  }