www.pudn.com > jnntbeta0.2f.zip > nn.~jav


import java.awt.*; 
import java.awt.event.*; 
import java.applet.*; 
import java.io.*; 
import java.net.*; 
import java.util.Properties; 
 
public class nn extends Applet { 
 
   
  Choice netChoice;//网络模型选择 
  Choice inNumChoice;//输入接点选择 
  Choice hideNumChoice;//隐含接点选择 
  Choice outNumChoice; //输出接点选择 
  Checkbox chkbox_save; 
  int task; 
  BpNet bpnet; 
  RbfNet rbfnet; 
  boolean clear; 
  int row; 
  int left; 
  int bottom; 
  int top; 
  double p1[][];//训练样本集输入 
  double t1[][];//训练样本集输出 
  double p2[][];//模拟数据输入 
  int inNum; 
  int hideNum; 
  int outNum; 
  int sampleNum; 
  int simNum; 
  PrintStream ps; 
  int epochs; 
  TextArea txtarea; 
 
  public void init() { 
    left=50; 
    top=80; 
    bottom=300; 
    clear=false; 
    row=0; 
    epochs=60000;//学习步长(可调) 
 
    setLayout(new BorderLayout()); 
    Panel topPanel=new Panel(); 
     
    Panel bottomPanel=new Panel(); 
 
    netChoice=new Choice(); 
 
 
    netChoice.addItem("BP network"); 
    netChoice.addItem("LBG clustering"); 
    netChoice.addItem("RBF network"); 
 
    netChoice.addItemListener(new ItemListener(){ 
        public void itemStateChanged (ItemEvent event){ 
        repaint(); 
        } 
    }); 
 
    topPanel.add(netChoice); 
 
    Label lab_inNum=new Label("输入节点数:"); 
    topPanel.add(lab_inNum); 
    inNumChoice=new Choice(); 
    for (int i=1;i<10;i++) 
       inNumChoice.addItem(String.valueOf(i)); 
    topPanel.add(inNumChoice); 
 
    Label lab_hideNum=new Label("隐含节点数:"); 
    topPanel.add(lab_hideNum); 
    hideNumChoice=new Choice(); 
    for (int i=1;i<10;i++) 
       hideNumChoice.addItem(String.valueOf(i)); 
    hideNumChoice.select("3"); 
    topPanel.add(hideNumChoice); 
 
    Label lab_outNum=new Label("输出节点数:"); 
    topPanel.add(lab_outNum); 
    outNumChoice=new Choice(); 
    for (int i=1;i<20;i++) 
       outNumChoice.addItem(String.valueOf(i)); 
    topPanel.add(outNumChoice); 
 
    Button button_load=new Button("LoadData"); 
    button_load.addActionListener(new ActionListener(){ 
        public void actionPerformed(ActionEvent event){ 
        task=10; 
        repaint(); 
        } 
    }); 
    topPanel.add(button_load); 
 
    Button button_train=new Button("Train"); 
    button_train.addActionListener(new ActionListener(){ 
        public void actionPerformed(ActionEvent event){ 
            task=1; 
            repaint(); 
        } 
    }); 
    topPanel.add(button_train); 
 
    Button button_sim=new Button("Simulation"); 
    button_sim.addActionListener(new ActionListener(){ 
        public void actionPerformed(ActionEvent event){ 
        task=2; 
        repaint(); 
        } 
    }); 
 
    topPanel.add(button_sim); 
 
 
    Button printButton=new Button("print..."); 
    printButton.setForeground(Color.black); 
   //p.add(printButton); 
    printButton.addActionListener(new ActionListener(){ 
        public void actionPerformed (ActionEvent event){ 
           printComponents(nn.this); 
           } 
     }); 
     topPanel.add(printButton); 
 
    chkbox_save=new Checkbox("save data"); 
    // bottomPanel.add(chkbox_save); 
 
    add("North",topPanel); 
    txtarea=new TextArea(12,105); 
    bottomPanel.add(txtarea); 
    add("South", bottomPanel); 
     
 
 
  } 
 
  public void paint(Graphics g){ 
  setBackground(Color.white); 
   switch (task) 
   { 
      case 1:if (netChoice.getSelectedItem()=="BP network") 
                 drawBpTrain(g); 
             else if  (netChoice.getSelectedItem()=="LBG clustering") 
                 drawLBG(g); 
             else if  (netChoice.getSelectedItem()=="RBF network") 
                 drawRbfTrain(g); 
             break; 
      case 2:if (netChoice.getSelectedItem()=="BP network") 
                 drawBpSim(g); 
             else if  (netChoice.getSelectedItem()=="RBF network") 
                 drawRbfSim(g); 
             break; 
      case 10:loadData(g); 
             break; 
     
    }//end switch 
    task=0; 
  } 
 
public void drawBpTrain(Graphics g){ 
 
  int time=1000; 
  double etime=1000.0; 
  //g.drawString(netChoice.getSelectedItem(),300,300); 
  // double p1[][]={{0.1},{0.2},{0.3},{0.4},{0.5}}; 
  // double t1[][]={{0.12},{0.22},{0.32},{0.42},{0.52}}; 
  bpnet=new BpNet(Integer.parseInt(inNumChoice.getSelectedItem()),Integer.parseInt(hideNumChoice.getSelectedItem()),Integer.parseInt(outNumChoice.getSelectedItem())); 
   //bpnet=new BpNet(2,3,1); 
  //绘制误差曲线坐标 
 
   g.setColor(getBackground()); 
   g.fillRect(0,0,left,bottom); 
   g.setColor(getForeground()); 
  g.drawLine(left,bottom,left+1000,bottom); 
  g.drawLine(left,bottom,left,0); 
 
 
  for (int i=0;i10) 
     { 
        row=0; 
        cleartext(g); 
     } 
     t2=bpnet.sim(p21); 
     row++; 
     //g.drawString("inputValue:",left,bottom+20+row*20); 
     txtarea.appendText("inputvalue:\n"); 
     for(int i=0;i10) 
     { 
        row=0; 
        cleartext(g); 
     } 
     t2=rbfnet.sim(p21); 
     row++; 
     //g.drawString("inputValue:",left,bottom+20+row*20); 
     txtarea.appendText("inputvalue:\n"); 
     for(int i=0;i0) 
                   //g.drawString(",",left+10+j*50,top+20*i); 
                   txtarea.appendText(","); 
               //g.drawString(String.valueOf(p1[i][j]),left+15+j*50,top+20*i); 
               txtarea.appendText(String.valueOf(p1[i][j])); 
            } 
        //g.drawString("}",left+50*inNum+25,top+20*i); 
        txtarea.appendText("}"); 
        //g.drawString(String.valueOf(lbg.indext[i]),left+40+50*inNum,top+20*i); 
        txtarea.appendText(String.valueOf(lbg.indext[i])+"\n"); 
    }//end for i 
    } 
 
public void loadData(Graphics g){//读取学习数据 
 
    //Frame f=new Frame("file open"); 
    //FileDialog fd=new FileDialog(f,"File Open"); 
    //fd.show(); 
 
    clearall(g); 
    row=0; 
    String str_p1=""; 
    String str_t1=""; 
    String str_p2=""; 
    try 
    {URL filedata=null; 
     DataInputStream dis=null; 
     //System.out.println(getCodeBase()+"nndata.txt"); 
     filedata=new URL(getCodeBase()+"nndata.txt"); 
     //filedata=new URL(getCodeBase()+fd.getFile()); 
     dis=new DataInputStream(filedata.openStream()); 
     String line=dis.readLine(); 
 
     row++; 
     simNum=1; 
     while(line!=null) 
       { //System.out.println(line); 
 
         if (line.indexOf("inNum:")>=0) 
           inNum=Integer.parseInt(line.substring(line.indexOf("inNum:")+6)); //得到输入接点数 
         if (line.indexOf("hideNum:")>=0) 
           hideNum=Integer.parseInt(line.substring(line.indexOf("hideNum:")+8));  //得到隐含接点数 
         if (line.indexOf("outNum:")>=0) 
           outNum=Integer.parseInt(line.substring(line.indexOf("outNum:")+7)); //得到输出接点数 
         if (line.indexOf("sampleNum:")>=0) 
           sampleNum=Integer.parseInt(line.substring(line.indexOf("sampleNum:")+10)); //得到学习样本数 
         if (line.indexOf("simNum:")>=0) 
           simNum=Integer.parseInt(line.substring(line.indexOf("simNum:")+7)); //得到仿真样本数 
 
         if (line.indexOf("p1:")>=0) 
           str_p1+=line.substring(line.indexOf("p1:")+3); 
         if (line.indexOf("t1:")>=0) 
           str_t1+=line.substring(line.indexOf("t1:")+3); 
         if (line.indexOf("p2:")>=0) 
           str_p2+=line.substring(line.indexOf("p2:")+3); 
        //cleartext(g); 
        //g.drawString(line,left,bottom+20+row*20); 
        txtarea.appendText(line+"\n"); 
        row++; 
        line=dis.readLine(); 
       }//end while 
      inNumChoice.select(String.valueOf(inNum)); 
      hideNumChoice.select(String.valueOf(hideNum)); 
      outNumChoice.select(String.valueOf(outNum)); 
     
     p1=new double[sampleNum][inNum]; 
     t1=new double[sampleNum][outNum]; 
     p2=new double[simNum][inNum]; 
      
     p1=readarray(sampleNum,inNum,str_p1);  //读取输入样本矩阵 
     System.out.println(str_p1); 
     //for(int i=0;i