?? ga.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 + -