Esempio n. 1
0
  /** Layout the components on the GUI. */
  public void layoutComponents() {
    list = new JList(examples);
    runButton = new JButton("Run Example");
    exitButton = new JButton("Exit");
    tabbedPane = new JTabbedPane();

    JPanel buttonPane = new JPanel();
    buttonPane.setLayout(new FlowLayout(FlowLayout.CENTER));
    buttonPane.add(runButton);
    buttonPane.add(exitButton);

    JPanel examplePane = new JPanel();
    examplePane.setLayout(new BorderLayout());
    examplePane.add(tabbedPane, BorderLayout.CENTER);
    examplePane.add(buttonPane, BorderLayout.SOUTH);

    JPanel contentPane = new JPanel();
    contentPane.setLayout(new BorderLayout());
    contentPane.add(new JScrollPane(list), BorderLayout.WEST);
    contentPane.add(examplePane, BorderLayout.CENTER);

    setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    getContentPane().setLayout(new BorderLayout());
    getContentPane().add(contentPane, BorderLayout.CENTER);
    setSize(840, 600);
    setIconImages(Settings.getIconImages());
  }
Esempio 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();
    }
  }