遗传算法求解迷宫问题的matlab代码——greatji_1994
2015-02-02 11:06
525 查看
转载请说明出处,尊重原创
function []=untitled()
global ifthrough sample waysign
sample = 20;
ifthrough = zeros(1,sample);
waysign = zeros(100,sample);
Mazz = [0 1 0 0 1 0 0 1 1 0 1;
0 0 0 0 0 1 0 0 1 1 0;
0 0 0 0 0 0 0 1 0 0 0;
0 1 0 0 1 0 0 0 0 1 1;
0 0 1 0 0 0 0 1 1 1 0;
1 0 0 0 1 0 0 0 1 0 1;
0 0 1 0 0 0 0 0 1 0 1;
1 0 0 0 0 1 0 0 0 0 1;
1 1 0 0 1 1 0 1 0 1 0;
1 1 1 0 0 0 0 0 0 0 0]
initx=1;
inity=1;
termx=10;
termy=10;
pc = 0.4;
pm = 0.3;
Generation = zeros(100,sample);
for k=1:1:sample
Generation(:,k) = fix(4*rand(100,1));
end
fitness = Cal_fitness(Mazz,initx,inity,termx,termy,Generation)
for t=0:1:1000
if(rand(1,1) < pc)
Generation = Crossover(Generation,fitness);
end
fitness = Cal_fitness(Mazz,initx,inity,termx,termy,Generation);
if(rand(1,1) < pm)
Generation = Mutation(Generation);
end
fitness = Cal_fitness(Mazz,initx,inity,termx,termy,Generation);
end
Generation;
fitness
for t=1:1:sample
if fitness(1,t) == 10*sqrt(2)
Generation(:,t)'
end
end
end
function f = Cal_fitness(Mazz,initx,inity,termx,termy,Generation)
global ifthrough sample
f = zeros(1,sample);
for s=1:1:sample
posx=initx;
posy=inity;
expresslength=1;
while posy <= 10 && posy >=1 && posx <= 10 && posx >= 1 && Mazz(posy,posx) == 0
if Generation(expresslength,s) == 0
posy = posy + 1;
elseif Generation(expresslength,s) == 1
posy = posy - 1;
elseif Generation(expresslength,s) == 2
posx = posx + 1;
elseif Generation(expresslength,s) == 3
posx = posx - 1;
end
expresslength = expresslength + 1;
end
if(posx == termx && posy == termy)
f(1,s) = 10*sqrt(2);
ifthrough(1,s) = 1;
Generation(:,s)'
Generation(:,s) = fix(4 * rand(1,100));
else
f(1,s) = 10*sqrt(2) - sqrt((10 - posx)^2 + (10 - posx)^2);
end
end
end
function r = Round_choose(fitness)
total = sum(fitness,2);
Possibility = fitness ./ total;
m = 0;
ran = rand(1,1);
i = 0;
while ran > m
i = i + 1;
m = m + Possibility(1,i);
end
r = i;
end
function c = Crossover(Generation,fitness)
global sample
c = Generation;
for k=1:1:sample
Choice = [Round_choose(fitness),Round_choose(fitness)];
if(fitness(1,Choice(1)) <= 100)
head = fix(100*rand(1,1)+1);
tail = fix((100-head)*rand(1,1))+head;
temp2 = Generation(:,Choice(2));
temp2 = temp2(head:tail,:);
c(:,k) = Generation(:,Choice(1));
c(head:tail,k) = temp2;
else
c(:,k) = Generation(:,Choice(1));
end
end
end
function m = Mutation(Generation)
global sample
m = Generation;
for s=1:1:sample
m(fix(100 * rand(1,1)) + 1, s) = fix(4 * rand(1,1));
end
end
function []=untitled()
global ifthrough sample waysign
sample = 20;
ifthrough = zeros(1,sample);
waysign = zeros(100,sample);
Mazz = [0 1 0 0 1 0 0 1 1 0 1;
0 0 0 0 0 1 0 0 1 1 0;
0 0 0 0 0 0 0 1 0 0 0;
0 1 0 0 1 0 0 0 0 1 1;
0 0 1 0 0 0 0 1 1 1 0;
1 0 0 0 1 0 0 0 1 0 1;
0 0 1 0 0 0 0 0 1 0 1;
1 0 0 0 0 1 0 0 0 0 1;
1 1 0 0 1 1 0 1 0 1 0;
1 1 1 0 0 0 0 0 0 0 0]
initx=1;
inity=1;
termx=10;
termy=10;
pc = 0.4;
pm = 0.3;
Generation = zeros(100,sample);
for k=1:1:sample
Generation(:,k) = fix(4*rand(100,1));
end
fitness = Cal_fitness(Mazz,initx,inity,termx,termy,Generation)
for t=0:1:1000
if(rand(1,1) < pc)
Generation = Crossover(Generation,fitness);
end
fitness = Cal_fitness(Mazz,initx,inity,termx,termy,Generation);
if(rand(1,1) < pm)
Generation = Mutation(Generation);
end
fitness = Cal_fitness(Mazz,initx,inity,termx,termy,Generation);
end
Generation;
fitness
for t=1:1:sample
if fitness(1,t) == 10*sqrt(2)
Generation(:,t)'
end
end
end
function f = Cal_fitness(Mazz,initx,inity,termx,termy,Generation)
global ifthrough sample
f = zeros(1,sample);
for s=1:1:sample
posx=initx;
posy=inity;
expresslength=1;
while posy <= 10 && posy >=1 && posx <= 10 && posx >= 1 && Mazz(posy,posx) == 0
if Generation(expresslength,s) == 0
posy = posy + 1;
elseif Generation(expresslength,s) == 1
posy = posy - 1;
elseif Generation(expresslength,s) == 2
posx = posx + 1;
elseif Generation(expresslength,s) == 3
posx = posx - 1;
end
expresslength = expresslength + 1;
end
if(posx == termx && posy == termy)
f(1,s) = 10*sqrt(2);
ifthrough(1,s) = 1;
Generation(:,s)'
Generation(:,s) = fix(4 * rand(1,100));
else
f(1,s) = 10*sqrt(2) - sqrt((10 - posx)^2 + (10 - posx)^2);
end
end
end
function r = Round_choose(fitness)
total = sum(fitness,2);
Possibility = fitness ./ total;
m = 0;
ran = rand(1,1);
i = 0;
while ran > m
i = i + 1;
m = m + Possibility(1,i);
end
r = i;
end
function c = Crossover(Generation,fitness)
global sample
c = Generation;
for k=1:1:sample
Choice = [Round_choose(fitness),Round_choose(fitness)];
if(fitness(1,Choice(1)) <= 100)
head = fix(100*rand(1,1)+1);
tail = fix((100-head)*rand(1,1))+head;
temp2 = Generation(:,Choice(2));
temp2 = temp2(head:tail,:);
c(:,k) = Generation(:,Choice(1));
c(head:tail,k) = temp2;
else
c(:,k) = Generation(:,Choice(1));
end
end
end
function m = Mutation(Generation)
global sample
m = Generation;
for s=1:1:sample
m(fix(100 * rand(1,1)) + 1, s) = fix(4 * rand(1,1));
end
end
相关文章推荐
- Matlab遗传算法优化问题求解的示例代码
- Matlab遗传算法优化问题求解的演示样例代码
- 遗传算法求解带非线性约束的单目标问题,matlab代码,基于K Deb的论文
- 遗传算法 求解旅行商 TSP 问题,matlab代码
- Matlab 遗传算法求解TSP问题
- 背包问题,动态规划求解,matlab代码,c++代码
- 遗传算法的应用(求解迷宫问题)
- Rational Krylov Method(克雷洛夫法)求解特征值问题matlab代码示例(和Arnoldi方法比较)
- 递归法求解迷宫问题
- 用python写成的排列问题求解代码
- N皇后问题的求解(C++代码)
- 数据结构和算法设计(迷宫求解问题的栈和队列的实现)
- MATLAB求解非线性规划问题的例子
- 迷宫问题的递归求解
- 数学建模常用Matlab/Lingo/c代码总结系列——整数规划问题
- 最大子字段和问题求解C++代码
- 迷宫求解问题——堆栈的使用
- 利用递归来求解迷宫问题
- Trie树的应用,一道算法问题求解 代码实现
- 用C语言实现迷宫求解问题