import java.security.SecureRandom;
import java.util.Random;
public class GetMaxValue {
//定义样本数
static int N = 10;
//定义DNA位数
static int M = 5;
//定义样本DNA数组
static int[][] DNA = new int[N][M];
//定义样本表现值
static int[] show = new int[N];
//定义样本存活累计概率0-1
static double[] alive = new double[N];
//定义淘汰阈值
static int sigma = 3;
//定义繁衍代数
static int T = 500;
//定义交换基因次数
static int C = 1;
//将样本10进制求2进制 j表示位数
static void tentotwo()
{
for (int i = 0; i < N;i++ )
{
int j = 0;
int tempshow = show[i];
while (tempshow != 0)
{
DNA[i][j] = tempshow% 2;
tempshow /= 2;
j++;
}
}
}
//将样本2进制转10进制
static void twototen()
{
for (int i = 0; i < N; i++)
{
int sum = 0;
for(int j=0;j<M;j++)
{
sum += DNA[i][j]*(int)Math.pow(2, j);
}
show[i] = sum;
}
}
//适应度函数
static int f(int x)
{
int result = -x * x + 31*x;
return (result);
}
//求累计存活概率函数
static void icanlive()
{
int sum = 0;
double alivesum = 0;
for (int i = 0; i < N; i++)
{
sum += f(show[i]);
}
for (int i = 0; i < N; i++)
{
alivesum += (double)f(show[i]) / (double)sum;
alive[i] = alivesum;
}
}
//淘汰函数
static void dieout()
{
int[] times = new int[N];
for (int i = 0; i < times.length;i++ )
{
times[i] = 0;
}
Random ro = new Random();
//遗传过程
for (int i = 0; i < 2*N; i++)
{
double myro = (double)ro.nextInt(100)/ 100;
for (int j = 0; j < N; j++)
{
if (myro<alive[j])
{
if (j != 0 && myro > alive[j - 1])
{
times[j] += 1;
break;
}
else if(j==0)
{
times[j] += 1;
break;
}
}
}
}
//找出本次遗传中最强势种族
int max = times[0];
int sign = 0;
for (int y = 0; y < N; y++)
{
if (times[y] > max)
{
max = times[y];
sign = y;
}
}
for (int y = 0; y < N; y++)
{
//小于阈值的样本被淘汰,改样本被适应度最强的样本取代
if (times[y] < sigma)
{
show[y] = show[sign];
}
}
}
static void exchangeDNA()
{
Random ro = new Random();
int a;
int b;
int x1;
int x2;
int y1;
int y2;
int iUp1 = M, iUp2 = N;
int iDown = 0;
int temp;
for (int i = 0; i < C; i++)
{
x1 = ro.nextInt(iUp1);
x2 = ro.nextInt(iUp1);
y1 = ro.nextInt(iUp1);
y2 = ro.nextInt(iUp1);
a = ro.nextInt(iUp2);
b = ro.nextInt(iUp2);
temp = DNA[a][x1];
DNA[a][ x1] = DNA[b][ y1];
DNA[b][ y1] = temp;
temp = DNA[a][ x2];
DNA[a][ x2] = DNA[b][ y2];
DNA[b][ y2] = temp;
}
}
static int Max (int[]shuzu)
{
int temp=0;
for(int i=0;i<shuzu.length;i++)
{
if(shuzu[i]>temp)
{
temp=shuzu[i];
}
}
return temp;
}
static int Min (int[]shuzu)
{
int temp=100000000;
for(int i=0;i<shuzu.length;i++)
{
if(shuzu[i]<temp)
{
temp=shuzu[i];
}
}
return temp;
}
public static void main(String[] args)
{
//初始化样本值
show[0] = 1;
show[1] = 2;
show[2] = 23;
show[3] = 12;
show[4] = 4;
show[5] = 7;
show[6] = 9;
show[7] = 20;
show[8] = 29;
show[9] = 10;
tentotwo();
System.out.println("\n父代样本\n");
System.out.println("样本表现值:");
for (int j = 0; j < N; j++)
{
System.out.println(String.valueOf(show[j]) + " ");
}
System.out.println("\n样本适应度:");
for (int j = 0; j < N; j++)
{
System.out.println(String.valueOf(f(show[j])) + " ");
}
for (int i = 0; i < T;i++ )
{
//交换DNA
exchangeDNA();
//DNA触发变现值
twototen();
//表现值求出存活概率
icanlive();
//根据存活概率淘汰低者
dieout();
//表现值触发DNA
tentotwo();
double max = Max(show);
double min = Min(show);
System.out.println("\n\n第" + (i + 1) + "代样本\n");
System.out.println("样本表现值:");
for (int j = 0; j < N; j++)
{
System.out.println(String.valueOf(show[j])+"");
}
System.out.println("\n样本适应度:");
for (int j = 0; j < N; j++)
{
System.out.println(String.valueOf(f(show[j]))+"");
}
//if (max == min)
//{
// break;
//}
}
System.out.println("\n\n程序运行完毕\n");
}
}