遗传算法求解迷宫问题的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

    

    原文作者:遗传算法
    原文地址: https://blog.csdn.net/greatji_1994/article/details/43406021
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞