/** GPApplet.java * J Scott Cameron * * This is an applet which uses Genetic Programming * for Symbolic Regression */ import javax.swing.*; import java.awt.*; import java.applet.*; import java.util.*; import java.awt.event.*; import java.io.*; import Operators; import GPPopulation; import GPNode; import GPPair; import GPNodeContainer; import GaGraph; import GPCompGraph; public class GPApplet extends JApplet implements ActionListener,ItemListener{ private Operators operators; /* operators for symbolic regression */ private Random random; private GPPopulation population; /* population of Genetic Programs*/ private Vector gpHistory; /* best GP's, for graph */ /* swing components*/ private JLabel bestGPScore; private JLabel bestGPGen; private JLabel generationsAdvanced; private JTextArea possibleFunctions; private JTextArea defineFunction; private JButton setFunction; private JTextField xMin; private JTextField xMax; private JLabel lMessage; private JButton resetGP; private JButton nextGen; private JButton manyGens; private JTextField numGens; private JTextField popSize; private JCheckBox mutation; private JCheckBox crossover; private JTextField mutationRate; private int generation; private GpGraph graph; private GPNodeContainer bestGP; private JButton graphComparison; private GPCompGraph compGraph; private JTextField compXMin; private JTextField compXMax; private JTextField compYMin; private JTextField compYMax; /* booleans to keep track of the state of their appropriate check * boxes */ private boolean crossoverBool = true; private boolean mutationBool = true; // This is a hack to avoid an ugly error message in 1.1. public GPApplet() { getRootPane().putClientProperty("defeatSystemEventQueueCheck", Boolean.TRUE); } /* initializes applet */ public void init() { /* set Look and Feel to standard Swing look and feel */ try { UIManager.setLookAndFeel( UIManager.getCrossPlatformLookAndFeelClassName()); } catch (Exception e) { } random = new Random(); operators = new Operators(); /* create an initail population */ population = new GPPopulation(operators,10,true,true,.01); /* initialize swing components */ JTabbedPane tp = new JTabbedPane(); tp.setBorder(BorderFactory.createMatteBorder(1,1,2,2,Color.black)); getContentPane().add(tp, BorderLayout.CENTER); tp.addTab("Equation Definition",equationPanel()); tp.addTab("Use Genetic Programming",gpPanel()); tp.addTab("Comparison Graph",compPanel()); /* set the initial function */ setFunctionPressed(); resetGPPressed(); } /* this returns a string of the available functions * to be displayed so the user can see their options */ public String getPossibleFunctionsString() { StringWriter sw = new StringWriter(); Vector temp; int i; temp = operators.getBinaryFunctions(); sw.write("Binary Functions:\n"); for(i=0;i