📄 acodemo.java
字号:
/*------------------------------------------------------------------*/ private JDialog createRandTSP () { /* --- create "Generate..." dialog */ final JDialog dlg = new JDialog(this, "Generate Random TSP..."); GridBagLayout g = new GridBagLayout(); GridBagConstraints lc = new GridBagConstraints(); GridBagConstraints rc = new GridBagConstraints(); JPanel grid = new JPanel(g); JPanel bbar; JLabel lbl; JTextArea help; JButton btn; grid.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5)); lc.fill = /* fill fields in both directions */ rc.fill = GridBagConstraints.BOTH; rc.weightx = 1.0; /* resize only the input fields, */ lc.weightx = 0.0; /* but not the labels */ lc.weighty = 0.0; /* resize lines equally */ rc.weighty = 0.0; /* in vertical direction */ lc.ipadx = 10; /* gap between labels and inputs */ lc.ipady = 10; /* make all lines of the same height */ rc.gridwidth = GridBagConstraints.REMAINDER; lbl = new JLabel("Number of vertices:"); g.setConstraints(lbl, lc); grid.add(lbl); final JSpinner vertcnt = new JSpinner( new SpinnerNumberModel(30, 1, 999999, 1)); g.setConstraints(vertcnt, rc); grid.add(vertcnt); lbl = new JLabel("Seed for random numbers:"); g.setConstraints(lbl, lc); grid.add(lbl); final JSpinner seed = new JSpinner( new SpinnerNumberModel(0, 0, 999999, 1)); g.setConstraints(seed, rc); grid.add(seed); help = new JTextArea( "If the seed for the pseudo-random number generator\n" +"is set to zero, the system time will be used instead."); help.setFont(small); help.setBackground(this.getBackground()); g.setConstraints(help, rc); grid.add(help); bbar = new JPanel(new GridLayout(1, 2, 5, 5)); bbar.setBorder(BorderFactory.createEmptyBorder(0, 5, 5, 3)); btn = new JButton("Ok"); bbar.add(btn); btn.addActionListener(new AbstractAction () { public void actionPerformed (ActionEvent e) { dlg.setVisible(false); ACODemo.this.genTSP(((Integer)vertcnt.getValue()).intValue(), ((Integer)seed.getValue()).longValue()); } } ); btn = new JButton("Apply"); bbar.add(btn); btn.addActionListener(new AbstractAction () { public void actionPerformed (ActionEvent e) { ACODemo.this.genTSP(((Integer)vertcnt.getValue()).intValue(), ((Integer)seed.getValue()).longValue()); } } ); 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, 0); dlg.pack(); return dlg; } /* createRandTSP() */ /*------------------------------------------------------------------*/ public void saveImage (File file) { /* --- save image to a file */ FileOutputStream stream; /* stream to write to */ if (file == null) { /* if no file name is given */ if (this.chooser == null) this.chooser = this.createChooser(); this.chooser.setDialogType(JFileChooser.SAVE_DIALOG); int r = this.chooser.showDialog(this, null); if (r != JFileChooser.APPROVE_OPTION) return; file = this.chooser.getSelectedFile(); } /* let the user choose a file name */ try { /* save the decision tree image */ ImageIO.write(this.panel.makeImage(), "png", new FileOutputStream(file)); } catch (java.io.IOException ioe) { String msg = "file " +file.getName() +":\n" +ioe.getMessage(); stat.setText(msg); System.err.println(msg); JOptionPane.showMessageDialog(this, msg, "alert", JOptionPane.ERROR_MESSAGE); } /* set the status text */ } /* saveImage() */ /*------------------------------------------------------------------*/ private JDialog createAnts () { /* --- create ant colony dialog */ final JDialog dlg = new JDialog(this, "Create Ant Colony..."); GridBagLayout g = new GridBagLayout(); GridBagConstraints lc = new GridBagConstraints(); GridBagConstraints rc = new GridBagConstraints(); JPanel grid = new JPanel(g); JPanel bbar; JLabel lbl; JTextArea help; JButton btn; grid.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5)); lc.fill = /* fill fields in both directions */ rc.fill = GridBagConstraints.BOTH; rc.weightx = 1.0; /* resize only the input fields, */ lc.weightx = 0.0; /* but not the labels */ lc.weighty = 0.0; /* resize lines equally */ rc.weighty = 0.0; /* in vertical direction */ lc.ipadx = 10; /* gap between labels and inputs */ lc.ipady = 10; /* make all lines of the same height */ rc.gridwidth = GridBagConstraints.REMAINDER; lbl = new JLabel("Number of ants:"); g.setConstraints(lbl, lc); grid.add(lbl); final JSpinner antcnt = new JSpinner( new SpinnerNumberModel(30, 1, 999999, 1)); g.setConstraints(antcnt, rc); grid.add(antcnt); lbl = new JLabel("Seed for random numbers:"); g.setConstraints(lbl, lc); grid.add(lbl); final JSpinner seed = new JSpinner( new SpinnerNumberModel(0, 0, 999999, 1)); g.setConstraints(seed, rc); grid.add(seed); help = new JTextArea( "If the seed for the pseudo-random number generator\n" +"is set to zero, the system time will be used instead."); help.setFont(small); help.setBackground(this.getBackground()); g.setConstraints(help, rc); grid.add(help); lbl = new JLabel("Initial pheromone:"); g.setConstraints(lbl, lc); grid.add(lbl); final JTextField phinit = new JTextField("0"); phinit.setFont(font); g.setConstraints(phinit, rc); grid.add(phinit); lbl = new JLabel("Exploitation probability:"); g.setConstraints(lbl, lc); grid.add(lbl); final JTextField exploit = new JTextField("0.2"); exploit.setFont(font); g.setConstraints(exploit, rc); grid.add(exploit); lbl = new JLabel("Pheromone trail weight:"); g.setConstraints(lbl, lc); grid.add(lbl); final JTextField alpha = new JTextField("1"); alpha.setFont(font); g.setConstraints(alpha, rc); grid.add(alpha); lbl = new JLabel("Inverse distance weight:"); g.setConstraints(lbl, lc); grid.add(lbl); final JTextField beta = new JTextField("1"); beta.setFont(font); g.setConstraints(beta, rc); grid.add(beta); lbl = new JLabel("Evaporation fraction:"); g.setConstraints(lbl, lc); grid.add(lbl); final JTextField evap = new JTextField("0.1"); evap.setFont(font); g.setConstraints(evap, rc); grid.add(evap); lbl = new JLabel("Trail laying exponent:"); g.setConstraints(lbl, lc); grid.add(lbl); final JTextField layexp = new JTextField("1"); layexp.setFont(font); g.setConstraints(layexp, rc); grid.add(layexp); lbl = new JLabel("Elite enhancement:"); g.setConstraints(lbl, lc); grid.add(lbl); final JTextField elite = new JTextField("0.1"); elite.setFont(font); g.setConstraints(elite, rc); grid.add(elite); bbar = new JPanel(new GridLayout(1, 2, 5, 5)); bbar.setBorder(BorderFactory.createEmptyBorder(0, 5, 5, 3)); btn = new JButton("Ok"); bbar.add(btn); btn.addActionListener(new AbstractAction () { public void actionPerformed (ActionEvent e) { dlg.setVisible(false); int s = ((Integer)seed.getValue()).intValue(); ACODemo.this.panel.initAnts( ((Integer)antcnt.getValue()).intValue(), Double.parseDouble(phinit.getText()), (s != 0) ? new Random(s) : new Random()); ACODemo.this.panel.setParams( Double.parseDouble(exploit.getText()), Double.parseDouble(alpha.getText()), Double.parseDouble(beta.getText()), Double.parseDouble(layexp.getText()), Double.parseDouble(elite.getText()), Double.parseDouble(evap.getText())); } } ); btn = new JButton("Apply"); bbar.add(btn); btn.addActionListener(new AbstractAction () { public void actionPerformed (ActionEvent e) { int s = ((Integer)seed.getValue()).intValue(); ACODemo.this.panel.initAnts( ((Integer)antcnt.getValue()).intValue(), Double.parseDouble(phinit.getText()), (s != 0) ? new Random(s) : new Random()); ACODemo.this.panel.setParams( Double.parseDouble(exploit.getText()), Double.parseDouble(alpha.getText()), Double.parseDouble(beta.getText()), Double.parseDouble(layexp.getText()), Double.parseDouble(elite.getText()), Double.parseDouble(evap.getText())); } } ); 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, 145); dlg.pack(); return dlg; } /* createAnts() */ /*------------------------------------------------------------------*/ private void runAnts (int epochs, int delay) { /* --- run the ants */ if (this.cnt >= 0) { /* check for running update */ this.timer.stop(); this.cnt = -1; return; } AntColony ants = this.panel.getAnts(); if (ants == null) return; /* get the ant colony */ if (delay <= 0) { /* if to update at the end */ while (--epochs >= 0) /* while more epochs to compute, */ this.panel.runAnts(); /* run the ants again */ this.panel.repaint(); /* redraw the window contents */ this.stat.setText("epoch: " +ants.getEpoch() +", best tour: " +ants.getBestLen()); return; /* show a status message, */ } /* then abort the function */ this.cnt = epochs; /* note the epochs */ this.timer = new Timer(delay, new ActionListener () { public void actionPerformed (ActionEvent e) { if (--ACODemo.this.cnt < 0) { ACODemo.this.timer.stop(); return; } ACODemo.this.panel.runAnts(); /* run the ants and */ ACODemo.this.panel.repaint(); /* redraw the window contents */ AntColony ants = ACODemo.this.panel.getAnts(); ACODemo.this.stat.setText("epoch: " +ants.getEpoch() +", best tour: " +ants.getBestLen()); } } ); /* update the status text */ this.timer.start(); /* start the status update timer */ } /* runAnts() */ /*------------------------------------------------------------------*/ private JDialog createRunOpt () { /* --- create run dialog */ final JDialog dlg = new JDialog(this, "Run Optimization..."); GridBagLayout g = new GridBagLayout(); GridBagConstraints lc = new GridBagConstraints(); GridBagConstraints rc = new GridBagConstraints(); JPanel grid = new JPanel(g); JPanel bbar; JLabel lbl; JTextArea help; JButton btn; grid.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5)); lc.fill = /* fill fields in both directions */ rc.fill = GridBagConstraints.BOTH; rc.weightx = 1.0; /* resize only the input fields, */ lc.weightx = 0.0; /* but not the labels */ lc.weighty = 0.0; /* resize lines equally */ rc.weighty = 0.0; /* in vertical direction */ lc.ipadx = 10; /* gap between labels and inputs */ lc.ipady = 10; /* make all lines of the same height */ rc.gridwidth = GridBagConstraints.REMAINDER; lbl = new JLabel("Number of epochs:"); g.setConstraints(lbl, lc); grid.add(lbl); final JSpinner epochs = new JSpinner( new SpinnerNumberModel(5000, 1, 999999, 1)); g.setConstraints(epochs, rc); grid.add(epochs); lbl = new JLabel("Delay between epochs:"); g.setConstraints(lbl, lc); grid.add(lbl); final JSpinner delay = new JSpinner( new SpinnerNumberModel(200, 0, 999999, 10)); g.setConstraints(delay, rc); grid.add(delay); bbar = new JPanel(new GridLayout(1, 2, 5, 5)); bbar.setBorder(BorderFactory.createEmptyBorder(0, 5, 5, 3)); btn = new JButton("Ok"); bbar.add(btn);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -