## CSDN博客

### 由SAT问题展开说（2）[演化计算c#实现下]

SAT问题展开说（2）[下]

private int GetScore(int i)

{

int score=0;

for(int j=0;j<maxNum;j++)

for(int k=0;k<valNum;k++)

if(int.Parse(satGenes[i].ToString().Substring(k,1))+bSentence[j,k]==2//

||int.Parse(satGenes[i].ToString().Substring(k,1))+bSentence[j,k]==-1)

{

score++;

break;

}

return score;

}

public int Sort()

{

int iBest=0,iWorst=0;

int bestScore=GetScore(0);//

int worstScore=GetScore(0);

for(int i=0;i<maxNum;i++)

{

score[i]=GetScore(i);

if(score[i]>bestScore)

{

iBest=i;

bestScore=score[i];

}

else if(score[i]<worstScore)

{

iWorst=i;

worstScore=score[i];

}

}

best=new StringBuilder(satGenes[iBest].ToString());

worst=new StringBuilder(satGenes[iWorst].ToString());

return bestScore;

}

1/3的情况进行某一位的取反变异，或以2/3的情况进行某连续两位的取反变异。

public void Mutation()

{

int mutType,start;

string subGene;

int i=randNum.Next(maxNum-1);

if(randNum.NextDouble()<=Pm)

{

mutType=randNum.Next(0,2);

switch(mutType)

{

case 0:

start=randNum.Next(0,valNum-1);

subGene=satGenes[i].ToString().Substring(start,1);

if(subGene=="0")

satGenes[i].Replace("0","1",start,1);

else

satGenes[i].Replace("1","0",start,1);

break;

case 1:case 2:

start=randNum.Next(0,valNum-2);

subGene=satGenes[i].ToString().Substring(start,2);

switch(subGene)

{

case "00":

satGenes[i].Replace("00","11",start,2);

break;

case "01":

satGenes[i].Replace("01","10",start,2);

break;

case "10":

satGenes[i].Replace("10","01",start,2);

break;

case "11":

satGenes[i].Replace("11","00",start,2);

break;

}

break;

default:

break;

}

}

}

public void Crossover()

{

int croType,start,length,i,j;

string i_subGene,j_subGene;

length=randNum.Next(5)+1;

start=randNum.Next(0,valNum-length);

i=randNum.Next(maxNum-1);

j=randNum.Next(maxNum-1);

i_subGene=satGenes[i].ToString().Substring(start,length);

j_subGene=satGenes[j].ToString().Substring(start,length);

croType=randNum.Next(2);

switch(croType)

{

case 0:case 2:

satGenes[i].Replace(i_subGene,j_subGene,start,length);

satGenes[j].Replace(j_subGene,i_subGene,start,length);

break;

case 1:

for(int k=0;k<length;k++)

{

switch(int.Parse(i_subGene.Substring(k,1))

+int.Parse(j_subGene.Substring(k,1)))

{

case 0:case 2:

satGenes[i].Replace(i_subGene.Substring(k,1),"1",start+k,1);

satGenes[j].Replace(j_subGene.Substring(k,1),"0",start+k,1);

break;

case 1:

satGenes[i].Replace(i_subGene.Substring(k,1),"0",start+k,1);

satGenes[j].Replace(j_subGene.Substring(k,1),"1",start+k,1);

break;

}

}

break;

default:

break;

}

}

private void geneCompute()

{

hh=DateTime.Now.Hour;

mm=DateTime.Now.Minute;

ss=DateTime.Now.Second;

ms=DateTime.Now.Millisecond;

SetTime();

oneSAT.InitGenes();

{

oneSAT.Mutation();

oneSAT.Crossover();

{

}

SetTime();

}

chListBoxString.ClearSelected();

}

oneSAT=new SAT(maxNum,valNum,0.8,0.02);

SetTime()是计算算法执行时间的跟踪，详细过程可见附录。而整个算法的执行，我们新创建一个线程computeGene，其过程如下：

{

computeGene.Start();

}

l          逻辑变量规模：200 300

l          种群规模：30

l          子句数量：30

l          杂交概率：0.8

l          变异概率：0.02

l          算法的出口为最好的染色体的分数等于非空的子句数量

00000000000000010000000000000000000011100000000000000000000000000000000000000000

00000000000000000000000000000000000000000000000000000000000000000000000000000000

0000011000000000000000000000000000000000

00000111100100000000011110000011000000000000000110101110000000000001100000011111

00000000000100000000000000000000000000000010000000000000001100000000100000110000

0000000001000000011110000010000001111100

00000000000000000000000000000000000000000000000000000000000000000000000000000000

00000000000000000000000000000000000000000000000000000000000000000000000000000000

00000000000000000000000000000000000111100000000000000000000000000000000000000000

000000000000000000000000000000000000000000000000000000000000

00001000000000000000000000000000000000000000111000000000000000000000000000000000

00000000000000000000000100000000000000000000000000001100000000000000000000000000

00000000000000000000010000000000000011110000000000000001111100000000000000000000

000000000000000000000000000000010000000000000000000000000000

1）康立山，智能计算及其应用讲义，中国地质大学内部讲义，20029

（2）Harry R.Lewis/Christos H.Papadimitriou, ELEMENTS OF THE THEORY OF COMPUTATION ,清华大学出版社（影印），1999年9月

2

0 0