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

📄 ccpanel.java

📁 著名的神经网络工具箱
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*************************************************************************

This program is copyrighted. Please refer to COPYRIGHT.txt for the
copyright notice.

This file is part of JavaNNS.

JavaNNS is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

JavaNNS is distributed in the hope that it will be useful,
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with JavaNNS; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

*************************************************************************/


package javanns;

import javax.swing.* ;
import java.awt.event.* ;
import java.awt.* ;
import javax.swing.border.* ;


/**
 * class CCPanel is the defining class for the layout of the cascade correlation
 * panel
 */
class CCPanel extends JTabbedPane implements ActionListener{
  Snns snns;
  CCFunctions functions;
  JPanel pGeneral, pMod, pCand, pOut, pInit, pLearn;
  JInternalFrame frame;

  // wird zur Erkennung in Snns.actionPerformed(...) benutzt:
  public static final String TITLE = "Cascade Correlation & TACOMA";

  double maxOutErr, minCovCh, errCh;
  double minInit, maxInit;

  int candPat, maxCovUp, maxCandNo, outPat, maxEpochs, cascades;

  double[] param = new double[5],
           learnParam = new double[5];

  ThreadChief tc;
  volatile boolean is_learning = false;


  Function initFn, updateFn, learnFnCascade, learnFnTacoma;

  // components for "General" panel:
  JTextField tMaxOutErr;
  JCheckBox cbCovNErr, cbCache, cbPrune;
  JLabel lMini;
  NamedComboBox ncbMini;

  // components for "Modification" panel:
  NamedComboBox ncbMod;
  JLabel[] lParam = new JLabel[5];
  JTextField[] tParam = new JTextField[5];

  // components for "Candidates" panel:
  JTextField tMinCovCh, tCandPat, tMaxCovUp, tMaxCandNo;
  NamedComboBox ncbActFn;

  // components for "Output" panel:
  JTextField tErrCh, tOutPat, tMaxEpochs;

  // components for "Init" panel:
  JTextField tMinInit, tMaxInit;

  // components for "Learn" panel:
  NamedComboBox ncbLearnFn;
  JLabel[] lLearnParam;
  JTextField[] tLearnParam;
  JTextField tCascades;

  Insets insets;
  JButton bInit, bLearn;

  public CCPanel( Snns s ) {
    snns = s;
    functions = new CCFunctions( snns );
    insets = new Insets(3, 3, 2, 2); // snns.panel_insets;

    initFn = new Function(snns, Function.INIT, "CC_Weights");
    learnFnCascade = new Function(snns, Function.LEARN, "CC", "snn-77-1.html");  /* !!!!!!!!!!!!!!!!!!!!!! */
    learnFnTacoma  = new Function(snns, Function.LEARN, "TACOMA");
    updateFn = new Function(snns, Function.UPDATE, "CC_Order");

    JPanel pMain = new JPanel();
    buildGeneral();
    buildMod();
    buildCand();
    buildOut();
    buildInit();
    buildLearn();

    setDefaultValues();

    addTab("General", pGeneral);
    addTab("Modification", pMod);
    addTab("Candidates", pCand);
    addTab("Output", pOut);
    addTab("Init", pInit);
    addTab("Learn", pLearn);

    setBorder(new EmptyBorder(insets));
    pMain.setLayout(new CenterLayout());
    pMain.add( this );

    actionPerformed( new ActionEvent( ncbMod, 0, "" ) );
    actionPerformed( new ActionEvent( ncbLearnFn, 0, "" ) );

    frame = new JInternalFrame( TITLE, false, true, false, true );
    frame.setContentPane( pMain );
    frame.pack();

    tc = new ThreadChief(){
      public void stopped(Object o){
        if( o instanceof Exception )
          snns.showException((Exception)o, this);
        is_learning = false;
        bLearn.setText("Learn");
      }
    };
    tc.sleep = 100;
    tc.awake = 1;
  }
/*------------------------ interfaces ----------------------------------------*/

  public void actionPerformed( ActionEvent evt ){
    //System.out.println("CascadeCorr.actionPerf()");
    Object src = evt.getSource();

    if(src == cbPrune) {
      ncbMini.setEnabled(cbPrune.isSelected());
      lMini.setEnabled(cbPrune.isSelected());
    }

    if( src == ncbMod ){
      SimpleFunction f = (SimpleFunction)ncbMod.getSelectedObject();
      if( f == null ) return;
      int i;
      Parameter parameter;
      for( i=0; i<f.parameter.length; i++ ){
        parameter = f.parameter[i];
        lParam[i].setVisible( true );
        lParam[i].setText( parameter.parStr );
        lParam[i].setToolTipText( parameter.toolTip );
        tParam[i].setVisible( true );
        tParam[i].setText( String.valueOf( parameter.getDefaultValue() ) );
        tParam[i].setToolTipText( parameter.toolTip );
      }
      for( i=f.parameter.length; i<5; i++ ){
        lParam[i].setVisible( false );
        tParam[i].setVisible( false );
      }
      if(f.getKernelName().equals("TACOMA")) {
        cbPrune.setEnabled(false);
        ncbMini.setEnabled(false);
        lMini.setEnabled(false);
      }
      else {
        cbPrune.setEnabled(true);
        ncbMini.setEnabled(cbPrune.isSelected());
        lMini.setEnabled(cbPrune.isSelected());
      }
    }

    else if( src == ncbLearnFn ){
      SimpleFunction f = (SimpleFunction)ncbLearnFn.getSelectedObject();
      if( f == null ) return;
      int i;
      Parameter parameter;
      for( i=0; i<f.parameter.length; i++ ){
        parameter = f.parameter[i];
        lLearnParam[i].setVisible( true );
        lLearnParam[i].setText( parameter.parStr );
        lLearnParam[i].setToolTipText( parameter.toolTip );
        tLearnParam[i].setVisible( true );
        tLearnParam[i].setText( String.valueOf( parameter.getDefaultValue() ) );
        tLearnParam[i].setToolTipText( parameter.toolTip );
      }
      for( i=f.parameter.length; i<5; i++ ){
        lLearnParam[i].setVisible( false );
        tLearnParam[i].setVisible( false );
      }
    }

    else if( src == bInit ){
      Network network = snns.network;

      try{ minInit = Double.valueOf( tMinInit.getText() ).doubleValue(); }
      catch( Exception e ){
        showException("Minimum initializing activation value has to be number");
        return;
      }
      try{ maxInit = Double.valueOf( tMaxInit.getText() ).doubleValue(); }
      catch( Exception e ){
        showException("Maximum initializing activation value has to be a number");
        return;
      }

      // sollte eigentlich der kernel 黚ernehmen:
      network.deselectUnits();
      int type;
      for( Unit u=network.getFirstUnit(); u!=null; u=network.getNextUnit() ){
        type = u.getType();
        if( !( type == UnitTTypes.INPUT || type == UnitTTypes.OUTPUT ) )
          network.selectUnit( u );
      }
      network.deleteUnits();

      try{
        network.setFunction( initFn, new double[]{ minInit, maxInit, 0, 0, 0 } );
        network.initNet();
      }
      catch( Exception e ){
        showException( "Couldn't initialize the network\n"+e.getMessage() );
      }
    }

    else if( src == bLearn ){
      if( is_learning ) tc.stop = true;
      else learn();
    }
  }

/*---------------------- private methods -------------------------------------*/

  /**
   * this method checks the value entries of the panels and puts them to the
   * kernel to train the net
   */
  private void learn(){
    Network network = snns.network;
    Function learnFn;

    try{ maxOutErr = Double.valueOf( tMaxOutErr.getText() ).doubleValue(); }
    catch( Exception e ){
      showException("Maximum output unit error has to be a double value");
      return;
    }
    try{ minCovCh = Double.valueOf( tMinCovCh.getText() ).doubleValue(); }
    catch( Exception e ){
      showException("Minimum covariance change has to be a double value");
      return;
    }
    try{ errCh = Double.valueOf( tErrCh.getText() ).doubleValue(); }
    catch( Exception e ){
      showException("Error change has to be a double value");
      return;
    }
    try{ candPat = Integer.parseInt( tCandPat.getText() ); }
    catch( Exception e ){
      showException("Candidate patience has to be an integer value");
      return;
    }
    try{ maxCovUp = Integer.parseInt( tMaxCovUp.getText() ); }
    catch( Exception e ){
      showException("Maximum number of covariance updates has to be an integer value");
      return;
    }
    try{ maxCandNo = Integer.parseInt( tMaxCandNo.getText() ); }
    catch( Exception e ){
      showException("Maximum number of candidate units has to be an integer value");
      return;
    }
    try{ outPat = Integer.parseInt( tOutPat.getText() ); }
    catch( Exception e ){
      showException("Output patience has to be an integer value");
      return;
    }
    try{ maxEpochs = Integer.parseInt( tMaxEpochs.getText() ); }
    catch( Exception e ){
      showException("Maximum number of epochs has to be an integer value");
      return;
    }

    String learn_func, actfunc, mini_func, modification;
    SimpleFunction f = (SimpleFunction)ncbLearnFn.getSelectedObject();
    if( f == null ) {
      showException("No learning function selected");
      return;
    }
    learn_func = f.kernel_name;

    f = (SimpleFunction)ncbActFn.getSelectedObject();
    if( f == null ) {
      showException("No activation function selected");
      return;
    }
    actfunc = f.kernel_name;

    f = (SimpleFunction)ncbMini.getSelectedObject();
    if( f == null ) {
      showException("No minimalizing function selected");
      return;
    }
    mini_func = f.kernel_name;

    f = (SimpleFunction)ncbMod.getSelectedObject();
    if(f == null) {
      showException("No modification selected");
      return;
    }
    modification = f.kernel_name;
    if(modification.equals("TACOMA")) learnFn = learnFnTacoma;
    else learnFn = learnFnCascade;

    boolean print_covar = cbCovNErr.isSelected(),
            cacheUnitAct = cbCache.isSelected(),
            prune_new_hidden = cbPrune.isSelected();

    double[] p = new double[5];
    try{
      for( int i=0; i<5; i++ )
      p[i] = Double.valueOf( tLearnParam[i].getText() ).doubleValue();
    }
    catch( Exception e ){
      showException("Learn parameters have to be numbers");
      return;
    }

    try{
      cascades = Integer.parseInt( String.valueOf( tCascades.getText() ) );
    }
    catch( Exception e ){
      showException("Number of cascades has to be an integer");
      return;
    }

    double[] modParam = new double[5];
    try{
      for( int i=0; i<5; i++ )
      modParam[i] = Double.valueOf( tLearnParam[i].getText() ).doubleValue();
    }
    catch( Exception e ){
      showException("Modification parameters have to be numbers");
      return;
    }

    network.setCascadeParams(maxOutErr,
                        learn_func,
                        print_covar,
                        prune_new_hidden,
                        mini_func,
                        minCovCh,
                        candPat,
                        maxCovUp,
                        maxCandNo,
                        actfunc,
                        errCh,
                        outPat,
                        maxEpochs,
                        modification,
                        modParam,
                        cacheUnitAct);

    bLearn.setText("Stop");
    tc.stop = false;
    is_learning = true;

    try{
      network.setFunction( updateFn, new double[] { 0, 0, 0, 0, 0 } );
      network.setFunction( learnFn, p );
      network.trainNet(tc, cascades, false, false);  // change booleans!!!!!!!!!!!!!!!!!!
    }
    catch( Exception e ){
      tc.stopped( e );
    }
  }


  /*
  the following method initialize the panels of the main tabbed pane
  */

  private void buildGeneral(){
    pGeneral = new JPanel();
    JLabel lMaxOutErr = new JLabel("Maximum output unit error: ");

    tMaxOutErr = new JTextField(10);
    cbCovNErr  = new JCheckBox("Print covariance and error", false );
    cbCache    = new JCheckBox("Cache the unit activation", false );
    cbPrune    = new JCheckBox("Prune new hidden unit", false );
    lMini      = new JLabel("Minimize: ");
    ncbMini    = new NamedComboBox();
    ncbMini.addItems(functions.mini);

    lMaxOutErr.setToolTipText("Abort learning if the error is below");
    tMaxOutErr.setToolTipText("Abort learning if the error is below");
    cbCovNErr.setToolTipText("Trace error development");
    cbCache.setToolTipText("Speeds up computing at cost of memory");
    cbPrune.setToolTipText("Enable Pruning Cascade Correlation");
    lMini.setToolTipText("Criterion for pruning");
    ncbMini.setToolTipText("Criterion for pruning");

    GridBagLayout gbl = new GridBagLayout();
    GridBagConstraints gbc = new GridBagConstraints();
    gbc.fill = gbc.HORIZONTAL;
    gbc.insets = insets;
    gbc.weightx = gbc.weighty = 0.5;
    pGeneral.setLayout( gbl );
    gbc.gridx = gbc.gridy = 0;

    gbl.setConstraints( lMaxOutErr, gbc );
    pGeneral.add( lMaxOutErr );

    gbc.gridx = 1;
    gbl.setConstraints( tMaxOutErr, gbc );

⌨️ 快捷键说明

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