📄 gaaaction.java
字号:
import java.awt.*;
import java.awt.event.*;
import java.text.*;
import java.net.*;
import java.util.Vector;
import java.util.Date;
import com.magelang.splitter.*;
public class GaaAction extends Panel
implements Runnable, ActionListener, MouseListener {
GaaProblem problem;
GaaPopulation pop;
GaaFunction function;
GaaMisc misc;
public static GaaCanvas canvas;
public static GaaLog deb;
GaaLog helpwin;
GaaLog rulewin;
Graphics gra;
Thread runner;
boolean first;
boolean running;
long sleep;
public static TextArea text;
TextField input1;
TextField input2;
Label statusLabel;
Button textButton;
Button graphicButton;
Button buttons[];
String butTips[];
public static Label mainTitle;
URL mainPage;
Font buttonFont;
Font labelFont;
Date startDate;
Dimension screenSize;
DecimalFormat dFormat;
public GaaAction(GaaProblem pr, GaaPopulation pl, GaaFunction fn) {
int i;
String s;
String txt;
buttonFont = new Font("TimesRoman",Font.BOLD,12);
labelFont = new Font("TimesRoman",Font.BOLD,12);
dFormat = new DecimalFormat("0.0000000");
misc = new GaaMisc();
deb = misc.deb;
deb.inLog = true;
statusLabel = GaaApplet.statusLabel;
first = true;
running = false;
sleep = 50;
setBackground(Color.lightGray);
setLayout(new BorderLayout());
Label title = new Label("GA Test");
text = new TextArea("",50,10);
input1 = new TextField();
input2 = new TextField();
input1.addMouseListener(this);
input2.addMouseListener(this);
problem = pr;
pop = pl;
function = fn;
setLayout(new BorderLayout());
Panel bot = new Panel();
String butTitles[] = {"Start","Stop"," Pause ","1 Step",
"Kick","Reset","Clear","Decode Test","Eval Exp","Reserve"};
String txts[] = {
"Initialize population and start running current problem",
"Stop running current problem and display current (last) population",
"Pause/Resume evolutionary process (a toggle button)",
"Execute one step (a single generation) and stop",
"Induce a 'radiation dose' to shake current population",
"Stop execution, initialize a new population, reset screens and wait",
"Erase Text and Graphic screens",
"Decode and Calculate the test chromosome and update graphic and chrmosome value boxes accordingly",
"Evaluate a string expression in the form f(x1,x2..x20);x1=;x2=.. e.g. sin(x1)/x2;x1=0.5;x2=2",
"Reserved for testing purposes"};
butTips = txts;
buttons = createButtonArray(butTitles, bot);
//if (GaaApplet.gaaAppMode.equals("Applet"))
// buttons[8].disable();
add("South",bot);
buttons[1].disable();
buttons[2].disable();
Panel west = new Panel();
west.add(new Label(" "));
add("West",west);
Panel east = new Panel();
east.add(new Label(" "));
add("East",east);
Panel north = new Panel();
Font BoldFont = new Font("TimesRoman",Font.BOLD,14);
north.setFont(BoldFont);
mainTitle = new Label("The GA Playground");
mainTitle.setForeground(Color.black);
north.add(mainTitle);
add("North",north);
canvas = new GaaCanvas(problem, pop, function);
canvas.repaint();
gra = canvas.getGra();
Panel center = new Panel();
center.setLayout(new GridLayout(1,2,10,10));
center.add(createLeftPanel());
center.add(createRightPanel());
add("Center",center);
Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
resize(d.width-20,300);
//resize(620,350);
first = true;
}
private Button[] createButtonArray(String[] titles, Panel panel) {
int i;
Button buts[];
Font f = new Font("TimesRoman",Font.BOLD,12);
buts = new Button[titles.length];
for (i=0;i<titles.length;i++) {
buts[i] = new Button(titles[i]);
buts[i].setFont(f);
buts[i].addMouseListener( this );
panel.add(buts[i]);
}
return(buts);
}
private Panel createLeftPanel() {
Panel panel = new Panel();
panel.setLayout(new BorderLayout());
Panel north = new Panel(new BorderLayout());
Panel south = new Panel(new BorderLayout());
textButton = new Button("Text Window (toggle)");
textButton.setFont(labelFont);
textButton.addMouseListener( this );
Label lblTop = new Label("Text Window",Label.CENTER);
Label lblBot = new Label("Test Chromosome Input Box",Label.CENTER);
lblTop.setFont(labelFont);
lblBot.setFont(labelFont);
north.add("North",textButton);
north.add("Center",text);
south.add("North",lblBot);
south.add("Center",input1);
panel.add("Center",north);
panel.add("South",south);
return(panel);
}
private Panel createRightPanel() {
Panel panel = new Panel();
panel.setLayout(new BorderLayout());
Panel north = new Panel(new BorderLayout());
Panel south = new Panel(new BorderLayout());
graphicButton = new Button("Graphic Window (Toggle)");
graphicButton.setFont(labelFont);
graphicButton.addMouseListener( this );
Label lblTop = new Label("Graphic Window",Label.CENTER);
Label lblBot = new Label("Test Chromosome Value",Label.CENTER);
lblTop.setFont(labelFont);
lblBot.setFont(labelFont);
north.add("North",graphicButton);
north.add("Center",canvas);
south.add("North",lblBot);
south.add("Center",input2);
panel.add("Center",north);
panel.add("South",south);
return(panel);
}
public void updateActionPanel(GaaProblem pr, GaaPopulation pl, GaaFunction fn) {
problem = pr;
pop = pl;
function = fn;
}
public void stop()
{
if (runner!=null)
{
runner.stop();
runner=null;
running = false;
}
}
public void run() {
String txt = "";
while (running) {
pop.newGeneration();
double d1 = pop.vals[0];
double d2 = pop.bestVal;
double d3 = problem.currentBestVal;
String s1 = pop.chroms[0];
String s2 = pop.bestChrom;
String s3 = problem.currentBestChrom;
if (problem.withGraphicWindow) {
canvas.repaint();
}
if (problem.withTextWindow) {
txt = "#" + pop.generation +" Current: " + pop.chroms[0] + " : " + GaaMisc.formatDouble(pop.vals[0],3) + "\n";
text.appendText(txt);
}
if (pop.exitFlag) {
showBest();
showDecodedChrom(pop.bestChrom);
canvas.repaint();
buttons[0].enable();
buttons[1].disable();
buttons[2].disable();
buttons[2].setLabel(" Pause ");
runner.stop();
runner = null;
running = false;
}
try { Thread.sleep(sleep);}
catch (InterruptedException e) {
//showStatus("Thread.sleep InterruptedException "+e.toString());
}
}
}
public void restart(boolean reset) {
if (runner!=null) {
runner.stop();
runner = null;
running = false;
}
if (reset) {
//HistoryData.removeAllElements();
}
boolean b = GaaApplet.keepOldPop;
pop.initPopulation();
GaaApplet.keepOldPop = false;
text.appendText("\n\n");
try {
runner = new Thread(this);
runner.start();
running = true;
}
catch (Exception e) {
deb.debug("Restart error: " + e.toString());
}
}
void displayParameters() {
text.setText(problem.parameterString());
}
void displayPopList(boolean descending) {
text.setText(pop.poplistTableString());
}
void showBest() {
pop.currentId = 0;
double value = function.getValue(pop.bestChrom);
pop.chroms[0] = pop.bestChrom;
input1.setText("Best chromosome: "+pop.bestChrom);
input2.setText("Best chromosome value: "+value);
canvas.repaint();
}
public void showDecodedChrom(String chrom) {
text.appendText(function.getDecodedChrom(chrom));
}
public void resizeFrame(Frame frame) {
Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
int w = d.width;
int h = d.height;
frame.reshape(0,0,w,h);
frame.show();
/*
int fw = 640;
int fh = 480;
int dw = (int) (w - fw)/2;
int dh = (int) (h - fh)/2;
frame.reshape(dw,dh,fw,fh);
*/
}
private void evalTestExpression() {
double d;
int i, ret;
String txt;
String expr;
double vars[];
String parts[];
String pairs[];
vars = new double[21];
for (i=0;i<21;i++)
vars[i] = 0;
txt = input1.getText();
parts = FileInput.parseStr(txt, ";");
if (parts.length > 1) {
expr = parts[0];
for (i=1;i<parts.length;i++) {
pairs = FileInput.parseStr(parts[i], "=");
vars[i] = Double.valueOf(pairs[1]).doubleValue();
}
}
else {
ret = MsgDialog.msg("Error", "Test string is not formatted correctly", MsgDialog.MB_OK, 0);
return;
}
d = JelProcs.calc(expr,vars);
txt = "Result: "+d;
input2.setText(txt);
GaaApplet.statusLabel.setText(txt);
}
public void centerFrame(Frame frame) {
Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
int w = d.width;
int h = d.height;
int fw = 640;
int fh = 480;
int dw = (int) (w - fw)/2;
int dh = (int) (h - fh)/1;
frame.move(dw,dh);
}
public void mouseEntered(MouseEvent e) {
int i, j;
if (problem.withStatusHelp) {
if (e.getSource() instanceof TextField) {
TextField txt = (TextField)e.getSource();
if (txt == input1)
GaaApplet.statusLabel.setText("Enter a legitimate chromosome string for checking");
else
if (txt == input2)
GaaApplet.statusLabel.setText("This text box displays the calculated value of the test chromosome");
}
else
if (e.getSource() instanceof Button) {
Button but = (Button)e.getSource();
for (i=0;i<buttons.length;i++)
if (but == buttons[i]) {
GaaApplet.statusLabel.setText(butTips[i]);
}
}
}
}
public void mouseExited(MouseEvent e) {
GaaApplet.statusLabel.setText("");
}
public void mouseClicked(MouseEvent e) {
int i,j,ret;
boolean rt = true;
String s1, s2, s3, s4;
long oldtime, newtime, timediff;
if (e.getSource() instanceof Button)
{
Button btn = (Button)e.getSource();
if (btn == buttons[0]) {
if ((GaaApplet.gaaAppMode.equals("Applet")) && (problem.problemCode == 3)) {
ret = MsgDialog.msg("Cannot run in Applet mode", "This problem can only be run in Application mode", MsgDialog.MB_OK, 0);
return;
}
text.setText("");
deb.clear();
deb.start();
canvas.clear();
buttons[0].disable();
buttons[1].enable();
buttons[2].enable();
buttons[2].setLabel(" Pause ");
restart(true);
}
else if (btn == buttons[1]) {
runner.stop();
runner = null;
running = false;
canvas.repaint();
buttons[0].enable();
buttons[1].disable();
buttons[2].disable();
buttons[2].setLabel(" Pause ");
showBest();
showDecodedChrom(pop.bestChrom);
}
else if (btn == buttons[2]) {
if (!(runner == null)) {
if (running) {
runner.suspend();
running = false;
canvas.repaint();
buttons[2].setLabel(" Continue ");
}
else {
runner.resume();
running = true;
buttons[2].setLabel(" Pause ");
}
rt = true;
}
}
else if (btn == buttons[3]) {
pop.runEvolution(1);
displayPopList(true);
canvas.repaint();
}
else if (btn == buttons[4]) {
pop.kick();
canvas.repaint();
displayPopList(true);
}
else if (btn == buttons[5]) {
text.setText("");
input1.setText("");
input2.setText("");
pop.initPopulation();
displayPopList(true);
canvas.repaint();
}
else if (btn == buttons[6]) {
text.setText("");
input1.setText("");
input2.setText("");
deb.clear();
canvas.clear();
}
else if (btn == buttons[7]) {
String txt = input1.getText();
String old = pop.chroms[0];
pop.currentId = 0;
double value = function.getValue(txt);
pop.chroms[0] = txt;
pop.bestChrom = txt;
input2.setText(""+value);
canvas.repaint();
showDecodedChrom(txt);
}
else if (btn == buttons[8]) {
if (GaaApplet.gaaAppMode.equals("Applet"))
ret = MsgDialog.msg("Not supported in Applet mode", "This option is available only in Application mode", MsgDialog.MB_OK, 0);
else
evalTestExpression();
}
else if (btn == buttons[9]) {
}
else if (btn == textButton) {
problem.withTextWindow = !problem.withTextWindow;
if (problem.withTextWindow)
textButton.setForeground(Color.blue);
else
textButton.setForeground(Color.black);
}
else if (btn == graphicButton) {
problem.withGraphicWindow = !problem.withGraphicWindow;
if (problem.withGraphicWindow)
graphicButton.setForeground(Color.blue);
else
graphicButton.setForeground(Color.black);
}
}
}
public void mousePressed(MouseEvent e) {
//GaaApplet.statusLabel.setText("Mouse Entered: "+e.toString());
if (e.isMetaDown()) {
//GaaApplet.statusLabel.setText("Right Button: xy= "+e.getX()+" / "+e.getY());
}
}
public void mouseReleased(MouseEvent e) {
//statusLabel.setText("");
}
public void actionPerformed( ActionEvent evt ) {
Object src = evt.getSource();
//GaaApplet.statusLabel.setText("Action evt= "+evt.toString()+" src= "+src.toString());
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -