interchanging the matrix element between two matrix to produce offsprings?

5 次查看(过去 30 天)
i have two parents
p1=[2 3 4 5 1 6 7 9 8 10]
p2=[1 3 5 6 7 8 9 10 2 4]
step 1:
here i wanna change in-between values by crossing [5 1 6 7] and [6 7 8 9] of p1 and p2 respectively
off1=[* * * 6 7 8 9 * * *]
off2=[* * * 5 1 6 7 * * *]
step 2:
then select values not in the crossing operation and such as [2 3 4 10] and change * if previously exist by comparing p1 and p2 to make off1 and off2.
off1=[2 3 4 6 7 8 9 * * 10]
off2=[* 3 * 5 1 6 7 10 3 4]
the remaining values are swapped based on interchange in first step 5=6;1=7;6=8;7=9 then swapped to other value if one of the number existing in p1 and p2. for eg we have 9 and 8 at p1 which remains *. it should be swapped based on step1 condition given above (5=6;1=7;6=8;7=9) so 9 replaced by 7 and 8 replaced by 6. similarly for off2
off1=[2 3 4 6 7 8 9 7 8 10]
off2=[7 3 8 5 1 6 7 10 3 4]
here when selecting 8 conflict exists 5=6 but 6 already existing so 6=8 is considered and changed accordingly.
help me in coding this...
thank you

回答(1 个)

Martin Seliga
Martin Seliga 2018-4-18
编辑:Martin Seliga 2018-4-18

Hi.

I think you don't need this anymore, but maybe someone will searching for the solution. Here is the code:

 function [childA, childB] = crossover(parentA, parentB)
    n = length(parentA);
    childA = zeros(1,n);
    childB = zeros(1,n);
    x1 = randperm(n,1);
    x2 = randperm(n-x1,1)+x1;
    childA(x1:x2) = parentB(x1:x2);
    childB(x1:x2) = parentA(x1:x2);
    for i = 1:x2-x1+1
        mapRelation(1:2,i)=[childA(x1+i-1), childB(x1+i-1)];
    end
    for i = 1:x1-1
        if(length(find(childA == parentA(i))) < 1)
            childA(i) = parentA(i);
        end
        if(length(find(childB == parentB(i))) < 1)
            childB(i) = parentB(i);
        end
    end
    for i = x2+1:n
        if(isempty(find(childA == parentA(i),1)))
            childA(i) = parentA(i);
        end
        if(isempty(find(childB == parentB(i),1)))
            childB(i) = parentB(i);
        end
    end
    while(~isempty(find(childA == 0,1)))
        mapA = mapRelation;
        i = find(childA == 0,1);
        v = parentA(i);
        while (~isempty(find(mapA == v,1)))
            [j, k] = find(mapA == v,1);
            if (j == 1)
                v = mapA(2,k);
            else
                v = mapA(1,k);
            end
            mapA(:,k) = [];
        end
        childA(i) = v;
    end
    while(~isempty(find(childB == 0,1)))
        mapB = mapRelation;
        i = find(childB == 0,1);
        v = parentB(i);
        while (~isempty(find(mapB == v,1)))
            [j, k] = find(mapB == v,1);
            if (j == 1)
                v = mapB(2,k);
            else
                v = mapB(1,k);
            end
            mapB(:,k) = [];
        end
        childB(i) = v;
    end
end

类别

Help CenterFile Exchange 中查找有关 Elementary Math 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by