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

📄 ga.java

📁 本程序利用java语言实现了遗传算法。并对算法进行了模拟。
💻 JAVA
字号:
import java.math.*;
import java.lang.*;
//import java.lang.Math;
import java.util.Random;
/*** <p>Title:遗传算法模拟程序 </p>
* <p>Description:Y = x1^2 + x2^2; </p>
* <p>Copyright: Copyright (c) 2005</p>
* <p>Company: research</p>
* @author zxg
* @version 1.0
*/
public class GA 
{
	private int x1,x2;//基因
	private static int T = 20;//最大进化代数
	private static int m = 4;//群体个数
	private int X1[] = {1,2,3,4,5,6,7};//变量空间
	private int X2[] = {1,2,3,4,5,6,7};
	private int n = 3;//二进制位数
	private int k,j,l;//循环变量
	private int args;
	private int numb;
	private long num;
	private int optimumsolution[] = new int[2];//最优解集合
	private int population[][] = new int[4][2];//一个群体集合
	private double probability1[] = new double[2];//第一个个体的概率空间
	private double probability2[] = new double[2];//第二个个体的概率空间
	private double probability3[] = new double[2];//第三个个体的概率空间
	private double probability4[] = new double[2];//第四个个体的概率空间
	private int twocrossarray[][] = new int[2][2];
	//Array
	private int decimalcode[] = new int[2];
	private double maxvalue = 1;
	//private char code1[] = new char[3];
	
	public double huntingProcess()
	{
		double randProbability[];
		randProbability = new double[4];
		for (k = 0;k<T+1;k++)
		{
			for (j = 0;j<4;j++)
			{
				//先生成四个随机整数
				for(l = 0;l<2;l++)
				{
					Random r = new Random();
					population[j][l] = (int)Math.floor(r.nextFloat()*7 + 1);	
				}
				Random r = new Random();
				randProbability[j] = r.nextFloat();//生成一个0到1的随机数
			}
			valueProbability(population[0],population[1],population[2],population[3]);
			//进行概率空间分配
			for(j = 0;j<4;j++)
			{
			if(randProbability[j]>=0&randProbability[j]<probability1[1])
			{
				population[j] = population[0];
			}
			else if(randProbability[j]>=probability1[1]&randProbability[j]<probability1[2])
			{
				population[j] = population[1];
			}
			else if(randProbability[j]>=probability1[2]&randProbability[j]<probability1[3])
			{
				population[j] = population[2];
			}
			else if(randProbability[j]>=probability1[3]&randProbability[j]<=probability1[4])
			{
				population[j] = population[3];
			}
			}//重新赋值结束
			twocrossarray = valueCross(population[0][0],population[0][1],population[1][0],population[1][1]);
			population[0] = twocrossarray[0];
			population[1] = twocrossarray[1];
			twocrossarray = valueCross(population[2][0],population[2][1],population[3][0],population[3][1]);
			population[2] = twocrossarray[0];
			population[3] = twocrossarray[1];
			//进行杂交
			
			//进行变异
			
		}
		return maxvalue;
	}
	
	public double valueProbability(int x1[],int x2[],int x3[],int x4[])
	{
	//对于每一个个体,通过个体值,给概率空间赋值
		double sum1,sum2,sum3,sum4,sum;
		sum1 = x1[1]^2 + x1[0]^2;	
		sum2 = x2[1]^2 + x2[0]^2;	
		sum3 = x3[1]^2 + x3[0]^2;	
		sum4 = x4[1]^2 + x4[0]^2;
		maxvalue = Math.max(Math.max(sum1,sum2),Math.max(sum3,sum4));
		switch (maxvalue)
		{
			case sum1:
				return sum1;
				break;
			case sum2:
				return sum2;
				break;
			case sum3:
				return sum3;
				break;
			case sum4:
				return sum4;
				break;
		}
		sum = sum1 + sum2 + sum3 + sum4;
		sum1 = sum1/sum;
		sum2 = sum2/sum;
		sum3 = sum3/sum;
		sum4 = sum4/sum;
		
		
		probability1[0] = 0;
		probability1[1] = sum1;
		probability2[0] = sum1;
		probability2[1] = sum1 + sum2;
		probability3[0] = sum1 + sum2;
		probability3[1] = sum1 + sum2 + sum3;
		probability4[0] = sum1 + sum2 + sum3;
		probability4[1] = sum;
	}
	public int[] decimalTransfer(String code)//将二进制数解码成两个十进制整数
	{
		try{
		char code1[],code11[];
		code1 = new char[3];
		code11 = new char[3];
		code1[0] = code.charAt(3);
		code1[1] = code.charAt(4);
		code1[2] = code.charAt(5);
		code11[0] = code.charAt(0);
		code11[1] = code.charAt(1);
		code11[2] = code.charAt(2);
		//System.out.println(code1[0]);
		//code2 = code1;
		StringBuffer code2 = new StringBuffer();
		StringBuffer code22 = new StringBuffer();
		for (k = 0;k<3;k++)
		{
			code2.append(code1[k]);
			code22.append(code11[k]);
		}
		decimalcode[0] = Integer.parseInt(code22.toString(),2);
		decimalcode[1] = Integer.parseInt(code2.toString(),2);
		//decimalcode[0] = decimalcode[0] - decimalcode[1];
		}catch(NumberFormatException e){}
		/*byte codeT ;
		code = "00" + code;
		
		//code2 = code1.toString();
		code2[0] = Integer.parseInt(code1[0].toString());
		code2[1] = Integer.parseInt(code1[1].toString());
		code2[2] = Integer.parseInt(code1[2].toString());
		
		//codeT = Byte.parseByte(code2);*/
		//decimalcode[0] = code2[0]*(int)Math.pow(2,2)+code2[1]*(int)Math.pow(1,2)+code2[2];	
		return decimalcode;
	} 
	public String binaryTransfer(int high,int low)//二进制转换,返回二进制编码
	{
		String binarynum;
		String high1,low1;
		high1 = Long.toBinaryString(high);
		low1 = Long.toBinaryString(low);
		if (high1.length()==2)
		{
			high1 = "0" + high1;	
		}
		if (high1.length()==1)
		{
			high1 = "00" + high1;	
		}
		if (low1.length()==2)
		{
			low1 = "0" + low1;	
		}
		if (low1.length()==1)
		{
			low1 = "00" + low1;	
		}
		binarynum = high1 + low1;
		return binarynum;
		
	}
	
	/*public int loopArgs()//计算编码成二进制的位数
	{
		args = 0;
		for (k = 0;k<n+1;k++)
		{
			args = (int)args + (int)Math.pow(k,2);	
		}
		return args;	
	}*/
	public int[][] valueCross(int max1,int min1,int max2,int min2)//杂交生成六位字符串的方法
	{
		int crossdot;
		int returnvalue[][];
		returnvalue = new int[2][2];
		String value1,value2;
		char valuechar1[],valuechar2[];
		valuechar1 = new char[6];
		valuechar2 = new char[6];
		Random r = new Random();
		crossdot = (int)Math.round(r.nextFloat()*5);
		value1 = binaryTransfer(max1,min1);
		value2 = binaryTransfer(max2,min2);
		for (k = 0;k<crossdot;k++)
		{
			valuechar1[k] = value1.charAt(k);
			valuechar2[k] = value2.charAt(k);	
		}
		for (k = crossdot;k<6;k++)
		{
			valuechar1[k] = value2.charAt(k);
			valuechar2[k] = value1.charAt(k);
		}
		value1 = null;
		value2 = null;
		for (k = 0;k<6;k++)
		{
			value1 = value1 + valuechar1[k];
			value2 = value2 + valuechar2[k];
		}
		returnvalue[0] = decimalTransfer(value1);
		returnvalue[1] = decimalTransfer(value2);
		return returnvalue;
	}
	
	public 	int[] randGenerate(int max,int min)//生成随机变异数
	{
		int randvariation[] = new int[m];
		for (k = 0;k <m+1;k++ )
		{
		Random r = new Random();
		randvariation[k] = (int)Math.floor(r.nextFloat()*(max - min + 1) + min);
		
		}
		return randvariation;
	}
	public  void main(String args[])
	{
		GA rr = new GA();
		double a;
		a = rr.huntingProcess();
		System.out.println(a);
	}
}

⌨️ 快捷键说明

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