📄 gaaapplet.java
字号:
try {
url = new URL("file:////" + dir + File.separator);
}
catch (Exception e) {
return;
}
problem = new GaaProblem(url,fil);
if (!problem.success) {
ret = MsgDialog.msg("Initialization Error", "Cannot load GA parameters from file "+fil, MsgDialog.MB_OK, 0);
}
else {
this.setCursor(new Cursor(Cursor.WAIT_CURSOR));
statusLabel.setText("Please wait...");
mainPanel.remove(paramsPanel);
mainPanel.remove(actionPanel);
pop = new GaaPopulation(problem);
function = problem.function;
paramsPanel = new GaaParams(problem,pop);
actionPanel = new GaaAction(problem,pop,function);
mainPanel.add("params",paramsPanel);
mainPanel.add("action",actionPanel);
statusLabel.setText("");
this.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
GaaAction.mainTitle.setText("The GA Playground - "+problem.problemTitle);
mainLayout.show(mainPanel,"action");
}
}
}
void saveParameters() {
int ret;
FileWriter fw;
String dir;
String fil;
int i;
String txt = "";
Date date;
String dateString;
GaaFileInput fileData;
String dataFields[][];
int dataTypes[];
int recordNumber;
if (gaaAppMode.equals("Applet"))
ret = MsgDialog.msg("Sorry", "This option is not supported in an applet", MsgDialog.MB_OK, 0);
else {
date = new Date();
dateString = DateFormat.getDateTimeInstance(DateFormat.LONG,DateFormat.LONG).format(date);
dir = System.getProperty("user.dir");
Frame f = new Frame("Save Parameters");
final FileDialog fd = new FileDialog(f,"Save current parameters to file",FileDialog.SAVE);
fd.setDirectory(dir);
fd.setFile("*.par");
fd.show();
fil = fd.getFile();
if (fil == null) {
return;
}
fil = dir + File.separator + fil;
try {
fw = new FileWriter(fil);
} catch(IOException e) {
return;
}
PrintWriter prw = new PrintWriter(fw, false);
try {
txt = "[" + problem.problemTitle + " parameters saved at: " + dateString + "]";
prw.write(txt);
prw.println();
prw.println();
fileData = problem.fileData;
recordNumber = fileData.recordNumber;
dataFields = fileData.dataFields;
dataTypes = fileData.dataTypes;
txt = "[Integers]";
prw.write(txt);
prw.println();
for (i=0;i<recordNumber;i++) {
if (!(dataFields[i][0].charAt(0) == '[')) {
if (dataTypes[i] == 0) {
txt = dataFields[i][0] + "=" + dataFields[i][1];
prw.write(txt);
prw.println();
}
}
}
prw.println();
txt = "[Reals]";
prw.write(txt);
prw.println();
for (i=0;i<recordNumber;i++) {
if (!(dataFields[i][0].charAt(0) == '[')) {
if (dataTypes[i] == 1) {
txt = dataFields[i][0] + "=" + dataFields[i][1];
prw.write(txt);
prw.println();
}
}
}
prw.println();
txt = "[Strings]";
prw.write(txt);
prw.println();
for (i=0;i<recordNumber;i++) {
if (!(dataFields[i][0].charAt(0) == '[')) {
if (dataTypes[i] == 2) {
txt = dataFields[i][0] + "=" + dataFields[i][1];
prw.write(txt);
prw.println();
}
}
}
prw.println();
txt = "[Flags]";
prw.write(txt);
prw.println();
for (i=0;i<recordNumber;i++) {
if (!(dataFields[i][0].charAt(0) == '[')) {
if (dataTypes[i] == 3) {
String t1 = dataFields[i][0];
String t2 = dataFields[i][1];
txt = dataFields[i][0] + "=" + dataFields[i][1];
prw.write(txt);
prw.println();
}
}
}
prw.println();
}
catch (Exception e) {
}
prw.flush();
prw.close();
ret = MsgDialog.msg("Ok", "Parameters written to "+fil, MsgDialog.MB_OK, 0);
}
}
public void itemStateChanged(ItemEvent e) {
int ret;
if (e.getSource() instanceof CheckboxMenuItem) {
CheckboxMenuItem mi = (CheckboxMenuItem)e.getSource();
if (mi == chkStatusHelp) {
if (mi.getState())
problem.withStatusHelp = true;
else
problem.withStatusHelp = false;
}
else
if (mi == chkWithText) {
if (mi.getState()) {
problem.withTextWindow = true;
problem.withStatusText = true;
problem.withGraphicText = true;
}
else {
problem.withTextWindow = false;
problem.withStatusText = false;
problem.withGraphicText = false;
}
}
else
if (mi == chkWithGraphics) {
if (mi.getState())
problem.withGraphicWindow = true;
else
problem.withGraphicWindow = false;
}
else
if (mi == chkWithSound) {
if (mi.getState())
problem.withSound = true;
else
problem.withSound = false;
}
if (mi == chkWithLog) {
if (mi.getState())
problem.withLogging = true;
else
problem.withLogging = false;
GaaAction.deb.inLog = problem.withLogging;
}
else
if (mi == chkDebug) {
if (mi.getState())
GaaMisc.debugOn = true;
else
GaaMisc.debugOn = false;
}
}
else
if (e.getSource() instanceof Choice) {
Choice ch = (Choice)e.getSource();
if (ch == chcProblem) {
int sel = ch.getSelectedIndex();
String pr = ch.getSelectedItem();
int pos = pr.indexOf(':');
String fname = pr.substring(0,pos) + ".par";
problem = new GaaProblem(getDocumentBase(),fname);
if (!problem.success) {
ret = MsgDialog.msg("Initialization Error", "Cannot load GA parameters from file "+fname, MsgDialog.MB_OK, 0);
}
else {
this.setCursor(new Cursor(Cursor.WAIT_CURSOR));
statusLabel.setText("Please wait...");
mainPanel.remove(paramsPanel);
mainPanel.remove(actionPanel);
pop = new GaaPopulation(problem);
function = problem.function;
paramsPanel = new GaaParams(problem,pop);
actionPanel = new GaaAction(problem,pop,function);
mainPanel.add("params",paramsPanel);
mainPanel.add("action",actionPanel);
statusLabel.setText("");
this.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
GaaAction.mainTitle.setText("The GA Playground - "+problem.problemTitle);
if (fname.equals("GenFunc.par")) {
if (gaaAppMode.equals("Applet"))
ret = MsgDialog.msg("Not supported in Applet mode", "This option can only be accessed in Application mode", MsgDialog.MB_OK, 0);
else {
mainLayout.show(mainPanel,"params");
GaaParams.tab.show(GaaParams.udfPanel);
}
}
else
mainLayout.show(mainPanel,"action");
}
}
}
}
void showFactorial() {
double ft = GaaMisc.factorial(problem.genesNumber);
String txt = " Factorial of number of genes = " + ft;
statusLabel.setText(txt);
}
void testRoutineOne() {
//checkMemory(1);
//checkMemory(2);
String t;
t = "Chrom[0]= "+pop.chroms[0]+" Vals[0]= "+pop.vals[0]+" Value(chroms[0])= "+function.getValue(pop.chroms[0]);
System.out.println(t);
GaaApplet.statusLabel.setText(t);
}
void testRoutineTwo() {
int i;
for (i=0;i<pop.popSize;i++) {
pop.chroms[i] = "ABABABABABABABABABABB";
}
problem.function.preBreed();
for (i=0;i<pop.popSize;i++) {
pop.currentId = i;
pop.vals[i] = problem.function.getValue(pop.chroms[i]);
pop.fits[i] = pop.vals[i];
String c = pop.chroms[i];
double v = pop.vals[i];
double f = pop.fits[i];
f = f;
}
problem.function.postBreed();
pop.processFitness();
pop.bestVal = pop.vals[0];
pop.bestChrom = pop.chroms[0];
pop.lastVal = pop.vals[0];
pop.lastChrom = pop.chroms[0];
problem.currentBestVal = pop.vals[0];
problem.currentBestChrom = pop.chroms[0];
problem.currentGeneration = 0;
double bestVal = pop.vals[0];
String bestChrom = pop.chroms[0];
bestVal = bestVal;
}
public void actionPerformed(ActionEvent e)
{
if (e.getSource() instanceof MenuItem)
{
MenuItem mi = (MenuItem)e.getSource();
MenuItem mp = (MenuItem)mi.getParent();
String li = mi.getLabel();
String lp = mp.getLabel();
if (lp.equals("GA")) {
if (li.equals("Parameters")) {
mainLayout.show(mainPanel,"params");
currentCard = "params";
}
else if (li.equals("Execution")) {
mainLayout.show(mainPanel,"action");
currentCard = "action";
}
else if (li.equals("Entry Screen")) {
mainLayout.show(mainPanel,"entry");
currentCard = "entry";
}
}
else if (lp.equals("File")) {
if (li.equals("Test")) {
test1();
}
else if (li.equals("Load Parameters")) {
loadParameters();
}
else if (li.equals("Load Population")) {
loadPopulation();
keepOldPop = true;
pop.initPopulation();
if (currentCard.equals("action")) {
GaaAction.text.setText(pop.poplistTableString());
GaaAction.canvas.repaint();
}
}
else if (li.equals("Save Parameters")) {
saveParameters();
}
else if (li.equals("Save Population")) {
saveCurpop();
}
else if (li.equals("Save Log")) {
saveLog();
}
else if (li.equals("Quit")) {
AppletFrame.closeIt();
}
else
notForApplet();
}
else if (lp.equals("Log")) {
if (li.equals("Log Parameters")) {
GaaMisc.dbg(problem.parameterString());
}
else if (li.equals("Log Current Population (Format #1)")) {
GaaMisc.dbg(pop.poplistTableString(1));
}
else if (li.equals("Log Current Population (Format #2)")) {
GaaMisc.dbg(pop.poplistTableString(2));
}
else if (li.equals("Log Current Population (Format #3)")) {
GaaMisc.dbg(pop.poplistTableString(3));
}
else if (li.equals("Log Current Population (Format #4)")) {
GaaMisc.dbg(pop.poplistTableString(4));
}
else if (li.equals("Log Current Mating Process")) {
pop.debugFlag = true;
}
else if (li.equals("Log Current Status")) {
GaaMisc.dbg(pop.popStatusString());
}
else if (li.equals("Show Log File")) {
GaaAction.deb.show();
}
else if (li.equals("Save Log File")) {
saveLog();
}
}
else if (lp.equals("Info")) {
if (li.equals("Parameters")) {
updateHelpScreen(infowin,problem.parameterString());
}
else if (li.equals("Current Population")) {
updateHelpScreen(infowin,pop.poplistTableString());
}
else if (li.equals("Log File")) {
GaaAction.deb.show();
}
//else if (li.equals("Best Solution")) {
// updateHelpScreen(infowin,"Results (not ready yet)");
//}
else if (li.equals("Check Memory")) {
checkMemory(0);
}
else if (li.equals("Factorial")) {
showFactorial();
}
}
else if (lp.equals("Options")) {
if (li.equals("Preferences")) {
mainLayout.show(mainPanel,"params");
GaaParams.tab.show("Options");
}
}
else if (lp.equals("Misc")) {
if (li.equals("Test #1")) {
testRoutineOne();
}
if (li.equals("Test #2")) {
testRoutineTwo();
}
}
else if (lp.equals("Help")) {
if (li.equals("General Help")) {
genhelpwin.show();
}
else if (li.equals("Input Files Help")) {
filhelpwin.show();
}
else if (li.equals("Credits")) {
creditsBox();
}
else if (li.equals("About")) {
aboutBox();
}
}
else
notReady();
}
}
/*
protected void initialize()
{
try
{
netscape.security.PrivilegeManager.enablePrivilege("UniversalPropertyRead");
}
catch (netscape.security.ForbiddenTargetException e)
{
showStatus("\tFailed! Permission to read system properties denied by user.");
}
catch (Exception e)
{
showStatus("\tFailed! Unknown exception while enabling privilege.");
}
try
{
netscape.security.PrivilegeManager.enablePrivilege("UniversalFileRead");
}
catch (netscape.security.ForbiddenTargetException e)
{
showStatus("Can't get file read access. Damn!");
}
catch (Exception e)
{
showStatus("Something else went wrong getting file read access"+e);
}
try
{
netscape.security.PrivilegeManager.enablePrivilege("UniversalFileWrite");
}
catch (netscape.security.ForbiddenTargetException e)
{
showStatus("Can't write to a file");
}
catch (Exception e)
{
showStatus("Something else went wrong getting file write access"+e);
}
try
{
netscape.security.PrivilegeManager.checkPrivilegeEnabled("UniversalFileRead");
netscape.security.PrivilegeManager.checkPrivilegeEnabled("UniversalFileWrite");
}
catch (netscape.security.ForbiddenTargetException e)
{
showStatus("Login is going to fail b/c it can't read the filesystem.");
}
catch (Exception e)
{
showStatus("Login is going to fail b/c it can't read the filesystem.");
showStatus(e.toString());
}
}
*/
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -