1. 概述
2. 编码
3. 初始群体的产生
4. 适应度计算
5. 选择运算
6. 交叉运算
7. 变异运算
8. 测试效果
参考资料

# 二，编码

image.png

``````public static final int GENG_LENGTH = 14;
public static final int MAX_X = 127;
public static final int MAX_Y = 127;
private int x,y;
private String gene;
``````

# 三，初始群体的产生

public Chromosome(String gene) //给定基因串构造染色体
public Chromosome(int x,int y) //给定表现型构造染色体

``````    public static ArrayList<Chromosome> initGroup(int size) {
ArrayList<Chromosome> list = new ArrayList<Chromosome>();
Random random = new Random();
for(int i = 0; i < size; i++) {
int x = random.nextInt() % 128;
int y = random.nextInt() % 128;
x = x < 0? (-x):x;
y = y < 0? (-y):y;
}
return list;
}
``````

# 四，适应度计算

``````public int calcFitness() {
return x*x+y*y;
}
``````

# 五，选择运算

``````public static ArrayList<Chromosome> selector(ArrayList<Chromosome> fatherGroup,int sonGroupSize)
{
ArrayList<Chromosome> sonGroup = new ArrayList<Chromosome>();
int totalFitness = 0;
double[] fitness = new double[fatherGroup.size()];
for(Chromosome chrom : fatherGroup) {
totalFitness += chrom.calcFitness();
}
int index = 0;
//计算适应度
for(Chromosome chrom : fatherGroup) {
fitness[index] = chrom.calcFitness() / ((double)totalFitness);
index++;
}
//计算累加适应度
for(int i = 1; i < fitness.length; i++) {
fitness[i] = fitness[i-1]+fitness[i];
}
//轮盘赌选择
for(int i = 0; i < sonGroupSize; i++) {
Random random = new Random();
double probability = random.nextDouble();
int choose;
for(choose = 1; choose < fitness.length - 1; choose++) {
if(probability < fitness[choose])
break;
}
}
return sonGroup;
}
``````

# 六，交叉运算

``````public static ArrayList<Chromosome> corssover(ArrayList<Chromosome> fatherGroup,double probability) {
ArrayList<Chromosome> sonGroup = new ArrayList<Chromosome>();
Random random = new Random();
for(int k = 0; k < fatherGroup.size() / 2; k++) {
if(probability > random.nextDouble()) {
int i = 0,j = 0;
do {
i = random.nextInt(fatherGroup.size());
j = random.nextInt(fatherGroup.size());
} while(i == j);
int position = random.nextInt(Chromosome.GENG_LENGTH);
String parent1 = fatherGroup.get(i).getGene();
String parent2 = fatherGroup.get(j).getGene();
String son1 = parent1.substring(0, position) +                                          parent2.substring(position);
String son2 = parent2.substring(0, position) +                                          parent1.substring(position);
}
}
return sonGroup;
}
``````

# 七，变异运算

``````public void selfMutation(String newGene) {
if(newGene.length() != Chromosome.GENG_LENGTH)
return;
this.gene = newGene;
String xStr = newGene.substring(0, Chromosome.GENG_LENGTH/2);
String yStr = newGene.substring(Chromosome.GENG_LENGTH/2);
this.x = Integer.parseInt(xStr,2);
this.y = Integer.parseInt(yStr,2);
}

public static void mutation(ArrayList<Chromosome> fatherGroup,double probability) {
Random random = new Random();
Chromosome bestOne = Chromosome.best(fatherGroup);
for(Chromosome c : fatherGroup) {
String newGene = c.getGene();
for(int i = 0; i < newGene.length();i++){
if(probability > random.nextDouble()) {
String newChar = newGene.charAt(i) == '0'?"1":"0";
newGene = newGene.substring(0, i) + newChar +                                   newGene.substring(i+1);
}
}
c.selfMutation(newGene);
}
}
``````

# 八，测试效果

``````final int GROUP_SIZE = 20;//种群规模
final double MUTATION_P = 0.01;//变异概率
ArrayList<Chromosome> group =                                               Chromosome.initGroup(GROUP_SIZE);
Chromosome theBest;
do{
Chromosome.mutation(group, MUTATION_P);
group = Chromosome.selector(group, GROUP_SIZE);
theBest = Chromosome.best(group);
System.out.println(theBest.calcFitness());
}while(theBest.calcFitness() < 32258);
``````

image.png
image.png
image.png

# 参考资料：

[1]遗传算法的例子 http://blog.csdn.net/b2b160/article/details/4680853/
[2]老师的课件 cn01-IntroGA-v1.00.ppt