如何在python3中执行部分映射的交叉?

时间:2018-11-11 23:57:36

标签: python python-3.x genetic-algorithm magic-square crossover

我是遗传算法的新手,前一天又重新创建了目标字符串。因此,我试图制造一个可以制造魔术广场的产品。没关系,直到我进入分频器部分,才意识到我不能只做单点分频。因此,我尝试执行“部分映射的交叉”,但是我无法而且仍然无法使它正常工作。我了解部分映射的交叉的工作原理,但我无法将其实现到python中。由于我的代码尚未完成,所以我将交叉功能隔离在另一个程序中,并对其进行了更改,因此父级是固定列表。

有人可以更正我的代码,或者如果完全错误,请告诉我如何对2个具有1到9的整数的列表执行部分映射交叉? 另外,我很抱歉,并且了解到我对变量的命名不是很好,但是我只是想让程序能够进行不断的编辑。

import random

parent1 = [1,2,3,4,5,6,7,8,9]
parent2 = [5,4,6,7,2,1,3,9,8]

firstCrossPoint = random.randint(0,len(parent1)-1)              #Creating parameters for random sublist
secondCrossPoint = random.randint(firstCrossPoint+1,len(parent1))

parent1MiddleCross = parent1[firstCrossPoint:secondCrossPoint]
parent2MiddleCross = parent2[firstCrossPoint:secondCrossPoint]

child1 = (parent1[:firstCrossPoint] + parent2MiddleCross + parent1[secondCrossPoint:])
child2 = (parent2[:firstCrossPoint] + parent1MiddleCross + parent2[secondCrossPoint:])

relationsWithDupes = []
for i in range(len(parent1MiddleCross)):
    relationsWithDupes.append([parent2MiddleCross[i], parent1MiddleCross[i]])

relations = []
for pair in relationsWithDupes:

    for i in range(len(relationsWithDupes)):
        if pair[0] in relationsWithDupes[i] or pair[1] in relationsWithDupes[i]:
            if pair != relationsWithDupes[i]:
                if pair[0] == relationsWithDupes[i][1]:
                    pair[0] = relationsWithDupes[i][0]

                else:
                    pair[1] = relationsWithDupes[i][1]

    if pair not in relations and pair[::-1] not in relations:
        relations.append(pair)

for i in child1[:firstCrossPoint]:
    for x in relations:
        if i == x[0]:
            i = x[1]

for i in child1[secondCrossPoint:]:
    for x in relations:
        if i == x[0]:
            i = x[1]

for i in child2[:firstCrossPoint]:
    for x in relations:
        if i == x[1]:
            i = x[0]

for i in child2[secondCrossPoint:]:
    for x in relations:
        if i == x[1]:
            i = x[0]

print(child1)
print(child2)

1 个答案:

答案 0 :(得分:0)

import numpy as np

parent1 = [1,2,3,4,5,6,7,8,9]
parent2 = [5,4,6,7,2,1,3,9,8]

firstCrossPoint = np.random.randint(0,len(parent1)-2)
secondCrossPoint = np.random.randint(firstCrossPoint+1,len(parent1)-1)

print(firstCrossPoint, secondCrossPoint)

parent1MiddleCross = parent1[firstCrossPoint:secondCrossPoint]
parent2MiddleCross = parent2[firstCrossPoint:secondCrossPoint]

temp_child1 = parent1[:firstCrossPoint] + parent2MiddleCross + parent1[secondCrossPoint:]

temp_child2 = parent2[:firstCrossPoint] + parent1MiddleCross + parent2[secondCrossPoint:]

relations = []
for i in range(len(parent1MiddleCross)):
    relations.append([parent2MiddleCross[i], parent1MiddleCross[i]])

print(relations)

def recursion1 (temp_child , firstCrossPoint , secondCrossPoint , parent1MiddleCross , parent2MiddleCross) :
    child = np.array([0 for i in range(len(parent1))])
    for i,j in enumerate(temp_child[:firstCrossPoint]):
        c=0
        for x in relations:
            if j == x[0]:
                child[i]=x[1]
                c=1
                break
        if c==0:
            child[i]=j
    j=0
    for i in range(firstCrossPoint,secondCrossPoint):
        child[i]=parent2MiddleCross[j]
        j+=1

    for i,j in enumerate(temp_child[secondCrossPoint:]):
        c=0
        for x in relations:
            if j == x[0]:
                child[i+secondCrossPoint]=x[1]
                c=1
                break
        if c==0:
            child[i+secondCrossPoint]=j
    child_unique=np.unique(child)
    if len(child)>len(child_unique):
        child=recursion1(child,firstCrossPoint,secondCrossPoint,parent1MiddleCross,parent2MiddleCross)
    return(child)

def recursion2(temp_child,firstCrossPoint,secondCrossPoint,parent1MiddleCross,parent2MiddleCross):
    child = np.array([0 for i in range(len(parent1))])
    for i,j in enumerate(temp_child[:firstCrossPoint]):
        c=0
        for x in relations:
            if j == x[1]:
                child[i]=x[0]
                c=1
                break
        if c==0:
            child[i]=j
    j=0
    for i in range(firstCrossPoint,secondCrossPoint):
        child[i]=parent1MiddleCross[j]
        j+=1

    for i,j in enumerate(temp_child[secondCrossPoint:]):
        c=0
        for x in relations:
            if j == x[1]:
                child[i+secondCrossPoint]=x[0]
                c=1
                break
        if c==0:
            child[i+secondCrossPoint]=j
    child_unique=np.unique(child)
    if len(child)>len(child_unique):
        child=recursion2(child,firstCrossPoint,secondCrossPoint,parent1MiddleCross,parent2MiddleCross)
    return(child)

child1=recursion1(temp_child1,firstCrossPoint,secondCrossPoint,parent1MiddleCross,parent2MiddleCross)
child2=recursion2(temp_child2,firstCrossPoint,secondCrossPoint,parent1MiddleCross,parent2MiddleCross)

print(child1)
print(child2)