📄 gaaapplet.java
字号:
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;
import java.util.*;
import java.text.*;
import MenuPanel;
public class GaaApplet extends Applet implements ActionListener, ItemListener {
public static GaaProblem problem;
public static GaaPopulation pop;
public static GaaFunction function;
public static GaaParams paramsPanel;
public static GaaAction actionPanel;
GaaLog infowin;
GaaLog genhelpwin;
GaaLog filhelpwin;
public static GaaLog udfhelpwin;
public static GaaLog strategyhelpwin;
URL mainPage;
public static URL mainDir;
public static String gaaAppMode;
String genhelpFileName;
String filhelpFileName;
String udfhelpFileName;
String strategyhelpFileName;
public static String parFileName;
String genhelpText;
String filhelpText;
String udfhelpText;
String strategyhelpText;
String currentCard;
AudioClip joy;
AudioClip beep;
Image picture;
AppletContext appcontext;
Dimension screenSize;
public static Frame appFrame;
public static boolean keepOldPop;
private Panel mainPanel;
private Panel menuPanel;
private Panel statusPanel;
private CardLayout mainLayout;
public static Label statusLabel;
Choice chcProblem;
public static CheckboxMenuItem chkStatusHelp;
public static CheckboxMenuItem chkWithText;
public static CheckboxMenuItem chkWithGraphics;
public static CheckboxMenuItem chkWithSound;
public static CheckboxMenuItem chkWithLog;
public static CheckboxMenuItem chkDebug;
public void init() {
int i, ret;
String s;
String txt;
gaaAppMode = "Applet";
genhelpFileName = "GaaHelp.txt";
filhelpFileName = "GaaFileHelp.txt";
udfhelpFileName = "GaaUdfHelp.txt";
strategyhelpFileName = "GaaStrategyHelp.txt";
keepOldPop = false;
GaaMisc.debug("Starting GA Playground");
mainDir = getDocumentBase();
try {
genhelpText = GaaMisc.getTextFromFile(new URL(getDocumentBase(),genhelpFileName));
filhelpText = GaaMisc.getTextFromFile(new URL(getDocumentBase(),filhelpFileName));
udfhelpText = GaaMisc.getTextFromFile(new URL(getDocumentBase(),udfhelpFileName));
strategyhelpText = GaaMisc.getTextFromFile(new URL(getDocumentBase(),strategyhelpFileName));
}
catch (Exception e) {
ret = MsgDialog.msg("Initialization Error", "Cannot load help files", MsgDialog.MB_OK, 0);
}
genhelpwin = createHelpScreen("General Help Screen",genhelpText,5);
filhelpwin = createHelpScreen("Input Files Help Screen",filhelpText,5);
infowin = createHelpScreen("Info Screen","",5);
udfhelpwin = createHelpScreen("Interactively Defined Function",udfhelpText,5);
strategyhelpwin = createHelpScreen("Strategy Evolution Experiments - Help Screen",strategyhelpText,5);
infowin.inLog = true;
setName("Gaa V1");
setLayout(new BorderLayout());
setSize(750, 420);
setBackground(Color.lightGray);
if (gaaAppMode.equals("Applet")) {
parFileName = GetOneParameter("paramFileName","");
}
else {
if (parFileName.equals("")) {
parFileName = "All.par";
}
}
GaaMisc.debug("Loading problem definition file");
problem = new GaaProblem(getDocumentBase(),parFileName);
if (!problem.success) {
ret = MsgDialog.msg("Initialization Error", "Cannot load GA parameters from file "+parFileName, MsgDialog.MB_OK, 0);
if (problem.problemCode == 3) {
stop();
destroy();
}
}
GaaMisc.debug("Creating population");
pop = new GaaPopulation(problem);
function = problem.function;
paramsPanel = new GaaParams(problem,pop);
actionPanel = new GaaAction(problem,pop,function);
GaaMisc.debug("Loading multimedia files");
if (gaaAppMode.equals("Applet")) {
appcontext = getAppletContext();
picture = getImage(getCodeBase(),"gaa.gif");
joy = getAudioClip(getCodeBase(), "joy.au");
beep = getAudioClip(getCodeBase(), "Beep.au");
}
GaaMisc.debug("Creating GUI");
menuPanel = new Panel();
MenuPanel mp = makeMenuPanel();
menuPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
Font fm = new Font("Helvetica",Font.BOLD,12);
menuPanel.setFont(fm);
mp.setFont(fm);
Label d1 = new Label();
Label d2 = new Label();
menuPanel.add(mp);
menuPanel.add(d1);
menuPanel.add(d2);
statusPanel = new Panel();
makeStatusPanel();
Panel entryPanel = makeEntryPanel();
mainLayout = new CardLayout();
mainPanel = new Panel(mainLayout);
mainPanel.add("entry",entryPanel);
mainPanel.add("params",paramsPanel);
mainPanel.add("action",actionPanel);
add("North", menuPanel);
add("South", statusLabel);
add("Center",mainPanel);
GaaMisc.debug("Finished loading");
if (parFileName.equals("All.par"))
mainLayout.show(mainPanel,"entry");
else {
GaaAction.mainTitle.setText("The GA Playground - "+problem.problemTitle);
mainLayout.show(mainPanel,"action");
}
}
public static void main(String argv[]){
gaaAppMode = "Application";
if (argv.length > 0)
parFileName = argv[0];
else
parFileName = "All.par";
AppletFrame.startApplet( "GaaApplet", "The GA Playground",argv);
/*
appFrame = new Frame("The GA Playground");
GaaApplet app = new GaaApplet();
app.init();
app.start();
appFrame.add(app);
appFrame.setVisible(true);
*/
}
public void destroy() {
GaaAction.deb.dispose();
genhelpwin.dispose();
filhelpwin.dispose();
udfhelpwin.dispose();
strategyhelpwin.dispose();
}
public URL getDocumentBase() {
try {
gaaAppMode = "Applet";
mainDir = super.getDocumentBase();
}
catch (Exception e) {
gaaAppMode = "Application";
String dir = System.getProperty("user.dir");
dir = "file://///" + dir + File.separator;
try {
mainDir = new URL(dir);
}
catch(Exception ee) {
mainDir = null;
String t = ee.toString();
}
}
return mainDir;
}
public URL getCodeBase() {
URL codeBase;
if (gaaAppMode.equals("Applet")) {
gaaAppMode = "Applet";
codeBase = super.getDocumentBase();
}
else {
String dir = System.getProperty("user.dir");
dir = "file://///" + dir + File.separator;
try {
codeBase = new URL(dir);
dir = dir;
}
catch(Exception ee) {
codeBase = null;
String t = ee.toString();
}
}
return codeBase;
}
private Panel makeEntryPanel() {
int i, j;
Panel panel = new Panel(new BorderLayout());
Font f1 = new Font("Helvetica",Font.BOLD,20);
Font f2 = new Font("Helvetica",Font.BOLD,14);
Panel left = new Panel();
Panel right = new Panel();
Panel center = new Panel();
String dummy = " ";
left.add(new Label(dummy));
right.add(new Label(dummy));
center.setLayout(new GridLayout(12,1));
Label labels[] = new Label[10];
//if (parFileName.equals("All.par")) {
if (true) {
Font f = new Font("Courier",Font.PLAIN,12);
chcProblem = new Choice();
chcProblem.addItem("TspDemo: Tsp where all cities are located on a circle");
chcProblem.addItem("TspBayg29: Tsp of 29 Cities in Bavaria (Groetschel,Juenger,Reinelt)");
chcProblem.addItem("TspAtt48: Tsp of 48 capitals of the US (Padberg/Rinaldi)");
chcProblem.addItem(" ");
chcProblem.addItem("Knapsack01: A single 0/1 Knapscak problem with 50 objects ");
chcProblem.addItem("KnapsackWeing1: A multiple 0/1 Knapsack problem with 2 knapsacks and 28 objects ");
chcProblem.addItem("KnapsackWeish01: A multiple 0/1 Knapsack problem with 5 knapsacks and 30 objects ");
chcProblem.addItem(" ");
chcProblem.addItem("Steiner: Allocating Facility Problem - all cities on a circle");
chcProblem.addItem("SteinerByFile: Allocating Facility Problem - city data read from a file");
chcProblem.addItem(" ");
chcProblem.addItem("Binpack1_00: u120_00 1D Binpacking Problem (Number of objects: 120, Capacity: 150)");
chcProblem.addItem("Binpack5_19: t60_19 1D Binpacking Problem (Number of objects: 60, Capacity: 100)");
chcProblem.addItem(" ");
chcProblem.addItem("Rosenbrock: Minimize: sum(100*(x(i)-x(i-1)^2)^2 + (1-x(i-1))^2");
chcProblem.addItem("Ackley: Minimize: 20+e-20*exp(-0.2*exp(sqrt((1/n)*sum(xi^2))-exp((1/n)*sum(cos(2*Pi*xi))");
chcProblem.addItem("Rastrigin: Minimize: 10.0*n + sum(x(i)^2 - 10.0*cos(2*Pi*x(i)))");
chcProblem.addItem("Schwefel: Minimize: 418.9829*n + sum(-x(i)*sin(sqrt(abs(x(i))))");
chcProblem.addItem("Griewank: Minimize: 1/4000*sum(x(i)-100)^2 - prod((x(i)-100)/sqrt(i)) + 1");
chcProblem.addItem(" ");
chcProblem.addItem("SphereModel: Minimize: sum((x(i)-1)^2)");
chcProblem.addItem("SingleVarMin: Minimize f = x^4 - 12*x + 15*x^2 + 56*x - 60");
chcProblem.addItem("MultiVarMin: Minimize f = x1*sin(x1)+1.7*x2*sin(x1)-1.5*x3-0.1*x4*cos(x4+x5-x1)+(0.2*x5^2-x2)-1");
chcProblem.addItem("Simpleton: A trivial Maximization Problem f = x1*x2*x3*x4*x5/(x6*x7*x8*x9*x10)");
chcProblem.addItem(" ");
chcProblem.addItem("GenFunc: Optimize an interactively defined function/expression (single or multi variable)");
chcProblem.setFont(f);
chcProblem.addItemListener(this);
String txts[] = {"Welcome to the GA Playground",
" ",
"Short Instructions:",
"1. Hide the toolbars in your browser to make the whole applet visible",
"2. Select a demo problem from the combo list below",
"3. Optionally select 'Parameters' from the 'GA' menu to modify problem attributes",
"4. Select 'Execute' from the 'GA' menu and run the problem",
"5. Return to this screen by selecting 'Entry Screen' from the 'GA' menu",
"6. Monitoring options can be toggled On or Off anytime from the 'Options' menus",
" "};
for (i=0;i<10;i++) {
if (i == 0) {
labels[i] = new Label(txts[i],Label.CENTER);
labels[i].setFont(f1);
//labels[i].setForeground(Color.blue);
}
else {
labels[i] = new Label(txts[i],Label.LEFT);
labels[i].setFont(f2);
}
center.add(labels[i]);
}
center.add(chcProblem);
}
else {
int pos = parFileName.indexOf(".");
String nam = parFileName.substring(0,pos);
String txt = "Current problem name: " + nam;
String txts[] = {"Welcome to the GA Playground",
" ",
"Short Instructions:",
"1. Hide the toolbars in your browser to make the whole applet visible",
"2. Optionally select 'Parameters' from the 'GA' menu to modify problem attributes",
"3. Select 'Execute' from the 'GA' menu and run the problem",
"4. Status bar help tips can be toggled on or off through the 'Options' menu",
"5. Text window evolution information can be toggled on or off through the 'Options/Switches' menu",
" ",txt};
for (i=0;i<10;i++) {
if (i == 0) {
labels[i] = new Label(txts[i],Label.CENTER);
labels[i].setFont(f1);
//labels[i].setForeground(Color.blue);
}
else {
labels[i] = new Label(txts[i],Label.LEFT);
labels[i].setFont(f2);
}
center.add(labels[i]);
}
}
panel.add("East",left);
panel.add("West",right);
panel.add("Center",center);
return(panel);
}
private MenuPanel makeMenuPanel() {
MenuBar mBar = new MenuBar();
Font f = new Font("Helvetica",Font.BOLD,12);
mBar.setFont(f);
mBar.add(MenuPanel.makeMenu("File",
new Object[] { "Load Parameters", "Load Population", null,"Save Parameters", "Save Population", "Save Log", null, "Print Report", "Quit" },
this) );
mBar.add(MenuPanel.makeMenu("GA",
new Object[] {"Parameters","Execution","Entry Screen"},
this) );
/*
mBar.add(MenuPanel.makeMenu("Log",
new Object[] { "Log Parameters", "Log Current Population","Log Current Status", "Show Log File","Save Log File"},
this) );
*/
mBar.add(MenuPanel.makeMenu("Log",
new Object[] { "Log Parameters",
"Log Current Population (Format #1)",
"Log Current Population (Format #2)",
"Log Current Population (Format #3)",
"Log Current Population (Format #4)",
"Log Current Mating Process",
"Log Current Status", "Show Log File","Save Log File"},
this) );
mBar.add(MenuPanel.makeMenu("Info",
new Object[] { "Parameters", "Current Population","Log File", "Check Memory","Factorial"},
this) );
chkStatusHelp = new CheckboxMenuItem("Enable Statusbar Help",problem.withStatusHelp);
chkStatusHelp.addItemListener( this);
chkWithText = new CheckboxMenuItem("Text Window Active",problem.withTextWindow);
chkWithText.addItemListener( this);
chkWithGraphics = new CheckboxMenuItem("Graphic Window Active",problem.withGraphicWindow);
chkWithGraphics.addItemListener( this);
chkWithSound = new CheckboxMenuItem("Sound Active",problem.withSound);
chkWithSound.addItemListener( this);
chkWithLog = new CheckboxMenuItem("Logging Active",problem.withLogging);
chkWithLog.addItemListener( this);
chkDebug = new CheckboxMenuItem("Debug Active",GaaMisc.debugOn);
chkDebug.addItemListener( this);
//GaaMisc.debugOn = true;
//chkDebug.setState(true);
mBar.add(MenuPanel.makeMenu("Options",
new Object[] { chkStatusHelp, "Preferences", null,
MenuPanel.makeMenu("Switches",
new Object[]
{chkWithText, chkWithGraphics,chkWithSound,chkWithLog,chkDebug},
this)
},
this) );
mBar.add(MenuPanel.makeMenu("Misc",
new Object[] {"Test #1","Test #2"},
this) );
mBar.add(MenuPanel.makeMenu("Help",
new Object[] { "General Help", "Input Files Help","Credits","About" },
this) );
MenuPanel mPanel = new MenuPanel();
mPanel.setMenuBar(mBar);
return(mPanel);
}
private void aboutBox() {
String txt = "The GA Playground; ;Ariel Dolan;Email: aridolan@netvision.net.il;Web Site: http://www.aridolan.com;";
int ret = MsgDialog.msg("About", txt, MsgDialog.MB_OK, 11);
}
private void creditsBox() {
String txt = "";
txt = txt + "The following programs, offered as free downloads by their authors,;were used in this applet:; ;";
txt = txt + ";JEL - Java Expressions Library; by Konstantin Metlov, http://galaxy.fzu.cz/JEL/;";
txt = txt + ";MenuPanel - A menu panel for applets; by Bruce Stone, http://members.aol.com/zstoner/MenuDemo/;";
txt = txt + ";MsgBox - A Windows-like message dialog box; by Sebastien Stormacq, http://www.ambrasoft.lu/external/developer/freesoft/default.asp;";
txt = txt + ";SplitterLayout - A layout manager that handles segmented panels; by Scott Stanchfield, http:www.scruz.net/~thetick;";
txt = txt + ";TabSplitter - A tab panel; by Scott Stanchfield, http:www.scruz.net/~thetick;";
//txt = txt + ";Eval - Evaluation of a mathematical expression given as a string; by Patrik Lundin, patriklundin@hotmail.com, f95-plu@nada.kth.se;";
//txt = txt + "; ;ScsGrid - A Java Grid;by SofTech Computer Systems, Inc.;Site: http://www.scscompany.com;Email: sales@scscompany.com;";
int ret = MsgDialog.msg("Credits - Free Java classes used in this applet", txt, MsgDialog.MB_OK, 12);
}
public static void notReady() {
int ret = MsgDialog.msg("Sorry", "This option is not ready yet", MsgDialog.MB_OK, 0);
}
public static void notForApplet() {
int ret;
if (gaaAppMode.equals("Applet"))
ret = MsgDialog.msg("Sorry", "This option is not supported in an applet", MsgDialog.MB_OK, 0);
else
notReady();
}
private Panel makeStatusPanel() {
Panel p = new Panel();
statusLabel = new Label();
Font f = new Font("Helvetica",Font.BOLD,12);
statusLabel.setFont(f);
p.add(statusLabel);
return(p);
}
private void showAbout() {
final Dialog d = new Dialog((Frame) this.getParent(),"About");
Button b = new Button("Ok");
b.addActionListener(this);
d.add(b);
//d.setBounds(50,50,280,100);
d.show();
}
public static GaaLog createHelpScreen(String title, String txt, int margin) {
GaaLog win = new GaaLog(title);
win.setForeground(Color.black);
win.setFont(new Font("Courier",Font.BOLD,12));
win.clear();
win.hide();
Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
int w = d.width;
int h = d.height;
win.reshape(margin,margin,w-2*margin,h-2*margin);
win.showMsg(txt);
return(win);
}
void updateHelpScreen(GaaLog win, String txt) {
win.clear();
win.showMsg(txt);
win.show();
}
double GetOneParameter(String name, double def) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -