Example #1
0
  @Test
  public void testLoad() throws IOException {
    for (String instance : instances) {
      File directory = new File("./data/atsp/");
      File instanceData = new File(directory, instance + ".atsp");

      if (instanceData.exists()) {
        System.out.println(instance);

        TSPInstance problem = new TSPInstance(instanceData);
        Assert.assertEquals(DataType.ATSP, problem.getDataType());
      }
    }
  }
Example #2
0
    @Override
    public Solution newSolution() {
      Solution solution = new Solution(1, 1);

      solution.setVariable(0, EncodingUtils.newPermutation(instance.getDimension()));

      return solution;
    }
Example #3
0
  public static void main(String[] args) throws IOException {
    TSPInstance problem = new TSPInstance(new File("./data/tsp/gr120.tsp"));
    problem.addTour(new File("./data/tsp/gr120.opt.tour"));

    TSPPanel panel = new TSPPanel(problem);

    for (int i = 0; i < 20; i++) {
      panel.displayTour(
          Tour.createRandomTour(problem.getDimension()), new Color(128, 128, 128, 64));
    }

    panel.displayTour(problem.getTours().get(0), Color.RED, new BasicStroke(2.0f));

    JFrame frame = new JFrame(problem.getName());
    frame.getContentPane().setLayout(new BorderLayout());
    frame.getContentPane().add(panel, BorderLayout.CENTER);
    frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    frame.setSize(500, 400);
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
  }
Example #4
0
  /**
   * Constructs a new panel for displaying a TSPLIB problem instance.
   *
   * @param problem the TSPLIB problem instance
   */
  public TSPPanel(TSPInstance problem) {
    super();
    this.problem = problem;

    if (DisplayDataType.NO_DISPLAY.equals(problem.getDisplayDataType())) {
      throw new IllegalArgumentException("problem instance does not support a graphical display");
    }

    tours = new LinkedHashMap<Tour, TourDisplaySetting>();
    nodeWidth = 4.0;
    insets = new Insets((int) nodeWidth, (int) nodeWidth, (int) nodeWidth, (int) nodeWidth);
    autoRepaint = true;

    setBackground(Color.WHITE);
    setForeground(Color.BLACK);
  }
Example #5
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();
    }
  }
Example #6
0
  @Override
  protected synchronized void paintComponent(Graphics g) {
    super.paintComponent(g);

    Graphics2D g2 = (Graphics2D) g;
    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

    // get the display data
    NodeCoordinates displayData = null;

    if (DisplayDataType.COORD_DISPLAY.equals(problem.getDisplayDataType())) {
      displayData = (NodeCoordinates) problem.getDistanceTable();
    } else {
      displayData = problem.getDisplayData();
    }

    // first determine bounds of the data
    boolean isGeographical = EdgeWeightType.GEO.equals(problem.getEdgeWeightType());
    double left = Double.POSITIVE_INFINITY;
    double right = Double.NEGATIVE_INFINITY;
    double bottom = Double.POSITIVE_INFINITY;
    double top = Double.NEGATIVE_INFINITY;

    for (int i = 1; i <= displayData.size(); i++) {
      Node node = displayData.get(i);
      double[] position = toDisplayCoordinates(node, isGeographical);

      left = Math.min(left, position[0]);
      right = Math.max(right, position[0]);
      bottom = Math.min(bottom, position[1]);
      top = Math.max(top, position[1]);
    }

    // calculate the bounds of the drawing
    int displayWidth = getWidth();
    int displayHeight = getHeight();
    double scaleX = (displayWidth - insets.right - insets.left) / (right - left);
    double scaleY = (displayHeight - insets.top - insets.bottom) / (top - bottom);
    double scale = Math.min(scaleX, scaleY);
    double offsetX = (displayWidth - insets.right - insets.left - scale * (right - left)) / 2.0;
    double offsetY = (displayHeight - insets.top - insets.bottom - scale * (top - bottom)) / 2.0;

    // draw the tours
    for (Entry<Tour, TourDisplaySetting> entry : tours.entrySet()) {
      Tour tour = entry.getKey();
      TourDisplaySetting displaySettings = entry.getValue();

      g2.setPaint(displaySettings.getPaint());
      g2.setStroke(displaySettings.getStroke());

      for (int i = 0; i < tour.size(); i++) {
        Node node1 = displayData.get(tour.get(i));
        Node node2 = displayData.get(tour.get(i + 1));
        double[] position1 = toDisplayCoordinates(node1, isGeographical);
        double[] position2 = toDisplayCoordinates(node2, isGeographical);

        Line2D line =
            new Line2D.Double(
                displayWidth - (offsetX + scale * (position1[0] - left) + insets.left),
                displayHeight - (offsetY + scale * (position1[1] - bottom) + insets.bottom),
                displayWidth - (offsetX + scale * (position2[0] - left) + insets.left),
                displayHeight - (offsetY + scale * (position2[1] - bottom) + insets.bottom));

        g2.draw(line);
      }
    }

    // draw the nodes
    g2.setColor(getForeground());

    for (int i = 1; i <= displayData.size(); i++) {
      Node node = displayData.get(i);
      double[] position = toDisplayCoordinates(node, isGeographical);

      Ellipse2D point =
          new Ellipse2D.Double(
              displayWidth
                  - (offsetX + scale * (position[0] - left) + insets.left)
                  - (nodeWidth / 2.0),
              displayHeight
                  - (offsetY + scale * (position[1] - bottom) + insets.bottom)
                  - (nodeWidth / 2.0),
              nodeWidth,
              nodeWidth);

      g2.fill(point);
      g2.draw(point);
    }
  }