FileDocCategorySizeDatePackage
GUIMain.javaAPI DocExample7595Tue Apr 04 16:05:36 BST 2000None

GUIMain.java

//GUIMain
import java.awt.event.*;
import java.awt.*;
import java.io.*;
import GARunModule;

class GUIMain extends Frame implements ActionListener,
  WindowListener, TextListener {

  public static boolean stoppit;

  TextField popText=null;
  TextField lengthText=null;
  TextField alphabetText=null;
  TextField alphabetString=null;
  TextField repText=null;
  TextField crossText =null;
  TextField mutText=null;
  TextField maxGenText=null;
  Button pushButton=null;
  Button stopButton=null;
  public static TextField gensStatusScreen=null;
  public static TextField fitnessStatusScreen=null;
  public static TextField bestStatusScreen=null;
  //Returned variables
  int M; // Koza's notation for population size
  int L; // Koza's string length
  int G; // Max number of generations
  float Pc; // crossover probability
  float Pr; // reproduction probability
  float Pm; // mutation probability


  public static void displayFit(String s) {
  // for status window
    fitnessStatusScreen.setText(s);
  }

  public static void displayGens(String s) {
    gensStatusScreen.setText(s);
  }

  public static void displayBest(String s) {
    bestStatusScreen.setText(s);
  }

  public GUIMain(String s) {
    super(s);
    //setBackground(Color.white);
    stoppit=false;
    addWindowListener(this);
    setLayout(new GridLayout(10,2));

    Label popLabel = new Label("Population Size (M)");
    add(popLabel);
    popText = new TextField("100",5);
    add(popText);
    popText.addActionListener(this);

    Label lengthLabel = new Label("String Length (L)");
    add(lengthLabel);
    lengthText = new TextField("18",3);
    add(lengthText);
    lengthText.addActionListener(this);

    Label maxGenLabel = new Label("Maximum Generations (G)");
    add(maxGenLabel);
    maxGenText=new TextField("200",3);
    add(maxGenText);
    maxGenText.addActionListener(this);

/***********
    Label alphabetLabel = new Label("Alphabet Size (K)");
    add(alphabetLabel);
    alphabetText = new TextField("2",2);
    add(alphabetText);
************/

/******************
    Label alphabetStringLabel = new Label("Alphabet String");
    add(alphabetStringLabel);
    alphabetString = new TextField("01",10);
    add(alphabetString);
*************************/
    Label repLabel = new Label("Reproduction Probability %");
    add(repLabel);
    repText = new TextField("80",3);
    add(repText);
    repText.addTextListener(this);

    Label crossLabel = new Label("Crossover Probability %");
    add(crossLabel);
    crossText = new TextField("17",3);
    add(crossText);
    crossText.addTextListener(this);

    Label mutLabel = new Label("Mutation Probability %");
    add(mutLabel);
    mutText = new TextField("3",3);
    add(mutText);
    mutText.addTextListener(this);

    pushButton = new Button("Go!");
    add(pushButton);
    pushButton.addActionListener(this);

    stopButton = new Button("Stop");
    add(stopButton);
    stopButton.addActionListener(this);

    Label fitLabel=new Label("Fitness");
    add(fitLabel);
    //status screen display
    fitnessStatusScreen = new TextField(20);
    fitnessStatusScreen.setEditable(false);
    add(fitnessStatusScreen);

    Label gensLabel=new Label("Gens %");
    add(gensLabel);
    gensStatusScreen = new TextField(5);
    gensStatusScreen.setEditable(false);
    add(gensStatusScreen);

    Label bestLabel=new Label("Best individual");
    add(bestLabel);
    bestStatusScreen=new TextField(L);
    bestStatusScreen.setEditable(false);
    add(bestStatusScreen);

  }

public void windowClosed(WindowEvent event){}
public void windowDeiconified(WindowEvent event){}
public void windowIconified(WindowEvent event){}
public void windowActivated(WindowEvent event){}
public void windowDeactivated(WindowEvent event){}
public void windowOpened(WindowEvent event){}

public void windowClosing(WindowEvent event) {
  System.exit(0);
}

  public void actionPerformed(ActionEvent event) {
  // Collects button command from data and does some validation    
  // Sort out source of command
    if(event.getSource() == stopButton) {
      stoppit=true;
    }
      
    if(event.getSource() == popText) {
      M = Integer.parseInt(popText.getText());
      System.out.println("M String is " + popText.getText());
      System.out.println("M Value is " + M);
    }
    if(event.getSource()==lengthText) {
      L= Integer.parseInt(lengthText.getText());
      System.out.println("L Value is " + L);
    }
    if(event.getSource()==maxGenText) {
      G = Integer.parseInt(lengthText.getText());
      System.out.print("G value is " + G);
    }

    if(event.getSource()==pushButton) {
      //Collect data explicitly, or else it won't be there!
      M = Integer.parseInt(popText.getText()); 
      L= Integer.parseInt(lengthText.getText());
      G= Integer.parseInt(maxGenText.getText());
      int PrPerCent = Integer.parseInt(repText.getText());
      Pr = (float)(PrPerCent);
      int PcPerCent=Integer.parseInt(crossText.getText());
      Pc = (float)(PcPerCent);
      int PmPerCent = Integer.parseInt(mutText.getText());
      Pm = (float)PmPerCent;

      //run the program
      System.out.println("Running!!! with M and L"+M+L);
      
      GARunModule.runGA(M,L,G,Pr,Pc,Pm);
    }
                 
  }

  public void textValueChanged(TextEvent event) {
  // The idea here is that the rep, cross and mut probs as 5 always add
  // up to 100.
  int PrPerCent =Integer.parseInt(repText.getText());
  int PcPerCent = Integer.parseInt(crossText.getText());
  int PmPerCent=Integer.parseInt(mutText.getText());
  try {
    if(event.getSource()==repText) {
       PrPerCent = Integer.parseInt(repText.getText());
       Pr = (float)(PrPerCent);
       if(PrPerCent>100) {
         PrPerCent=100;
         repText.setText(String.valueOf(PrPerCent));
       }
       if(PrPerCent+PcPerCent+PmPerCent != 100) {
         PcPerCent=100-PrPerCent;
         crossText.setText(String.valueOf(PcPerCent));
         PmPerCent=0;
         mutText.setText(String.valueOf(PmPerCent));
       }
                    
       System.out.println(Pr);
    }
    if(event.getSource()==crossText) {
      PcPerCent=Integer.parseInt(crossText.getText());
      if(PcPerCent>100) {
        PcPerCent=100;
        crossText.setText(String.valueOf(PcPerCent));
      }
      if(PrPerCent+PcPerCent+PmPerCent>100) {
       // PcPerCent=100-PrPerCent;
       // crossText.setText(String.valueOf(PcPerCent));
        PmPerCent=0;
        mutText.setText(String.valueOf(PmPerCent));
      }
      if(PrPerCent+PcPerCent+PmPerCent<100) {
        PmPerCent = 100-PrPerCent-PcPerCent;
        mutText.setText(String.valueOf(PmPerCent));
      }
      Pc = (float)(PcPerCent);
      System.out.println("Value is "+Pc);
    }
    if(event.getSource()==mutText) {
      PmPerCent=Integer.parseInt(mutText.getText());
      if(PmPerCent>100) {
        PmPerCent=100;
        mutText.setText(String.valueOf(PmPerCent));
      }
      if(PrPerCent+PcPerCent+PmPerCent > 100) {
       PcPerCent=PcPerCent-PmPerCent;
       crossText.setText(String.valueOf(PcPerCent));
       // PmPerCent=100-PrPerCent-PmPerCent;
        mutText.setText(String.valueOf(PmPerCent));
      }
      Pm = (float)(PmPerCent);
      System.out.println("Value is "+ Pm);
    }
  }
  catch (NumberFormatException e) {}
    
  }

 

}// end class