📄 reproduce.java
字号:
import java.awt.*;
import java.awt.event.*;
import java.math.*;
public class reproduce extends Frame implements ActionListener
{
TextField diedai,guimo,cishu;
Label t1,t2,t3;
Button start,clear;
int width=800,height=600;
int centerX=width/10,centerY=height*4/6;
double a=0,b=10;
int drawPen=0;
int n_reproduce = 8;
int N = 200;
int code_length = 40;
int x[][] = new int[N][code_length];
double x_Value[] = new double[N];
double fitness[] = new double[N];
int x2[][] = new int[N][code_length];
double x2_Value[] = new double[N];
public void sortFitness()
{
int i,j;
double temp;
int tempIndex;
int index[] = new int[N];
for(i=0; i<N; i++)
{
index[i] = i;
}
for(i=N-1; i>=0; i--)
{
for(j=0; j<i; j++)
if(fitness[j] < fitness[j+1])
{
temp = fitness[j];
fitness[j] = fitness[j+1];
fitness[j+1] = temp;
tempIndex = index[j];
index[j] = index[j+1];
index[j+1] = tempIndex;
}
}
int tempx[][] = new int[N][code_length];
double tempx_Value[] = new double[N];
for(i=0; i<N; i++)
{
for(j=0; j<code_length; j++)
tempx[i][j] = x[i][j];
tempx_Value[i] = x_Value[i];
}
for(i=0; i<N; i++)
{
for(j=0; j<code_length; j++)
x[i][j] = tempx[index[i]][j];
x_Value[i] = tempx_Value[i];
}
}
public void calculateX_Value()
{
int i,j;
double num;
for(i=0; i<N; i++)
{
x_Value[i] = 0;
num = 0.5;
for(j=0; j<code_length; j++)
{
x_Value[i] += x[i][j]*num;
num /= 2.0;
}
x_Value[i] *= b;
}
}
public void calculateFitness()
{
int i;
for(i=0; i<N; i++)
{
fitness[i] = Math.sin(x_Value[i]);
}
}
public void init()
{
int i,j;
for(i=0; i<N; i++)
{
for(j=0; j<code_length; j++)
{
x[i][j] = (int)(Math.random()+0.5);
}
}
calculateX_Value();
calculateFitness();
}
public void choose()
{
int i,j;
int chooseIndex;
for(i=0; i<N; i++)
{
chooseIndex = (int)( (Math.random())*(Math.random())*N );
for(j=0; j<code_length; j++)
x2[i][j] = x[chooseIndex][j];
x2_Value[i] = x_Value[chooseIndex];
}
}
public void interlace()
{
int i,j;
int mother;
int father;
int pos;
for(i=0; i<N; i+=2)
{
mother = (int)( Math.random()*N );
father = (int)( Math.random()*N );
pos = (int)( Math.random()*code_length );
for(j=0; j<pos; j++)
{
x[i][j] = x2[father][j];
x[i+1][j] = x2[mother][j];
}
for(j=pos; j<code_length; j++)
{
x[i][j] = x2[mother][j];
x[i+1][j] = x2[father][j];
}
}
}
public void mutation()
{
int i = (int)( Math.random()*N );
int pos = (int)( Math.random()*code_length );
x[i][pos] = (x[i][pos]==1) ? 0:1;
}
public double f(double x)
{
return Math.sin(x);
}
public reproduce()
{
super("遗传算法");
setSize(width,height);
setLocation(100,0);
setResizable(false);
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
Panel p1=new Panel();
p1.setBackground(Color.pink);
start=new Button("开始");
clear=new Button("清空");
p1.add(start);
p1.add(clear);
add(p1,"North");
start.addActionListener(this);
clear.addActionListener(this);
Panel p2=new Panel();
p2.setBackground(Color.pink);
t1=new Label("迭代次数");
t2=new Label("规模");
t3=new Label("码长");
diedai=new TextField("5",2);
guimo=new TextField("200",3);
cishu=new TextField("50",3);
p2.add(t1);
p2.add(diedai);
p2.add(t2);
p2.add(guimo);
p2.add(t3);
p2.add(cishu);
add(p2,"South");
}
public void paint(Graphics g)
{
int scale=60;
double step=0.001;
if(drawPen==-1)
{
g.clearRect(0,0,width,height);
drawPen=0;
}
{
double x,y;
g.setColor(Color.black);
g.drawLine(width/10,centerY,width*9/10,centerY);
g.drawLine(width*9/10-10,centerY-5,width*9/10,centerY);
g.drawLine(width*9/10-10,centerY+5,width*9/10,centerY);
g.drawString("x",width*9/10-10,centerY+15);
g.drawLine(centerX,height/10,centerX,height*8/10+20);
g.drawLine(centerX,height/10,centerX-5,height/10+20);
g.drawLine(centerX,height/10,centerX+5,height/10+20);
g.drawString("y",100,height/10+30);
g.drawString("0",width/10-10,centerY+15);
g.drawString("10",centerX+10*scale-5,centerY+15);
g.drawString("f = sin(x)",centerX+100,centerY-80);
for(x=a; x<=b; x+=step)
{
y=Math.sin(x);
g.fillOval((int)(centerX+x*scale),(int)(centerY-y*scale),1,1);
}
}
if(drawPen==1)
{
int i,j;
int time = 1;
double x,y;
init();
while(time < n_reproduce)
{
g.setColor(Color.pink);
System.out.println(time+" : "+x_Value[0]+" "+fitness[0]);
for(i=0; i<N; i++)
{
g.fillOval((int)(centerX+x_Value[i]*scale),(int)(centerY-f(x_Value[i])*scale),5,5);
}
try
{
(new Thread()).sleep(1000);
}
catch(Exception e)
{
}
g.setColor(Color.white);
for(i=0; i<N; i++)
{
g.fillOval((int)(centerX+x_Value[i]*scale),(int)(centerY-f(x_Value[i])*scale),5,5);
}
try
{
(new Thread()).sleep(1000);
}
catch(Exception e)
{
}
sortFitness();
choose();
interlace();
mutation();
calculateX_Value();
calculateFitness();
g.setColor(Color.black);
for(x=a; x<=b; x+=step)
{
y=Math.sin(x);
g.fillOval((int)(centerX+x*scale),(int)(centerY-y*scale),1,1);
}
time++;
}
g.setColor(Color.pink);
for(i=0; i<N; i++)
{
g.fillOval((int)(centerX+x_Value[i]*scale),(int)(centerY-f(x_Value[i])*scale),5,5);
}
drawPen = 0;
}
}
public void update(Graphics g)
{
paint(g);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==start)
{
drawPen=1;
repaint();
}
if(e.getSource()==clear)
{
drawPen=-1;
repaint();
}
}
public static void main(String arg[])
{
new reproduce().setVisible(true);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -