matlab解决迷宫问题,遗传算法求解迷宫问题的matlab代码——greatji_1994

转载请说明出处,尊重原创

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个用MATLAB实现遗传算法求解迷宫寻宝藏问题的代码: ```matlab % 迷宫地图 maze = [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0; 0, 1, 1, 1, 1, 1, 0, 0, 0, 0; 0, 0, 0, 0, 0, 1, 0, 0, 0, 0; 0, 0, 0, 1, 0, 1, 1, 1, 1, 0; 0, 0, 0, 1, 0, 0, 0, 0, 1, 0; 0, 0, 0, 1, 1, 1, 1, 0, 1, 0; 0, 0, 0, 0, 0, 0, 1, 0, 1, 0; 0, 0, 0, 1, 1, 1, 1, 0, 1, 0; 0, 0, 0, 1, 0, 0, 0, 0, 1, 0; 0, 0, 0, 1, 1, 1, 1, 1, 1, 0; 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ]; % 起点和终点 start_pos = [2, 2]; end_pos = [10, 9]; % 移动操作 moves = {'up', 'down', 'left', 'right'}; % 遗传算法参数 POPULATION_SIZE = 50; GENERATIONS = 100; MUTATION_RATE = 0.1; % 生成随机个体 function individual = generate_individual() individual = randi(4, 1, 100); end % 计算适应度 function fitness = calculate_fitness(individual) x = start_pos(2); y = start_pos(1); for i = 1:length(individual) if strcmp(individual(i), 'up') y = y - 1; elseif strcmp(individual(i), 'down') y = y + 1; elseif strcmp(individual(i), 'left') x = x - 1; elseif strcmp(individual(i), 'right') x = x + 1; end if isequal([y, x], end_pos) fitness = 1; return; end if maze(y, x) == 1 fitness = 0; return; end end fitness = 0; end % 选择操作 function parents = selection(population) fitnesses = zeros(1, length(population)); for i = 1:length(population) fitnesses(i) = calculate_fitness(population{i}); end total_fitness = sum(fitnesses); probabilities = fitnesses / total_fitness; parents = cell(1, 2); for i = 1:2 idx = randsample(length(population), 1, true, probabilities); parents{i} = population{idx}; end end % 交叉操作 function children = crossover(parents) crossover_point = randi(99); child1 = [parents{1}(1:crossover_point), parents{2}(crossover_point+1:end)]; child2 = [parents{2}(1:crossover_point), parents{1}(crossover_point+1:end)]; children = {child1, child2}; end % 变异操作 function individual = mutation(individual) for i = 1:length(individual) if rand() < MUTATION_RATE individual(i) = randi(4); end end end % 遗传算法主函数 function genetic_algorithm() population = cell(1, POPULATION_SIZE); for i = 1:POPULATION_SIZE population{i} = generate_individual(); end for generation = 1:GENERATIONS new_population = cell(1, POPULATION_SIZE); for i = 1:2:POPULATION_SIZE parents = selection(population); children = crossover(parents); children{1} = mutation(children{1}); children{2} = mutation(children{2}); new_population{i} = children{1}; new_population{i+1} = children{2}; end population = new_population; fitnesses = zeros(1, length(population)); for i = 1:length(population) fitnesses(i) = calculate_fitness(population{i}); end [~, idx] = max(fitnesses); best_individual = population{idx}; fprintf('Generation: %d, Best individual: %s, Fitness: %d\n', generation, mat2str(best_individual), calculate_fitness(best_individual)); end end % 运行遗传算法 genetic_algorithm(); ``` 代码说明: 1. 首先定义了迷宫地图、起点和终点、移动操作等必要的变量。 2. 然后定义了几个遗传算法的基本操作函数,包括生成随机个体、计算适应度、选择、交叉、变异等。 3. 最后定义了遗传算法主函数,其中首先生成初始种群,然后进行多代进化,每一代都进行选择、交叉、变异等操作,最后输出每一代的最佳个体和适应度。 4. 运行遗传算法函数即可得到寻宝的最优路径。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值