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

📄 psodemo.java

📁 A program to demonstrate the optimization process of particle swarm optimization. A two-dimensional
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
  /*------------------------------------------------------------------*/  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(100, 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);    btn.addActionListener(new AbstractAction () {      public void actionPerformed (ActionEvent e) {        dlg.setVisible(false);        PSODemo.this.updateSwarm(          ((Integer)epochs.getValue()).intValue(),          ((Integer)delay.getValue()).intValue());      } } );    btn = new JButton("Apply"); bbar.add(btn);    btn.addActionListener(new AbstractAction () {      public void actionPerformed (ActionEvent e) {        PSODemo.this.updateSwarm(          ((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, 339);    dlg.pack();    return dlg;  }  /* createRunOpt() */  /*------------------------------------------------------------------*/  private JDialog createAbout ()  {                             /* --- create "About..." dialog box */    final JDialog dlg  = new JDialog(this, "About PSODemo...", 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      ("PSODemo\n"      +"A Particle Swarm Optimization Demo\n"      +"Version 1.1, 2005.12.02\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 run ()  {                             /* --- create GUI of swarm 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"));    item = menu.add(new JMenuItem("Save PNG Image..."));    item.setMnemonic('i');    item.addActionListener(new AbstractAction() {      public void actionPerformed (ActionEvent e) {        PSODemo.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 (PSODemo.this.about != null)            PSODemo.this.about.setVisible(false);          PSODemo.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("Select Function..."));    item.setMnemonic('c');    item.addActionListener(new AbstractAction() {      public void actionPerformed (ActionEvent e) {        if (PSODemo.this.fnsel == null)          PSODemo.this.fnsel = PSODemo.this.createFnSel();        PSODemo.this.fnsel.setVisible(true);      } } );    item = menu.add(new JMenuItem("Create Particle Swarm..."));    item.setMnemonic('c');    item.addActionListener(new AbstractAction() {      public void actionPerformed (ActionEvent e) {        if (PSODemo.this.swarm == null)          PSODemo.this.swarm = PSODemo.this.createSwarm();        PSODemo.this.swarm.setVisible(true);      } } );    item = menu.add(new JMenuItem("Run Optimization..."));    item.setMnemonic('o');    item.addActionListener(new AbstractAction() {      public void actionPerformed (ActionEvent e) {        if (PSODemo.this.runopt == null)          PSODemo.this.runopt = PSODemo.this.createRunOpt();        PSODemo.this.runopt.setVisible(true);      } } );    menu.addSeparator();    item = menu.add(new JMenuItem("Redraw"));    item.setMnemonic('r');    item.addActionListener(new AbstractAction() {      public void actionPerformed (ActionEvent e) {        PSODemo.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 (PSODemo.this.about == null)           PSODemo.this.about = PSODemo.this.createAbout();        PSODemo.this.about.setVisible(true);      } } );    /* --- create and set the main panel --- */    this.panel = new PSOPanel();     this.panel.setLayout(new BorderLayout());    this.panel.setPreferredSize(new Dimension(656, 656));    this.getContentPane().add(this.panel, 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("PSODemo");    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("PSODemo 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 PSODemo (boolean isProg)  { this.isprog = isProg;    try { EventQueue.invokeAndWait(this); } catch (Exception e) {} }  public PSODemo ()  { this.isprog = false;    try { EventQueue.invokeAndWait(this); } catch (Exception e) {} }  public PSODemo (String title)  { this(false); this.setTitle(title); }  /*------------------------------------------------------------------*/  public static void main (String args[])  {                             /* --- main function */    PSODemo v = new PSODemo(true);  }  /* main() */               /* create an swarm demo viewer */}  /* class PSODemo */

⌨️ 快捷键说明

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