我试图在运行代码时使父数组中的父元素只被使用一次。为了排除故障,我缩短了数字。我要我的程序从数组中选择一个单一的组合。一旦这个元素被选中,我希望它再次无法使用。在
我当前使用的代码:
#Importing the needed functions
import pandas as pd
import math
import numpy as np
import matplotlib.pyplot as plt
import random
from itertools import permutations as permutation
#Defining my mating function where I want each parent in the parent array to be only once and not repeat.
def mating (parents):
parent1 = 0
parent2 = 0
parent1 = permutation(parents,2)
parent2 = permutation(parents , 2)
print ("parent1: ", list(parent1))
print ("parent2: ", list(parent2))
#Function used to create my 4 parents and 3 genes
def generateRandom(population , m):
Population = np.random.rand (population,m)
return(Population)
# main code where I am setting returns from the functions to other vairables to call later
for i in range(2):
candidateSolution = generateRandom(4,3)
print ("the solutions: ", candidateSolution)
full_population = mating(candidateSolution
我现在得到的输出是一个置换,其中元素0与元素1、2、3相对应。在
我想要它做的是将一个随机的父对象分配给父对象1,将一个未使用的随机父对象指定为父对象2。我只想让每一位家长使用一次。在
我看到的一个主题显示了我得到了什么:Pair combinations of elements in dictionary without repetition
但是,他们希望在元素0被多次使用的地方使用它。我只想用一次。在
您可以选择无序列表中定义的项目,而不是在有序列表中选择唯一的随机组合。这样,你就确定了父母的唯一性,保证了繁殖的随机性。在
适应你的日常生活,它会变成:
^{pr2}$用于修复此问题的代码是:
这是因为我的长度是偶数。在
相关问题 更多 >
编程相关推荐