首页
社区
课程
招聘
[旧帖] [求助]使用遗传算法跑迷宫,出现长时间得不到结果的情况 0.00雪花
发表于: 2011-9-18 20:40 1353

[旧帖] [求助]使用遗传算法跑迷宫,出现长时间得不到结果的情况 0.00雪花

2011-9-18 20:40
1353
小弟第一次发帖,在网上看了一篇遗传算法入门的文章(http://blog.csdn.net/zzwu/article/details/561577)
觉得很有意思,于是想自己练习一下

解释:
程序中先提供一张地图,设置好入口和出口,随机生成100条染色体,每个染色体有40个节点
每个节点可能出现的数据为0、1、2、3(0代表向上↑,1代表向下↓,2代表向左←,3代表向右→),从出发点出发,依次读取染色体的节点,如果碰到墙壁(地图中的1)或者入口(地图中的8)就跳过该节点,继续读取下一节点,当所在方位是出口时,给予此染色体1分(满分),若走完一条染色体之后却没有到达出口,则给予此染色体一个适应性评分(为此染色体离出口最近的横向距离与纵向距离的绝对值只和)

使用单点交叉的杂交方法,和单点取反的变异方法

初学编程   编程水平有限  请各位指点

疑问:
在实验中,程序出现过几次很久都没有跑出结果的情况。请各位说一下我哪里出问题了

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
#include "stdio.h"
#include "stdlib.h"
#include "time.h"
#include "iostream.h"
#include "windows.h"
#include "math.h"
 
#define       GENE_COUNT           100                       //染色体的数量
#define       GENE_LENG              40                         //每个染色体所具有的节点个数
#define       MAP_WIDTH              15                          //地图的宽
#define       MAP_HEIGHT             10                         //地图的高
 
#define     CROSSOVER_RATE       0.7                       //杂交率
#define       MUTATION_RATE        0.001                     //变异率
 
 
inline int    RandInt(int x,int y) {return rand()%(y-x+1)+x;}
inline double RandFloat()       {return (rand())/(RAND_MAX+1.0);}
 
 
const int map[MAP_HEIGHT][MAP_WIDTH] = 
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 1,
 8, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 1,
 1, 0, 0, 0, 1, 1, 1, 0, 0, 1, 0, 0, 0, 0, 1,
 1, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 1, 0, 1,
 1, 1, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 1, 0, 1,
 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 1, 0, 1,
 1, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 5,
 1, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1,
 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};
 
 
class GeneticAlgorithm
{
private:
  char Gene[GENE_COUNT][GENE_LENG];
  char GeneBuffer[GENE_COUNT][GENE_LENG];
  double GeneScore[GENE_COUNT];
  int Top;
public:
  GeneticAlgorithm::GeneticAlgorithm();
  MakeScore();
  int WheelRoulette();
  Hybr();
  Hybr_main();
  Show();
  Variation(char *);
  int CheckScore();
};
GeneticAlgorithm::Show()
{
  for(int i=0;i<GENE_COUNT;i++)
  {
    cout<<"the gene is:";
    for(int j =0;j<GENE_LENG;j++)
    {
      printf(" %d",Gene[i][j]);
      //cout<<" "<<Gene[i][j];
    }
    printf("\n");
    cout<<endl<<"the score is:"<<GeneScore[i]<<endl;
  }
}
 
 
GeneticAlgorithm::GeneticAlgorithm()
{
  Top =0;
  srand((unsigned) time(NULL));
  for(int k = 0;k <GENE_COUNT; k ++)
  {
    for(int i = 0;i < GENE_LENG;i ++)
    {
      Gene[k][i] =(char)RandInt(0,3);
    }
  }
  for(int j = 0;j <GENE_COUNT ;j ++)
  {
    GeneScore[j] = 0;
  }
}
 
GeneticAlgorithm::MakeScore()
{
  int x,y,m,n;
  m =0;
  n =2;
  double adaptiveScores,t,temp;
 
  temp = 0;
  for(int i =0;i<GENE_COUNT;i++)
  {
    x = 0;
    y = 2;
    adaptiveScores = 0;
    for(int j = 0;j< GENE_LENG;j++)
    {
      temp = abs(14-m)+abs(7-n)+1;
      t = 1/temp;
      m = x;
      n = y;
      if(Gene[i][j]==0)
        y--;
 
      if(Gene[i][j]==1)
        y++;
 
      if(Gene[i][j]==2)
        x--;
 
      if(Gene[i][j]==3)
        x++;
      if(map[y][x]==1||map[y][x]==8||(x<0)||(y<0))
      {
        x = m;
        y = n;
        continue;
      }
      if(map[y][x]==5)
      {
        adaptiveScores = 1;
        break;
      }
      if(map[y][x]==0)
      {
        temp = abs(14-m)+abs(7-n)+1;
        t = 1/temp;
      }
      if(adaptiveScores<t)
        adaptiveScores = t;
    }
    GeneScore[i] = adaptiveScores;
  }
}
 
 
int GeneticAlgorithm::WheelRoulette()
{
  double sum = 0;
  for(int i= 0;i<GENE_COUNT;i++)
  {
    sum  sum +GeneScore[i];
  }
back:
  double p = RandFloat()*sum;
  sum=0;
  for(int j = 0;j<GENE_COUNT;j++)
  {
    sum sum+GeneScore[j];
    if(sum > p)
    {
      if(GeneScore[j]==0)
        goto back;
      return j;
    }
  }
}
 
 
GeneticAlgorithm::Hybr()
{
  char gene1[GENE_LENG];
  char gene2[GENE_LENG];
  int temp;
  int a,b;
  a = 0;
  b = 0;
  double pro= RandFloat();
  while(a==b)
  {
    a = WheelRoulette();
    b = WheelRoulette();
  }
  temp = RandInt(0,GENE_LENG-1);
  if(pro < CROSSOVER_RATE)
  {
    for(int k = 0;k<temp;k++)
    {
      gene1[k] = Gene[a][k];
    }
    for(int l = temp;l <GENE_LENG;l++)
    {
      gene1[l] = Gene[b][l];
    }
 
 
    for(int m = 0;m<temp;m++)
    {
      gene2[m] = Gene[b][m];
    }
    for(int n = temp;n <GENE_LENG;n++)
    {
      gene2[n] = Gene[a][n];
    }
  }
  else
  {
    for(int o = 0;o <GENE_LENG;o++)
    {
      gene1[o] = Gene[b][o];
    }
    for(int p = 0;p <GENE_LENG;p++)
    {
      gene2[p] = Gene[a][p];
    }
  }
 
 
  Variation(gene1);
  Variation(gene2);
 
  for(int j = 0;j<GENE_LENG;j++)
  {
    GeneBuffer[Top][j] =gene1[j];
     
  }
  Top++;
  for(int i = 0;i<GENE_LENG;i++)
  {
    GeneBuffer[Top][i] =gene2[i];
     
  }
  Top++;
  //printf("Hybr  is finished\n");
}
 
 
GeneticAlgorithm::Hybr_main()
{
  for(int i = 0;i<50;i++)
  {
    Hybr();
  //  printf("this is Hybr_main()\n");
  //  cout<<i<<endl;
  }
  Top = 0 ;
  memcpy(Gene,GeneBuffer,GENE_COUNT*GENE_LENG*sizeof(char));
}
 
GeneticAlgorithm::Variation(char *gene)
{
  double p = MUTATION_RATE;
  int  point = RandInt(1,GENE_LENG*2-1);
  if(RandFloat() < p)
  {
    if(point%2==0)
    {
      if(gene[point/2]==0)
        gene[point/2] =2;
      if(gene[point/2]==1)
        gene[point/2] =3;
      if(gene[point/2]==2)
        gene[point/2] = 0;
      if(gene[point/2]==3)
        gene[point/2]= 1;
    }
    else
    {
      if(gene[point/2]==0)
        gene[point/2] =1;
      if(gene[point/2]==1)
        gene[point/2] =0;
      if(gene[point/2]==2)
        gene[point/2] = 3;
      if(gene[point/2]==3)
        gene[point/2]= 2;
    }
  }
}
 
int GeneticAlgorithm::CheckScore()
{
  for(int i=0;i<GENE_COUNT;i++)
  {
    if(GeneScore[i]==1)
    {
      for(int m =0;m<GENE_LENG;m++)
      {
        printf(" %d",Gene[i][m]);
      }
      return 1;
    }
  }
  return 0;
}
 
int main()
{
  GeneticAlgorithm temp;
  for(int i= 0;i<MAP_HEIGHT;i++)
  {
    for(int j =0;j<MAP_WIDTH;j++)
    {
      printf(" %d",map[i][j]);
    }
    printf("\n");
  }
  printf("\n");
  for(int n=1;;n++)
  {
    temp.MakeScore();
    if(temp.CheckScore()==1)
    {
      cout<<"the generation is :"<<n<<endl;
      break;
    }
    temp.Hybr_main();
  }
  cout<<endl<<"Congratulation!!!!"<<endl;
  return 1;
}

[培训]内核驱动高级班,冲击BAT一流互联网大厂工作,每周日13:00-18:00直播授课

收藏
免费
支持
分享
最新回复 (4)
雪    币: 4
活跃值: (10)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
2
无法学习,只能xx!
2011-9-18 23:02
0
雪    币: 27
活跃值: (122)
能力值: ( LV8,RANK:120 )
在线值:
发帖
回帖
粉丝
3
不懂,帮顶..
2011-9-18 23:46
0
雪    币: 5
活跃值: (10)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
4
遗传算法没怎么实际用过,也没仔细想,反正有的模型是很难得到最优解的,有时容易收敛到局部最优解,在局部最优解附近程序出不去了。通常多运行几次,看所求解的稳定性。
2011-9-19 23:02
0
雪    币: 87
活跃值: (10)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
5
谢谢你哈,我再找书看看,理论实践起来还真不容易
2011-9-20 00:33
0
游客
登录 | 注册 方可回帖
返回

账号登录
验证码登录

忘记密码?
没有账号?立即免费注册