Ejemplo n.º 1
0
  public static void main(String[] args) {
    Properties properties = new Properties();
    properties.setProperty("populationSize", "10");

    Problem problem = new OneMax(100);
    Algorithm algorithm =
        AlgorithmFactory.getInstance().getAlgorithm("NSGAII", properties, problem);

    while (!algorithm.isTerminated()) {
      algorithm.step();

      NondominatedPopulation population = algorithm.getResult();

      if (population.get(0).getObjective(0) == 0) {
        // if all bits are 1
        System.out.println(
            "Found optimal solution after " + algorithm.getNumberOfEvaluations() + " evaluations!");
        break;
      }
    }
  }
Ejemplo n.º 2
0
  /**
   * Solves this TSPLIB instance while displaying a GUI showing the optimization progress.
   *
   * @param instance the TSPLIB instance to solve
   */
  public static void solve(TSPInstance instance) {
    TSPPanel panel = new TSPPanel(instance);
    panel.setAutoRepaint(false);

    // create other components on the display
    StringBuilder progress = new StringBuilder();
    JTextArea progressText = new JTextArea();

    JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
    splitPane.setTopComponent(panel);
    splitPane.setBottomComponent(new JScrollPane(progressText));
    splitPane.setDividerLocation(300);
    splitPane.setResizeWeight(1.0);

    // display the panel on a window
    JFrame frame = new JFrame(instance.getName());
    frame.getContentPane().setLayout(new BorderLayout());
    frame.getContentPane().add(splitPane, BorderLayout.CENTER);
    frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    frame.setSize(500, 400);
    frame.setLocationRelativeTo(null);
    frame.setIconImages(Settings.getIconImages());
    frame.setVisible(true);

    // create the optimization problem and evolutionary algorithm
    Problem problem = new TSPProblem(instance);

    Properties properties = new Properties();
    properties.setProperty("swap.rate", "0.7");
    properties.setProperty("insertion.rate", "0.9");
    properties.setProperty("pmx.rate", "0.4");

    Algorithm algorithm =
        AlgorithmFactory.getInstance().getAlgorithm("NSGAII", properties, problem);

    int iteration = 0;

    // now run the evolutionary algorithm
    while (frame.isVisible()) {
      algorithm.step();
      iteration++;

      // clear existing tours in display
      panel.clearTours();

      // display population with light gray lines
      if (algorithm instanceof EvolutionaryAlgorithm) {
        EvolutionaryAlgorithm ea = (EvolutionaryAlgorithm) algorithm;

        for (Solution solution : ea.getPopulation()) {
          panel.displayTour(toTour(solution), lightGray);
        }
      }

      // display current optimal solutions with red line
      Tour best = toTour(algorithm.getResult().get(0));
      panel.displayTour(best, Color.RED, new BasicStroke(2.0f));
      progress.insert(0, "Iteration " + iteration + ": " + best.distance(instance) + "\n");
      progressText.setText(progress.toString());

      // repaint the TSP display
      panel.repaint();
    }
  }