⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 first.java

📁 标准的遗传算法
💻 JAVA
字号:
import java.awt.BorderLayout; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 
import javax.swing.JScrollPane; 
import javax.swing.JTextArea; 
import javax.swing.JTextField; 
class Best { 
	public int generations; //最佳适应值代号 
	public String str; //最佳染色体 
	public double fitness; //最佳适应值 
	} 
public class first extends JFrame{
	private JTextArea textArea; 
	private String str = ""; 
	private Best best = null; //最佳染色体 
	private String[] ipop = new String[10]; //染色体 
	private int gernation = 0; //染色体代号 
	public static final int GENE = 22; //基因数 

	public static void main(String[] args) {
		try { 
			first frame = new first(); 
			frame.setVisible(true); 
			} catch (Exception e) { 
			e.printStackTrace(); 
			}

	}
	public first() { 
		super(); 

		this.ipop = inialPops(); 

		getContentPane().setLayout(null); 
		setBounds(100, 100, 461, 277); 
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

		final JLabel label = new JLabel(); 
		label.setText("X的区间:"); 
		label.setBounds(23, 10, 88, 15); 
		getContentPane().add(label); 

		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(new ActionListener() { 
		public void actionPerformed(final ActionEvent e) { 
		first s = new first(); 
		str = str + s.process() + "\n"; 
		textArea.setText(str); 
		} 
		}); 
		button.setText("求最小值"); 
		button.setBounds(323, 27, 99, 23); 
		getContentPane().add(button); 

		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); 

		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 < GENE; 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 < 10; 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 < 10; 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 < 10; 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 < 10; i++) { 

		double r = Math.random(); 
		if (r <= q[0]) { 
		ipop[i] = ipop[0]; 

		} else { 
		for (int j = 1; j < 10; j++) { 
		if (r < q[j]) { 
		ipop[i] = ipop[j]; 
		break; 
		} 
		} 
		} 
		} 
		} 

		/** 
		* 交叉操作 
		* 交叉率为25%,平均为25%的染色体进行交叉 
		*/ 
		private void cross() { 
		String temp1, temp2; 
		for (int i = 0; i < 10; i++) { 
		if (Math.random() < 0.25) { 
		double r = Math.random(); 
		int pos = (int) (Math.round(r * 1000)) % GENE; 
		if (pos == 0) { 
		pos = 1; 
		} 
		temp1 = ipop[i].substring(0, pos) 
		+ ipop[(i + 1) % 10].substring(pos); 
		temp2 = ipop[(i + 1) % 10].substring(0, pos) 
		+ ipop[i].substring(pos); 
		ipop[i] = temp1; 
		ipop[(i + 1) / 10] = temp2; 
		} 
		} 
		} 

		/** 
		* 基因突变操作 
		* 1%基因变异m*pop_size 共180个基因,为了使每个基因都有相同机会发生变异, 
		* 需要产生[1--180]上均匀分布的 
		*/ 
		private void mutation() { 
		for (int i = 0; i < 4; i++) { 
		int num = (int) (Math.random() * GENE * 10 + 1); 
		int chromosomeNum = (int) (num / GENE) + 1; // 染色体号 

		int mutationNum = num - (chromosomeNum - 1) * GENE; // 基因号 
		if (mutationNum == 0) 
		mutationNum = 1; 
		chromosomeNum = chromosomeNum - 1; 
		if (chromosomeNum >= 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 < 10000; i++) { 
		this.select(); 
		this.cross(); 
		this.mutation(); 
		gernation = i; 
		} 
		str = "最小值" + best.fitness + ",第" + best.generations + "个染色体"; 
		return str; 
		} 
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -