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

📄 acodemo.java

📁 A program to demonstrate the optimization process of ant colony optimization for the traveling salem
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
    btn.addActionListener(new AbstractAction () {      public void actionPerformed (ActionEvent e) {        dlg.setVisible(false);        ACODemo.this.runAnts(((Integer)epochs.getValue()).intValue(),                             ((Integer)delay.getValue()).intValue());      } } );    btn = new JButton("Apply"); bbar.add(btn);    btn.addActionListener(new AbstractAction () {      public void actionPerformed (ActionEvent e) {        ACODemo.this.runAnts(((Integer)epochs.getValue()).intValue(),                             ((Integer)delay.getValue()).intValue());      } } );    btn = new JButton("Close"); bbar.add(btn);    btn.addActionListener(new AbstractAction () {      public void actionPerformed (ActionEvent e) {        dlg.setVisible(false); } } );    dlg.getContentPane().add(grid, BorderLayout.CENTER);    dlg.getContentPane().add(bbar, BorderLayout.SOUTH);    dlg.setLocationRelativeTo(this);    dlg.setLocation(664, 465);    dlg.pack();    return dlg;  }  /* createRunOpt() */  /*------------------------------------------------------------------*/  private JDialog createAbout ()  {                             /* --- create "About..." dialog box */    final JDialog dlg  = new JDialog(this, "About ACODemo...", true);    Container     pane = dlg.getContentPane();    LogoPanel     logo = new LogoPanel();    JButton       btn  = new JButton("Ok");    JPanel        rest = new JPanel(new BorderLayout(2, 2));    JTextArea     text = new JTextArea      ("ACODemo\n"      +"An Ant Colony Optimization Demo\n"      +"Version 1.2, 2006.05.13\n\n"      +"written by Christian Borgelt\n"      +"Otto-von-Guericke-University of Magdeburg\n"      +"Universitatsplatz 2, D-39106 Magdeburg\n"      +"e-mail: borgelt@iws.cs.uni-magdeburg.de");    text.setBackground(this.getBackground());    text.setFont(new Font("Dialog", Font.BOLD, 12));    text.setEditable(false);    btn.addActionListener(new AbstractAction () {      public void actionPerformed (ActionEvent e) {        dlg.setVisible(false); } } );    rest.add(logo, BorderLayout.NORTH);    rest.add(btn,  BorderLayout.SOUTH);    pane.setLayout(new FlowLayout());    pane.add(text);    pane.add(rest);    dlg.setLocationRelativeTo(this);    dlg.pack();    dlg.setResizable(false);    return dlg;  }  /* createAbout() */  /*------------------------------------------------------------------*/  public void mousePressed (MouseEvent me)  {                             /* --- handle mouse clicks */    int m;                      /* event modifiers */    if (this.tsp == null) return;  /* check for a TSP */    this.mode = 0;              /* clear the operation mode */    this.mx = me.getX();        /* note the coordinates of the point */     this.my = me.getY();        /* at which the mouse was pressed */    m = me.getModifiers();      /* get the modifiers (buttons) */    switch (m) {                /* depending on the mouse button */      case InputEvent.BUTTON1_MASK:        this.mode = 1; break;   /* panning mode */      case InputEvent.BUTTON2_MASK:  /* scaling mode (low  factor) */      case InputEvent.BUTTON3_MASK:  /* scaling mode (high factor) */        this.mode = (m == InputEvent.BUTTON2_MASK) ? 2 : 3;        this.scale = this.panel.getScale();        this.my   -= this.scroll.getViewport().getViewPosition().y;        break;                  /* adapt position to viewport */    }                           /* (set the mouse operation mode) */  }  /* mousePressed() */  /*------------------------------------------------------------------*/  public void mouseDragged (MouseEvent e)  {                             /* --- handle mouse movement */    JViewport view;             /* viewport of scrollpane */    Point     refp;             /* coordinates of upper left corner */    Dimension size;             /* extension of viewing area */    int       xmax, ymax, d;    /* maximum view position, movement */    double    scl;              /* new scaling factor */    if ((this.tsp == null)         /* check for a TSP and */    ||  (this.mode <= 0)) return;  /* for a valid operation mode */    view = this.scroll.getViewport();    refp = view.getViewPosition(); /* get reference point */    if (this.mode == 1) {       /* if to do panning */      refp.x += (int)this.mx -e.getX();      refp.y += (int)this.my -e.getY();      size  = this.panel.getPreferredSize();      xmax  = size.width; ymax  = size.height;      size  = view.getExtentSize();  /* get maximum reference point */      xmax -= size.width; ymax -= size.height;      if (refp.x > xmax) { this.mx -= refp.x -xmax; refp.x = xmax; }      if (refp.x < 0)    { this.mx -= refp.x;       refp.x = 0;    }      if (refp.y > ymax) { this.my -= refp.y -ymax; refp.y = ymax; }      if (refp.y < 0)    { this.my -= refp.y;       refp.y = 0;    }      view.setViewPosition(refp); }    else {                      /* if to do scaling */      d = (e.getY() -refp.y) -this.my;      scl = Math.pow((this.mode > 2) ? 1.004 : 1.02, d);      this.panel.setScale(this.scale *scl);    }                           /* set new scaling factor */    this.panel.revalidate();    /* adapt the enclosing scroll pane */    this.panel.repaint();       /* and redraw the TSP */  }  /* mouseDragged() */  /*------------------------------------------------------------------*/  public void mouseReleased (MouseEvent e) { this.mode = 0; }  public void mouseClicked  (MouseEvent e) { }  public void mouseEntered  (MouseEvent e) { }  public void mouseExited   (MouseEvent e) { }  public void mouseMoved    (MouseEvent e) { }  /*------------------------------------------------------------------*/  public void run ()  {                             /* --- create GUI of ACO viewer */    JMenuBar  mbar;             /* menu bar */    JMenu     menu;             /* to create menu titles */    JMenuItem item;             /* to create menu items */    this.getContentPane().setLayout(new BorderLayout());    /* --- create and set the menu bar --- */    mbar = new JMenuBar();    this.getContentPane().add(mbar, BorderLayout.NORTH);    menu = mbar.add(new JMenu("File"));    menu.setMnemonic('f');    item = menu.add(new JMenuItem("Load TSP..."));    item.setMnemonic('l');    item.addActionListener(new AbstractAction() {      public void actionPerformed (ActionEvent e) {        ACODemo.this.loadTSP(null); } } );    item = menu.add(new JMenuItem("Reload TSP"));    item.setMnemonic('r');    item.addActionListener(new AbstractAction() {      public void actionPerformed (ActionEvent e) {        ACODemo.this.loadTSP(ACODemo.this.curr); } } );    item = menu.add(new JMenuItem("Save TSP"));    item.setMnemonic('s');    item.addActionListener(new AbstractAction() {      public void actionPerformed (ActionEvent e) {        ACODemo.this.saveTSP(ACODemo.this.curr); } } );    item = menu.add(new JMenuItem("Save TSP as..."));    item.setMnemonic('a');    item.addActionListener(new AbstractAction() {      public void actionPerformed (ActionEvent e) {        ACODemo.this.saveTSP(null); } } );    item = menu.add(new JMenuItem("Save PNG Image..."));    item.setMnemonic('i');    item.addActionListener(new AbstractAction() {      public void actionPerformed (ActionEvent e) {        ACODemo.this.saveImage(null); } } );    menu.addSeparator();    item = menu.add(new JMenuItem("Quit"));    item.setMnemonic('q');    if (this.isprog) {          /* if stand-alone program */      item.addActionListener(new AbstractAction() {        public void actionPerformed (ActionEvent e) {          System.exit(0); } } ); }     /* terminate the program */    else {                      /* if only visualization module */      item.addActionListener(new AbstractAction() {        public void actionPerformed (ActionEvent e) {          if (ACODemo.this.about != null)            ACODemo.this.about.setVisible(false);          if (ACODemo.this.randtsp != null)            ACODemo.this.randtsp.setVisible(false);          if (ACODemo.this.antcol != null)            ACODemo.this.antcol.setVisible(false);          if (ACODemo.this.runopt != null)            ACODemo.this.runopt.setVisible(false);          if (ACODemo.this.params != null)            ACODemo.this.params.setVisible(false);          ACODemo.this.setVisible(false);        } } );                  /* only close the window */    }                           /* and the dialog boxes */    menu = mbar.add(new JMenu("Actions"));    menu.setMnemonic('a');    item = menu.add(new JMenuItem("Generate Random TSP..."));    item.setMnemonic('g');    item.addActionListener(new AbstractAction() {      public void actionPerformed (ActionEvent e) {        if (ACODemo.this.randtsp == null)          ACODemo.this.randtsp = createRandTSP();        ACODemo.this.randtsp.setVisible(true);      } } );    item = menu.add(new JMenuItem("Create Ant Colony..."));    item.setMnemonic('c');    item.addActionListener(new AbstractAction() {      public void actionPerformed (ActionEvent e) {        if (ACODemo.this.antcol == null)          ACODemo.this.antcol = createAnts();        ACODemo.this.antcol.setVisible(true);      } } );    item = menu.add(new JMenuItem("Run Optimization..."));    item.setMnemonic('o');    item.addActionListener(new AbstractAction() {      public void actionPerformed (ActionEvent e) {        if (ACODemo.this.runopt == null)          ACODemo.this.runopt = createRunOpt();        ACODemo.this.runopt.setVisible(true);      } } );    item = menu.add(new JMenuItem("Stop Optimization"));    item.setMnemonic('s');    item.addActionListener(new AbstractAction() {      public void actionPerformed (ActionEvent e) {        if (ACODemo.this.timer == null) return;        ACODemo.this.timer.stop();        ACODemo.this.cnt = -1;      } } );    menu.addSeparator();    item = menu.add(new JMenuItem("Redraw"));    item.setMnemonic('r');    item.addActionListener(new AbstractAction() {      public void actionPerformed (ActionEvent e) {        ACODemo.this.panel.repaint(); } } );    menu = mbar.add(new JMenu("Help"));    menu.setMnemonic('h');    item = menu.add(new JMenuItem("About..."));    item.setMnemonic('a');    item.addActionListener(new AbstractAction() {      public void actionPerformed (ActionEvent e) {        if (ACODemo.this.about == null)           ACODemo.this.about = ACODemo.this.createAbout();        ACODemo.this.about.setVisible(true);      } } );    /* --- create and set the main panel --- */    this.panel = new ACOPanel();     this.panel.setLayout(new BorderLayout());    this.panel.setPreferredSize(new Dimension(656, 656));    this.panel.addMouseListener(this);    this.panel.addMouseMotionListener(this);    this.scroll = new JScrollPane(this.panel);    this.getContentPane().add(this.scroll, BorderLayout.CENTER);    /* --- create and set a status bar --- */    this.stat = new JTextField("");    this.stat.setEditable(false);    this.getContentPane().add(this.stat, BorderLayout.SOUTH);    /* --- show the frame window --- */    this.setTitle("ACODemo");    this.setDefaultCloseOperation(this.isprog      ? JFrame.EXIT_ON_CLOSE : JFrame.HIDE_ON_CLOSE);    this.setLocation(0, 0);    this.pack();    if (this.isprog) this.setVisible(true);    this.stat.setText("ACODemo is up and running.");  }  /* run() */  /* Following the recommendations in the Java tutorial, the user   */  /* interface is created in the "run" method, which is invoked     */  /* from the event queue, in order to avoid problems with threads. */  /*------------------------------------------------------------------*/  public ACODemo (boolean isProg)  { this.isprog = isProg;    try { EventQueue.invokeAndWait(this); } catch (Exception e) {} }  public ACODemo ()  { this.isprog = false;    try { EventQueue.invokeAndWait(this); } catch (Exception e) {} }  public ACODemo (String title)  { this(false); this.setTitle(title); }  public ACODemo (File file)  { this(false); this.loadTSP(file); }  public ACODemo (String title, File file)  { this(title); this.loadTSP(file); }  /*------------------------------------------------------------------*/  public static void main (String args[])  {                             /* --- main function */    ACODemo v = new ACODemo(true);    /* create an ACO demo viewer */    if (args.length > 0) v.loadTSP(new File(args[0]));  }  /* main() */               /* load traveling salesman problem */}  /* class ACODemo */

⌨️ 快捷键说明

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