遗传算法的应用实例(进化算法和遗传算法)
导入Java。啊。Bordlayout
导入Java。啊。事件。行动事件;
导入Java。啊。事件。actionlistener
导入javax。摇摆。JButton
导入javax。摇摆。jframe
导入javax。摇摆。jlabel
导入javax。摇摆。jpanel
导入javax。摇摆。jscrollpane
导入javax。摇摆。jtextarea
导入javax。摇摆。jtextfield
最佳等级{
公共代际;//最佳适应值代号
公共字符串;//最佳染色体
大众双重健身;//最佳适应值
}
公共类SGAFrame扩展了JFrame {
私有JTextArea文本区域
私有字符串字符串=' ';
私人最佳=空;//最佳染色体
私有字符串[] ipop=新字符串[10];//染色体
private int gernation=0;//染色体代号
public static final int GENE=22//基因数
/**
*启动应用程序
* @param参数
*/
公共静态void main(字符串参数[]){ 0
尝试{
SGAFrame frame=new SGAFrame();
框架。set visible(true);
}捕获(例外e){ 0
e。print stack trace();
}
}
/**
*创建框架
*/
public SGAFrame(){ 0
super();
这个。ipop=Inialpops();
getContentPane().setLayout(null);
setBounds(100,100,461,277);
setDefaultCloseOperation(JFrame .EXIT _ ON _ CLOSE);
最终组件标签=new JLabel();
label.setText('X的区间:');
label.setBounds(23,10,88,15);
getContentPane().添加(标签);
final JLabel label _ 1=new JLabel();
label_1.setText('[-255,255]');
label_1.setBounds(92,10,84,15);
getContentPane().add(label _ 1);
final JButton button=new JButton();
button.addActionListener(新的ActionListener(){ 0
公共无效操作已执行(最终操作事件e){ 0
SGAFrame s=new SGAFrame();
str=str . s进程()' \ n ';
textArea.setText(字符串);
}
});
button.setText('求最小值');
button.setBounds(323,27,99,23);
getContentPane().添加(按钮);
final JLabel label _ 2=new JLabel();
label_2.setText('利用标准遗传算法求解函数f(x)=(x-5)*(x-5)的最小值:');
label_2.setBounds(23,31,318,15);
getContentPane().add(label _ 2);
p>final JPanel panel = new JPanel();
panel.setLayout(new BorderLayout());
panel.setBounds(23, 65, 399, 164);
getContentPane().add(panel);
final JScrollPane scrollPane = new JScrollPane();
panel.add(scrollPane, BorderLayout.CENTER);
textArea = new JTextArea();
scrollPane.setViewportView(textArea);
//
}
/**
* 初始化一条染色体(用二进制字符串表示)
* @return 一条染色体
*/
private String inialPop() {
String res = "";
for (int i = 0; i
if (Math.random() > 0.5) {
res += "0";
} else {
res += "1";
}
}
return res;
}
/**
* 初始化一组染色体
* @return 染色体组
*/
private String[] inialPops() {
String[] ipop = new String[10];
for (int i = 0; i
ipop[i] = inialPop();
}
return ipop;
}
/**
* 将染色体转换成x的值
* @param str 染色体
* @return 染色体的适应值
*/
private double calculatefitnessvalue(String str) {
int b = Integer.parseInt(str, 2);
//String str1 = "" + "/n";
double x = -255 + b * (255 - (-255)) / (Math.pow(2, GENE) - 1);
//System.out.println("X = " + x);
double fitness = -(x - 5) * (x - 5);
//System.out.println("f(x)=" + fitness);
//str1 = str1 + "X=" + x + "/n"
//+ "f(x)=" + "fitness" + "/n";
//textArea.setText(str1);
return fitness;
}
/**
* 计算群体上每个个体的适应度值;
* 按由个体适应度值所决定的某个规则选择将进入下一代的个体;
*/
private void select() {
double evals[] = new double[10]; // 所有染色体适应值
double p[] = new double[10]; // 各染色体选择概率
double q[] = new double[10]; // 累计概率
double F = 0; // 累计适应值总和
for (int i = 0; i
evals[i] = calculatefitnessvalue(ipop[i]);
if (best == null) {
best = new Best();
best.fitness = evals[i];
best.generations = 0;
best.str = ipop[i];
} else {
if (evals[i] > best.fitness) // 最好的记录下来
{
best.fitness = evals[i];
best.generations = gernation;
best.str = ipop[i];
}
}
F = F + evals[i]; // 所有染色体适应值总和
}
for (int i = 0; i
p[i] = evals[i] / F;
if (i == 0)
q[i] = p[i];
else {
q[i] = q[i - 1] + p[i];
}
}
for (int i = 0; i
double r = Math.random();
if (r = 10)
chromosomeNum = 9;
//System.out.println("变异前" + ipop[chromosomeNum]);
String temp;
if (ipop[chromosomeNum].charAt(mutationNum - 1) == '0') {
if (mutationNum == 1) {
temp = "1" + ipop[chromosomeNum].substring
(mutationNum);
} else {
if (mutationNum != GENE) {
temp = ipop[chromosomeNum].substring(0, mutationNum -
1) + "1" + ipop
[chromosomeNum].substring(mutationNum);
} else {
temp = ipop[chromosomeNum].substring(0, mutationNum -
1) + "1";
}
}
} else {
if (mutationNum == 1) {
temp = "0" + ipop[chromosomeNum].substring
(mutationNum);
} else {
if (mutationNum != GENE) {
temp = ipop[chromosomeNum].substring(0, mutationNum -
1) + "0" + ipop
[chromosomeNum].substring(mutationNum);
} else {
temp = ipop[chromosomeNum].substring(0, mutationNum -
1) + "1";
}
}
}
ipop[chromosomeNum] = temp;
//System.out.println("变异后" + ipop[chromosomeNum]);
}
}
/**
* 执行遗传算法
*/
public String process() {
String str = "";
for (int i = 0; i
this.select();
this.cross();
this.mutation();
gernation = i;
}
str = "最小值" + best.fitness + ",第" + best.generations + "个染色体";
return str;
}
}