📄 shortcut.java
字号:
tempNum=1;
int endIndex = population[popindex].chrom[point2+1];
AdjArray adjPoint = new AdjArray();
while (pIndex != endIndex) {
adjPoint = getAdjacencyPointIndexInGaTwo(pIndex);
if(adjPoint.hasEndPoint)
{
pIndex = this.endPointIndex;
temp[tempNum] = pIndex;
tabooTable[endPointIndex] = false;
}
else
{
int n = adjPoint.AllNum;
while (n == 0) { //如果没有可选的邻接结点,则删除该点,回退到上个结点
tabooTable[pIndex] = false;
tempNum--;
pIndex = temp[tempNum - 1];
adjPoint = getAdjacencyPointIndexInGaTwo(pIndex);
n = adjPoint.AllNum;
}
if(adjPoint.hasEndPoint)
{
pIndex = this.endPointIndex;
temp[tempNum] = pIndex;
tabooTable[endPointIndex] = false;
}
else
{
n=adjPoint.FirstNum;
if(n>0)
{
int ran = random(0, n - 1);
pIndex = adjPoint.FirstCollection[ran];
temp[tempNum] = pIndex;
tabooTable[pIndex] = false;
}
else //n=0
{
n=adjPoint.SecondNum;
if (n > 0)
{
int ran = random(0, n - 1);
pIndex = adjPoint.SecondCollection[ran];
temp[tempNum] = pIndex;
tabooTable[pIndex] = false;
}
else
{ //n=0
n=adjPoint.ThirdNum;
int ran = random(0, n - 1);
pIndex = adjPoint.ThirdCollection[ran];
temp[tempNum] = pIndex;
tabooTable[pIndex] = false;
}
}
}
}
tempWeight[tempNum] = tempWeight[tempNum-1] +getIndWeight(temp[tempNum - 1], pIndex);
tempNum++;
if (tempWeight[tempNum-1]+0.0001 >= popWeight) {
tag=true;
break;
}
} //完成一个染色体的变异操作
if (tag == false){
int end=nchrom-1-point2;
population[popindex].geneNum=point1+(tempNum-2)+end;
int curChrom[]=new int[end];
double curWeight[]=new double[end];
int start=point2+1;
for (int j = 0; j < end; j++)
{
curChrom[j]=population[popindex].chrom[start+j];
curWeight[j]=population[popindex].geneWeight[start+j]-population[popindex].geneWeight[start];
}
start=point1-1;
for (int j = 1; j < tempNum; j++) {
population[popindex].chrom[start+j]=temp[j];
population[popindex].geneWeight[start+j]=population[popindex].geneWeight[start]+tempWeight[j];
}
start=point1+tempNum-2;
end=population[popindex].geneNum;
for (int j = point1+tempNum-1,k=1; j < end; j++,k++) {
population[popindex].chrom[j]=curChrom[k];
population[popindex].geneWeight[j]=population[popindex].geneWeight[start]+curWeight[k];
}
}
}
//System.out.println("矫正操作成功执行一次!"+"\n");
}
}
}
}
/**
* 遗传算法第二步:交叉操作
*/
private void Crossover()
{
int point; //随机产生一个点位
int temp; //运算过程中的临时变量
double p; //存储随机产生一个小数概率
int index[] = new int[POP_SIZE];
CommonPointArray commonP = new CommonPointArray();
//产生一对随机个体
for (int i = 0; i < POP_SIZE; i++) {
index[i] = sortIndex[i];
}
for (int i = 0; i < POP_SIZE; i++) {
point = random(0, POP_SIZE - i - 1);
temp = index[i];
index[i] = index[point + i];
index[point + i] = temp;
}
int num = POP_SIZE - 1;
//单点交叉操作
for (int i = 0; i < num; i += 2) {
p = Math.random();
double pointWeight;
double pointWeight2;
if (p < Pc){
if (!(population[index[i]].chrom.equals(population[index[i +1]].chrom))){
//得到公共点
commonP = getCommonPointInd(population[index[i]],population[index[i + 1]]);
int curpNum = commonP.ArrayNum;
if (curpNum > 0) {
point = random(0,curpNum - 1);
int point1, point2;
point1 = commonP.commonPointIndex1[point]; //得到第一个染色体的交叉点
point2 = commonP.commonPointIndex2[point]; //得到第二个染色体的交叉点
int index1,index2;
index1=index[i];
index2=index[i+1];
pointWeight = population[index1].geneWeight[point1];
pointWeight2 = population[index2].geneWeight[point2];
if(pointWeight!=pointWeight2)
//
{
int a, b; //保存一对交叉染色体的长度
a = population[index1].geneNum;
b = population[index2].geneNum;
double curPointWeight = population[index1].geneWeight[a - 1] - pointWeight;
double curPointWeight2 = population[index2].geneWeight[b - 1] - pointWeight2;
if (curPointWeight > curPointWeight2)
{
if(population[index1].geneWeight[a - 1]<population[index2].geneWeight[b - 1])
{
if(point2>point1)
{
for(int j=0;j<=point1;j++)
{
population[index2].chrom[j] = population[index1].chrom[j]; //得到第一个交叉个体中交叉点之后的基因
population[index2].geneWeight[j] = population[index1].geneWeight[j];
}
for (int j = point2 + 1,k=1; j < b; j++,k++)
{
population[index2].chrom[point1+k] = population[index2].chrom[j]; //得到第二个交叉个体中交叉点之后的基因
population[index2].geneWeight[point1+k] = pointWeight+population[index2].geneWeight[j] - pointWeight2;
}
population[index2].geneNum = point1 + b - point2;
}
else if(point2==point1)
{
for(int j=0;j<=point1;j++)
{
population[index2].chrom[j] = population[index1].chrom[j]; //得到第二个交叉个体中交叉点之后的基因
population[index2].geneWeight[j] = population[index1].geneWeight[j];
}
for (int j = point2 + 1,k=1; j < b; j++,k++)
{
population[index2].geneWeight[point1+k] = pointWeight+population[index2].geneWeight[j] - pointWeight2;
}
population[index2].geneNum = point1 + b - point2;
}
else //point2<point1
{
int curNum=b-1-point2;
population[index2].geneNum = point1 + b - point2;
int temp2[]=new int[curNum];
double geneWeight2[]=new double[curNum];
int indstart=point2+1;
for(int j=point2+1;j<b;j++)
{
temp2[j-indstart]=population[index2].chrom[j]; //得到第一个交叉个体中交叉点之后的基因,保存到temp2中
geneWeight2[j-indstart]=population[index2].geneWeight[j];
}
for(int j=0;j<=point1;j++)
{
population[index2].chrom[j] = population[index1].chrom[j]; //得到第一个交叉个体中交叉点之后的基因
population[index2].geneWeight[j] = population[index1].geneWeight[j];
}
point1=point1+1;
for (int j = 0; j < curNum; j++)
{
population[index2].chrom[point1+j] = temp2[j]; //得到第二个交叉个体中交叉点之后的基因
population[index2].geneWeight[point1+j] = pointWeight+geneWeight2[j] - pointWeight2;
}
}
}
else
{
for (int j = point2 + 1,k=1; j < b; j++,k++)
{
population[index1].chrom[point1+k] = population[index2].chrom[j]; //得到第一个交叉个体中交叉点之后的基因
population[index1].geneWeight[point1+k] = pointWeight+population[index2].geneWeight[j] - pointWeight2;
}
population[index1].geneNum = point1 + b - point2;
}
}
else if (curPointWeight < curPointWeight2)//pointWeight!=pointWeight2
{
if(population[index2].geneWeight[b - 1]<population[index1].geneWeight[a - 1])
{
if(point1>point2)
{
for(int j=0;j<=point2;j++)
{
population[index1].chrom[j] = population[index2].chrom[j]; //得到第二个交叉个体中交叉点之后的基因
population[index1].geneWeight[j] = population[index2].geneWeight[j];
}
for (int j = point1 + 1,k=1; j < a; j++,k++)
{
population[index1].chrom[point2+k] = population[index1].chrom[j]; //得到第二个交叉个体中交叉点之后的基因
population[index1].geneWeight[point2+k] = pointWeight2+population[index1].geneWeight[j] - pointWeight;
}
population[index1].geneNum = point2 + a - point1;
}
else if(point1==point2)
{
for(int j=0;j<=point2;j++)
{
population[index1].chrom[j] = population[index2].chrom[j]; //得到第二个交叉个体中交叉点之后的基因
population[index1].geneWeight[j] = population[index2].geneWeight[j];
}
for (int j = point1 + 1,k=1; j < b; j++,k++)
{
population[index1].geneWeight[point2+k] = pointWeight2+population[index1].geneWeight[j] - pointWeight;
}
population[index1].geneNum = point2 + a - point1;
}
else //point1<point2
{
int curNum=a-1-point1;
population[index1].geneNum = point2 + a - point1;
int temp1[]=new int[curNum];
double geneWeight1[]=new double[curNum];
int indstart=point1+1;
for(int j=point1+1;j<a;j++)
{
temp1[j-indstart]=population[index1].chrom[j]; //得到第一个交叉个体中交叉点之后的基因,保存到temp2中
geneWeight1[j-indstart]=population[index1].geneWeight[j];
}
for(int j=0;j<=point2;j++)
{
population[index1].chrom[j] = population[index2].chrom[j]; //得到第一个交叉个体中交叉点之后的基因
population[index1].geneWeight[j] = population[index2].geneWeight[j];
}
point2=point2+1;
for (int j=0; j < curNum; j++)
{
population[index1].chrom[point2+j] = temp1[j]; //得到第二个交叉个体中交叉点之后的基因
population[index1].geneWeight[point2+j] = pointWeight2+geneWeight1[j] - pointWeight;
}
}
}
else
{
for (int j = point1 + 1,k=1; j < a; j++,k++)
{
population[index2].chrom[point2+k] = population[index1].chrom[j]; //得到第二个交叉个体中交叉点之后的基因
population[index2].geneWeight[point2+k] = pointWeight2+pop
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -