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

来源:互联网 发布:怎么在淘宝头条上发帖 编辑:程序博客网 时间:2024/06/06 02:16

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

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
原创粉丝点击