001 package org.jaga.hooks;
002
003
004 import javax.swing.*;
005 import java.awt.*;
006 import java.awt.event.*;
007 import org.jaga.util.FittestIndividualResult;
008
009
010 /**
011 * TODO: Complete these comments.
012 *
013 * <p><u>Project:</u> JAGA - Java API for Genetic Algorithms.</p>
014 *
015 * <p><u>Company:</u> University College London and JAGA.Org
016 * (<a href="http://www.jaga.org" target="_blank">http://www.jaga.org</a>).
017 * </p>
018 *
019 * <p><u>Copyright:</u> (c) 2004 by G. Paperin.<br/>
020 * This program is free software; you can redistribute it and/or modify
021 * it under the terms of the GNU General Public License as published by
022 * the Free Software Foundation, ONLY if you include a note of the original
023 * author(s) in any redistributed/modified copy.<br/>
024 * This program is distributed in the hope that it will be useful,
025 * but WITHOUT ANY WARRANTY; without even the implied warranty of
026 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
027 * GNU General Public License for more details.<br/>
028 * You should have received a copy of the GNU General Public License
029 * along with this program; if not, write to the Free Software
030 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
031 * or see http://www.gnu.org/licenses/gpl.html</p>
032 *
033 * @author Greg Paperin (greg@jaga.org)
034 *
035 * @version JAGA public release 1.0 beta
036 */
037
038 public class GAAnalysisFrame extends JFrame {
039
040 private JPanel buttonPanel = new JPanel();
041 private JButton closeButton = new JButton();
042 private JSplitPane splitPane = new JSplitPane();
043 private JTextArea statsText = new JTextArea();
044 private JPanel graphPanel = null;
045
046 private AnalysisHook hook = null;
047 private AnalysisFrameUpdateThread updateThread = null;
048
049 private GAAnalysisFrame() {
050 throw new UnsupportedOperationException("Use GAAnalysisFrame(AnalysisHook hook) instead");
051 }
052
053 public GAAnalysisFrame(AnalysisHook hook) {
054 super();
055 if (null == hook)
056 throw new NullPointerException("Analysis hook may not be null");
057 this.hook = hook;
058 graphPanel = new AnalysisGraphPanel(hook);
059 try {
060 jbInit();
061 } catch (Exception e) {
062 e.printStackTrace();
063 }
064 this.setTitle(hook.getPlotFrameTitle());
065 long delay = hook.getUpdateDelay();
066 if (delay > 0) {
067 updateThread = new AnalysisFrameUpdateThread(delay, this, hook);
068 updateThread.start();
069 } else {
070 updateThread = null;
071 }
072 }
073
074 private void jbInit() throws Exception {
075 buttonPanel.setMinimumSize(new Dimension(10, 10));
076 buttonPanel.setPreferredSize(new Dimension(550, 35));
077 closeButton.setMaximumSize(new Dimension(120, 25));
078 closeButton.setMinimumSize(new Dimension(130, 25));
079 closeButton.setPreferredSize(new Dimension(130, 25));
080 closeButton.setText("Close window");
081 closeButton.addActionListener(new java.awt.event.ActionListener() {
082 public void actionPerformed(ActionEvent e) {
083 closeButton_actionPerformed(e);
084 }
085 });
086 splitPane.setOrientation(JSplitPane.VERTICAL_SPLIT);
087 splitPane.setBorder(null);
088 splitPane.setDebugGraphicsOptions(0);
089 statsText.setBackground(SystemColor.info);
090 statsText.setMinimumSize(new Dimension(6, 50));
091 statsText.setOpaque(true);
092 statsText.setPreferredSize(new Dimension(550, 230));
093 statsText.setCaretPosition(0);
094 statsText.setEditable(false);
095 statsText.setText("Statistics:");
096 graphPanel.setMinimumSize(new Dimension(10, 50));
097 graphPanel.setPreferredSize(new Dimension(550, 250));
098 this.setTitle("Genetic Algorithm Analysis");
099 this.getContentPane().add(buttonPanel, BorderLayout.SOUTH);
100 buttonPanel.add(closeButton, null);
101 this.getContentPane().add(splitPane, BorderLayout.CENTER);
102 splitPane.add(statsText, JSplitPane.BOTTOM);
103 splitPane.add(graphPanel, JSplitPane.TOP);
104 splitPane.setDividerLocation(300);
105
106 this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
107 this.setLocation(50, 50);
108 }
109
110 void closeButton_actionPerformed(ActionEvent e) {
111 hide();
112 dispose();
113 }
114
115 public void dispose() {
116 if (null != updateThread) {
117 updateThread.quit();
118 updateThread = null;
119 }
120 hook.frameDisposed();
121 super.dispose();
122 }
123
124 public void updateView() {
125 updateTextDisplay();
126 graphPanel.repaint();
127 hook.viewUpdateComplete();
128 }
129
130 private void updateTextDisplay() {
131 StringBuffer out = new StringBuffer("STATISTICS:");
132 if (hook.isAnalyseGenAge()) {
133 out.append("\nGeneration: ");
134 out.append(hook.getGenerationNum());
135 }
136 if (hook.isAnalyseTotalFitEvals()) {
137 out.append("\nFitness evaluations: ");
138 out.append(hook.getFitnessCalculations());
139 }
140 if (hook.isAnalyseRunTime()) {
141 out.append("\nRun time: ");
142 out.append(hook.getRunTime());
143 }
144 if (hook.isAnalyseBestFitness()) {
145 FittestIndividualResult r = hook.getBestResult();
146 if (null != r) {
147 out.append("\nCurrently best result: ");
148 out.append(r.toString());
149 }
150 out.append("\n[BLUE] Best fitness to date: ");
151 out.append(hook.getBestFitnessValue(hook.getBestFitnessValueCount() - 1).toStrBuf());
152 }
153 if (hook.isAnalyseGenMinFit()) {
154 out.append("\n[RED] Minimal fitness in last generation: ");
155 out.append(hook.getMinFitness(hook.getMinFitnessCount() - 1).toStrBuf());
156 }
157 if (hook.isAnalyseGenMaxFit()) {
158 out.append("\n[GREEN] Maximal fitness in last generation: ");
159 out.append(hook.getMaxFitness(hook.getMaxFitnessCount() - 1).toStrBuf());
160 }
161 if (hook.isAnalyseGenAverageFit()) {
162 out.append("\n[BLACK] Average fitness of the last generation: ");
163 out.append(hook.getAverageFitness(hook.getAverageFitnessCount() - 1).toStrBuf());
164 }
165 if (hook.isAnalyseGenFitStdDeviation()) {
166 int g = hook.getAverageFitness(hook.getAverageFitnessCount() - 1).getGeneration();
167 out.append("\n[ORANGE] Standard deviation of fitness in the last generation: ");
168 out.append(hook.getStdDeviation(g));
169 }
170 Runtime rt = Runtime.getRuntime();
171 out.append("\nMemory: free: ");
172 out.append(rt.freeMemory() / 1024);
173 out.append(" KB, total: ");
174 out.append(rt.totalMemory() / 1024);
175 out.append(" KB, max: ");
176 out.append(rt.maxMemory() / 1024);
177 out.append(" KB.");
178
179
180 statsText.setText(out.toString());
181 }
182
183 }