遗传算法解迷宫问题

/*

Zhangyuan Li

Siena College

all right reserved

*/

 主函数:
#include <utility>
#include <vector>
#include <iostream>
#include <Windows.h>
#include "Bobmap.h"
#include <time.h>

using namespace std;


int main()
{
::system("color 02");
cout<<" hi testing"<<endl;
for(int i=0;i<8;i++)
{
for(int j=0;j<8;j++)
{
cout<<map[i][j]<<" ";
}
cout<<endl;
}
srand(time(0));
path p;
p.generation=4;
if(!p.win)
{
for(int k=0;k<p.generation;k++)
{    
cout<<" "<<endl;
cout<<"generation:"<<" "<<k;
cout<<" "<<endl;
p.epoch();
}
}
system("pause");
return 0;
}
/*
下面是path 类和地图类
*/
#include <utility>
#include <vector>
#include <iostream>
#include <Windows.h>
#include <time.h>
#include <memory>
#include <math.h>
using namespace std;
int map[8][8]={{2,1,1,0,0,1,1,0},{0,1,0,0,1,1,1,1},{0,1,1,0,1,0,0,3},{0,0,0,0,0,0,1,1},{1,1,1,1,1,1,1,1},{1,1,1,1,1,1,1,1},{1,1,1,1,1,1,1,1},{1,1,1,1,1,1,1,1}};

vector<int> decode (vector<int>&chrome,vector<int>&direction)
{     
for(int j=0;j<chrome.size()*0.5;j++)
{int sum=0;
     for(int k=0;k<2;k++)
{
sum=(2-k)*chrome[2*j+k]+sum;
 
}
direction.push_back(sum);
}
return direction;
}
float RandFloat()
{

return (float)(rand()%100)/100;

}
struct chromesome
{
int length;
float fitness;
vector<int>chrome;
chromesome()
{
chrome.clear();

length;
fitness=0.0f;
}
};

class mymap
{
public:


friend class path;
};
class path// main class we need
{
public :
int x;
int y;
int generation;
int popsize;
int chromlength;
float crossrate;
float mutaterate;
bool win;
float Sumfitness;
vector<chromesome>gnome;
void epoch();
chromesome RouletteWheelSelection();
int mem[8][8];

path()
{
generation=0;
crossrate=0.7f;
mutaterate=0.01f;
win=false;
Sumfitness=0.0f;
chromlength=32;
popsize=10;
gnome.clear();
for(int i=0;i<popsize;i++)
{      chromesome t;
for(int j=0;j<chromlength;j++)
{
t.chrome.push_back(rand()%2);
}
gnome.push_back(t);
}
for(int i=0;i<8;i++)
{
for(int j=0;j<8;j++)
{
mem[i][j]=map[i][j];
}
}

}
void resetmem()
{for(int i=0;i<8;i++)
{
for(int j=0;j<8;j++)
{
mem[i][j]=map[i][j];
}
}
}
void cross(const vector<int> &mum,const vector<int> &dad,vector<int>&baby1,vector<int>&baby2)
{
int cp=rand()%chromlength;
for (int i=0; i<cp; ++i)
{
baby1.push_back(mum[i]);
baby2.push_back(dad[i]);
}
for (int i=cp; i<mum.size(); ++i)
{
baby1.push_back(dad[i]);
baby2.push_back(mum[i]);
}
}
void mutate(vector<int> &vecBits)
{
for (int curBit=0; curBit<vecBits.size(); curBit++)
{
vecBits[curBit] = !vecBits[curBit];
     }
   }
float fitfunction(int a,int b,int dx,int dy)
{     
int tx=::abs(dx-a);
int ty=::abs(dy-b);
return (float)1/(tx+ty+1);//max value is 1
}
void testroad(chromesome g)
{   resetmem();
int X=0;
int Y=0;
vector<int>temp;
bool run=true;
/*for(int i=0;i<8;i++)
{
for(int j=0;j<8;j++)
{
cout<<mem[i][j]<<" ";
}
cout<<endl;
}
*/
temp=::decode(g.chrome,temp);
mem[X][Y]=1;
for(int m=0;m<temp.size();m++)
{

   switch(temp[m])
{case 0:
         if(Y>0&&mem[X][Y-1]!=1&&mem[X][Y-1]!=3)
{
Y--;
}
break;
case 1:
    if(X>0&&mem[X-1][Y]!=1&&mem[X-1][Y]!=3)
{
X--;
}

break;
case 2:
    if(Y<7&&mem[X][Y+1]!=1&&mem[X][Y+1]!=3)
{
Y++;
}
break;
case 3:
    if(X<7&&mem[X+1][Y]!=1&&mem[X+1][Y]!=3)
{
X++;
}

break;
   }
   mem[X][Y]=1;
}
g.fitness=fitfunction(X,Y,2,7);
if(g.fitness==0.5)
{
win=true;
}
//end for
for(int i=0;i<8;i++)
{
cout<<endl;
for(int j=0;j<8;j++)
{
cout<<mem[i][j]<<" ";
}
}
cout<<g.fitness;

}
virtual ~path()
{}

};//class end
chromesome path::RouletteWheelSelection()
{ double fSlice= RandFloat()*Sumfitness;
double cfTotal= 0.0;
intSelectedGenome = 0;
for(int i=0;i<popsize;i++)
{
cfTotal+=gnome[i].fitness;
      if(cfTotal>fSlice)
  {
  SelectedGenome=i;
  break;
  }
}
return gnome[SelectedGenome];
}

void path::epoch()
{   
if(!win){
int i=0;
vector<chromesome>baby;
chromesome baby0;
chromesome baby1;
baby.clear();
for(int i=0;i<gnome.size();i++)
{   testroad(gnome[i]);
cout<<endl;
//cout<<gnome[i].fitness<<" ";
cout<<endl;
}
while(i<popsize)
{
      chromesome mum=RouletteWheelSelection();
 chromesome dad =RouletteWheelSelection();
 if(RandFloat()<0.7)
 {
 cross(mum.chrome,dad.chrome,baby0.chrome,baby1.chrome);
 
 }
 else if(RandFloat()<0.02)
 {
 mutate(baby0.chrome);
 mutate(baby1.chrome);
 }
 else
 {
 baby0=mum;
 baby1=dad;
 }
 baby.push_back(baby0);
 baby.push_back(baby1);
i+=2;
}
cout<<baby.size();
cout<<endl;
gnome=baby;
}
else
{
return;
}
} 
    原文作者:迷宫问题
    原文地址: https://blog.csdn.net/DannyPassante/article/details/10131329
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞