⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 jgapclientgp.java

📁 JGAP是一种遗传算法和遗传规划的组成部分提供了一个Java框架。它提供了基本的遗传机制
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
            }
            // Store result to disk if it is fit enough.
            // -----------------------------------------
            double minFitness;
            minFitness = m_gridConfig.getMinFitnessToStore();
            if (minFitness < 0.0001d) {/**@todo allow fitness 0.0*/
              minFitness = 5000;
            }
            if (best != null && best.getFitnessValue() >= minFitness) {
              String filename = getResultFilename(result);
              log.info("Writing result to file " + filename);
              writeToFile(best, m_workDir, filename);
            }
            // Now remove the result from the online store.
            // --------------------------------------------
            /**@todo do this here explicitely and not in receiveWorkResult*/
          }
        }
      }
    }
    else {
      for (int i = 0; i < size; i++) {
        feedback.setProgressValue(i + workList.length);
        receiveWorkResult(workList, feedback);
        if (this.isInterrupted()) {
          break;
        }
      }
    }
  }

  protected String getResultFilename(JGAPResultGP a_result) {
    IGPProgram fittest = WANUtils.getFittest(a_result);
    String fitness = "";
    if (fittest == null) {
      // Should not happen at all!
      log.error("No fittest program found!");
    }
    else {
      fitness = NumberKit.niceDecimalNumber(fittest.getFitnessValue(), 2);
    }
    return "result_"
        + fitness
        + "_"
        + getRunID()
        + "_"
        + a_result.getID()
        + "_"
        + a_result.getSessionName()
        + "_" + a_result.getChunk()
        + ".jgap";
  }

  private JGAPResultGP receiveWorkResult(Object a_result,
      IClientFeedbackGP feedback, boolean a_remove)
      throws Exception {
    // Object reference is realized via context id.
    // --------------------------------------------
    MessageContext context = new MessageContext(MODULE_WS /**@todo later: SC*/,
        CONTEXT_WORK_RESULT, a_result);
    GridMessageWorkResult gmwr = (GridMessageWorkResult) m_gcmed.
        getGridMessage(context, null, TIMEOUT_SECONDS, WAITTIME_SECONDS,
                       a_remove);
    if (gmwr == null) {
      throw new WorkResultNotFoundException();
    }
    else {
      String s = " ";
      if (a_remove) {
        s += "and removed from WAN";
      }
      log.info("Work result received" + s);
    }
    JGAPResultGP workResult = (JGAPResultGP) gmwr.getWorkResult();
    int idx = workResult.getChunk();
    // Fire listener.
    // --------------
    feedback.receivedFragmentResult(null, workResult, idx);
    return workResult;
  }

  private JGAPResultGP receiveWorkResult(JGAPRequestGP[] workList,
      IClientFeedbackGP feedback)
      throws Exception {
    /**@todo make this asynchronous with fall-back and reconnect!*/
    MessageContext context = new MessageContext(MODULE_WS /**@todo later: SC*/,
        CONTEXT_WORK_RESULT, CONTEXT_ID_EMPTY);
    GridMessageWorkResult gmwr = (GridMessageWorkResult) m_gcmed.
        getGridMessage(context, null, TIMEOUT_SECONDS, WAITTIME_SECONDS, true);
    if (gmwr == null) {
      throw new NoWorkResultsFoundException();
    }
    else {
      log.info("Work result received!");
    }
    JGAPResultGP workResult = (JGAPResultGP) gmwr.getWorkResult();
    m_gridConfig.getClientEvolveStrategy().resultReceived(workResult);
    int idx = workResult.getChunk();
    // Fire listener.
    // --------------
    JGAPRequestGP req;
    if (workList == null || workList.length < 1) {
      req = null;
    }
    else {
      req = workList[idx];
    }
    feedback.receivedFragmentResult(req, workResult, idx);
    IGPProgram best = workResult.getFittest();
    if (best == null) {
      best = workResult.getPopulation().determineFittestProgram();
    }
    resultReceived(best);
    return workResult;
  }

  /**
   * If necessary: override to implement your evolution cycle individually.
   *
   * @param a_gcmed the GridClient mediator
   * @param a_receiveOnly false: Don't send any work requests, just receive
   * results from former evolutions
   *
   * @throws Exception
   */
  protected void evolve(IGridClientMediator a_gcmed, boolean a_receiveOnly)
      throws Exception {
    // Do the complete evolution cycle until end.
    // ------------------------------------------
    IClientFeedbackGP feedback = m_gridConfig.getClientFeedback();
    feedback.beginWork();
    IClientEvolveStrategyGP evolver = m_gridConfig.getClientEvolveStrategy();
    IRequestSplitStrategyGP splitter = m_gridConfig.getRequestSplitStrategy();
    int evolutionIndex = 0;
    do {
      JGAPRequestGP[] workRequests = null;
      boolean deferRequests = false;
      if (!a_receiveOnly) {
        try {
          // Care that not too much work requests are online, do a listing
          // from time to time. If enough requests already there, don't create
          // them any more.
          // -----------------------------------------------------------------
          long lastListing = m_objects.getLastListingRequestsMillis();
          long current = System.currentTimeMillis();
          if (current - lastListing > 60 * 60 * 1) { //60 Seconds * 60 Minutes * 1 Hour
            // Do a listing again after 60 minutes or more.
            // --------------------------------------------
            MessageContext context = new MessageContext(MODULE_CS,
                CONTEXT_WORK_REQUEST, CONTEXT_ID_EMPTY);
            List requests = a_gcmed.listRequests(context, null, null);
            m_objects.setLastListingRequestsMillis(current);
            m_persister.save();
            if (requests != null && requests.size() > 100) {
              deferRequests = true;
              log.info("Deferring creating and sending further requests"
                       + ", maximum reached ("
                       + requests.size() + " found).");
            }
            if (requests != null && requests.size() > 0) {
              // Remove requests from database that are not in list any more.
              // ------------------------------------------------------------
              Map foundKeys = new HashMap();
              Object first = requests.get(0);
              if (String.class.isAssignableFrom(first.getClass())) {
                // Requests of type String can be handled directly.
                // ------------------------------------------------
                for (Object key : requests) {
                  foundKeys.put(key, "");
                }
              }
              else {
                // Requests of type that sub classes have to handle.
                // -------------------------------------------------
                for (Object obj : requests) {
                  Object key = getKeyFromObject(obj);
                  if (key != null) {
                    foundKeys.put(key, "");
                  }
                }
              }
              removeEntries(foundKeys, m_objects.getRequests());
            }
          }
          if (!deferRequests) {
            workRequests = sendWorkRequests(evolutionIndex, evolver, splitter,
                feedback);
          }
          else {
            // Defer creating and sending additional requests.
            // -----------------------------------------------
          }
        } catch (WorkRequestsSendException wex) {
          errorOnSendWorkRequests(wex.getCause(), wex.getWorkRequests());
        }
        if (!deferRequests && !afterSendWorkRequests(workRequests)) {
          break;
        }
      }
      if (this.isInterrupted()) {
        break;
      }
      if (!deferRequests && !a_receiveOnly) {
        evolver.afterWorkRequestsSent();
      }
      if (!m_no_comm) {
        try {
          receiveWorkResults(workRequests);
        } catch (Exception ex) {
          onErrorReceiveWorkResults(workRequests, ex);
        }
      }
      if (!a_receiveOnly && !m_no_evolution) {
        evolver.evolve();
        // Fire listener that one evolution cycle is complete.
        // ---------------------------------------------------
        feedback.completeFrame(evolutionIndex);
        evolutionIndex++;
        // Check if evolution is finished.
        // -------------------------------
        if (evolver.isEvolutionFinished(evolutionIndex)) {
          evolver.onFinished();
          break;
        }
      }
      else {
        a_gcmed.disconnect();
        log.info("Sleeping a while before beginning again...");
        Thread.sleep(40000);
        a_gcmed.connect();
      }
    } while (true);
    try {
      a_gcmed.disconnect();
    } catch (Exception ex) {
      log.error("Disconnecting from server failed!", ex);
    }
    m_gridConfig.getClientFeedback().endWork();
  }

  public void start() {
    try {
      m_gridConfig.validate();
    } catch (Exception ex) {
      throw new RuntimeException(ex);
    }
    super.start();
  }

  public GPConfiguration getConfiguration() {
    return m_gridConfig.getConfiguration();
  }

  public IGridClientMediator getGridClientMediator() {
    return m_gcmed;
  }

  protected IGridConfigurationGP getGridConfigurationGP() {
    return m_gridConfig;
  }

  /**
   * Writes an object to a local file.
   *
   * @param a_obj the object to persist
   * @param a_dir directory to write the file to
   * @param a_filename name of the file to create
   * @throws Exception
   *
   * @author Klaus Meffert
   * @since 3.3.3
   */
  public void writeToFile(Object a_obj, String a_dir, String a_filename)
      throws Exception {
    JGAPGPXStream xstream = new JGAPGPXStream();
    File f = new File(a_dir, a_filename);
//    FileOutputStream fos = new FileOutputStream(f);
    FileWriter fw = new FileWriter(f);
    CompactWriter compact = new CompactWriter(fw);
    xstream.marshal(a_obj, compact);
    fw.close();
  }

  public void setWorkDirectory(String a_workDir)
      throws IOException {
    m_workDir = a_workDir;
    FileKit.createDirectory(m_workDir);
    log.info("Work dir: " + m_workDir);
  }

  public String getWorkDirectory() {
    return m_workDir;
  }

  protected void checkForUpdates(String a_URL, String a_libDir,
                                 String a_workDir)
      throws Exception {
    GridKit.updateModuleLibrary(a_URL, "rjgrid", a_libDir, a_workDir);
  }

  /**
   * Override in sub classes: list available requests
   */
  protected void listRequests() {
  }

  /**
   * Override in sub classes: list available results
   *
   * @author Klaus Meffert
   * @since 3.3.3
   */
  protected void listResults() {
  }

  /**
   * @return false: normal mode, true: do not communicate with the server
   *
   * @author Klaus Meffert
   * @since 3.3.3
   */
  public boolean isNoCommunication() {
    return m_no_comm;
  }

  /**
   * Starts a client (first parameter: name of specific setup class).
   *
   * @param args String[]
   *
   * @author Klaus Meffert
   * @since 3.01
   */
  public static void main(String[] args) {
    try {
      // Setup logging.
      // --------------
      MainCmd.setUpLog4J("client", true);
      // Command line options.
      // ---------------------
      GridNodeClientConfig config = new GridNodeClientConfig();
      Options options = makeOptions();
      options.addOption("l", true, "LAN or WAN");
//      options.addOption("receive_only", false,
//                        "Only receive results, don't send requests");
//      options.addOption("list", false,
//                        "List requests and results");
//      options.addOption("no_comm", false,
//                        "Don't receive any results, don't send requests");
//      options.addOption("no_evolution", false,
//                        "Don't perform genetic evolution");
//      options.addOption("help", false,
//                        "Display all available options");
      CommandLine cmd = MainCmd.parseCommonOptions(options, config, args);
      SystemKit.printHelp(cmd, options);
      String networkMode = cmd.getOptionValue("l", "LAN");
      boolean inWAN;
      if (networkMode != null && networkMode.equals("LAN")) {
        inWAN = false;
      }
      else {
        inWAN = true;
      }
      if (!cmd.hasOption("config")) {
        System.out.println(
            "Please provide a name of the grid configuration class to use");
        System.out.println("An example class would be "
                           +
                           "examples.grid.fitnessDistributed.GridConfiguration");
        System.exit(1);
      }
//      if (args.length < 2) {
//        System.out.println(
//            "Please provide an application name of the grid (textual identifier");
//        System.exit(1);
//      }
      String clientClassName = cmd.getOptionValue("config");
      boolean receiveOnly = cmd.hasOption("receive_only");
      boolean list = cmd.hasOption("list");
      boolean noComm = cmd.hasOption("no_comm");
      boolean noEvolution = cmd.hasOption("no_evolution");
      boolean endless = cmd.hasOption("endless");
      int max_fetch_results = Integer.valueOf(cmd.getOptionValue(
          "max_fetch_results", "0"));
      // Setup and start client.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -